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.

62 lines
2.0 KiB

  1. # Copyright 2019 Camptocamp SA
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from lxml import html
  4. from odoo.tests import SavepointCase
  5. class TestMailInlineStyles(SavepointCase):
  6. @classmethod
  7. def setUpClass(cls):
  8. super().setUpClass()
  9. cls.mail_template = cls.env.ref(
  10. 'mail_inline_css.email_template_demo')
  11. cls.demo_user = cls.env.ref('base.user_demo')
  12. def to_xml_node(self, html_):
  13. return html.fragments_fromstring(html_)
  14. def parse_node_style(self, node):
  15. """ Convert node CSS string to Python dict"""
  16. res = {}
  17. for style in node.attrib.get('style', '').split(';'):
  18. l = style.split(':')
  19. res[l[0].strip()] = l[1].strip()
  20. return res
  21. def find_by_id(self, node, html_id):
  22. return node.xpath('//*[@id="{}"]'.format(html_id))
  23. def assertNodeStyle(self, node, expected):
  24. self.assertIn('style', node.attrib)
  25. self.assertEqual(self.parse_node_style(node), expected)
  26. def test_generate_mail(self):
  27. res = self.mail_template.generate_email(
  28. [self.demo_user.id], fields=['body_html']
  29. )
  30. body_html_string = res[self.demo_user.id].get('body_html')
  31. html_node = self.to_xml_node(body_html_string)[0]
  32. expected = {
  33. 'main_logo': {
  34. 'max-width': '300px'
  35. },
  36. 'main_wrapper': {
  37. 'max-width': '620px',
  38. 'margin': '0 auto',
  39. 'border': '1px solid #ccc',
  40. 'font-size': '18px',
  41. 'font-family': 'verdana',
  42. 'color': '#6B6E71'
  43. },
  44. 'main_footer': {
  45. 'padding-top': '0',
  46. 'font-size': '120%',
  47. 'padding': '30px 40px'
  48. }
  49. }
  50. for html_id, expected_style in expected.items():
  51. node = self.find_by_id(html_node, html_id)[0]
  52. self.assertNodeStyle(node, expected_style)