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.

128 lines
4.9 KiB

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