probe_spec.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # Copyright (C) 2012-2023 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. },
  34. }
  35. end
  36. let(:adapter) { 'imap' }
  37. let(:port) { 993 }
  38. let(:user) { 'some@example.com' }
  39. let(:password) { 'password' }
  40. context 'with unknown adapter' do
  41. let(:adapter) { 'imap2' }
  42. let(:host) { 'nonexisting_host' }
  43. let(:message_human) { "Unknown adapter '#{adapter}'" }
  44. it { is_expected.to eq(expected_result_failed) }
  45. end
  46. context 'when network issues are present' do
  47. let(:host) { 'nonexisting_host' }
  48. let(:message_human) { 'The hostname could not be found.' }
  49. include_examples 'probe tests with invalid result'
  50. end
  51. context 'when an imap service with a blocked port is used' do
  52. let(:host) { '127.0.0.1' }
  53. let(:port) { 8 } # no service to be expected
  54. let(:message_human) { 'The connection was refused.' }
  55. include_examples 'probe tests with invalid result'
  56. end
  57. context 'when host is not reachable' do
  58. let(:host) { nil }
  59. let(:message_human) { [ 'This host cannot be reached.', 'There is no route to this host.' ] }
  60. before do
  61. allow(Socket).to receive(:tcp).and_raise(Errno::EHOSTUNREACH)
  62. end
  63. include_examples 'probe tests with invalid result'
  64. end
  65. context 'when authentication fails' do
  66. let(:host) { ENV['MAIL_SERVER'] }
  67. let(:message_human) { [ 'Authentication failed.', 'This host cannot be reached.' ] }
  68. include_examples 'probe tests with invalid result'
  69. end
  70. context 'when doing a real test' do
  71. let(:host) { ENV['MAIL_SERVER'] }
  72. let(:user) { ENV['MAIL_ADDRESS'] }
  73. let(:password) { ENV['MAIL_PASS'] }
  74. it { is_expected.to include(result: 'ok') }
  75. end
  76. end
  77. describe '#outbound' do
  78. subject(:probe_result) { described_class.outbound(outbound_params, user) }
  79. let(:outbound_params) do
  80. {
  81. adapter: adapter,
  82. options: {
  83. host: host,
  84. port: port,
  85. start_tls: true,
  86. user: user,
  87. password: password,
  88. },
  89. }
  90. end
  91. let(:adapter) { 'smtp' }
  92. let(:port) { 25 }
  93. let(:user) { 'some@example.com' }
  94. let(:password) { 'password' }
  95. context 'with unknown adapter' do
  96. let(:adapter) { 'imap2' }
  97. let(:host) { 'nonexisting_host' }
  98. let(:message_human) { "Unknown adapter '#{adapter}'" }
  99. it { is_expected.to eq(expected_result_failed) }
  100. end
  101. context 'when network issues are present' do
  102. let(:host) { 'nonexisting_host' }
  103. let(:message_human) { 'The hostname could not be found.' }
  104. include_examples 'probe tests with invalid result'
  105. end
  106. context 'when an imap service with a blocked port is used' do
  107. let(:host) { '127.0.0.1' }
  108. let(:port) { 8 } # no service to be expected
  109. let(:message_human) { 'The connection was refused.' }
  110. include_examples 'probe tests with invalid result'
  111. end
  112. context 'when host is not reachable' do
  113. let(:host) { nil }
  114. let(:message_human) { [ 'This host cannot be reached.', 'There is no route to this host.' ] }
  115. before do
  116. allow(Socket).to receive(:tcp).and_raise(Errno::EHOSTUNREACH)
  117. end
  118. include_examples 'probe tests with invalid result'
  119. end
  120. context 'when authentication fails' do
  121. let(:host) { ENV['MAIL_SERVER'] }
  122. let(:port) { 25 }
  123. let(:message_human) { 'Authentication failed.' }
  124. include_examples 'probe tests with invalid result'
  125. end
  126. context 'when doing a real test' do
  127. let(:host) { ENV['MAIL_SERVER'] }
  128. let(:port) { 25 }
  129. let(:user) { ENV['MAIL_ADDRESS'] }
  130. let(:password) { ENV['MAIL_PASS'] }
  131. let(:outbound_params) do
  132. {
  133. adapter: adapter,
  134. options: {
  135. host: host,
  136. port: port,
  137. user: user,
  138. ssl: false,
  139. password: password,
  140. },
  141. }
  142. end
  143. it { is_expected.to include(result: 'ok') }
  144. end
  145. end
  146. describe '#full' do
  147. subject(:probe_result) { described_class.full(full_params) }
  148. let(:full_params) do
  149. {
  150. email: email,
  151. password: password,
  152. }
  153. end
  154. context 'when providing invalid information' do
  155. let(:email) { 'invalid_format' }
  156. let(:password) { 'somepass' }
  157. it 'contains all information for an invalid probe' do
  158. expect(probe_result)
  159. .to include(
  160. result: 'invalid'
  161. )
  162. .and not_include('setting')
  163. end
  164. end
  165. context 'when doing real tests' do
  166. let(:email) { ENV['MAIL_ADDRESS'] }
  167. let(:password) { ENV['MAIL_PASS'] }
  168. shared_examples 'do real testing' do
  169. it 'contains all information for a successful probe' do
  170. expect(probe_result).to include(result: 'ok')
  171. .and include(
  172. setting: include(
  173. inbound: include(
  174. options: include(
  175. host: host
  176. ),
  177. ),
  178. ),
  179. )
  180. .and include(
  181. setting: include(
  182. outbound: include(
  183. options: include(
  184. host: host,
  185. ),
  186. ),
  187. ),
  188. )
  189. end
  190. end
  191. context 'when doing a real test' do
  192. let(:host) { ENV['MAIL_SERVER'] }
  193. include_examples 'do real testing'
  194. end
  195. end
  196. end
  197. end