probe_spec.rb 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe EmailHelper::Probe, integration: true do
  4. let(:expected_result_failed) do
  5. {
  6. result: 'failed',
  7. message: message_human,
  8. }
  9. end
  10. let(:expected_result_invalid) do
  11. {
  12. result: 'invalid',
  13. message_human: message_human,
  14. }
  15. end
  16. shared_examples 'probe tests with invalid result' do
  17. it 'contains all information for an invalid result' do # rubocop:disable RSpec/ExampleLength
  18. expect(probe_result).to include(
  19. result: expected_result_invalid[:result],
  20. message_human: be_in(expected_result_invalid[:message_human]),
  21. settings: include(
  22. options: include(
  23. host: host,
  24. ),
  25. ),
  26. )
  27. end
  28. end
  29. describe '#inbound' do
  30. subject(:probe_result) { described_class.inbound(inbound_params) }
  31. let(:inbound_params) do
  32. {
  33. adapter: adapter,
  34. options: {
  35. host: host,
  36. port: port,
  37. ssl: true,
  38. user: user,
  39. password: password,
  40. },
  41. }
  42. end
  43. let(:adapter) { 'imap' }
  44. let(:port) { 993 }
  45. let(:user) { 'some@example.com' }
  46. let(:password) { 'password' }
  47. context 'with unknown adapter' do
  48. let(:adapter) { 'imap2' }
  49. let(:host) { 'nonexisting_host' }
  50. let(:message_human) { "Unknown adapter '#{adapter}'" }
  51. it { is_expected.to eq(expected_result_failed) }
  52. end
  53. context 'when network issues are present' do
  54. let(:host) { 'nonexisting_host' }
  55. let(:message_human) { 'Hostname not found!' }
  56. include_examples 'probe tests with invalid result'
  57. end
  58. context 'when an imap service with a blocked port is used' do
  59. let(:host) { '127.0.0.1' }
  60. let(:port) { 8 } # no service to be expected
  61. let(:message_human) { 'Connection refused!' }
  62. include_examples 'probe tests with invalid result'
  63. end
  64. context 'when host is not reachable' do
  65. let(:host) { '192.168.254.254' }
  66. let(:message_human) { [ 'Host not reachable!', 'No route to host!' ] }
  67. before do
  68. stub_const('Channel::Driver::Imap::CHECK_ONLY_TIMEOUT', 1.second)
  69. end
  70. include_examples 'probe tests with invalid result'
  71. end
  72. context 'when incorrect credentials are used' do
  73. let(:host) { 'imap.gmail.com' }
  74. let(:message_human) { [ 'Authentication failed, username incorrect!', 'Authentication failed, invalid credentials!' ] }
  75. include_examples 'probe tests with invalid result'
  76. end
  77. context 'when authentication fails' do
  78. let(:host) { 'mx2.zammad.com' }
  79. let(:message_human) { [ 'Authentication failed!', 'Host not reachable!' ] }
  80. before do
  81. stub_const('Channel::Driver::Imap::CHECK_ONLY_TIMEOUT', 1.second)
  82. end
  83. include_examples 'probe tests with invalid result'
  84. end
  85. context 'when doing a real test', required_envs: %w[EMAILHELPER_MAILBOX_1] do
  86. let(:host) { 'mx2.zammad.com' }
  87. let(:real_user_data) { ENV['EMAILHELPER_MAILBOX_1'].split(':') }
  88. let(:user) { real_user_data.first }
  89. let(:password) { real_user_data.last }
  90. it { is_expected.to include(result: 'ok') }
  91. end
  92. end
  93. describe '#outbound' do
  94. subject(:probe_result) { described_class.outbound(outbound_params, user) }
  95. let(:outbound_params) do
  96. {
  97. adapter: adapter,
  98. options: {
  99. host: host,
  100. port: port,
  101. start_tls: true,
  102. user: user,
  103. password: password,
  104. },
  105. }
  106. end
  107. let(:adapter) { 'smtp' }
  108. let(:port) { 25 }
  109. let(:user) { 'some@example.com' }
  110. let(:password) { 'password' }
  111. context 'with unknown adapter' do
  112. let(:adapter) { 'imap2' }
  113. let(:host) { 'nonexisting_host' }
  114. let(:message_human) { "Unknown adapter '#{adapter}'" }
  115. it { is_expected.to eq(expected_result_failed) }
  116. end
  117. context 'when network issues are present' do
  118. let(:host) { 'nonexisting_host' }
  119. let(:message_human) { 'Hostname not found!' }
  120. include_examples 'probe tests with invalid result'
  121. end
  122. context 'when an imap service with a blocked port is used' do
  123. let(:host) { '127.0.0.1' }
  124. let(:port) { 8 } # no service to be expected
  125. let(:message_human) { 'Connection refused!' }
  126. include_examples 'probe tests with invalid result'
  127. end
  128. context 'when host is not reachable' do
  129. let(:host) { '192.168.254.254' }
  130. let(:message_human) { [ 'Host not reachable!', 'No route to host!' ] }
  131. before do
  132. stub_const('Channel::Driver::Smtp::DEFAULT_OPEN_TIMEOUT', 2.seconds)
  133. stub_const('Channel::Driver::Smtp::DEFAULT_READ_TIMEOUT', 4.seconds)
  134. end
  135. include_examples 'probe tests with invalid result'
  136. end
  137. context 'when incorrect credentials are used' do
  138. let(:host) { 'smtp.gmail.com' }
  139. let(:message_human) { 'Authentication failed!' }
  140. include_examples 'probe tests with invalid result'
  141. end
  142. context 'when authentication fails' do
  143. let(:host) { 'mx2.zammad.com' }
  144. let(:port) { 587 }
  145. let(:message_human) { 'Authentication failed!' }
  146. before do
  147. stub_const('Channel::Driver::Smtp::DEFAULT_OPEN_TIMEOUT', 5.seconds)
  148. stub_const('Channel::Driver::Smtp::DEFAULT_READ_TIMEOUT', 10.seconds)
  149. end
  150. include_examples 'probe tests with invalid result'
  151. end
  152. context 'when doing a real test', required_envs: %w[EMAILHELPER_MAILBOX_1] do
  153. let(:host) { 'mx2.zammad.com' }
  154. let(:port) { 587 }
  155. let(:real_user_data) { ENV['EMAILHELPER_MAILBOX_1'].split(':') }
  156. let(:user) { real_user_data.first }
  157. let(:password) { real_user_data.last }
  158. let(:outbound_params) do
  159. {
  160. adapter: adapter,
  161. options: {
  162. host: host,
  163. port: port,
  164. user: user,
  165. password: password,
  166. },
  167. }
  168. end
  169. before do
  170. stub_const('Channel::Driver::Smtp::DEFAULT_OPEN_TIMEOUT', 5.seconds)
  171. stub_const('Channel::Driver::Smtp::DEFAULT_READ_TIMEOUT', 10.seconds)
  172. end
  173. it { is_expected.to include(result: 'ok') }
  174. end
  175. end
  176. describe '#full' do
  177. subject(:probe_result) { described_class.full(full_params) }
  178. let(:full_params) do
  179. {
  180. email: email,
  181. password: password,
  182. }
  183. end
  184. context 'when providing invalid information' do
  185. let(:email) { 'invalid_format' }
  186. let(:password) { 'somepass' }
  187. it 'contains all information for an invalid probe' do
  188. expect(probe_result)
  189. .to include(
  190. result: 'invalid'
  191. )
  192. .and not_include('setting')
  193. end
  194. end
  195. context 'when doing real tests' do
  196. let(:email) { real_user_data.first }
  197. let(:password) { real_user_data.last }
  198. shared_examples 'do real testing' do
  199. it 'contains all information for a successful probe' do # rubocop:disable RSpec/ExampleLength
  200. expect(probe_result).to include(result: 'ok')
  201. .and include(
  202. setting: include(
  203. inbound: include(
  204. options: include(
  205. host: inbound_host
  206. ),
  207. ),
  208. ),
  209. )
  210. .and include(
  211. setting: include(
  212. outbound: include(
  213. options: include(
  214. host: outbound_host,
  215. ),
  216. ),
  217. ),
  218. )
  219. end
  220. end
  221. context 'with zammad', required_envs: %w[EMAILHELPER_MAILBOX_1] do
  222. let(:real_user_data) { ENV['EMAILHELPER_MAILBOX_1'].split(':') }
  223. let(:inbound_host) { 'mx2.zammad.com' }
  224. let(:outbound_host) { inbound_host }
  225. before do
  226. stub_const('Channel::Driver::Smtp::DEFAULT_OPEN_TIMEOUT', 5.seconds)
  227. stub_const('Channel::Driver::Smtp::DEFAULT_READ_TIMEOUT', 10.seconds)
  228. options = {
  229. host: inbound_host,
  230. port: 993,
  231. ssl: true,
  232. auth_type: 'LOGIN',
  233. user: email,
  234. password: password,
  235. }
  236. imap_delete_old_mails(options)
  237. end
  238. include_examples 'do real testing'
  239. end
  240. context 'with gmail', required_envs: %w[EMAILHELPER_MAILBOX_2] do
  241. let(:real_user_data) { ENV['EMAILHELPER_MAILBOX_2'].split(':') }
  242. let(:inbound_host) { 'pop.gmail.com' }
  243. let(:outbound_host) { 'smtp.gmail.com' }
  244. include_examples 'do real testing'
  245. end
  246. end
  247. end
  248. end