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.

292 lines
11 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import mock
  5. import base64
  6. import time
  7. from openerp import http
  8. from openerp.tests.common import TransactionCase
  9. from ..controllers.main import MailTrackingController, BLANK
  10. mock_send_email = ('openerp.addons.base.ir.ir_mail_server.'
  11. 'ir_mail_server.send_email')
  12. class FakeUserAgent(object):
  13. browser = 'Test browser'
  14. platform = 'Test platform'
  15. def __str__(self):
  16. """Return name"""
  17. return 'Test suite'
  18. class TestMailTracking(TransactionCase):
  19. def setUp(self, *args, **kwargs):
  20. super(TestMailTracking, self).setUp(*args, **kwargs)
  21. self.sender = self.env['res.partner'].create({
  22. 'name': 'Test sender',
  23. 'email': 'sender@example.com',
  24. 'notify_email': 'always',
  25. })
  26. self.recipient = self.env['res.partner'].create({
  27. 'name': 'Test recipient',
  28. 'email': 'recipient@example.com',
  29. 'notify_email': 'always',
  30. })
  31. self.last_request = http.request
  32. http.request = type('obj', (object,), {
  33. 'db': self.env.cr.dbname,
  34. 'env': self.env,
  35. 'endpoint': type('obj', (object,), {
  36. 'routing': [],
  37. }),
  38. 'httprequest': type('obj', (object,), {
  39. 'remote_addr': '123.123.123.123',
  40. 'user_agent': FakeUserAgent(),
  41. }),
  42. })
  43. def tearDown(self, *args, **kwargs):
  44. http.request = self.last_request
  45. return super(TestMailTracking, self).tearDown(*args, **kwargs)
  46. def test_message_post(self):
  47. # This message will generate a notification for recipient
  48. message = self.env['mail.message'].create({
  49. 'subject': 'Message test',
  50. 'author_id': self.sender.id,
  51. 'email_from': self.sender.email,
  52. 'type': 'comment',
  53. 'model': 'res.partner',
  54. 'res_id': self.recipient.id,
  55. 'partner_ids': [(4, self.recipient.id)],
  56. 'body': '<p>This is a test message</p>',
  57. })
  58. # Search tracking created
  59. tracking_email = self.env['mail.tracking.email'].search([
  60. ('mail_message_id', '=', message.id),
  61. ('partner_id', '=', self.recipient.id),
  62. ])
  63. # The tracking email must be sent
  64. self.assertTrue(tracking_email)
  65. self.assertEqual(tracking_email.state, 'sent')
  66. # message_dict read by web interface
  67. message_dict = message.message_read()
  68. # First item in threads is message content
  69. message_dict = message_dict['threads'][0][0]
  70. self.assertTrue(len(message_dict['partner_ids']) > 0)
  71. # First partner is recipient
  72. partner_id = message_dict['partner_ids'][0][0]
  73. self.assertEqual(partner_id, self.recipient.id)
  74. status = message_dict['partner_trackings'][0]
  75. # Tracking status must be sent and
  76. # mail tracking must be the one search before
  77. self.assertEqual(status[0], 'sent')
  78. self.assertEqual(status[1], tracking_email.id)
  79. self.assertEqual(status[2], self.recipient.display_name)
  80. self.assertEqual(status[3], self.recipient.id)
  81. # And now open the email
  82. metadata = {
  83. 'ip': '127.0.0.1',
  84. 'user_agent': 'Odoo Test/1.0',
  85. 'os_family': 'linux',
  86. 'ua_family': 'odoo',
  87. }
  88. tracking_email.event_create('open', metadata)
  89. self.assertEqual(tracking_email.state, 'opened')
  90. def mail_send(self, recipient):
  91. mail = self.env['mail.mail'].create({
  92. 'subject': 'Test subject',
  93. 'email_from': 'from@domain.com',
  94. 'email_to': recipient,
  95. 'body_html': '<p>This is a test message</p>',
  96. })
  97. mail.send()
  98. # Search tracking created
  99. tracking_email = self.env['mail.tracking.email'].search([
  100. ('mail_id', '=', mail.id),
  101. ])
  102. return mail, tracking_email
  103. def test_mail_send(self):
  104. controller = MailTrackingController()
  105. db = self.env.cr.dbname
  106. image = base64.decodestring(BLANK)
  107. mail, tracking = self.mail_send(self.recipient.email)
  108. self.assertEqual(mail.email_to, tracking.recipient)
  109. self.assertEqual(mail.email_from, tracking.sender)
  110. res = controller.mail_tracking_open(db, tracking.id)
  111. self.assertEqual(image, res.response[0])
  112. # Two events: sent and open
  113. self.assertEqual(2, len(tracking.tracking_event_ids))
  114. # Fake event: tracking_email_id = False
  115. res = controller.mail_tracking_open(db, False)
  116. self.assertEqual(image, res.response[0])
  117. # Two events again because no tracking_email_id found for False
  118. self.assertEqual(2, len(tracking.tracking_event_ids))
  119. def test_concurrent_open(self):
  120. mail, tracking = self.mail_send(self.recipient.email)
  121. ts = time.time()
  122. metadata = {
  123. 'ip': '127.0.0.1',
  124. 'user_agent': 'Odoo Test/1.0',
  125. 'os_family': 'linux',
  126. 'ua_family': 'odoo',
  127. 'timestamp': ts,
  128. }
  129. # First open event
  130. tracking.event_create('open', metadata)
  131. opens = tracking.tracking_event_ids.filtered(
  132. lambda r: r.event_type == 'open'
  133. )
  134. self.assertEqual(len(opens), 1)
  135. # Concurrent open event
  136. metadata['timestamp'] = ts + 2
  137. tracking.event_create('open', metadata)
  138. opens = tracking.tracking_event_ids.filtered(
  139. lambda r: r.event_type == 'open'
  140. )
  141. self.assertEqual(len(opens), 1)
  142. # Second open event
  143. metadata['timestamp'] = ts + 350
  144. tracking.event_create('open', metadata)
  145. opens = tracking.tracking_event_ids.filtered(
  146. lambda r: r.event_type == 'open'
  147. )
  148. self.assertEqual(len(opens), 2)
  149. def test_concurrent_click(self):
  150. mail, tracking = self.mail_send(self.recipient.email)
  151. ts = time.time()
  152. metadata = {
  153. 'ip': '127.0.0.1',
  154. 'user_agent': 'Odoo Test/1.0',
  155. 'os_family': 'linux',
  156. 'ua_family': 'odoo',
  157. 'timestamp': ts,
  158. 'url': 'https://www.example.com/route/1',
  159. }
  160. # First click event (URL 1)
  161. tracking.event_create('click', metadata)
  162. opens = tracking.tracking_event_ids.filtered(
  163. lambda r: r.event_type == 'click'
  164. )
  165. self.assertEqual(len(opens), 1)
  166. # Concurrent click event (URL 1)
  167. metadata['timestamp'] = ts + 2
  168. tracking.event_create('click', metadata)
  169. opens = tracking.tracking_event_ids.filtered(
  170. lambda r: r.event_type == 'click'
  171. )
  172. self.assertEqual(len(opens), 1)
  173. # Second click event (URL 1)
  174. metadata['timestamp'] = ts + 350
  175. tracking.event_create('click', metadata)
  176. opens = tracking.tracking_event_ids.filtered(
  177. lambda r: r.event_type == 'click'
  178. )
  179. self.assertEqual(len(opens), 2)
  180. # Concurrent click event (URL 2)
  181. metadata['timestamp'] = ts + 2
  182. metadata['url'] = 'https://www.example.com/route/2'
  183. tracking.event_create('click', metadata)
  184. opens = tracking.tracking_event_ids.filtered(
  185. lambda r: r.event_type == 'click'
  186. )
  187. self.assertEqual(len(opens), 3)
  188. def test_smtp_error(self):
  189. with mock.patch(mock_send_email) as mock_func:
  190. mock_func.side_effect = Warning('Test error')
  191. mail, tracking = self.mail_send(self.recipient.email)
  192. self.assertEqual('error', tracking.state)
  193. self.assertEqual('Warning', tracking.error_type)
  194. self.assertEqual('Test error', tracking.error_description)
  195. self.assertTrue(self.recipient.email_bounced)
  196. def test_partner_email_change(self):
  197. mail, tracking = self.mail_send(self.recipient.email)
  198. tracking.event_create('open', {})
  199. orig_score = self.recipient.email_score
  200. orig_count = self.recipient.tracking_emails_count
  201. orig_email = self.recipient.email
  202. self.recipient.email = orig_email + '2'
  203. self.assertEqual(50.0, self.recipient.email_score)
  204. self.assertEqual(0, self.recipient.tracking_emails_count)
  205. self.recipient.email = orig_email
  206. self.assertEqual(orig_score, self.recipient.email_score)
  207. self.assertEqual(orig_count, self.recipient.tracking_emails_count)
  208. def test_process_hard_bounce(self):
  209. mail, tracking = self.mail_send(self.recipient.email)
  210. tracking.event_create('hard_bounce', {})
  211. self.assertEqual('bounced', tracking.state)
  212. self.assertTrue(self.recipient.email_score < 50.0)
  213. def test_process_soft_bounce(self):
  214. mail, tracking = self.mail_send(self.recipient.email)
  215. tracking.event_create('soft_bounce', {})
  216. self.assertEqual('soft-bounced', tracking.state)
  217. self.assertTrue(self.recipient.email_score < 50.0)
  218. def test_process_delivered(self):
  219. mail, tracking = self.mail_send(self.recipient.email)
  220. tracking.event_create('delivered', {})
  221. self.assertEqual('delivered', tracking.state)
  222. self.assertTrue(self.recipient.email_score > 50.0)
  223. def test_process_deferral(self):
  224. mail, tracking = self.mail_send(self.recipient.email)
  225. tracking.event_create('deferral', {})
  226. self.assertEqual('deferred', tracking.state)
  227. def test_process_spam(self):
  228. mail, tracking = self.mail_send(self.recipient.email)
  229. tracking.event_create('spam', {})
  230. self.assertEqual('spam', tracking.state)
  231. self.assertTrue(self.recipient.email_score < 50.0)
  232. def test_process_unsub(self):
  233. mail, tracking = self.mail_send(self.recipient.email)
  234. tracking.event_create('unsub', {})
  235. self.assertEqual('unsub', tracking.state)
  236. self.assertTrue(self.recipient.email_score < 50.0)
  237. def test_process_reject(self):
  238. mail, tracking = self.mail_send(self.recipient.email)
  239. tracking.event_create('reject', {})
  240. self.assertEqual('rejected', tracking.state)
  241. self.assertTrue(self.recipient.email_score < 50.0)
  242. def test_process_open(self):
  243. mail, tracking = self.mail_send(self.recipient.email)
  244. tracking.event_create('open', {})
  245. self.assertEqual('opened', tracking.state)
  246. self.assertTrue(self.recipient.email_score > 50.0)
  247. def test_process_click(self):
  248. mail, tracking = self.mail_send(self.recipient.email)
  249. tracking.event_create('click', {})
  250. self.assertEqual('opened', tracking.state)
  251. self.assertTrue(self.recipient.email_score > 50.0)
  252. def test_process_several_bounce(self):
  253. for i in range(1, 10):
  254. mail, tracking = self.mail_send(self.recipient.email)
  255. tracking.event_create('hard_bounce', {})
  256. self.assertEqual('bounced', tracking.state)
  257. self.assertEqual(0.0, self.recipient.email_score)
  258. def test_db(self):
  259. db = self.env.cr.dbname
  260. controller = MailTrackingController()
  261. not_found = controller.mail_tracking_all('not_found_db')
  262. self.assertEqual('NOT FOUND', not_found.response[0])
  263. none = controller.mail_tracking_all(db)
  264. self.assertEqual('NONE', none.response[0])
  265. none = controller.mail_tracking_event(db, 'open')
  266. self.assertEqual('NONE', none.response[0])