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.

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