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.

290 lines
11 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo.tools import mute_logger
  5. from odoo.tests.common import TransactionCase
  6. class TestMailgun(TransactionCase):
  7. def mail_send(self):
  8. mail = self.env['mail.mail'].create({
  9. 'subject': 'Test subject',
  10. 'email_from': 'from@example.com',
  11. 'email_to': self.recipient,
  12. 'body_html': '<p>This is a test message</p>',
  13. })
  14. mail.send()
  15. # Search tracking created
  16. tracking_email = self.env['mail.tracking.email'].search([
  17. ('mail_id', '=', mail.id),
  18. ])
  19. return mail, tracking_email
  20. def setUp(self):
  21. super(TestMailgun, self).setUp()
  22. self.recipient = u'to@example.com'
  23. self.mail, self.tracking_email = self.mail_send()
  24. self.api_key = u'key-12345678901234567890123456789012'
  25. self.token = u'f1349299097a51b9a7d886fcb5c2735b426ba200ada6e9e149'
  26. self.timestamp = u'1471021089'
  27. self.signature = ('4fb6d4dbbe10ce5d620265dcd7a3c0b8'
  28. 'ca0dede1433103891bc1ae4086e9d5b2')
  29. self.env['ir.config_parameter'].set_param(
  30. 'mailgun.apikey', self.api_key)
  31. self.event = {
  32. 'Message-Id': u'<xxx.xxx.xxx-openerp-xxx-res.partner@test_db>',
  33. 'X-Mailgun-Sid': u'WyIwNjgxZSIsICJ0b0BleGFtcGxlLmNvbSIsICI3MG'
  34. 'I0MWYiXQ==',
  35. 'token': self.token,
  36. 'timestamp': self.timestamp,
  37. 'signature': self.signature,
  38. 'domain': u'example.com',
  39. 'message-headers': u'[]',
  40. 'recipient': self.recipient,
  41. 'odoo_db': self.env.cr.dbname,
  42. 'tracking_email_id': u'%s' % self.tracking_email.id
  43. }
  44. self.metadata = {
  45. 'ip': '127.0.0.1',
  46. 'user_agent': False,
  47. 'os_family': False,
  48. 'ua_family': False,
  49. }
  50. def event_search(self, event_type):
  51. event = self.env['mail.tracking.event'].search([
  52. ('tracking_email_id', '=', self.tracking_email.id),
  53. ('event_type', '=', event_type),
  54. ])
  55. self.assertTrue(event)
  56. return event
  57. def test_no_api_key(self):
  58. self.env['ir.config_parameter'].set_param('mailgun.apikey', '')
  59. self.test_event_delivered()
  60. @mute_logger('odoo.addons.mail_tracking_mailgun.models'
  61. '.mail_tracking_email')
  62. def test_bad_signature(self):
  63. self.event.update({
  64. 'event': u'delivered',
  65. 'signature': u'bad_signature',
  66. })
  67. response = self.env['mail.tracking.email'].event_process(
  68. None, self.event, self.metadata)
  69. self.assertEqual('ERROR: Signature', response)
  70. @mute_logger('odoo.addons.mail_tracking_mailgun.models'
  71. '.mail_tracking_email')
  72. def test_bad_event_type(self):
  73. self.event.update({
  74. 'event': u'bad_event',
  75. })
  76. response = self.env['mail.tracking.email'].event_process(
  77. None, self.event, self.metadata)
  78. self.assertEqual('ERROR: Event type not supported', response)
  79. @mute_logger('odoo.addons.mail_tracking_mailgun.models'
  80. '.mail_tracking_email')
  81. def test_bad_db(self):
  82. self.event.update({
  83. 'event': u'delivered',
  84. 'odoo_db': u'bad_db',
  85. })
  86. response = self.env['mail.tracking.email'].event_process(
  87. None, self.event, self.metadata)
  88. self.assertEqual('ERROR: Invalid DB', response)
  89. def test_bad_ts(self):
  90. timestamp = u'7a' # Now time will be used instead
  91. signature = ('06cc05680f6e8110e59b41152b2d1c0f'
  92. '1045d755ef2880ff922344325c89a6d4')
  93. self.event.update({
  94. 'event': u'delivered',
  95. 'timestamp': timestamp,
  96. 'signature': signature,
  97. })
  98. response = self.env['mail.tracking.email'].event_process(
  99. None, self.event, self.metadata)
  100. self.assertEqual('OK', response)
  101. @mute_logger('odoo.addons.mail_tracking_mailgun.models'
  102. '.mail_tracking_email')
  103. def test_tracking_not_found(self):
  104. self.event.update({
  105. 'event': u'delivered',
  106. 'tracking_email_id': u'bad_id',
  107. })
  108. response = self.env['mail.tracking.email'].event_process(
  109. None, self.event, self.metadata)
  110. self.assertEqual('ERROR: Tracking not found', response)
  111. # https://documentation.mailgun.com/user_manual.html#tracking-deliveries
  112. def test_event_delivered(self):
  113. self.event.update({
  114. 'event': u'delivered',
  115. })
  116. response = self.env['mail.tracking.email'].event_process(
  117. None, self.event, self.metadata)
  118. self.assertEqual('OK', response)
  119. event = self.event_search('delivered')
  120. self.assertEqual(event.timestamp, float(self.timestamp))
  121. self.assertEqual(event.recipient, self.recipient)
  122. # https://documentation.mailgun.com/user_manual.html#tracking-opens
  123. def test_event_opened(self):
  124. ip = u'127.0.0.1'
  125. user_agent = u'Odoo Test/8.0 Gecko Firefox/11.0'
  126. os_family = u'Linux'
  127. ua_family = u'Firefox'
  128. ua_type = u'browser'
  129. self.event.update({
  130. 'event': u'opened',
  131. 'city': u'Mountain View',
  132. 'country': u'US',
  133. 'region': u'CA',
  134. 'client-name': ua_family,
  135. 'client-os': os_family,
  136. 'client-type': ua_type,
  137. 'device-type': u'desktop',
  138. 'ip': ip,
  139. 'user-agent': user_agent,
  140. })
  141. response = self.env['mail.tracking.email'].event_process(
  142. None, self.event, self.metadata)
  143. self.assertEqual('OK', response)
  144. event = self.event_search('open')
  145. self.assertEqual(event.timestamp, float(self.timestamp))
  146. self.assertEqual(event.recipient, self.recipient)
  147. self.assertEqual(event.ip, ip)
  148. self.assertEqual(event.user_agent, user_agent)
  149. self.assertEqual(event.os_family, os_family)
  150. self.assertEqual(event.ua_family, ua_family)
  151. self.assertEqual(event.ua_type, ua_type)
  152. self.assertEqual(event.mobile, False)
  153. self.assertEqual(event.user_country_id.code, 'US')
  154. # https://documentation.mailgun.com/user_manual.html#tracking-clicks
  155. def test_event_clicked(self):
  156. ip = u'127.0.0.1'
  157. user_agent = u'Odoo Test/8.0 Gecko Firefox/11.0'
  158. os_family = u'Linux'
  159. ua_family = u'Firefox'
  160. ua_type = u'browser'
  161. url = u'https://odoo-community.org'
  162. self.event.update({
  163. 'event': u'clicked',
  164. 'city': u'Mountain View',
  165. 'country': u'US',
  166. 'region': u'CA',
  167. 'client-name': ua_family,
  168. 'client-os': os_family,
  169. 'client-type': ua_type,
  170. 'device-type': u'tablet',
  171. 'ip': ip,
  172. 'user-agent': user_agent,
  173. 'url': url,
  174. })
  175. response = self.env['mail.tracking.email'].event_process(
  176. None, self.event, self.metadata, event_type='click')
  177. self.assertEqual('OK', response)
  178. event = self.event_search('click')
  179. self.assertEqual(event.timestamp, float(self.timestamp))
  180. self.assertEqual(event.recipient, self.recipient)
  181. self.assertEqual(event.ip, ip)
  182. self.assertEqual(event.user_agent, user_agent)
  183. self.assertEqual(event.os_family, os_family)
  184. self.assertEqual(event.ua_family, ua_family)
  185. self.assertEqual(event.ua_type, ua_type)
  186. self.assertEqual(event.mobile, True)
  187. self.assertEqual(event.url, url)
  188. # https://documentation.mailgun.com/user_manual.html#tracking-unsubscribes
  189. def test_event_unsubscribed(self):
  190. ip = u'127.0.0.1'
  191. user_agent = u'Odoo Test/8.0 Gecko Firefox/11.0'
  192. os_family = u'Linux'
  193. ua_family = u'Firefox'
  194. ua_type = u'browser'
  195. self.event.update({
  196. 'event': u'unsubscribed',
  197. 'city': u'Mountain View',
  198. 'country': u'US',
  199. 'region': u'CA',
  200. 'client-name': ua_family,
  201. 'client-os': os_family,
  202. 'client-type': ua_type,
  203. 'device-type': u'mobile',
  204. 'ip': ip,
  205. 'user-agent': user_agent,
  206. })
  207. response = self.env['mail.tracking.email'].event_process(
  208. None, self.event, self.metadata)
  209. self.assertEqual('OK', response)
  210. event = self.event_search('unsub')
  211. self.assertEqual(event.timestamp, float(self.timestamp))
  212. self.assertEqual(event.recipient, self.recipient)
  213. self.assertEqual(event.ip, ip)
  214. self.assertEqual(event.user_agent, user_agent)
  215. self.assertEqual(event.os_family, os_family)
  216. self.assertEqual(event.ua_family, ua_family)
  217. self.assertEqual(event.ua_type, ua_type)
  218. self.assertEqual(event.mobile, True)
  219. # https://documentation.mailgun.com/
  220. # user_manual.html#tracking-spam-complaints
  221. def test_event_complained(self):
  222. self.event.update({
  223. 'event': u'complained',
  224. })
  225. response = self.env['mail.tracking.email'].event_process(
  226. None, self.event, self.metadata)
  227. self.assertEqual('OK', response)
  228. event = self.event_search('spam')
  229. self.assertEqual(event.timestamp, float(self.timestamp))
  230. self.assertEqual(event.recipient, self.recipient)
  231. self.assertEqual(event.error_type, 'spam')
  232. # https://documentation.mailgun.com/user_manual.html#tracking-bounces
  233. def test_event_bounced(self):
  234. code = u'550'
  235. error = (u"5.1.1 The email account does not exist.\n"
  236. "5.1.1 double-checking the recipient's email address")
  237. notification = u"Please, check recipient's email address"
  238. self.event.update({
  239. 'event': u'bounced',
  240. 'code': code,
  241. 'error': error,
  242. 'notification': notification,
  243. })
  244. response = self.env['mail.tracking.email'].event_process(
  245. None, self.event, self.metadata)
  246. self.assertEqual('OK', response)
  247. event = self.event_search('hard_bounce')
  248. self.assertEqual(event.timestamp, float(self.timestamp))
  249. self.assertEqual(event.recipient, self.recipient)
  250. self.assertEqual(event.error_type, code)
  251. self.assertEqual(event.error_description, error)
  252. self.assertEqual(event.error_details, notification)
  253. # https://documentation.mailgun.com/user_manual.html#tracking-failures
  254. def test_event_dropped(self):
  255. reason = u'hardfail'
  256. code = u'605'
  257. description = u'Not delivering to previously bounced address'
  258. self.event.update({
  259. 'event': u'dropped',
  260. 'reason': reason,
  261. 'code': code,
  262. 'description': description,
  263. })
  264. response = self.env['mail.tracking.email'].event_process(
  265. None, self.event, self.metadata)
  266. self.assertEqual('OK', response)
  267. event = self.event_search('reject')
  268. self.assertEqual(event.timestamp, float(self.timestamp))
  269. self.assertEqual(event.recipient, self.recipient)
  270. self.assertEqual(event.error_type, reason)
  271. self.assertEqual(event.error_description, code)
  272. self.assertEqual(event.error_details, description)