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.

410 lines
17 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Tecnativa - Antonio Espinosa
  3. # Copyright 2017 Tecnativa - David Vidal
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from openerp.tests.common import TransactionCase
  6. from openerp.exceptions import UserError, ValidationError
  7. import mock
  8. import json
  9. _packagepath = 'openerp.addons.mail_tracking_mailgun'
  10. class TestMailgun(TransactionCase):
  11. def mail_send(self):
  12. mail = self.env['mail.mail'].create({
  13. 'subject': 'Test subject',
  14. 'email_from': 'from@example.com',
  15. 'email_to': self.recipient,
  16. 'body_html': '<p>This is a test message</p>',
  17. })
  18. mail.send()
  19. # Search tracking created
  20. tracking_email = self.env['mail.tracking.email'].search([
  21. ('mail_id', '=', mail.id),
  22. ])
  23. return mail, tracking_email
  24. def setUp(self):
  25. super(TestMailgun, self).setUp()
  26. self.recipient = u'to@example.com'
  27. self.mail, self.tracking_email = self.mail_send()
  28. self.api_key = u'key-12345678901234567890123456789012'
  29. self.domain = u'example.com'
  30. self.token = u'f1349299097a51b9a7d886fcb5c2735b426ba200ada6e9e149'
  31. self.timestamp = u'1471021089'
  32. self.signature = ('4fb6d4dbbe10ce5d620265dcd7a3c0b8'
  33. 'ca0dede1433103891bc1ae4086e9d5b2')
  34. self.env['ir.config_parameter'].set_param(
  35. 'mailgun.apikey', self.api_key)
  36. self.env['ir.config_parameter'].set_param(
  37. 'mail.catchall.domain', self.domain)
  38. self.env['ir.config_parameter'].set_param(
  39. 'mailgun.validation_key', self.api_key)
  40. self.event = {
  41. 'Message-Id': u'<xxx.xxx.xxx-openerp-xxx-res.partner@test_db>',
  42. 'X-Mailgun-Sid': u'WyIwNjgxZSIsICJ0b0BleGFtcGxlLmNvbSIsICI3MG'
  43. 'I0MWYiXQ==',
  44. 'token': self.token,
  45. 'timestamp': self.timestamp,
  46. 'signature': self.signature,
  47. 'domain': u'example.com',
  48. 'message-headers': u'[]',
  49. 'recipient': self.recipient,
  50. 'odoo_db': self.env.cr.dbname,
  51. 'tracking_email_id': u'%s' % self.tracking_email.id
  52. }
  53. self.metadata = {
  54. 'ip': '127.0.0.1',
  55. 'user_agent': False,
  56. 'os_family': False,
  57. 'ua_family': False,
  58. }
  59. self.partner = self.env['res.partner'].create({
  60. 'name': 'Mr. Odoo',
  61. 'email': 'mrodoo@example.com',
  62. })
  63. self.response = {
  64. "items": [{
  65. "log-level": "info",
  66. "id": "oXAVv5URCF-dKv8c6Sa7T",
  67. "timestamp": 1509119329.0,
  68. "message": {
  69. "headers": {
  70. "to": "test@test.com",
  71. "message-id": "test-id@f187c54734e8",
  72. "from": "Mr. Odoo <mrodoo@odoo.com>",
  73. "subject": "This is a test"
  74. },
  75. },
  76. "event": "delivered"
  77. }]
  78. }
  79. def event_search(self, event_type):
  80. event = self.env['mail.tracking.event'].search([
  81. ('tracking_email_id', '=', self.tracking_email.id),
  82. ('event_type', '=', event_type),
  83. ])
  84. self.assertTrue(event)
  85. return event
  86. def test_no_api_key(self):
  87. self.env['ir.config_parameter'].set_param('mailgun.apikey', '')
  88. self.test_event_delivered()
  89. with self.assertRaises(ValidationError):
  90. self.env['mail.tracking.email']._mailgun_values()
  91. def test_no_domain(self):
  92. self.env['ir.config_parameter'].set_param('mail.catchall.domain', '')
  93. self.test_event_delivered()
  94. with self.assertRaises(ValidationError):
  95. self.env['mail.tracking.email']._mailgun_values()
  96. def test_bad_signature(self):
  97. self.event.update({
  98. 'event': u'delivered',
  99. 'signature': u'bad_signature',
  100. })
  101. response = self.env['mail.tracking.email'].event_process(
  102. None, self.event, self.metadata)
  103. self.assertEqual('ERROR: Signature', response)
  104. def test_bad_event_type(self):
  105. self.event.update({
  106. 'event': u'bad_event',
  107. })
  108. response = self.env['mail.tracking.email'].event_process(
  109. None, self.event, self.metadata)
  110. self.assertEqual('ERROR: Event type not supported', response)
  111. def test_bad_db(self):
  112. self.event.update({
  113. 'event': u'delivered',
  114. 'odoo_db': u'bad_db',
  115. })
  116. response = self.env['mail.tracking.email'].event_process(
  117. None, self.event, self.metadata)
  118. self.assertEqual('ERROR: Invalid DB', response)
  119. def test_bad_ts(self):
  120. timestamp = u'7a' # Now time will be used instead
  121. signature = ('06cc05680f6e8110e59b41152b2d1c0f'
  122. '1045d755ef2880ff922344325c89a6d4')
  123. self.event.update({
  124. 'event': u'delivered',
  125. 'timestamp': timestamp,
  126. 'signature': signature,
  127. })
  128. response = self.env['mail.tracking.email'].event_process(
  129. None, self.event, self.metadata)
  130. self.assertEqual('OK', response)
  131. def test_tracking_not_found(self):
  132. self.event.update({
  133. 'event': u'delivered',
  134. 'tracking_email_id': u'bad_id',
  135. })
  136. response = self.env['mail.tracking.email'].event_process(
  137. None, self.event, self.metadata)
  138. self.assertEqual('ERROR: Tracking not found', response)
  139. # https://documentation.mailgun.com/user_manual.html#tracking-deliveries
  140. def test_event_delivered(self):
  141. self.event.update({
  142. 'event': u'delivered',
  143. })
  144. response = self.env['mail.tracking.email'].event_process(
  145. None, self.event, self.metadata)
  146. self.assertEqual('OK', response)
  147. event = self.event_search('delivered')
  148. self.assertEqual(event.timestamp, float(self.timestamp))
  149. self.assertEqual(event.recipient, self.recipient)
  150. # https://documentation.mailgun.com/user_manual.html#tracking-opens
  151. def test_event_opened(self):
  152. ip = u'127.0.0.1'
  153. user_agent = u'Odoo Test/8.0 Gecko Firefox/11.0'
  154. os_family = u'Linux'
  155. ua_family = u'Firefox'
  156. ua_type = u'browser'
  157. self.event.update({
  158. 'event': u'opened',
  159. 'city': u'Mountain View',
  160. 'country': u'US',
  161. 'region': u'CA',
  162. 'client-name': ua_family,
  163. 'client-os': os_family,
  164. 'client-type': ua_type,
  165. 'device-type': u'desktop',
  166. 'ip': ip,
  167. 'user-agent': user_agent,
  168. })
  169. response = self.env['mail.tracking.email'].event_process(
  170. None, self.event, self.metadata)
  171. self.assertEqual('OK', response)
  172. event = self.event_search('open')
  173. self.assertEqual(event.timestamp, float(self.timestamp))
  174. self.assertEqual(event.recipient, self.recipient)
  175. self.assertEqual(event.ip, ip)
  176. self.assertEqual(event.user_agent, user_agent)
  177. self.assertEqual(event.os_family, os_family)
  178. self.assertEqual(event.ua_family, ua_family)
  179. self.assertEqual(event.ua_type, ua_type)
  180. self.assertEqual(event.mobile, False)
  181. self.assertEqual(event.user_country_id.code, 'US')
  182. # https://documentation.mailgun.com/user_manual.html#tracking-clicks
  183. def test_event_clicked(self):
  184. ip = u'127.0.0.1'
  185. user_agent = u'Odoo Test/8.0 Gecko Firefox/11.0'
  186. os_family = u'Linux'
  187. ua_family = u'Firefox'
  188. ua_type = u'browser'
  189. url = u'https://odoo-community.org'
  190. self.event.update({
  191. 'event': u'clicked',
  192. 'city': u'Mountain View',
  193. 'country': u'US',
  194. 'region': u'CA',
  195. 'client-name': ua_family,
  196. 'client-os': os_family,
  197. 'client-type': ua_type,
  198. 'device-type': u'tablet',
  199. 'ip': ip,
  200. 'user-agent': user_agent,
  201. 'url': url,
  202. })
  203. response = self.env['mail.tracking.email'].event_process(
  204. None, self.event, self.metadata, event_type='click')
  205. self.assertEqual('OK', response)
  206. event = self.event_search('click')
  207. self.assertEqual(event.timestamp, float(self.timestamp))
  208. self.assertEqual(event.recipient, self.recipient)
  209. self.assertEqual(event.ip, ip)
  210. self.assertEqual(event.user_agent, user_agent)
  211. self.assertEqual(event.os_family, os_family)
  212. self.assertEqual(event.ua_family, ua_family)
  213. self.assertEqual(event.ua_type, ua_type)
  214. self.assertEqual(event.mobile, True)
  215. self.assertEqual(event.url, url)
  216. # https://documentation.mailgun.com/user_manual.html#tracking-unsubscribes
  217. def test_event_unsubscribed(self):
  218. ip = u'127.0.0.1'
  219. user_agent = u'Odoo Test/8.0 Gecko Firefox/11.0'
  220. os_family = u'Linux'
  221. ua_family = u'Firefox'
  222. ua_type = u'browser'
  223. self.event.update({
  224. 'event': u'unsubscribed',
  225. 'city': u'Mountain View',
  226. 'country': u'US',
  227. 'region': u'CA',
  228. 'client-name': ua_family,
  229. 'client-os': os_family,
  230. 'client-type': ua_type,
  231. 'device-type': u'mobile',
  232. 'ip': ip,
  233. 'user-agent': user_agent,
  234. })
  235. response = self.env['mail.tracking.email'].event_process(
  236. None, self.event, self.metadata)
  237. self.assertEqual('OK', response)
  238. event = self.event_search('unsub')
  239. self.assertEqual(event.timestamp, float(self.timestamp))
  240. self.assertEqual(event.recipient, self.recipient)
  241. self.assertEqual(event.ip, ip)
  242. self.assertEqual(event.user_agent, user_agent)
  243. self.assertEqual(event.os_family, os_family)
  244. self.assertEqual(event.ua_family, ua_family)
  245. self.assertEqual(event.ua_type, ua_type)
  246. self.assertEqual(event.mobile, True)
  247. # https://documentation.mailgun.com/
  248. # user_manual.html#tracking-spam-complaints
  249. def test_event_complained(self):
  250. self.event.update({
  251. 'event': u'complained',
  252. })
  253. response = self.env['mail.tracking.email'].event_process(
  254. None, self.event, self.metadata)
  255. self.assertEqual('OK', response)
  256. event = self.event_search('spam')
  257. self.assertEqual(event.timestamp, float(self.timestamp))
  258. self.assertEqual(event.recipient, self.recipient)
  259. self.assertEqual(event.error_type, 'spam')
  260. # https://documentation.mailgun.com/user_manual.html#tracking-bounces
  261. def test_event_bounced(self):
  262. code = u'550'
  263. error = (u"5.1.1 The email account does not exist.\n"
  264. "5.1.1 double-checking the recipient's email address")
  265. notification = u"Please, check recipient's email address"
  266. self.event.update({
  267. 'event': u'bounced',
  268. 'code': code,
  269. 'error': error,
  270. 'notification': notification,
  271. })
  272. response = self.env['mail.tracking.email'].event_process(
  273. None, self.event, self.metadata)
  274. self.assertEqual('OK', response)
  275. event = self.event_search('hard_bounce')
  276. self.assertEqual(event.timestamp, float(self.timestamp))
  277. self.assertEqual(event.recipient, self.recipient)
  278. self.assertEqual(event.error_type, code)
  279. self.assertEqual(event.error_description, error)
  280. self.assertEqual(event.error_details, notification)
  281. # https://documentation.mailgun.com/user_manual.html#tracking-failures
  282. def test_event_dropped(self):
  283. reason = u'hardfail'
  284. code = u'605'
  285. description = u'Not delivering to previously bounced address'
  286. self.event.update({
  287. 'event': u'dropped',
  288. 'reason': reason,
  289. 'code': code,
  290. 'description': description,
  291. })
  292. response = self.env['mail.tracking.email'].event_process(
  293. None, self.event, self.metadata)
  294. self.assertEqual('OK', response)
  295. event = self.event_search('reject')
  296. self.assertEqual(event.timestamp, float(self.timestamp))
  297. self.assertEqual(event.recipient, self.recipient)
  298. self.assertEqual(event.error_type, reason)
  299. self.assertEqual(event.error_description, code)
  300. self.assertEqual(event.error_details, description)
  301. @mock.patch(_packagepath + '.models.res_partner.requests')
  302. def test_email_validity(self, mock_request):
  303. self.partner.email_bounced = False
  304. self.partner.email = 'info@tecnativa.com'
  305. mock_request.get.return_value.apparent_encoding = 'ascii'
  306. mock_request.get.return_value.status_code = 200
  307. mock_request.get.return_value.content = json.dumps({
  308. 'is_valid': True,
  309. 'mailbox_verification': 'true',
  310. }, ensure_ascii=True)
  311. self.partner.check_email_validity()
  312. self.assertFalse(self.partner.email_bounced)
  313. self.partner.email = 'xoxoxoxo@tecnativa.com'
  314. # Not a valid mailbox
  315. mock_request.get.return_value.content = json.dumps({
  316. 'is_valid': True,
  317. 'mailbox_verification': 'false',
  318. }, ensure_ascii=True)
  319. with self.assertRaises(UserError):
  320. self.partner.check_email_validity()
  321. # Not a valid mail address
  322. mock_request.get.return_value.content = json.dumps({
  323. 'is_valid': False,
  324. 'mailbox_verification': 'false',
  325. }, ensure_ascii=True)
  326. with self.assertRaises(UserError):
  327. self.partner.check_email_validity()
  328. # Unable to fully validate
  329. mock_request.get.return_value.content = json.dumps({
  330. 'is_valid': True,
  331. 'mailbox_verification': 'unknown',
  332. }, ensure_ascii=True)
  333. with self.assertRaises(UserError):
  334. self.partner.check_email_validity()
  335. self.assertTrue(self.partner.email_bounced)
  336. @mock.patch(_packagepath + '.models.res_partner.requests')
  337. def test_email_validity_exceptions(self, mock_request):
  338. mock_request.get.return_value.status_code = 404
  339. with self.assertRaises(UserError):
  340. self.partner.check_email_validity()
  341. self.env['ir.config_parameter'].set_param('mailgun.validation_key', '')
  342. with self.assertRaises(UserError):
  343. self.partner.check_email_validity()
  344. @mock.patch(_packagepath + '.models.res_partner.requests')
  345. def test_bounced(self, mock_request):
  346. self.partner.email_bounced = True
  347. mock_request.get.return_value.status_code = 404
  348. self.partner.check_email_bounced()
  349. self.assertFalse(self.partner.email_bounced)
  350. mock_request.get.return_value.status_code = 200
  351. self.partner.force_set_bounced()
  352. self.partner.check_email_bounced()
  353. self.assertTrue(self.partner.email_bounced)
  354. mock_request.delete.return_value.status_code = 200
  355. self.partner.force_unset_bounced()
  356. self.assertFalse(self.partner.email_bounced)
  357. def test_email_bounced_set(self):
  358. message_number = len(self.partner.message_ids) + 1
  359. self.partner._email_bounced_set('test_error', self.event)
  360. self.assertEqual(len(self.partner.message_ids), message_number)
  361. self.partner.email = ""
  362. self.partner._email_bounced_set('test_error', self.event)
  363. self.assertEqual(len(self.partner.message_ids), message_number)
  364. @mock.patch(_packagepath + '.models.mail_tracking_email.requests')
  365. def test_manual_check(self, mock_request):
  366. mock_request.get.return_value.content = json.dumps(self.response,
  367. ensure_ascii=True)
  368. mock_request.get.return_value.apparent_encoding = 'ascii'
  369. mock_request.get.return_value.status_code = 200
  370. self.tracking_email.action_manual_check_mailgun()
  371. event = self.env['mail.tracking.event'].search(
  372. [('mailgun_id', '=', self.response['items'][0]['id'])])
  373. self.assertEqual(event.event_type, self.response['items'][0]['event'])
  374. @mock.patch(_packagepath + '.models.mail_tracking_email.requests')
  375. def test_manual_check_exceptions(self, mock_request):
  376. mock_request.get.return_value.status_code = 404
  377. with self.assertRaises(ValidationError):
  378. self.tracking_email.action_manual_check_mailgun()
  379. mock_request.get.return_value.status_code = 200
  380. mock_request.get.return_value.content = json.dumps('{}',
  381. ensure_ascii=True)
  382. mock_request.get.return_value.apparent_encoding = 'ascii'
  383. with self.assertRaises(ValidationError):
  384. self.tracking_email.action_manual_check_mailgun()