email_helper_spec.rb 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe EmailHelper do
  4. # This should continue to be a live test using DNS.
  5. describe '#mx_records', integration: true do
  6. context 'when checking for regular domains' do
  7. subject(:result) { described_class.mx_records(domain) }
  8. let(:domain) { 'zammad.com' }
  9. it { is_expected.to eq(['mx2.zammad.com']) }
  10. end
  11. end
  12. describe '#parse_email' do
  13. subject(:result) { described_class.parse_email(mail_address) }
  14. context 'when parsing a well formatted mail address' do
  15. let(:mail_address) { 'somebody@example.com' }
  16. let(:result) { [ 'somebody', 'example.com' ] }
  17. it { is_expected.to eq(result) }
  18. end
  19. context 'when parsing another well formatted mail address' do
  20. let(:mail_address) { 'somebody+test@example.com' }
  21. let(:result) { [ 'somebody+test', 'example.com' ] }
  22. it { is_expected.to eq(result) }
  23. end
  24. context 'when parsing an invalid mail address' do
  25. let(:mail_address) { 'somebody+testexample.com' }
  26. let(:result) { [ nil, nil ] }
  27. it { is_expected.to eq(result) }
  28. end
  29. end
  30. describe '#provider' do
  31. subject(:result) { described_class.provider(mail_address, password) }
  32. context 'when checking for gmail' do
  33. let(:mail_address) { 'linus@kernel.org' }
  34. let(:password) { 'some_pw' }
  35. context 'when inbound' do
  36. let(:ssl_options) { { key: 'ssl', value: true } }
  37. let(:expected_result_inbound) { provider_setting('imap', 993, mail_address, password, ssl_options) }
  38. it 'contains correct inbound provider information' do
  39. expect(described_class.provider(mail_address, password)[:google_imap][:inbound]).to eq(expected_result_inbound)
  40. end
  41. end
  42. context 'when outbound' do
  43. let(:ssl_options) { { key: 'start_tls', value: true } }
  44. let(:expected_result_outbound) { provider_setting('smtp', 587, mail_address, password, ssl_options) }
  45. it 'contains correct outbound provider information' do
  46. expect(described_class.provider(mail_address, password)[:google_imap][:outbound]).to eq(expected_result_outbound)
  47. end
  48. end
  49. def provider_setting(adapter, port, user, password, ssl_options)
  50. {
  51. adapter: adapter,
  52. options: {
  53. host: "#{adapter}.gmail.com",
  54. port: port,
  55. user: user,
  56. password: password,
  57. ssl_options[:key].to_sym => ssl_options[:value],
  58. },
  59. }
  60. end
  61. end
  62. end
  63. describe '#provider_inbound_mx' do
  64. subject(:result) { described_class.provider_inbound_mx(user, email, password, [mx_domain]) }
  65. let(:email) { 'linus@zammad.com' }
  66. let(:password) { 'some_pw' }
  67. let(:user) { 'linus' }
  68. let(:domain) { 'zammad.com' }
  69. let(:mx_domain) { 'mx2.zammad.com' }
  70. let(:expected_result) do
  71. [
  72. provider_inbound_mx_setting(mx_domain, 993, user, password),
  73. provider_inbound_mx_setting(mx_domain, 993, email, password),
  74. ]
  75. end
  76. def provider_inbound_mx_setting(host, port, user, password)
  77. {
  78. adapter: 'imap',
  79. options: {
  80. host: host,
  81. port: port,
  82. ssl: true,
  83. user: user,
  84. password: password,
  85. },
  86. }
  87. end
  88. it { is_expected.to eq(expected_result) }
  89. end
  90. describe '#provider_inbound_guess' do
  91. subject(:result) { described_class.provider_inbound_guess(user, email, password, domain) }
  92. let(:email) { 'linus@zammad.com' }
  93. let(:password) { 'some_pw' }
  94. let(:user) { 'linus' }
  95. let(:domain) { 'zammad.com' }
  96. let(:expected_result) do
  97. [
  98. provider_inbound_guess_setting('imap', 'mail.zammad.com', 993, user, password),
  99. provider_inbound_guess_setting('imap', 'mail.zammad.com', 993, email, password),
  100. provider_inbound_guess_setting('imap', 'imap.zammad.com', 993, user, password),
  101. provider_inbound_guess_setting('imap', 'imap.zammad.com', 993, email, password),
  102. provider_inbound_guess_setting('pop3', 'mail.zammad.com', 995, user, password),
  103. provider_inbound_guess_setting('pop3', 'mail.zammad.com', 995, email, password),
  104. provider_inbound_guess_setting('pop3', 'pop.zammad.com', 995, user, password),
  105. provider_inbound_guess_setting('pop3', 'pop.zammad.com', 995, email, password),
  106. provider_inbound_guess_setting('pop3', 'pop3.zammad.com', 995, user, password),
  107. provider_inbound_guess_setting('pop3', 'pop3.zammad.com', 995, email, password),
  108. ]
  109. end
  110. def provider_inbound_guess_setting(adapter, host, port, user, password)
  111. {
  112. adapter: adapter,
  113. options: {
  114. host: host,
  115. port: port,
  116. ssl: true,
  117. user: user,
  118. password: password,
  119. }
  120. }
  121. end
  122. it { is_expected.to eq(expected_result) }
  123. end
  124. describe '#provider_outbound_mx' do
  125. subject(:result) { described_class.provider_outbound_mx(user, email, password, [mx_domain]) }
  126. let(:email) { 'linus@zammad.com' }
  127. let(:password) { 'some_pw' }
  128. let(:user) { 'linus' }
  129. let(:domain) { 'zammad.com' }
  130. let(:mx_domain) { 'mx.zammad.com' }
  131. let(:expected_result) do
  132. [
  133. provider_outbound_mx_setting(mx_domain, 465, true, user, password),
  134. provider_outbound_mx_setting(mx_domain, 465, true, email, password),
  135. provider_outbound_mx_setting(mx_domain, 587, nil, user, password),
  136. provider_outbound_mx_setting(mx_domain, 587, nil, email, password),
  137. provider_outbound_mx_setting(mx_domain, 25, true, user, password),
  138. provider_outbound_mx_setting(mx_domain, 25, true, email, password),
  139. ]
  140. end
  141. def provider_outbound_mx_setting(host, port, with_ssl, user, password)
  142. options = {
  143. host: host,
  144. port: port,
  145. user: user,
  146. password: password,
  147. }
  148. if with_ssl.present?
  149. options[:start_tls] = with_ssl
  150. end
  151. {
  152. adapter: 'smtp',
  153. options: options,
  154. }
  155. end
  156. it { is_expected.to eq(expected_result) }
  157. end
  158. describe '#provider_outbound_guess' do
  159. subject(:result) { described_class.provider_outbound_guess(user, email, password, domain) }
  160. let(:email) { 'linus@zammad.com' }
  161. let(:password) { 'some_pw' }
  162. let(:user) { 'linus' }
  163. let(:domain) { 'zammad.com' }
  164. let(:expected_result) do
  165. [
  166. provider_outbound_guess_setting('mail.zammad.com', 465, user, password),
  167. provider_outbound_guess_setting('mail.zammad.com', 465, email, password),
  168. provider_outbound_guess_setting('smtp.zammad.com', 465, user, password),
  169. provider_outbound_guess_setting('smtp.zammad.com', 465, email, password),
  170. provider_outbound_guess_setting('mail.zammad.com', 587, user, password),
  171. provider_outbound_guess_setting('mail.zammad.com', 587, email, password),
  172. provider_outbound_guess_setting('smtp.zammad.com', 587, user, password),
  173. provider_outbound_guess_setting('smtp.zammad.com', 587, email, password),
  174. provider_outbound_guess_setting('mail.zammad.com', 25, user, password),
  175. provider_outbound_guess_setting('mail.zammad.com', 25, email, password),
  176. provider_outbound_guess_setting('smtp.zammad.com', 25, user, password),
  177. provider_outbound_guess_setting('smtp.zammad.com', 25, email, password),
  178. ]
  179. end
  180. def provider_outbound_guess_setting(host, port, user, password)
  181. {
  182. adapter: 'smtp',
  183. options: {
  184. host: host,
  185. port: port,
  186. start_tls: true,
  187. user: user,
  188. password: password,
  189. }
  190. }
  191. end
  192. it { is_expected.to eq(expected_result) }
  193. end
  194. end