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. import threading
  5. from openerp.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 = u'%s' % self.env['mail.tracking.email'].\
  28. _mailgun_signature(self.api_key, self.timestamp, self.token)
  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': getattr(threading.currentThread(), 'dbname', None),
  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. def test_bad_signature(self):
  61. self.event.update({
  62. 'event': u'delivered',
  63. 'signature': u'bad_signature',
  64. })
  65. response = self.env['mail.tracking.email'].event_process(
  66. None, self.event, self.metadata)
  67. self.assertEqual('ERROR: Signature', response)
  68. def test_bad_event_type(self):
  69. self.event.update({
  70. 'event': u'bad_event',
  71. })
  72. response = self.env['mail.tracking.email'].event_process(
  73. None, self.event, self.metadata)
  74. self.assertEqual('ERROR: Event type not supported', response)
  75. def test_bad_db(self):
  76. self.event.update({
  77. 'event': u'delivered',
  78. 'odoo_db': u'bad_db',
  79. })
  80. response = self.env['mail.tracking.email'].event_process(
  81. None, self.event, self.metadata)
  82. self.assertEqual('ERROR: Invalid DB', response)
  83. def test_bad_ts(self):
  84. timestamp = u'7a', # Now time will be used instead
  85. signature = u'%s' % self.env['mail.tracking.email'].\
  86. _mailgun_signature(self.api_key, timestamp, self.token)
  87. self.event.update({
  88. 'event': u'delivered',
  89. 'timestamp': timestamp,
  90. 'signature': signature,
  91. })
  92. response = self.env['mail.tracking.email'].event_process(
  93. None, self.event, self.metadata)
  94. self.assertEqual('OK', response)
  95. def test_tracking_not_found(self):
  96. self.event.update({
  97. 'event': u'delivered',
  98. 'tracking_email_id': u'bad_id',
  99. })
  100. response = self.env['mail.tracking.email'].event_process(
  101. None, self.event, self.metadata)
  102. self.assertEqual('ERROR: Tracking not found', response)
  103. # https://documentation.mailgun.com/user_manual.html#tracking-deliveries
  104. def test_event_delivered(self):
  105. self.event.update({
  106. 'event': u'delivered',
  107. })
  108. response = self.env['mail.tracking.email'].event_process(
  109. None, self.event, self.metadata)
  110. self.assertEqual('OK', response)
  111. event = self.event_search('delivered')
  112. self.assertEqual(event.timestamp, float(self.timestamp))
  113. self.assertEqual(event.recipient, self.recipient)
  114. # https://documentation.mailgun.com/user_manual.html#tracking-opens
  115. def test_event_opened(self):
  116. ip = u'127.0.0.1'
  117. user_agent = u'Odoo Test/8.0 Gecko Firefox/11.0'
  118. os_family = u'Linux'
  119. ua_family = u'Firefox'
  120. ua_type = u'browser'
  121. self.event.update({
  122. 'event': u'opened',
  123. 'city': u'Mountain View',
  124. 'country': u'US',
  125. 'region': u'CA',
  126. 'client-name': ua_family,
  127. 'client-os': os_family,
  128. 'client-type': ua_type,
  129. 'device-type': u'desktop',
  130. 'ip': ip,
  131. 'user-agent': user_agent,
  132. })
  133. response = self.env['mail.tracking.email'].event_process(
  134. None, self.event, self.metadata)
  135. self.assertEqual('OK', response)
  136. event = self.event_search('open')
  137. self.assertEqual(event.timestamp, float(self.timestamp))
  138. self.assertEqual(event.recipient, self.recipient)
  139. self.assertEqual(event.ip, ip)
  140. self.assertEqual(event.user_agent, user_agent)
  141. self.assertEqual(event.os_family, os_family)
  142. self.assertEqual(event.ua_family, ua_family)
  143. self.assertEqual(event.ua_type, ua_type)
  144. self.assertEqual(event.mobile, False)
  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)