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.

315 lines
12 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 odoo import http
  8. from odoo.tests.common import TransactionCase
  9. from ..controllers.main import MailTrackingController, BLANK
  10. mock_send_email = ('odoo.addons.base.ir.ir_mail_server.'
  11. 'IrMailServer.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_email_lower(self):
  47. self.recipient.write({'email': 'UPPER@example.com'})
  48. self.assertEqual('upper@example.com', self.recipient.email)
  49. def test_empty_email(self):
  50. self.recipient.write({'email_bounced': True})
  51. self.recipient.write({'email': False})
  52. self.assertEqual(False, self.recipient.email)
  53. self.assertEqual(False, self.recipient.email_bounced)
  54. self.recipient.write({'email_bounced': True})
  55. self.recipient.write({'email': ''})
  56. self.assertEqual(False, self.recipient.email)
  57. self.assertEqual(False, self.recipient.email_bounced)
  58. self.assertEqual(
  59. False,
  60. self.env['mail.tracking.email'].email_is_bounced(False))
  61. self.assertEqual(
  62. 0.,
  63. self.env['mail.tracking.email'].email_score_from_email(False))
  64. def test_recipient_address_compute(self):
  65. mail, tracking = self.mail_send(self.recipient.email)
  66. tracking.write({'recipient': False})
  67. self.assertEqual(False, tracking.recipient_address)
  68. def test_message_post(self):
  69. # This message will generate a notification for recipient
  70. message = self.env['mail.message'].create({
  71. 'subject': 'Message test',
  72. 'author_id': self.sender.id,
  73. 'email_from': self.sender.email,
  74. 'message_type': 'comment',
  75. 'model': 'res.partner',
  76. 'res_id': self.recipient.id,
  77. 'partner_ids': [(4, self.recipient.id)],
  78. 'body': '<p>This is a test message</p>',
  79. })
  80. # Search tracking created
  81. tracking_email = self.env['mail.tracking.email'].search([
  82. ('mail_message_id', '=', message.id),
  83. ('partner_id', '=', self.recipient.id),
  84. ])
  85. # The tracking email must be sent
  86. self.assertTrue(tracking_email)
  87. self.assertEqual(tracking_email.state, 'sent')
  88. # message_dict read by web interface
  89. message_dict = message.message_format()[0]
  90. self.assertTrue(len(message_dict['partner_ids']) > 0)
  91. # First partner is recipient
  92. partner_id = message_dict['partner_ids'][0][0]
  93. self.assertEqual(partner_id, self.recipient.id)
  94. status = message_dict['partner_trackings'][0]
  95. # Tracking status must be sent and
  96. # mail tracking must be the one search before
  97. self.assertEqual(status[0], 'sent')
  98. self.assertEqual(status[1], tracking_email.id)
  99. self.assertEqual(status[2], self.recipient.display_name)
  100. self.assertEqual(status[3], self.recipient.id)
  101. # And now open the email
  102. metadata = {
  103. 'ip': '127.0.0.1',
  104. 'user_agent': 'Odoo Test/1.0',
  105. 'os_family': 'linux',
  106. 'ua_family': 'odoo',
  107. }
  108. tracking_email.event_create('open', metadata)
  109. self.assertEqual(tracking_email.state, 'opened')
  110. def mail_send(self, recipient):
  111. mail = self.env['mail.mail'].create({
  112. 'subject': 'Test subject',
  113. 'email_from': 'from@domain.com',
  114. 'email_to': recipient,
  115. 'body_html': '<p>This is a test message</p>',
  116. })
  117. mail.send()
  118. # Search tracking created
  119. tracking_email = self.env['mail.tracking.email'].search([
  120. ('mail_id', '=', mail.id),
  121. ])
  122. return mail, tracking_email
  123. def test_mail_send(self):
  124. controller = MailTrackingController()
  125. db = self.env.cr.dbname
  126. image = base64.decodestring(BLANK)
  127. mail, tracking = self.mail_send(self.recipient.email)
  128. self.assertEqual(mail.email_to, tracking.recipient)
  129. self.assertEqual(mail.email_from, tracking.sender)
  130. res = controller.mail_tracking_open(db, tracking.id)
  131. self.assertEqual(image, res.response[0])
  132. # Two events: sent and open
  133. self.assertEqual(2, len(tracking.tracking_event_ids))
  134. # Fake event: tracking_email_id = False
  135. res = controller.mail_tracking_open(db, False)
  136. self.assertEqual(image, res.response[0])
  137. # Two events again because no tracking_email_id found for False
  138. self.assertEqual(2, len(tracking.tracking_event_ids))
  139. def test_concurrent_open(self):
  140. mail, tracking = self.mail_send(self.recipient.email)
  141. ts = time.time()
  142. metadata = {
  143. 'ip': '127.0.0.1',
  144. 'user_agent': 'Odoo Test/1.0',
  145. 'os_family': 'linux',
  146. 'ua_family': 'odoo',
  147. 'timestamp': ts,
  148. }
  149. # First open event
  150. tracking.event_create('open', metadata)
  151. opens = tracking.tracking_event_ids.filtered(
  152. lambda r: r.event_type == 'open'
  153. )
  154. self.assertEqual(len(opens), 1)
  155. # Concurrent open event
  156. metadata['timestamp'] = ts + 2
  157. tracking.event_create('open', metadata)
  158. opens = tracking.tracking_event_ids.filtered(
  159. lambda r: r.event_type == 'open'
  160. )
  161. self.assertEqual(len(opens), 1)
  162. # Second open event
  163. metadata['timestamp'] = ts + 350
  164. tracking.event_create('open', metadata)
  165. opens = tracking.tracking_event_ids.filtered(
  166. lambda r: r.event_type == 'open'
  167. )
  168. self.assertEqual(len(opens), 2)
  169. def test_concurrent_click(self):
  170. mail, tracking = self.mail_send(self.recipient.email)
  171. ts = time.time()
  172. metadata = {
  173. 'ip': '127.0.0.1',
  174. 'user_agent': 'Odoo Test/1.0',
  175. 'os_family': 'linux',
  176. 'ua_family': 'odoo',
  177. 'timestamp': ts,
  178. 'url': 'https://www.example.com/route/1',
  179. }
  180. # First click event (URL 1)
  181. tracking.event_create('click', metadata)
  182. opens = tracking.tracking_event_ids.filtered(
  183. lambda r: r.event_type == 'click'
  184. )
  185. self.assertEqual(len(opens), 1)
  186. # Concurrent click event (URL 1)
  187. metadata['timestamp'] = ts + 2
  188. tracking.event_create('click', metadata)
  189. opens = tracking.tracking_event_ids.filtered(
  190. lambda r: r.event_type == 'click'
  191. )
  192. self.assertEqual(len(opens), 1)
  193. # Second click event (URL 1)
  194. metadata['timestamp'] = ts + 350
  195. tracking.event_create('click', metadata)
  196. opens = tracking.tracking_event_ids.filtered(
  197. lambda r: r.event_type == 'click'
  198. )
  199. self.assertEqual(len(opens), 2)
  200. # Concurrent click event (URL 2)
  201. metadata['timestamp'] = ts + 2
  202. metadata['url'] = 'https://www.example.com/route/2'
  203. tracking.event_create('click', metadata)
  204. opens = tracking.tracking_event_ids.filtered(
  205. lambda r: r.event_type == 'click'
  206. )
  207. self.assertEqual(len(opens), 3)
  208. def test_smtp_error(self):
  209. with mock.patch(mock_send_email) as mock_func:
  210. mock_func.side_effect = Warning('Test error')
  211. mail, tracking = self.mail_send(self.recipient.email)
  212. self.assertEqual('error', tracking.state)
  213. self.assertEqual('Warning', tracking.error_type)
  214. self.assertEqual('Test error', tracking.error_description)
  215. self.assertTrue(self.recipient.email_bounced)
  216. def test_partner_email_change(self):
  217. mail, tracking = self.mail_send(self.recipient.email)
  218. tracking.event_create('open', {})
  219. orig_score = self.recipient.email_score
  220. orig_count = self.recipient.tracking_emails_count
  221. orig_email = self.recipient.email
  222. self.recipient.email = orig_email + '2'
  223. self.assertEqual(50.0, self.recipient.email_score)
  224. self.assertEqual(0, self.recipient.tracking_emails_count)
  225. self.recipient.email = orig_email
  226. self.assertEqual(orig_score, self.recipient.email_score)
  227. self.assertEqual(orig_count, self.recipient.tracking_emails_count)
  228. def test_process_hard_bounce(self):
  229. mail, tracking = self.mail_send(self.recipient.email)
  230. tracking.event_create('hard_bounce', {})
  231. self.assertEqual('bounced', tracking.state)
  232. self.assertTrue(self.recipient.email_score < 50.0)
  233. def test_process_soft_bounce(self):
  234. mail, tracking = self.mail_send(self.recipient.email)
  235. tracking.event_create('soft_bounce', {})
  236. self.assertEqual('soft-bounced', tracking.state)
  237. self.assertTrue(self.recipient.email_score < 50.0)
  238. def test_process_delivered(self):
  239. mail, tracking = self.mail_send(self.recipient.email)
  240. tracking.event_create('delivered', {})
  241. self.assertEqual('delivered', tracking.state)
  242. self.assertTrue(self.recipient.email_score > 50.0)
  243. def test_process_deferral(self):
  244. mail, tracking = self.mail_send(self.recipient.email)
  245. tracking.event_create('deferral', {})
  246. self.assertEqual('deferred', tracking.state)
  247. def test_process_spam(self):
  248. mail, tracking = self.mail_send(self.recipient.email)
  249. tracking.event_create('spam', {})
  250. self.assertEqual('spam', tracking.state)
  251. self.assertTrue(self.recipient.email_score < 50.0)
  252. def test_process_unsub(self):
  253. mail, tracking = self.mail_send(self.recipient.email)
  254. tracking.event_create('unsub', {})
  255. self.assertEqual('unsub', tracking.state)
  256. self.assertTrue(self.recipient.email_score < 50.0)
  257. def test_process_reject(self):
  258. mail, tracking = self.mail_send(self.recipient.email)
  259. tracking.event_create('reject', {})
  260. self.assertEqual('rejected', tracking.state)
  261. self.assertTrue(self.recipient.email_score < 50.0)
  262. def test_process_open(self):
  263. mail, tracking = self.mail_send(self.recipient.email)
  264. tracking.event_create('open', {})
  265. self.assertEqual('opened', tracking.state)
  266. self.assertTrue(self.recipient.email_score > 50.0)
  267. def test_process_click(self):
  268. mail, tracking = self.mail_send(self.recipient.email)
  269. tracking.event_create('click', {})
  270. self.assertEqual('opened', tracking.state)
  271. self.assertTrue(self.recipient.email_score > 50.0)
  272. def test_process_several_bounce(self):
  273. for i in range(1, 10):
  274. mail, tracking = self.mail_send(self.recipient.email)
  275. tracking.event_create('hard_bounce', {})
  276. self.assertEqual('bounced', tracking.state)
  277. self.assertEqual(0.0, self.recipient.email_score)
  278. def test_db(self):
  279. db = self.env.cr.dbname
  280. controller = MailTrackingController()
  281. not_found = controller.mail_tracking_all('not_found_db')
  282. self.assertEqual('NOT FOUND', not_found.response[0])
  283. none = controller.mail_tracking_all(db)
  284. self.assertEqual('NONE', none.response[0])
  285. none = controller.mail_tracking_event(db, 'open')
  286. self.assertEqual('NONE', none.response[0])