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.

95 lines
3.2 KiB

  1. # Copyright 2018 Camptocamp SA
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
  3. from odoo.tests.common import SavepointCase
  4. from odoo import fields
  5. from base64 import urlsafe_b64encode
  6. from datetime import datetime
  7. from lxml import html
  8. from werkzeug.urls import url_parse
  9. from ..controllers.browser_view import EmailBrowserViewController
  10. from mock import patch
  11. class MailBrowserView(SavepointCase):
  12. @classmethod
  13. def setUpClass(cls):
  14. super(MailBrowserView, cls).setUpClass()
  15. cls.mail = cls.env['mail.mail']
  16. cls.mail0 = cls.env.ref('mail_browser_view.browser_view_demo')
  17. cls.valid_token = cls.mail0.view_in_browser_url.split('/')[-1]
  18. def _forge_token(self, access_token, rec_id):
  19. return urlsafe_b64encode(
  20. (access_token + str(rec_id)).encode()
  21. ).decode()
  22. def _test_token(self, token, expected_result):
  23. rec = self.mail.get_record_for_token(token)
  24. self.assertEqual(rec, expected_result)
  25. def test_mail_browser_view(self):
  26. self._test_token(self.valid_token, self.mail0)
  27. def test_invalid_b64(self):
  28. self._test_token(self.valid_token[::2], self.mail)
  29. def test_invalid_access_token(self):
  30. bad_token = self._forge_token('0000000', self.mail0.id)
  31. self._test_token(bad_token, self.mail)
  32. def test_nonexistent_id(self):
  33. bad_token = self._forge_token(self.mail0.access_token, 999999)
  34. self._test_token(bad_token, self.mail)
  35. def test_token_expiration(self):
  36. self.mail0.mail_message_id.date = fields.Datetime.to_string(
  37. datetime.fromtimestamp(0)
  38. )
  39. self._test_token(self.valid_token, self.mail)
  40. self.env.ref('mail_browser_view.token_expiration_hours').value = '0'
  41. self.mail0.refresh()
  42. self._test_token(self.valid_token, self.mail0)
  43. def test_html_render(self):
  44. html_node = html.fromstring(self.mail0.body_html)
  45. link_node = html_node.xpath("//a[hasclass('view_in_browser_url')]")
  46. self.assertEqual(
  47. url_parse(link_node[0].get('href')).path,
  48. self.mail0.view_in_browser_url
  49. )
  50. self.mail0.auto_delete = True
  51. self.mail0._replace_view_url()
  52. self.mail0.refresh()
  53. html_node = html.fromstring(self.mail0.body_html)
  54. link_node = html_node.xpath("//a[hasclass('view_in_browser_url')]")
  55. self.assertEqual(link_node, [])
  56. link_node = html_node.xpath(
  57. "//a[not(hasclass('view_in_browser_url'))]"
  58. )
  59. self.assertEqual(len(link_node), 1)
  60. self.assertEqual(link_node[0].get('href'), 'https://www.google.com')
  61. p_node = html_node.xpath("//p")
  62. self.assertEqual(len(p_node), 2)
  63. @patch('odoo.addons.mail_browser_view.'
  64. 'controllers.browser_view.request')
  65. def test_controller(self, req):
  66. # Mock
  67. req.env = self.env
  68. controller = EmailBrowserViewController()
  69. controller.email_view(self.valid_token[::2])
  70. req.not_found.assert_called_once_with()
  71. req.make_response.assert_not_called()
  72. req.reset_mock()
  73. controller.email_view(self.valid_token)
  74. req.not_found.assert_not_called()
  75. req.make_response.assert_called_with(self.mail0.body_html)