email_helper_test.rb 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class EmailHelperTest < ActiveSupport::TestCase
  4. test 'a mx_records' do
  5. domain = 'znuny.com'
  6. mx_domains = EmailHelper.mx_records(domain)
  7. assert_equal('arber.znuny.com', mx_domains[0])
  8. end
  9. test 'a email parser test' do
  10. user, domain = EmailHelper.parse_email('somebody@example.com')
  11. assert_equal('somebody', user)
  12. assert_equal('example.com', domain)
  13. user, domain = EmailHelper.parse_email('somebody+test@example.com')
  14. assert_equal('somebody+test', user)
  15. assert_equal('example.com', domain)
  16. user, domain = EmailHelper.parse_email('somebody+testexample.com')
  17. assert_not(user)
  18. assert_not(domain)
  19. end
  20. test 'provider test' do
  21. email = 'linus@kernel.org'
  22. password = 'some_pw'
  23. map = EmailHelper.provider(email, password)
  24. assert_equal('imap', map[:google_imap][:inbound][:adapter])
  25. assert_equal('imap.gmail.com', map[:google_imap][:inbound][:options][:host])
  26. assert_equal(993, map[:google_imap][:inbound][:options][:port])
  27. assert_equal(email, map[:google_imap][:inbound][:options][:user])
  28. assert_equal(password, map[:google_imap][:inbound][:options][:password])
  29. assert_equal('smtp', map[:google_imap][:outbound][:adapter])
  30. assert_equal('smtp.gmail.com', map[:google_imap][:outbound][:options][:host])
  31. assert_equal(25, map[:google_imap][:outbound][:options][:port])
  32. assert_equal(true, map[:google_imap][:outbound][:options][:start_tls])
  33. assert_equal(email, map[:google_imap][:outbound][:options][:user])
  34. assert_equal(password, map[:google_imap][:outbound][:options][:password])
  35. end
  36. test 'provider_inbound_mx' do
  37. email = 'linus@znuny.com'
  38. password = 'some_pw'
  39. user, domain = EmailHelper.parse_email(email)
  40. mx_domains = EmailHelper.mx_records(domain)
  41. map = EmailHelper.provider_inbound_mx(user, email, password, mx_domains)
  42. assert_equal('imap', map[0][:adapter])
  43. assert_equal('arber.znuny.com', map[0][:options][:host])
  44. assert_equal(993, map[0][:options][:port])
  45. assert_equal(user, map[0][:options][:user])
  46. assert_equal(password, map[0][:options][:password])
  47. assert_equal('imap', map[1][:adapter])
  48. assert_equal('arber.znuny.com', map[1][:options][:host])
  49. assert_equal(993, map[1][:options][:port])
  50. assert_equal(email, map[1][:options][:user])
  51. assert_equal(password, map[1][:options][:password])
  52. end
  53. test 'provider_inbound_guess' do
  54. email = 'linus@znuny.com'
  55. password = 'some_pw'
  56. user, domain = EmailHelper.parse_email(email)
  57. map = EmailHelper.provider_inbound_guess(user, email, password, domain)
  58. assert_equal('imap', map[0][:adapter])
  59. assert_equal('mail.znuny.com', map[0][:options][:host])
  60. assert_equal(993, map[0][:options][:port])
  61. assert_equal(user, map[0][:options][:user])
  62. assert_equal(password, map[0][:options][:password])
  63. assert_equal('imap', map[1][:adapter])
  64. assert_equal('mail.znuny.com', map[1][:options][:host])
  65. assert_equal(993, map[1][:options][:port])
  66. assert_equal(email, map[1][:options][:user])
  67. assert_equal(password, map[1][:options][:password])
  68. end
  69. test 'provider_outbound_mx' do
  70. email = 'linus@znuny.com'
  71. password = 'some_pw'
  72. user, domain = EmailHelper.parse_email(email)
  73. mx_domains = EmailHelper.mx_records(domain)
  74. map = EmailHelper.provider_outbound_mx(user, email, password, mx_domains)
  75. assert_equal('smtp', map[0][:adapter])
  76. assert_equal('arber.znuny.com', map[0][:options][:host])
  77. assert_equal(25, map[0][:options][:port])
  78. assert_equal(true, map[0][:options][:start_tls])
  79. assert_equal(user, map[0][:options][:user])
  80. assert_equal(password, map[0][:options][:password])
  81. assert_equal('smtp', map[1][:adapter])
  82. assert_equal('arber.znuny.com', map[1][:options][:host])
  83. assert_equal(25, map[1][:options][:port])
  84. assert_equal(true, map[1][:options][:start_tls])
  85. assert_equal(email, map[1][:options][:user])
  86. assert_equal(password, map[1][:options][:password])
  87. end
  88. test 'provider_outbound_guess' do
  89. email = 'linus@znuny.com'
  90. password = 'some_pw'
  91. user, domain = EmailHelper.parse_email(email)
  92. map = EmailHelper.provider_outbound_guess(user, email, password, domain)
  93. assert_equal('smtp', map[0][:adapter])
  94. assert_equal('mail.znuny.com', map[0][:options][:host])
  95. assert_equal(25, map[0][:options][:port])
  96. assert_equal(true, map[0][:options][:start_tls])
  97. assert_equal(user, map[0][:options][:user])
  98. assert_equal(password, map[0][:options][:password])
  99. assert_equal('smtp', map[1][:adapter])
  100. assert_equal('mail.znuny.com', map[1][:options][:host])
  101. assert_equal(25, map[1][:options][:port])
  102. assert_equal(true, map[1][:options][:start_tls])
  103. assert_equal(email, map[1][:options][:user])
  104. assert_equal(password, map[1][:options][:password])
  105. end
  106. test 'z probe_inbound' do
  107. # invalid adapter
  108. result = EmailHelper::Probe.inbound(
  109. adapter: 'imap2',
  110. options: {
  111. host: 'not_existsing_host',
  112. port: 993,
  113. ssl: true,
  114. user: 'some@example.com',
  115. password: 'password',
  116. }
  117. )
  118. assert_equal('failed', result[:result])
  119. # network issues
  120. result = EmailHelper::Probe.inbound(
  121. adapter: 'imap',
  122. options: {
  123. host: 'not_existsing_host',
  124. port: 993,
  125. ssl: true,
  126. user: 'some@example.com',
  127. password: 'password',
  128. }
  129. )
  130. assert_equal('invalid', result[:result])
  131. assert_equal('Hostname not found!', result[:message_human])
  132. assert_equal('not_existsing_host', result[:settings][:options][:host])
  133. result = EmailHelper::Probe.inbound(
  134. adapter: 'imap',
  135. options: {
  136. host: 'www.znuny.com',
  137. port: 993,
  138. ssl: true,
  139. user: 'some@example.com',
  140. password: 'password',
  141. }
  142. )
  143. assert_equal('invalid', result[:result])
  144. assert_equal('Connection refused!', result[:message_human])
  145. assert_equal('www.znuny.com', result[:settings][:options][:host])
  146. result = EmailHelper::Probe.inbound(
  147. adapter: 'imap',
  148. options: {
  149. host: '172.42.42.42',
  150. port: 993,
  151. ssl: true,
  152. user: 'some@example.com',
  153. password: 'password',
  154. }
  155. )
  156. assert_equal('invalid', result[:result])
  157. assert_match(/Host not reachable|No route to host/, result[:message_human])
  158. assert_equal('172.42.42.42', result[:settings][:options][:host])
  159. # gmail
  160. result = EmailHelper::Probe.inbound(
  161. adapter: 'imap',
  162. options: {
  163. host: 'imap.gmail.com',
  164. port: 993,
  165. ssl: true,
  166. user: 'some@example.com',
  167. password: 'password',
  168. }
  169. )
  170. assert_equal('invalid', result[:result])
  171. assert_equal('Authentication failed, username incorrect!', result[:message_human])
  172. assert_equal('imap.gmail.com', result[:settings][:options][:host])
  173. result = EmailHelper::Probe.inbound(
  174. adapter: 'imap',
  175. options: {
  176. host: 'imap.gmail.com',
  177. port: 993,
  178. ssl: true,
  179. user: 'frank.tailor05@googlemail.com',
  180. password: 'password',
  181. }
  182. )
  183. assert_equal('invalid', result[:result])
  184. # if we have to many failed logins, we need to handle another error message
  185. if result[:message_human] && !result[:message_human].empty?
  186. assert_equal('Authentication failed, invalid credentials!', result[:message_human])
  187. else
  188. assert_match(/Web login required/, result[:message])
  189. end
  190. assert_equal('imap.gmail.com', result[:settings][:options][:host])
  191. # dovecot
  192. result = EmailHelper::Probe.inbound(
  193. adapter: 'imap',
  194. options: {
  195. host: 'arber.znuny.com',
  196. port: 993,
  197. ssl: true,
  198. user: 'some@example.com',
  199. password: 'password',
  200. }
  201. )
  202. assert_equal('invalid', result[:result])
  203. assert_equal('Authentication failed!', result[:message_human])
  204. assert_equal('arber.znuny.com', result[:settings][:options][:host])
  205. # realtest - test I
  206. if !ENV['EMAILHELPER_MAILBOX_1']
  207. raise "Need EMAILHELPER_MAILBOX_1 as ENV variable like export EMAILHELPER_MAILBOX_1='unittestemailhelper01@znuny.com:somepass'"
  208. end
  209. mailbox_user = ENV['EMAILHELPER_MAILBOX_1'].split(':')[0]
  210. mailbox_password = ENV['EMAILHELPER_MAILBOX_1'].split(':')[1]
  211. user, domain = EmailHelper.parse_email(mailbox_user)
  212. result = EmailHelper::Probe.inbound(
  213. adapter: 'imap',
  214. options: {
  215. host: 'arber.znuny.com',
  216. port: 993,
  217. ssl: true,
  218. user: user,
  219. password: mailbox_password,
  220. }
  221. )
  222. assert_equal('ok', result[:result])
  223. end
  224. test 'z probe_outbound' do
  225. # invalid adapter
  226. result = EmailHelper::Probe.outbound(
  227. {
  228. adapter: 'smtp2',
  229. options: {
  230. host: 'not_existsing_host',
  231. port: 25,
  232. start_tls: true,
  233. user: 'some@example.com',
  234. password: 'password',
  235. },
  236. },
  237. 'some@example.com',
  238. )
  239. assert_equal('failed', result[:result])
  240. # network issues
  241. result = EmailHelper::Probe.outbound(
  242. {
  243. adapter: 'smtp',
  244. options: {
  245. host: 'not_existsing_host',
  246. port: 25,
  247. start_tls: true,
  248. user: 'some@example.com',
  249. password: 'password',
  250. }
  251. },
  252. 'some@example.com',
  253. )
  254. assert_equal('invalid', result[:result])
  255. assert_equal('Hostname not found!', result[:message_human])
  256. assert_equal('not_existsing_host', result[:settings][:options][:host])
  257. result = EmailHelper::Probe.outbound(
  258. {
  259. adapter: 'smtp',
  260. options: {
  261. host: 'www.znuny.com',
  262. port: 26,
  263. start_tls: true,
  264. user: 'some@example.com',
  265. password: 'password',
  266. }
  267. },
  268. 'some@example.com',
  269. )
  270. assert_equal('invalid', result[:result])
  271. assert_equal('Connection refused!', result[:message_human])
  272. assert_equal('www.znuny.com', result[:settings][:options][:host])
  273. result = EmailHelper::Probe.outbound(
  274. {
  275. adapter: 'smtp',
  276. options: {
  277. host: '172.42.42.42',
  278. port: 25,
  279. start_tls: true,
  280. user: 'some@example.com',
  281. password: 'password',
  282. }
  283. },
  284. 'some@example.com',
  285. )
  286. assert_equal('invalid', result[:result])
  287. assert_match(/Host not reachable|No route to host/, result[:message_human])
  288. assert_equal('172.42.42.42', result[:settings][:options][:host])
  289. # gmail
  290. result = EmailHelper::Probe.outbound(
  291. {
  292. adapter: 'smtp',
  293. options: {
  294. host: 'smtp.gmail.com',
  295. port: 25,
  296. start_tls: true,
  297. user: 'some@example.com',
  298. password: 'password',
  299. }
  300. },
  301. 'some@example.com',
  302. )
  303. assert_equal('invalid', result[:result])
  304. assert_equal('Authentication failed!', result[:message_human])
  305. assert_equal('smtp.gmail.com', result[:settings][:options][:host])
  306. result = EmailHelper::Probe.outbound(
  307. {
  308. adapter: 'smtp',
  309. options: {
  310. host: 'smtp.gmail.com',
  311. port: 25,
  312. start_tls: true,
  313. user: 'frank.tailor05@googlemail.com',
  314. password: 'password',
  315. }
  316. },
  317. 'some@example.com',
  318. )
  319. assert_equal('invalid', result[:result])
  320. # if we have to many failed logins, we need to handle another error message
  321. if result[:message_human] && !result[:message_human].empty?
  322. assert_equal('Authentication failed!', result[:message_human])
  323. else
  324. assert_match(/Please log in with your web browser and then try again/, result[:message])
  325. end
  326. assert_equal('smtp.gmail.com', result[:settings][:options][:host])
  327. # dovecot
  328. result = EmailHelper::Probe.outbound(
  329. {
  330. adapter: 'smtp',
  331. options: {
  332. host: 'arber.znuny.com',
  333. port: 25,
  334. start_tls: true,
  335. user: 'some@example.com',
  336. password: 'password',
  337. }
  338. },
  339. 'some@example.com',
  340. )
  341. assert_equal('invalid', result[:result])
  342. assert_equal('Authentication failed!', result[:message_human])
  343. assert_equal('arber.znuny.com', result[:settings][:options][:host])
  344. # realtest - test I
  345. if !ENV['EMAILHELPER_MAILBOX_1']
  346. raise "Need EMAILHELPER_MAILBOX_1 as ENV variable like export EMAILHELPER_MAILBOX_1='unittestemailhelper01@znuny.com:somepass'"
  347. end
  348. mailbox_user = ENV['EMAILHELPER_MAILBOX_1'].split(':')[0]
  349. mailbox_password = ENV['EMAILHELPER_MAILBOX_1'].split(':')[1]
  350. user, domain = EmailHelper.parse_email(mailbox_user)
  351. result = EmailHelper::Probe.outbound(
  352. {
  353. adapter: 'smtp',
  354. options: {
  355. host: 'arber.znuny.com',
  356. port: 25,
  357. start_tls: true,
  358. user: user,
  359. password: mailbox_password,
  360. }
  361. },
  362. mailbox_user,
  363. )
  364. assert_equal('ok', result[:result])
  365. end
  366. test 'zz probe' do
  367. result = EmailHelper::Probe.full(
  368. email: 'invalid_format',
  369. password: 'somepass',
  370. )
  371. assert_equal('invalid', result[:result])
  372. assert_not(result[:setting])
  373. # realtest - test I, with imap
  374. if !ENV['EMAILHELPER_MAILBOX_1']
  375. raise "Need EMAILHELPER_MAILBOX_1 as ENV variable like export EMAILHELPER_MAILBOX_1='unittestemailhelper01@znuny.com:somepass'"
  376. end
  377. mailbox_user = ENV['EMAILHELPER_MAILBOX_1'].split(':')[0]
  378. mailbox_password = ENV['EMAILHELPER_MAILBOX_1'].split(':')[1]
  379. result = EmailHelper::Probe.full(
  380. email: mailbox_user,
  381. password: mailbox_password,
  382. )
  383. assert_equal('ok', result[:result])
  384. assert_equal('arber.znuny.com', result[:setting][:inbound][:options][:host])
  385. assert_equal('arber.znuny.com', result[:setting][:outbound][:options][:host])
  386. # realtest - test II, gmail with only pop3
  387. if !ENV['EMAILHELPER_MAILBOX_2']
  388. raise "Need EMAILHELPER_MAILBOX_2 as ENV variable like export EMAILHELPER_MAILBOX_2='hansb36621@gmail.com:somepass'"
  389. end
  390. mailbox_user = ENV['EMAILHELPER_MAILBOX_2'].split(':')[0]
  391. mailbox_password = ENV['EMAILHELPER_MAILBOX_2'].split(':')[1]
  392. result = EmailHelper::Probe.full(
  393. email: mailbox_user,
  394. password: mailbox_password,
  395. )
  396. assert_equal(nil, result[:reason])
  397. assert_equal('ok', result[:result])
  398. assert_equal('pop.gmail.com', result[:setting][:inbound][:options][:host])
  399. assert_equal('smtp.gmail.com', result[:setting][:outbound][:options][:host])
  400. end
  401. test 'zz verify' do
  402. # realtest - test I, with imap
  403. if !ENV['EMAILHELPER_MAILBOX_1']
  404. raise "Need EMAILHELPER_MAILBOX_1 as ENV variable like export EMAILHELPER_MAILBOX_1='unittestemailhelper01@znuny.com:somepass'"
  405. end
  406. mailbox_user = ENV['EMAILHELPER_MAILBOX_1'].split(':')[0]
  407. mailbox_password = ENV['EMAILHELPER_MAILBOX_1'].split(':')[1]
  408. user, domain = EmailHelper.parse_email(mailbox_user)
  409. result = EmailHelper::Verify.email(
  410. inbound: {
  411. adapter: 'imap',
  412. options: {
  413. host: 'arber.znuny.com',
  414. port: 993,
  415. ssl: true,
  416. user: user,
  417. password: mailbox_password,
  418. },
  419. },
  420. outbound: {
  421. adapter: 'smtp',
  422. options: {
  423. host: 'arber.znuny.com',
  424. port: 25,
  425. start_tls: true,
  426. user: user,
  427. password: mailbox_password,
  428. },
  429. },
  430. sender: mailbox_user,
  431. )
  432. assert_equal('ok', result[:result])
  433. # realtest - test II, gmail with pop3
  434. if !ENV['EMAILHELPER_MAILBOX_2']
  435. raise "Need EMAILHELPER_MAILBOX_2 as ENV variable like export EMAILHELPER_MAILBOX_2='hansb36621@gmail.com:somepass'"
  436. end
  437. mailbox_user = ENV['EMAILHELPER_MAILBOX_2'].split(':')[0]
  438. mailbox_password = ENV['EMAILHELPER_MAILBOX_2'].split(':')[1]
  439. user, domain = EmailHelper.parse_email(mailbox_user)
  440. result = EmailHelper::Verify.email(
  441. inbound: {
  442. adapter: 'pop3',
  443. options: {
  444. host: 'pop.gmail.com',
  445. port: 995,
  446. ssl: true,
  447. user: mailbox_user,
  448. password: mailbox_password,
  449. },
  450. },
  451. outbound: {
  452. adapter: 'smtp',
  453. options: {
  454. host: 'smtp.gmail.com',
  455. port: 25,
  456. start_tls: true,
  457. user: mailbox_user,
  458. password: mailbox_password,
  459. },
  460. },
  461. sender: mailbox_user,
  462. )
  463. assert_equal('ok', result[:result])
  464. end
  465. end