You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

520 lines
21 KiB

  1. # -*- coding: utf-8 -*-
  2. # License AGPL-3: Antiun Ingenieria S.L. - Antonio Espinosa
  3. # See README.rst file on addon root folder for more details
  4. import json
  5. from openerp.tests.common import TransactionCase
  6. from openerp.tools.safe_eval import safe_eval
  7. class TestMailMandrill(TransactionCase):
  8. def setUp(self):
  9. super(TestMailMandrill, self).setUp()
  10. message_obj = self.env['mail.mandrill.message']
  11. self.partner_01 = self.env.ref('base.res_partner_1')
  12. self.partner_02 = self.env.ref('base.res_partner_2')
  13. self.model = 'res.partner'
  14. self.res_id = self.partner_02.id
  15. self.mandrill_message_id = '0123456789abcdef0123456789abcdef'
  16. self.event_deferral = {
  17. 'msg': {
  18. 'sender': 'username01@example.com',
  19. 'tags': [],
  20. 'smtp_events': [
  21. {
  22. 'destination_ip': '123.123.123.123',
  23. 'diag': 'Event description',
  24. 'source_ip': '145.145.145.145',
  25. 'ts': 1455192896,
  26. 'type': 'deferred',
  27. 'size': 19513
  28. },
  29. ],
  30. 'ts': 1455008558,
  31. 'clicks': [],
  32. 'resends': [],
  33. 'state': 'deferred',
  34. '_version': '1abcdefghijkABCDEFGHIJ',
  35. 'template': None,
  36. '_id': self.mandrill_message_id,
  37. 'email': 'username02@example.com',
  38. 'metadata': {
  39. 'odoo_id': self.res_id,
  40. 'odoo_db': 'test',
  41. 'odoo_model': self.model,
  42. },
  43. 'opens': [],
  44. 'subject': 'My favorite subject'
  45. },
  46. 'diag': '454 4.7.1 <username02@example.com>: Relay access denied',
  47. '_id': self.mandrill_message_id,
  48. 'event': 'deferral',
  49. 'ts': 1455201028,
  50. }
  51. self.event_send = {
  52. 'msg': {
  53. '_id': self.mandrill_message_id,
  54. 'subaccount': None,
  55. 'tags': [],
  56. 'smtp_events': [],
  57. 'ts': 1455201157,
  58. 'email': 'username02@example.com',
  59. 'metadata': {
  60. 'odoo_id': self.res_id,
  61. 'odoo_db': 'test',
  62. 'odoo_model': self.model,
  63. },
  64. 'state': 'sent',
  65. 'sender': 'username01@example.com',
  66. 'template': None,
  67. 'reject': None,
  68. 'resends': [],
  69. 'clicks': [],
  70. 'opens': [],
  71. 'subject': 'My favorite subject',
  72. },
  73. '_id': self.mandrill_message_id,
  74. 'event': 'send',
  75. 'ts': 1455201159,
  76. }
  77. self.event_hard_bounce = {
  78. 'msg': {
  79. 'bounce_description': 'bad_mailbox',
  80. 'sender': 'username01@example.com',
  81. 'tags': [],
  82. 'diag': 'smtp;550 5.4.1 [username02@example.com]: '
  83. 'Recipient address rejected: Access denied',
  84. 'smtp_events': [],
  85. 'ts': 1455194565,
  86. 'template': None,
  87. '_version': 'abcdefghi123456ABCDEFG',
  88. 'metadata': {
  89. 'odoo_id': self.res_id,
  90. 'odoo_db': 'test',
  91. 'odoo_model': self.model,
  92. },
  93. 'resends': [],
  94. 'state': 'bounced',
  95. 'bgtools_code': 10,
  96. '_id': self.mandrill_message_id,
  97. 'email': 'username02@example.com',
  98. 'subject': 'My favorite subject',
  99. },
  100. '_id': self.mandrill_message_id,
  101. 'event': 'hard_bounce',
  102. 'ts': 1455195340
  103. }
  104. self.event_soft_bounce = {
  105. 'msg': {
  106. 'bounce_description': 'general',
  107. 'sender': 'username01@example.com',
  108. 'tags': [],
  109. 'diag': 'X-Notes; Error transferring to FQDN.EXAMPLE.COM\n ; '
  110. 'SMTP Protocol Returned a Permanent Error 550 5.7.1 '
  111. 'Unable to relay\n\n--==ABCDEFGHIJK12345678ABCDEFGH',
  112. 'smtp_events': [],
  113. 'ts': 1455194562,
  114. 'template': None,
  115. '_version': 'abcdefghi123456ABCDEFG',
  116. 'metadata': {
  117. 'odoo_id': self.res_id,
  118. 'odoo_db': 'test',
  119. 'odoo_model': self.model,
  120. },
  121. 'resends': [],
  122. 'state': 'soft-bounced',
  123. 'bgtools_code': 40,
  124. '_id': self.mandrill_message_id,
  125. 'email': 'username02@example.com',
  126. 'subject': 'My favorite subject',
  127. },
  128. '_id': self.mandrill_message_id,
  129. 'event': 'soft_bounce',
  130. 'ts': 1455195622
  131. }
  132. self.event_open = {
  133. 'ip': '111.111.111.111',
  134. 'ts': 1455189075,
  135. 'location': {
  136. 'country_short': 'PT',
  137. 'city': 'Porto',
  138. 'country': 'Portugal',
  139. 'region': 'Porto',
  140. 'longitude': -8.61098957062,
  141. 'postal_code': '-',
  142. 'latitude': 41.1496086121,
  143. 'timezone': '+01:00',
  144. },
  145. 'msg': {
  146. 'sender': 'username01@example.com',
  147. 'tags': [],
  148. 'smtp_events': [
  149. {
  150. 'destination_ip': '222.222.222.222',
  151. 'diag': '250 2.0.0 ABCDEFGHIJK123456ABCDE mail '
  152. 'accepted for delivery',
  153. 'source_ip': '111.1.1.1',
  154. 'ts': 1455185877,
  155. 'type': 'sent',
  156. 'size': 30276,
  157. },
  158. ],
  159. 'ts': 1455185876,
  160. 'clicks': [],
  161. 'metadata': {
  162. 'odoo_id': self.res_id,
  163. 'odoo_db': 'test',
  164. 'odoo_model': self.model,
  165. },
  166. 'resends': [],
  167. 'state': 'sent',
  168. '_version': 'abcdefghi123456ABCDEFG',
  169. 'template': None,
  170. '_id': self.mandrill_message_id,
  171. 'email': 'username02@example.com',
  172. 'opens': [
  173. {
  174. 'ip': '111.111.111.111',
  175. 'ua': 'Windows/Windows 7/Outlook 2010/Outlook 2010',
  176. 'ts': 1455186247,
  177. 'location':
  178. 'Porto, PT'
  179. }, {
  180. 'ip': '111.111.111.111',
  181. 'ua': 'Windows/Windows 7/Outlook 2010/Outlook 2010',
  182. 'ts': 1455189075,
  183. 'location': 'Porto, PT'
  184. },
  185. ],
  186. 'subject': 'My favorite subject',
  187. },
  188. '_id': self.mandrill_message_id,
  189. 'user_agent_parsed': {
  190. 'ua_name': 'Outlook 2010',
  191. 'mobile': False,
  192. 'ua_company_url': 'http://www.microsoft.com/',
  193. 'os_icon': 'http://cdn.mandrill.com/img/email-client-icons/'
  194. 'windows-7.png',
  195. 'os_company': 'Microsoft Corporation.',
  196. 'ua_version': None,
  197. 'os_name': 'Windows 7',
  198. 'ua_family': 'Outlook 2010',
  199. 'os_url': 'http://en.wikipedia.org/wiki/Windows_7',
  200. 'os_company_url': 'http://www.microsoft.com/',
  201. 'ua_company': 'Microsoft Corporation.',
  202. 'os_family': 'Windows',
  203. 'type': 'Email Client',
  204. 'ua_icon': 'http://cdn.mandrill.com/img/email-client-icons/'
  205. 'outlook-2010.png',
  206. 'ua_url': 'http://en.wikipedia.org/wiki/Microsoft_Outlook',
  207. },
  208. 'event': 'open',
  209. 'user_agent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; '
  210. 'Trident/7.0; SLCC2; .NET CLR 2.0.50727; '
  211. '.NET CLR 3.5.30729; .NET CLR 3.0.30729; '
  212. 'Media Center PC 6.0; .NET4.0C; .NET4.0E; BRI/2; '
  213. 'Tablet PC 2.0; GWX:DOWNLOADED; '
  214. 'Microsoft Outlook 14.0.7166; ms-office; '
  215. 'MSOffice 14)',
  216. }
  217. self.event_click = {
  218. 'url': 'http://www.example.com/index.php',
  219. 'ip': '111.111.111.111',
  220. 'ts': 1455186402,
  221. 'user_agent': 'Mozilla/5.0 (Windows NT 6.1) '
  222. 'AppleWebKit/537.36 (KHTML, like Gecko) '
  223. 'Chrome/48.0.2564.103 Safari/537.36',
  224. 'msg': {
  225. 'sender': 'username01@example.com',
  226. 'tags': [],
  227. 'smtp_events': [
  228. {
  229. 'destination_ip': '222.222.222.222',
  230. 'diag': '250 2.0.0 Ok: queued as 12345678',
  231. 'source_ip': '111.1.1.1',
  232. 'ts': 1455186065,
  233. 'type': 'sent',
  234. 'size': 30994,
  235. },
  236. ],
  237. 'ts': 1455186063,
  238. 'clicks': [
  239. {
  240. 'url': 'http://www.example.com/index.php',
  241. 'ip': '111.111.111.111',
  242. 'ua': 'Windows/Windows 7/Chrome/Chrome 48.0.2564.103',
  243. 'ts': 1455186402,
  244. 'location': 'Madrid, ES',
  245. },
  246. ],
  247. 'metadata': {
  248. 'odoo_id': self.res_id,
  249. 'odoo_db': 'test',
  250. 'odoo_model': self.model,
  251. },
  252. 'resends': [],
  253. 'state': 'sent',
  254. '_version': 'abcdefghi123456ABCDEFG',
  255. 'template': None,
  256. '_id': self.mandrill_message_id,
  257. 'email': 'username02@example.com',
  258. 'opens': [
  259. {
  260. 'ip': '111.111.111.111',
  261. 'ua': 'Windows/Windows 7/Chrome/Chrome 48.0.2564.103',
  262. 'ts': 1455186402,
  263. 'location': 'Madrid, ES',
  264. },
  265. ],
  266. 'subject': 'My favorite subject',
  267. },
  268. '_id': self.mandrill_message_id,
  269. 'user_agent_parsed': {
  270. 'ua_name': 'Chrome 48.0.2564.103',
  271. 'mobile': False,
  272. 'ua_company_url': 'http://www.google.com/',
  273. 'os_icon': 'http://cdn.mandrill.com/img/email-client-icons/'
  274. 'windows-7.png',
  275. 'os_company': 'Microsoft Corporation.',
  276. 'ua_version': '48.0.2564.103',
  277. 'os_name': 'Windows 7',
  278. 'ua_family': 'Chrome',
  279. 'os_url': 'http://en.wikipedia.org/wiki/Windows_7',
  280. 'os_company_url': 'http://www.microsoft.com/',
  281. 'ua_company': 'Google Inc.',
  282. 'os_family': 'Windows',
  283. 'type': 'Browser',
  284. 'ua_icon': 'http://cdn.mandrill.com/img/email-client-icons/'
  285. 'chrome.png',
  286. 'ua_url': 'http://www.google.com/chrome',
  287. },
  288. 'event': 'click',
  289. 'location': {
  290. 'country_short': 'ES',
  291. 'city': 'Madrid',
  292. 'country': 'Spain',
  293. 'region': 'Madrid',
  294. 'longitude': -3.70255994797,
  295. 'postal_code': '-',
  296. 'latitude': 40.4165000916,
  297. 'timezone': '+02:00',
  298. },
  299. }
  300. self.event_spam = {
  301. 'msg': {
  302. 'sender': 'username01@example.com',
  303. 'tags': [],
  304. 'smtp_events': [],
  305. 'ts': 1455186007,
  306. 'clicks': [],
  307. 'metadata': {
  308. 'odoo_id': self.res_id,
  309. 'odoo_db': 'test',
  310. 'odoo_model': self.model,
  311. },
  312. 'resends': [],
  313. 'state': 'spam',
  314. '_version': 'abcdefghi123456ABCDEFG',
  315. 'template': None,
  316. '_id': self.mandrill_message_id,
  317. 'email': 'username02@example.com',
  318. 'opens': [],
  319. 'subject': 'My favorite subject',
  320. },
  321. '_id': self.mandrill_message_id,
  322. 'event': 'spam',
  323. 'ts': 1455186366
  324. }
  325. self.event_reject = {
  326. 'msg': {
  327. '_id': self.mandrill_message_id,
  328. 'subaccount': None,
  329. 'tags': [],
  330. 'smtp_events': [],
  331. 'ts': 1455194291,
  332. 'email': 'username02@example.com',
  333. 'metadata': {
  334. 'odoo_id': self.res_id,
  335. 'odoo_db': 'test',
  336. 'odoo_model': self.model,
  337. },
  338. 'state': 'rejected',
  339. 'sender': 'username01@example.com',
  340. 'template': None,
  341. 'reject': None,
  342. 'resends': [],
  343. 'clicks': [],
  344. 'opens': [],
  345. 'subject': 'My favorite subject',
  346. },
  347. '_id': self.mandrill_message_id,
  348. 'event': 'reject',
  349. 'ts': 1455194291,
  350. }
  351. self.event_unsub = {
  352. 'msg': {
  353. '_id': self.mandrill_message_id,
  354. 'subaccount': None,
  355. 'tags': [],
  356. 'smtp_events': [],
  357. 'ts': 1455194291,
  358. 'email': 'username02@example.com',
  359. 'metadata': {
  360. 'odoo_id': self.res_id,
  361. 'odoo_db': 'test',
  362. 'odoo_model': self.model,
  363. },
  364. 'state': 'unsub',
  365. 'sender': 'username01@example.com',
  366. 'template': None,
  367. 'reject': None,
  368. 'resends': [],
  369. 'clicks': [],
  370. 'opens': [],
  371. 'subject': 'My favorite subject',
  372. },
  373. '_id': self.mandrill_message_id,
  374. 'event': 'unsub',
  375. 'ts': 1455194291,
  376. }
  377. self.message = message_obj.create(
  378. message_obj._message_prepare(
  379. self.mandrill_message_id, 'deferral', self.event_deferral))
  380. # Test Unit: mail_mail.py
  381. def test_mandrill_headers_add(self):
  382. mail_obj = self.env['mail.mail']
  383. message = self.env['mail.message'].create({
  384. 'author_id': self.partner_01.id,
  385. 'subject': 'Test subject',
  386. 'body': 'Test body',
  387. 'partner_ids': [(4, self.partner_02.id)],
  388. 'model': self.model,
  389. 'res_id': self.res_id,
  390. })
  391. mail = mail_obj.create({
  392. 'mail_message_id': message.id,
  393. })
  394. mail._mandrill_headers_add()
  395. headers = safe_eval(mail.headers)
  396. self.assertIn('X-MC-Metadata', headers)
  397. metadata = json.loads(headers.get('X-MC-Metadata', '[]'))
  398. self.assertIn('odoo_db', metadata)
  399. self.assertIn('odoo_model', metadata)
  400. self.assertIn('odoo_id', metadata)
  401. self.assertEqual(metadata['odoo_model'], self.model)
  402. self.assertEqual(metadata['odoo_id'], self.res_id)
  403. # Test Unit: mail_mandrill_message.py
  404. def test_message_prepare(self):
  405. data = self.env['mail.mandrill.message']._message_prepare(
  406. self.mandrill_message_id, 'deferral', self.event_deferral)
  407. self.assertEqual(data['mandrill_id'], self.mandrill_message_id)
  408. self.assertEqual(data['timestamp'],
  409. self.event_deferral['msg']['ts'])
  410. self.assertEqual(data['recipient'],
  411. self.event_deferral['msg']['email'])
  412. self.assertEqual(data['sender'],
  413. self.event_deferral['msg']['sender'])
  414. self.assertEqual(data['name'],
  415. self.event_deferral['msg']['subject'])
  416. def test_event_prepare(self):
  417. data = self.env['mail.mandrill.message']._event_prepare(
  418. self.message, 'deferral', self.event_deferral)
  419. self.assertEqual(self.message.state, 'deferred')
  420. self.assertEqual(data['message_id'], self.message.id)
  421. self.assertEqual(data['event_type'], 'deferral')
  422. self.assertEqual(data['timestamp'], self.event_deferral['ts'])
  423. def test_process(self):
  424. event = self.env['mail.mandrill.message'].process(
  425. self.mandrill_message_id, 'deferral', self.event_deferral)
  426. self.assertEqual(event.message_id.mandrill_id,
  427. self.mandrill_message_id)
  428. self.assertEqual(event.message_id.state, 'deferred')
  429. self.assertEqual(event.event_type, 'deferral')
  430. self.assertEqual(event.timestamp, self.event_deferral['ts'])
  431. # Test Unit: mail_mandrill_event.py
  432. def test_process_send(self):
  433. data = self.env['mail.mandrill.event'].process_send(
  434. self.message, self.event_send)
  435. self.assertEqual(self.message.state, 'sent')
  436. self.assertEqual(data['message_id'], self.message.id)
  437. self.assertEqual(data['event_type'], 'send')
  438. self.assertEqual(data['timestamp'], self.event_send['ts'])
  439. def test_process_deferral(self):
  440. data = self.env['mail.mandrill.event'].process_deferral(
  441. self.message, self.event_deferral)
  442. self.assertEqual(self.message.state, 'deferred')
  443. self.assertEqual(data['message_id'], self.message.id)
  444. self.assertEqual(data['event_type'], 'deferral')
  445. self.assertEqual(data['timestamp'], self.event_deferral['ts'])
  446. def test_process_hard_bounce(self):
  447. data = self.env['mail.mandrill.event'].process_hard_bounce(
  448. self.message, self.event_hard_bounce)
  449. self.assertEqual(self.message.state, 'bounced')
  450. self.assertEqual(self.message.bounce_type,
  451. self.event_hard_bounce['msg']['bounce_description'])
  452. self.assertEqual(self.message.bounce_description,
  453. self.event_hard_bounce['msg']['diag'])
  454. self.assertEqual(data['message_id'], self.message.id)
  455. self.assertEqual(data['event_type'], 'hard_bounce')
  456. self.assertEqual(data['timestamp'], self.event_hard_bounce['ts'])
  457. def test_process_soft_bounce(self):
  458. data = self.env['mail.mandrill.event'].process_soft_bounce(
  459. self.message, self.event_soft_bounce)
  460. self.assertEqual(self.message.state, 'bounced')
  461. self.assertEqual(self.message.bounce_type,
  462. self.event_soft_bounce['msg']['bounce_description'])
  463. self.assertEqual(self.message.bounce_description,
  464. self.event_soft_bounce['msg']['diag'])
  465. self.assertEqual(data['message_id'], self.message.id)
  466. self.assertEqual(data['event_type'], 'soft_bounce')
  467. self.assertEqual(data['timestamp'], self.event_soft_bounce['ts'])
  468. def test_process_open(self):
  469. data = self.env['mail.mandrill.event'].process_open(
  470. self.message, self.event_open)
  471. self.assertEqual(self.message.state, 'opened')
  472. self.assertEqual(data['message_id'], self.message.id)
  473. self.assertEqual(data['event_type'], 'open')
  474. self.assertEqual(data['timestamp'], self.event_open['ts'])
  475. self.assertEqual(data['ip'], self.event_open['ip'])
  476. def test_process_click(self):
  477. data = self.env['mail.mandrill.event'].process_click(
  478. self.message, self.event_open)
  479. self.assertEqual(self.message.state, 'opened')
  480. self.assertEqual(data['message_id'], self.message.id)
  481. self.assertEqual(data['event_type'], 'click')
  482. self.assertEqual(data['timestamp'], self.event_open['ts'])
  483. self.assertEqual(data['ip'], self.event_open['ip'])
  484. def test_process_spam(self):
  485. data = self.env['mail.mandrill.event'].process_spam(
  486. self.message, self.event_spam)
  487. self.assertEqual(self.message.state, 'spam')
  488. self.assertEqual(data['message_id'], self.message.id)
  489. self.assertEqual(data['event_type'], 'spam')
  490. self.assertEqual(data['timestamp'], self.event_spam['ts'])
  491. def test_process_reject(self):
  492. data = self.env['mail.mandrill.event'].process_reject(
  493. self.message, self.event_reject)
  494. self.assertEqual(self.message.state, 'rejected')
  495. self.assertEqual(data['message_id'], self.message.id)
  496. self.assertEqual(data['event_type'], 'reject')
  497. self.assertEqual(data['timestamp'], self.event_reject['ts'])
  498. def test_process_unsub(self):
  499. data = self.env['mail.mandrill.event'].process_unsub(
  500. self.message, self.event_unsub)
  501. self.assertEqual(self.message.state, 'unsub')
  502. self.assertEqual(data['message_id'], self.message.id)
  503. self.assertEqual(data['event_type'], 'unsub')
  504. self.assertEqual(data['timestamp'], self.event_unsub['ts'])