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.

316 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 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_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. '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 = self.env['mail.message'].message_read(message.id)
  90. # First item is message content
  91. self.assertTrue(len(message_dict) > 0)
  92. message_dict = message_dict[0]
  93. self.assertTrue(len(message_dict['partner_ids']) > 0)
  94. # First partner is recipient
  95. partner_id = message_dict['partner_ids'][0][0]
  96. self.assertEqual(partner_id, self.recipient.id)
  97. status = message_dict['partner_trackings'][str(partner_id)]
  98. # Tracking status must be sent and
  99. # mail tracking must be the one search before
  100. self.assertEqual(status[0], 'sent')
  101. self.assertEqual(status[1], tracking_email.id)
  102. # And now open the email
  103. metadata = {
  104. 'ip': '127.0.0.1',
  105. 'user_agent': 'Odoo Test/1.0',
  106. 'os_family': 'linux',
  107. 'ua_family': 'odoo',
  108. }
  109. tracking_email.event_create('open', metadata)
  110. self.assertEqual(tracking_email.state, 'opened')
  111. def mail_send(self, recipient):
  112. mail = self.env['mail.mail'].create({
  113. 'subject': 'Test subject',
  114. 'email_from': 'from@domain.com',
  115. 'email_to': recipient,
  116. 'body_html': '<p>This is a test message</p>',
  117. })
  118. mail.send()
  119. # Search tracking created
  120. tracking_email = self.env['mail.tracking.email'].search([
  121. ('mail_id', '=', mail.id),
  122. ])
  123. return mail, tracking_email
  124. def test_mail_send(self):
  125. controller = MailTrackingController()
  126. db = self.env.cr.dbname
  127. image = base64.decodestring(BLANK)
  128. mail, tracking = self.mail_send(self.recipient.email)
  129. self.assertEqual(mail.email_to, tracking.recipient)
  130. self.assertEqual(mail.email_from, tracking.sender)
  131. res = controller.mail_tracking_open(db, tracking.id)
  132. self.assertEqual(image, res.response[0])
  133. # Two events: sent and open
  134. self.assertEqual(2, len(tracking.tracking_event_ids))
  135. # Fake event: tracking_email_id = False
  136. res = controller.mail_tracking_open(db, False)
  137. self.assertEqual(image, res.response[0])
  138. # Two events again because no tracking_email_id found for False
  139. self.assertEqual(2, len(tracking.tracking_event_ids))
  140. def test_concurrent_open(self):
  141. mail, tracking = self.mail_send(self.recipient.email)
  142. ts = time.time()
  143. metadata = {
  144. 'ip': '127.0.0.1',
  145. 'user_agent': 'Odoo Test/1.0',
  146. 'os_family': 'linux',
  147. 'ua_family': 'odoo',
  148. 'timestamp': ts,
  149. }
  150. # First open event
  151. tracking.event_create('open', metadata)
  152. opens = tracking.tracking_event_ids.filtered(
  153. lambda r: r.event_type == 'open'
  154. )
  155. self.assertEqual(len(opens), 1)
  156. # Concurrent open event
  157. metadata['timestamp'] = ts + 2
  158. tracking.event_create('open', metadata)
  159. opens = tracking.tracking_event_ids.filtered(
  160. lambda r: r.event_type == 'open'
  161. )
  162. self.assertEqual(len(opens), 1)
  163. # Second open event
  164. metadata['timestamp'] = ts + 350
  165. tracking.event_create('open', metadata)
  166. opens = tracking.tracking_event_ids.filtered(
  167. lambda r: r.event_type == 'open'
  168. )
  169. self.assertEqual(len(opens), 2)
  170. def test_concurrent_click(self):
  171. mail, tracking = self.mail_send(self.recipient.email)
  172. ts = time.time()
  173. metadata = {
  174. 'ip': '127.0.0.1',
  175. 'user_agent': 'Odoo Test/1.0',
  176. 'os_family': 'linux',
  177. 'ua_family': 'odoo',
  178. 'timestamp': ts,
  179. 'url': 'https://www.example.com/route/1',
  180. }
  181. # First click event (URL 1)
  182. tracking.event_create('click', metadata)
  183. opens = tracking.tracking_event_ids.filtered(
  184. lambda r: r.event_type == 'click'
  185. )
  186. self.assertEqual(len(opens), 1)
  187. # Concurrent click event (URL 1)
  188. metadata['timestamp'] = ts + 2
  189. tracking.event_create('click', metadata)
  190. opens = tracking.tracking_event_ids.filtered(
  191. lambda r: r.event_type == 'click'
  192. )
  193. self.assertEqual(len(opens), 1)
  194. # Second click event (URL 1)
  195. metadata['timestamp'] = ts + 350
  196. tracking.event_create('click', metadata)
  197. opens = tracking.tracking_event_ids.filtered(
  198. lambda r: r.event_type == 'click'
  199. )
  200. self.assertEqual(len(opens), 2)
  201. # Concurrent click event (URL 2)
  202. metadata['timestamp'] = ts + 2
  203. metadata['url'] = 'https://www.example.com/route/2'
  204. tracking.event_create('click', metadata)
  205. opens = tracking.tracking_event_ids.filtered(
  206. lambda r: r.event_type == 'click'
  207. )
  208. self.assertEqual(len(opens), 3)
  209. def test_smtp_error(self):
  210. with mock.patch(mock_send_email) as mock_func:
  211. mock_func.side_effect = Warning('Test error')
  212. mail, tracking = self.mail_send(self.recipient.email)
  213. self.assertEqual('error', tracking.state)
  214. self.assertEqual('Warning', tracking.error_type)
  215. self.assertEqual('Test error', tracking.error_description)
  216. self.assertTrue(self.recipient.email_bounced)
  217. def test_partner_email_change(self):
  218. mail, tracking = self.mail_send(self.recipient.email)
  219. tracking.event_create('open', {})
  220. orig_score = self.recipient.email_score
  221. orig_count = self.recipient.tracking_emails_count
  222. orig_email = self.recipient.email
  223. self.recipient.email = orig_email + '2'
  224. self.assertEqual(50.0, self.recipient.email_score)
  225. self.assertEqual(0, self.recipient.tracking_emails_count)
  226. self.recipient.email = orig_email
  227. self.assertEqual(orig_score, self.recipient.email_score)
  228. self.assertEqual(orig_count, self.recipient.tracking_emails_count)
  229. def test_process_hard_bounce(self):
  230. mail, tracking = self.mail_send(self.recipient.email)
  231. tracking.event_create('hard_bounce', {})
  232. self.assertEqual('bounced', tracking.state)
  233. self.assertTrue(self.recipient.email_score < 50.0)
  234. def test_process_soft_bounce(self):
  235. mail, tracking = self.mail_send(self.recipient.email)
  236. tracking.event_create('soft_bounce', {})
  237. self.assertEqual('soft-bounced', tracking.state)
  238. self.assertTrue(self.recipient.email_score < 50.0)
  239. def test_process_delivered(self):
  240. mail, tracking = self.mail_send(self.recipient.email)
  241. tracking.event_create('delivered', {})
  242. self.assertEqual('delivered', tracking.state)
  243. self.assertTrue(self.recipient.email_score > 50.0)
  244. def test_process_deferral(self):
  245. mail, tracking = self.mail_send(self.recipient.email)
  246. tracking.event_create('deferral', {})
  247. self.assertEqual('deferred', tracking.state)
  248. def test_process_spam(self):
  249. mail, tracking = self.mail_send(self.recipient.email)
  250. tracking.event_create('spam', {})
  251. self.assertEqual('spam', tracking.state)
  252. self.assertTrue(self.recipient.email_score < 50.0)
  253. def test_process_unsub(self):
  254. mail, tracking = self.mail_send(self.recipient.email)
  255. tracking.event_create('unsub', {})
  256. self.assertEqual('unsub', tracking.state)
  257. self.assertTrue(self.recipient.email_score < 50.0)
  258. def test_process_reject(self):
  259. mail, tracking = self.mail_send(self.recipient.email)
  260. tracking.event_create('reject', {})
  261. self.assertEqual('rejected', tracking.state)
  262. self.assertTrue(self.recipient.email_score < 50.0)
  263. def test_process_open(self):
  264. mail, tracking = self.mail_send(self.recipient.email)
  265. tracking.event_create('open', {})
  266. self.assertEqual('opened', tracking.state)
  267. self.assertTrue(self.recipient.email_score > 50.0)
  268. def test_process_click(self):
  269. mail, tracking = self.mail_send(self.recipient.email)
  270. tracking.event_create('click', {})
  271. self.assertEqual('opened', tracking.state)
  272. self.assertTrue(self.recipient.email_score > 50.0)
  273. def test_process_several_bounce(self):
  274. for i in range(1, 10):
  275. mail, tracking = self.mail_send(self.recipient.email)
  276. tracking.event_create('hard_bounce', {})
  277. self.assertEqual('bounced', tracking.state)
  278. self.assertEqual(0.0, self.recipient.email_score)
  279. def test_db(self):
  280. db = self.env.cr.dbname
  281. controller = MailTrackingController()
  282. not_found = controller.mail_tracking_all('not_found_db')
  283. self.assertEqual('NOT FOUND', not_found.response[0])
  284. none = controller.mail_tracking_all(db)
  285. self.assertEqual('NONE', none.response[0])
  286. none = controller.mail_tracking_event(db, 'open')
  287. self.assertEqual('NONE', none.response[0])