probe_spec.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe EmailHelper::Probe, integration: true, required_envs: %w[MAIL_SERVER MAIL_ADDRESS MAIL_PASS] do
  4. let(:expected_result_failed) { { result: 'failed', message: message_human, } }
  5. let(:expected_result_invalid) { { result: 'invalid', message_human: message_human, } }
  6. before do
  7. allow(EmailHelper).to receive(:mx_records).and_return([ ENV['MAIL_SERVER'] ])
  8. end
  9. shared_examples 'probe tests with invalid result' do
  10. it 'contains all information for an invalid result' do
  11. expect(probe_result).to include(
  12. result: expected_result_invalid[:result],
  13. message_human: be_in(expected_result_invalid[:message_human]),
  14. settings: include(
  15. options: include(
  16. host: host,
  17. ),
  18. ),
  19. )
  20. end
  21. end
  22. describe '#inbound' do
  23. subject(:probe_result) { described_class.inbound(inbound_params) }
  24. let(:inbound_params) do
  25. {
  26. adapter: adapter,
  27. options: {
  28. host: host,
  29. port: port,
  30. ssl: true,
  31. user: user,
  32. password: password,
  33. ssl_verify: false,
  34. },
  35. }
  36. end
  37. let(:adapter) { 'imap' }
  38. let(:port) { 993 }
  39. let(:user) { 'some@example.com' }
  40. let(:password) { 'password' }
  41. context 'with unknown adapter' do
  42. let(:adapter) { 'imap2' }
  43. let(:host) { 'nonexisting_host' }
  44. let(:message_human) { "Unknown adapter '#{adapter}'" }
  45. it { is_expected.to eq(expected_result_failed) }
  46. end
  47. context 'when network issues are present' do
  48. let(:host) { 'nonexisting_host' }
  49. let(:message_human) { 'The hostname could not be found.' }
  50. include_examples 'probe tests with invalid result'
  51. end
  52. context 'when an imap service with a blocked port is used' do
  53. let(:host) { '127.0.0.1' }
  54. let(:port) { 8 } # no service to be expected
  55. let(:message_human) { 'The connection was refused.' }
  56. include_examples 'probe tests with invalid result'
  57. end
  58. context 'when host is not reachable' do
  59. let(:host) { nil }
  60. let(:message_human) { [ 'This host cannot be reached.', 'There is no route to this host.' ] }
  61. before do
  62. allow(Socket).to receive(:tcp).and_raise(Errno::EHOSTUNREACH)
  63. end
  64. include_examples 'probe tests with invalid result'
  65. end
  66. context 'when authentication fails' do
  67. let(:host) { ENV['MAIL_SERVER'] }
  68. let(:message_human) { [ 'Authentication failed.', 'This host cannot be reached.' ] }
  69. include_examples 'probe tests with invalid result'
  70. end
  71. context 'when doing a real test' do
  72. let(:host) { ENV['MAIL_SERVER'] }
  73. let(:user) { ENV['MAIL_ADDRESS'] }
  74. let(:password) { ENV['MAIL_PASS'] }
  75. it { is_expected.to include(result: 'ok') }
  76. end
  77. end
  78. describe '#outbound' do
  79. subject(:probe_result) { described_class.outbound(outbound_params, user) }
  80. let(:outbound_params) do
  81. {
  82. adapter: adapter,
  83. options: {
  84. host: host,
  85. port: port,
  86. start_tls: true,
  87. user: user,
  88. password: password,
  89. ssl_verify: false,
  90. },
  91. }
  92. end
  93. let(:adapter) { 'smtp' }
  94. let(:port) { 25 }
  95. let(:user) { 'some@example.com' }
  96. let(:password) { 'password' }
  97. context 'with unknown adapter' do
  98. let(:adapter) { 'imap2' }
  99. let(:host) { 'nonexisting_host' }
  100. let(:message_human) { "Unknown adapter '#{adapter}'" }
  101. it { is_expected.to eq(expected_result_failed) }
  102. end
  103. context 'when network issues are present' do
  104. let(:host) { 'nonexisting_host' }
  105. let(:message_human) { 'The hostname could not be found.' }
  106. include_examples 'probe tests with invalid result'
  107. end
  108. context 'when an imap service with a blocked port is used' do
  109. let(:host) { '127.0.0.1' }
  110. let(:port) { 8 } # no service to be expected
  111. let(:message_human) { 'The connection was refused.' }
  112. include_examples 'probe tests with invalid result'
  113. end
  114. context 'when host is not reachable' do
  115. let(:host) { nil }
  116. let(:message_human) { [ 'This host cannot be reached.', 'There is no route to this host.' ] }
  117. before do
  118. allow(TCPSocket).to receive(:open).and_raise(Errno::EHOSTUNREACH)
  119. end
  120. include_examples 'probe tests with invalid result'
  121. end
  122. context 'when authentication fails' do
  123. let(:host) { ENV['MAIL_SERVER'] }
  124. let(:port) { 25 }
  125. let(:message_human) { 'Authentication failed.' }
  126. include_examples 'probe tests with invalid result'
  127. end
  128. context 'when doing a real test' do
  129. let(:host) { ENV['MAIL_SERVER'] }
  130. let(:port) { 25 }
  131. let(:user) { ENV['MAIL_ADDRESS'] }
  132. let(:password) { ENV['MAIL_PASS'] }
  133. let(:outbound_params) do
  134. {
  135. adapter: adapter,
  136. options: {
  137. host: host,
  138. port: port,
  139. user: user,
  140. ssl: false,
  141. password: password,
  142. ssl_verify: false,
  143. },
  144. }
  145. end
  146. it { is_expected.to include(result: 'ok') }
  147. end
  148. end
  149. describe '#full' do
  150. subject(:probe_result) { described_class.full(full_params) }
  151. let(:full_params) do
  152. {
  153. email: email,
  154. password: password,
  155. ssl_verify: (ssl_verify if defined? ssl_verify),
  156. }
  157. end
  158. context 'when providing invalid information' do
  159. let(:email) { 'invalid_format' }
  160. let(:password) { 'somepass' }
  161. it 'contains all information for an invalid probe' do
  162. expect(probe_result)
  163. .to include(
  164. result: 'invalid'
  165. )
  166. .and not_include('setting')
  167. end
  168. end
  169. context 'when SSL Verify setting is set according to provider configuration', :aggregate_failures do
  170. before do
  171. allow(described_class).to receive_messages(
  172. inbound: { result: 'ok' },
  173. outbound: { result: 'ok' }
  174. )
  175. end
  176. let(:mock_server) do
  177. {
  178. adapter: 'imap',
  179. options: {
  180. host: 'imap',
  181. port: 993,
  182. ssl: mock_server_ssl,
  183. start_tls: mock_server_starttls,
  184. user: 'user',
  185. password: 'password',
  186. },
  187. }
  188. end
  189. let(:mock_providers) do
  190. {
  191. bigtech: {
  192. domain: 'bigtechmail.com',
  193. inbound: mock_server,
  194. outbound: mock_server,
  195. },
  196. }
  197. end
  198. context 'when using a provider' do
  199. before { allow(EmailHelper).to receive(:provider).and_return(mock_providers) }
  200. context 'when server has no SSL or STARTTLS' do
  201. let(:mock_server_ssl) { false }
  202. let(:mock_server_starttls) { false }
  203. let(:config) { mock_server.deep_dup.tap { _1[:options][:ssl_verify] = false } }
  204. it 'adds ssl_verify set to false' do
  205. described_class.full(email: 'user@bigtechmail.com')
  206. expect(described_class).to have_received(:inbound).with(config)
  207. expect(described_class).to have_received(:outbound).with(config, anything)
  208. end
  209. end
  210. context 'when server has SSL' do
  211. let(:mock_server_ssl) { true }
  212. let(:mock_server_starttls) { false }
  213. let(:config) { mock_server.deep_dup.tap { _1[:options][:ssl_verify] = true } }
  214. it 'adds ssl_verify set to true' do
  215. described_class.full(email: 'user@bigtechmail.com')
  216. expect(described_class).to have_received(:inbound).with(config)
  217. expect(described_class).to have_received(:outbound).with(config, anything)
  218. end
  219. end
  220. context 'when server has STARTTLS' do
  221. let(:mock_server_ssl) { false }
  222. let(:mock_server_starttls) { true }
  223. let(:config) { mock_server.deep_dup.tap { _1[:options][:ssl_verify] = true } }
  224. it 'adds ssl_verify set to true' do
  225. described_class.full(email: 'user@bigtechmail.com')
  226. expect(described_class).to have_received(:inbound).with(config)
  227. expect(described_class).to have_received(:outbound).with(config, anything)
  228. end
  229. end
  230. end
  231. context 'when using a custom server' do
  232. before do
  233. allow(EmailHelper).to receive_messages(
  234. provider_inbound_mx: [],
  235. provider_outbound_mx: [],
  236. provider_inbound_guess: [mock_server],
  237. provider_outbound_guess: [mock_server]
  238. )
  239. end
  240. context 'when server has no SSL or STARTTLS' do
  241. let(:mock_server_ssl) { false }
  242. let(:mock_server_starttls) { false }
  243. let(:config) { mock_server.deep_dup.tap { _1[:options][:ssl_verify] = false } }
  244. it 'adds ssl_verify set to false' do
  245. described_class.full(email: 'user@example.com')
  246. expect(described_class).to have_received(:inbound).with(config)
  247. expect(described_class).to have_received(:outbound).with(config, anything)
  248. end
  249. end
  250. context 'when server has SSL' do
  251. let(:mock_server_ssl) { true }
  252. let(:mock_server_starttls) { false }
  253. let(:config) { mock_server.deep_dup.tap { _1[:options][:ssl_verify] = true } }
  254. it 'adds ssl_verify set to true' do
  255. described_class.full(email: 'user@example.com')
  256. expect(described_class).to have_received(:inbound).with(config)
  257. expect(described_class).to have_received(:outbound).with(config, anything)
  258. end
  259. end
  260. context 'when server has STARTTLS' do
  261. let(:mock_server_ssl) { false }
  262. let(:mock_server_starttls) { true }
  263. let(:config) { mock_server.deep_dup.tap { _1[:options][:ssl_verify] = true } }
  264. it 'adds ssl_verify set to true' do
  265. described_class.full(email: 'user@example.com')
  266. expect(described_class).to have_received(:inbound).with(config)
  267. expect(described_class).to have_received(:outbound).with(config, anything)
  268. end
  269. end
  270. end
  271. end
  272. context 'when doing real tests' do
  273. let(:email) { ENV['MAIL_ADDRESS'] }
  274. let(:password) { ENV['MAIL_PASS'] }
  275. shared_examples 'do real testing' do
  276. it 'contains all information for a successful probe' do
  277. expect(probe_result).to include(result: 'ok')
  278. .and include(
  279. setting: include(
  280. inbound: include(
  281. options: include(
  282. host: host
  283. ),
  284. ),
  285. ),
  286. )
  287. .and include(
  288. setting: include(
  289. outbound: include(
  290. options: include(
  291. host: host,
  292. ),
  293. ),
  294. ),
  295. )
  296. end
  297. end
  298. context 'when doing a real test' do
  299. let(:host) { ENV['MAIL_SERVER'] }
  300. context 'with ssl verification turned on' do
  301. let(:ssl_verify) { true }
  302. before do
  303. # Import CA certificate into the trust store.
  304. SSLCertificate.create!(certificate: Rails.root.join('spec/fixtures/files/imap/ca.crt').read)
  305. end
  306. include_examples 'do real testing'
  307. end
  308. context 'with ssl verification turned off' do
  309. let(:ssl_verify) { false }
  310. include_examples 'do real testing'
  311. end
  312. end
  313. end
  314. end
  315. end