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.

129 lines
5.4 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 LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  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({
  17. 'name': 'New Test Website'
  18. })
  19. self.website.company_id = self.company
  20. self.mail_server_id = self.env['ir.mail_server'].create({
  21. 'name': 'mail server',
  22. 'smtp_host': 'mail.example.com',
  23. })
  24. self.website.mail_server_id = self.mail_server_id
  25. user_admin = self.env.ref('base.user_admin')
  26. # copy-paste from mail.tests.test_mail_template
  27. self._attachments = [{
  28. 'name': '_Test_First',
  29. 'datas_fname':
  30. 'first.txt',
  31. 'datas': base64.b64encode(b'My first attachment'),
  32. 'res_model': 'res.partner',
  33. 'res_id': user_admin.partner_id.id
  34. }, {
  35. 'name': '_Test_Second',
  36. 'datas_fname': 'second.txt',
  37. 'datas': base64.b64encode(b'My second attachment'),
  38. 'res_model': 'res.partner',
  39. 'res_id': user_admin.partner_id.id
  40. }]
  41. self.partner_1 = self.env['res.partner'].create({'name': 'partner_1'})
  42. self.partner_2 = self.env['res.partner'].create({'name': 'partner_2'})
  43. self.email_1 = 'test1@example.com'
  44. self.email_2 = 'test2@example.com'
  45. self.email_3 = self.partner_1.email
  46. self.email_template = self.env['mail.template'].create({
  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': [(0, 0, self._attachments[0]), (0, 0, self._attachments[1])],
  53. 'partner_to': '%s,%s' % (self.partner_2.id, self.user_employee.partner_id.id),
  54. 'email_to': '%s, %s' % (self.email_1, self.email_2),
  55. 'email_cc': '%s' % self.email_3})
  56. def switch_user_website(self):
  57. # add website to allowed
  58. self.env.user.write(dict(
  59. backend_website_ids=[(4, self.website.id)],
  60. backend_website_id=self.website.id,
  61. company_id=self.company.id,
  62. company_ids=[(4, self.company.id)]
  63. ))
  64. def test_website_in_render_variables(self):
  65. """Mail values are per website"""
  66. self.env.user.backend_website_id = None
  67. TestModel = self.env['mail.test'].with_context({
  68. 'mail_create_nolog': True,
  69. 'mail_create_nosubscribe': True,
  70. })
  71. self.test_pigs = TestModel.create({
  72. 'name': 'Pigs',
  73. 'description': 'Fans of Pigs, unite !',
  74. 'alias_name': 'pigs',
  75. 'alias_contact': 'followers',
  76. })
  77. # sending without website
  78. mail_id = self.email_template.send_mail(self.test_pigs.id)
  79. mail = self.env['mail.mail'].browse(mail_id)
  80. self.assertEqual(mail.subject, '')
  81. self.assertFalse(mail.mail_server_id)
  82. # sending from frontend
  83. self.test_pigs.company_id = None
  84. mail_id = self.email_template.with_context(wdb=True, website_id=self.website.id).send_mail(self.test_pigs.id)
  85. mail = self.env['mail.mail'].browse(mail_id)
  86. self.assertEqual(mail.subject, self.website.name)
  87. self.assertEqual(mail.mail_server_id, self.mail_server_id)
  88. # copy-pasted tests
  89. self.assertEqual(mail.email_to, self.email_template.email_to)
  90. # for some reason self.email_template.email_cc might return u'False'
  91. self.assertEqual(mail.email_cc or 'False', self.email_template.email_cc or 'False')
  92. self.assertEqual(mail.recipient_ids, self.partner_2 | self.user_employee.partner_id)
  93. # sending from frontend
  94. self.switch_user_website()
  95. mail_id = self.email_template.send_mail(self.test_pigs.id)
  96. mail = self.env['mail.mail'].browse(mail_id)
  97. self.assertEqual(mail.subject, self.website.name)
  98. def _test_message_post_with_template(self):
  99. # It's deactivated, because workaround is based on checking host value in get_current_website()
  100. """Simulate sending email on eCommerce checkout"""
  101. self.switch_user_website()
  102. self.env.user.email = self.email
  103. self.env.user.invalidate_cache()
  104. self.env.user.invalidate_cache()
  105. self.assertEqual(self.env.user.email, self.email)
  106. # switch admin user back
  107. self.env.user.company_id = self.original_company
  108. self.env.user.invalidate_cache()
  109. self.assertEqual(self.env.user.email, self.original_email)
  110. self.test_pigs.with_context(website_id=self.website.id).message_post_with_template(self.email_template.id)
  111. message = self.env['mail.message'].search([], order='id desc', limit=1)
  112. self.assertIn('<%s>' % self.email, message.email_from)