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.

146 lines
5.7 KiB

  1. # Copyright 2018 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
  2. # Copyright 2018 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
  3. # License MIT (https://opensource.org/licenses/MIT).
  4. import base64
  5. from odoo.addons.test_mail.tests.test_mail_mail import TestMail
  6. class TestRender(TestMail):
  7. at_install = True
  8. post_install = True
  9. def setUp(self):
  10. super(TestRender, self).setUp()
  11. self.original_email = self.env.user.email
  12. self.original_company = self.env.user.company_id
  13. self.email = "superadmin@second-website.example"
  14. self.assertNotEqual(self.original_email, self.email)
  15. self.website = self.env.ref("website.website2")
  16. self.company = self.env["res.company"].create({"name": "New Test Website"})
  17. self.website.company_id = self.company
  18. self.mail_server_id = self.env["ir.mail_server"].create(
  19. {"name": "mail server", "smtp_host": "mail.example.com"}
  20. )
  21. self.website.mail_server_id = self.mail_server_id
  22. user_admin = self.env.ref("base.user_admin")
  23. # copy-paste from mail.tests.test_mail_template
  24. self._attachments = [
  25. {
  26. "name": "_Test_First",
  27. "datas_fname": "first.txt",
  28. "datas": base64.b64encode(b"My first attachment"),
  29. "res_model": "res.partner",
  30. "res_id": user_admin.partner_id.id,
  31. },
  32. {
  33. "name": "_Test_Second",
  34. "datas_fname": "second.txt",
  35. "datas": base64.b64encode(b"My second attachment"),
  36. "res_model": "res.partner",
  37. "res_id": user_admin.partner_id.id,
  38. },
  39. ]
  40. self.partner_1 = self.env["res.partner"].create({"name": "partner_1"})
  41. self.partner_2 = self.env["res.partner"].create({"name": "partner_2"})
  42. self.email_1 = "test1@example.com"
  43. self.email_2 = "test2@example.com"
  44. self.email_3 = self.partner_1.email
  45. self.email_template = self.env["mail.template"].create(
  46. {
  47. "model_id": self.env["ir.model"]._get("mail.test").id,
  48. "name": "Pigs Template",
  49. "subject": "${website.name}",
  50. "body_html": "${object.description}",
  51. "user_signature": False,
  52. "attachment_ids": [
  53. (0, 0, self._attachments[0]),
  54. (0, 0, self._attachments[1]),
  55. ],
  56. "partner_to": "%s,%s"
  57. % (self.partner_2.id, self.user_employee.partner_id.id),
  58. "email_to": "{}, {}".format(self.email_1, self.email_2),
  59. "email_cc": "%s" % self.email_3,
  60. }
  61. )
  62. def switch_user_website(self):
  63. # add website to allowed
  64. self.env.user.write(
  65. dict(
  66. backend_website_ids=[(4, self.website.id)],
  67. backend_website_id=self.website.id,
  68. company_id=self.company.id,
  69. company_ids=[(4, self.company.id)],
  70. )
  71. )
  72. def test_website_in_render_variables(self):
  73. """Mail values are per website"""
  74. self.env.user.backend_website_id = None
  75. TestModel = self.env["mail.test"].with_context(
  76. {"mail_create_nolog": True, "mail_create_nosubscribe": True}
  77. )
  78. self.test_pigs = TestModel.create(
  79. {
  80. "name": "Pigs",
  81. "description": "Fans of Pigs, unite !",
  82. "alias_name": "pigs",
  83. "alias_contact": "followers",
  84. }
  85. )
  86. # sending without website
  87. mail_id = self.email_template.send_mail(self.test_pigs.id)
  88. mail = self.env["mail.mail"].browse(mail_id)
  89. self.assertEqual(mail.subject, "")
  90. self.assertFalse(mail.mail_server_id)
  91. # sending from frontend
  92. self.test_pigs.company_id = None
  93. mail_id = self.email_template.with_context(
  94. wdb=True, website_id=self.website.id
  95. ).send_mail(self.test_pigs.id)
  96. mail = self.env["mail.mail"].browse(mail_id)
  97. self.assertEqual(mail.subject, self.website.name)
  98. self.assertEqual(mail.mail_server_id, self.mail_server_id)
  99. # copy-pasted tests
  100. self.assertEqual(mail.email_to, self.email_template.email_to)
  101. # for some reason self.email_template.email_cc might return u'False'
  102. self.assertEqual(
  103. mail.email_cc or "False", self.email_template.email_cc or "False"
  104. )
  105. self.assertEqual(
  106. mail.recipient_ids, self.partner_2 | self.user_employee.partner_id
  107. )
  108. # sending from frontend
  109. self.switch_user_website()
  110. mail_id = self.email_template.send_mail(self.test_pigs.id)
  111. mail = self.env["mail.mail"].browse(mail_id)
  112. self.assertEqual(mail.subject, self.website.name)
  113. def _test_message_post_with_template(self):
  114. # It's deactivated, because workaround is based on checking host value in get_current_website()
  115. """Simulate sending email on eCommerce checkout"""
  116. self.switch_user_website()
  117. self.env.user.email = self.email
  118. self.env.user.invalidate_cache()
  119. self.env.user.invalidate_cache()
  120. self.assertEqual(self.env.user.email, self.email)
  121. # switch admin user back
  122. self.env.user.company_id = self.original_company
  123. self.env.user.invalidate_cache()
  124. self.assertEqual(self.env.user.email, self.original_email)
  125. self.test_pigs.with_context(
  126. website_id=self.website.id
  127. ).message_post_with_template(self.email_template.id)
  128. message = self.env["mail.message"].search([], order="id desc", limit=1)
  129. self.assertIn("<%s>" % self.email, message.email_from)