test_mailing_statistics_sms.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from lxml import html
  4. from odoo.addons.mail.tests.common import mail_new_test_user
  5. from odoo.addons.test_mass_mailing.tests.common import TestMassSMSCommon
  6. from odoo.tests.common import users
  7. from odoo.tests import tagged
  8. from odoo.tools import mute_logger
  9. @tagged('digest', 'mass_mailing', 'mass_mailing_sms')
  10. class TestMailingStatistics(TestMassSMSCommon):
  11. @classmethod
  12. def setUpClass(cls):
  13. super(TestMailingStatistics, cls).setUpClass()
  14. cls.user_marketing_2 = mail_new_test_user(
  15. cls.env,
  16. groups='base.group_user,base.group_partner_manager,mass_mailing.group_mass_mailing_user',
  17. login='user_marketing_2',
  18. name='Marie Marketing',
  19. signature='--\nMarie'
  20. )
  21. @users('user_marketing')
  22. @mute_logger('odoo.addons.mass_mailing_sms.models.mailing_mailing', 'odoo.addons.mail.models.mail_mail', 'odoo.addons.mail.models.mail_thread')
  23. def test_mailing_statistics_sms(self):
  24. mailing = self.env['mailing.mailing'].browse(self.mailing_sms.ids)
  25. target_records = self.env['mail.test.sms'].browse(self.records.ids)
  26. mailing.write({'mailing_domain': [('id', 'in', target_records.ids)], 'user_id': self.user_marketing_2.id})
  27. mailing.action_put_in_queue()
  28. with self.mockSMSGateway():
  29. mailing.action_send_sms()
  30. # simulate some replies and clicks
  31. self.gateway_sms_click(mailing, target_records[0])
  32. self.gateway_sms_click(mailing, target_records[2])
  33. self.gateway_sms_click(mailing, target_records[3])
  34. # check mailing statistics
  35. self.assertEqual(mailing.clicked, 3)
  36. self.assertEqual(mailing.delivered, 10)
  37. self.assertEqual(mailing.opened, 3)
  38. self.assertEqual(mailing.opened_ratio, 30)
  39. self.assertEqual(mailing.sent, 10)
  40. with self.mock_mail_gateway(mail_unlink_sent=True):
  41. mailing._action_send_statistics()
  42. self.assertEqual(len(self._new_mails), 1, "Mailing: a mail should have been created for statistics")
  43. mail = self._new_mails[0]
  44. # test email values
  45. self.assertEqual(mail.author_id, self.user_marketing_2.partner_id)
  46. self.assertEqual(mail.email_from, self.user_marketing_2.email_formatted)
  47. self.assertEqual(mail.email_to, self.user_marketing_2.email_formatted)
  48. self.assertEqual(mail.reply_to, self.company_admin.partner_id.email_formatted)
  49. self.assertEqual(mail.state, 'outgoing')
  50. # test body content: KPIs
  51. body_html = html.fromstring(mail.body_html)
  52. kpi_values = body_html.xpath('//div[@data-field="sms"]//*[hasclass("kpi_value")]/text()')
  53. self.assertEqual(
  54. [t.strip().strip('%') for t in kpi_values],
  55. ['100', str(mailing.opened_ratio), str(mailing.replied_ratio)]
  56. )
  57. # test body content: clicks (a bit hackish but hey we are in stable)
  58. kpi_click_values = body_html.xpath('//div[hasclass("global_layout")]/table//tr[contains(@style,"color: #888888")]/td[contains(@style,"width: 30%")]/text()')
  59. first_link_value = int(kpi_click_values[0].strip().split()[1].strip('()'))
  60. self.assertEqual(first_link_value, mailing.clicked)