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.

114 lines
4.7 KiB

  1. # Copyright 2018 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  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({
  16. 'name': 'New Test Website'
  17. })
  18. self.website.company_id = self.company
  19. self.mail_server_id = self.env['ir.mail_server'].create({
  20. 'name': 'mail server',
  21. 'smtp_host': 'mail.example.com',
  22. })
  23. self.website.mail_server_id = self.mail_server_id
  24. # copy-paste from mail.tests.test_mail_template
  25. self._attachments = [{
  26. 'name': '_Test_First',
  27. 'datas_fname':
  28. 'first.txt',
  29. 'datas': base64.b64encode(b'My first attachment'),
  30. 'res_model': 'res.partner',
  31. 'res_id': self.user_admin.partner_id.id
  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': self.user_admin.partner_id.id
  38. }]
  39. self.email_1 = 'test1@example.com'
  40. self.email_2 = 'test2@example.com'
  41. self.email_3 = self.partner_1.email
  42. self.email_template = self.env['mail.template'].create({
  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': [(0, 0, self._attachments[0]), (0, 0, self._attachments[1])],
  49. 'partner_to': '%s,%s' % (self.partner_2.id, self.user_employee.partner_id.id),
  50. 'email_to': '%s, %s' % (self.email_1, self.email_2),
  51. 'email_cc': '%s' % self.email_3})
  52. def switch_user_website(self):
  53. # add website to allowed
  54. self.env.user.write(dict(
  55. backend_website_ids=[(4, self.website.id)],
  56. backend_website_id=self.website.id,
  57. company_id=self.company.id,
  58. company_ids=[(4, self.company.id)]
  59. ))
  60. def test_website_in_render_variables(self):
  61. """Mail values are per website"""
  62. self.env.user.backend_website_id = None
  63. # sending without website
  64. mail_id = self.email_template.send_mail(self.test_pigs.id)
  65. mail = self.env['mail.mail'].browse(mail_id)
  66. self.assertEqual(mail.subject, '')
  67. self.assertFalse(mail.mail_server_id)
  68. # sending from frontend
  69. self.test_pigs.company_id = None
  70. mail_id = self.email_template.with_context(wdb=True, website_id=self.website.id).send_mail(self.test_pigs.id)
  71. mail = self.env['mail.mail'].browse(mail_id)
  72. self.assertEqual(mail.subject, self.website.name)
  73. self.assertEqual(mail.mail_server_id, self.mail_server_id)
  74. # copy-pasted tests
  75. self.assertEqual(mail.email_to, self.email_template.email_to)
  76. self.assertEqual(mail.email_cc, self.email_template.email_cc)
  77. self.assertEqual(mail.recipient_ids, self.partner_2 | self.user_employee.partner_id)
  78. # sending from frontend
  79. self.switch_user_website()
  80. mail_id = self.email_template.send_mail(self.test_pigs.id)
  81. mail = self.env['mail.mail'].browse(mail_id)
  82. self.assertEqual(mail.subject, self.website.name)
  83. def _test_message_post_with_template(self):
  84. # It's deactivated, because workaround is based on checking host value in get_current_website()
  85. """Simulate sending email on eCommerce checkout"""
  86. self.switch_user_website()
  87. self.env.user.email = self.email
  88. self.env.user.invalidate_cache()
  89. self.env.user.invalidate_cache()
  90. self.assertEqual(self.env.user.email, self.email)
  91. # switch admin user back
  92. self.env.user.company_id = self.original_company
  93. self.env.user.invalidate_cache()
  94. self.assertEqual(self.env.user.email, self.original_email)
  95. self.test_pigs.with_context(website_id=self.website.id).message_post_with_template(self.email_template.id)
  96. message = self.env['mail.message'].search([], order='id desc', limit=1)
  97. self.assertIn('<%s>' % self.email, message.email_from)