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.

59 lines
2.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 Therp BV <https://therp.nl>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from odoo.tests.common import TransactionCase
  5. from requests import get
  6. from base64 import b64encode
  7. from lxml.html.soupparser import fromstring
  8. from odoo.addons.base.ir.ir_mail_server import encode_header_param
  9. class TestMailEmbedImage(TransactionCase):
  10. post_install = True
  11. at_install = False
  12. def test_mail_embed_image(self):
  13. """ The following are tested here:
  14. 1) Create an email with no image, send it.
  15. 2) Create an email with three images, one base64 content, one http
  16. full url and one of the format `/web/image/.*`
  17. We assert that nothing has changed on 1) and only the `/web/image/.*`
  18. has changed in 2)
  19. """
  20. model_mail_mail = self.env['mail.mail']
  21. base_url = self.env['ir.config_parameter'].get_param('web.base.url')
  22. image_url = base_url + '/mail_embed_image/static/description/icon.png'
  23. image = get(image_url).content
  24. body1 = '<div>this is an email</div>'
  25. email1 = model_mail_mail.create({'body': body1})
  26. email1.send()
  27. message_body = self.env.cr.execute(
  28. "SELECT body FROM mail_message WHERE message_id = %s LIMIT 1",
  29. (email1.message_id, ))
  30. self.assertEquals(message_body, body1)
  31. body2 = """
  32. <div>
  33. this is an email
  34. <img src="base64: %s"></img>
  35. <img src="%s"></img>
  36. <img src="%s"></img>
  37. </div>""" % (
  38. b64encode(image),
  39. image_url,
  40. '/web/image/res.partner/1/image',
  41. )
  42. email2 = model_mail_mail.create({'body': body2})
  43. email2.send()
  44. message_body = self.env.cr.execute(
  45. "SELECT body FROM mail_message WHERE message_id = %s LIMIT 1",
  46. (email1.message_id, ))
  47. body2final = fromstring(message_body)
  48. self.assertEquals(len(body2final.xpath('//img')), 3)
  49. srcs = [x.get('src') for x in body2final.xpath('//img')]
  50. self.assertIn('base64:', srcs[0])
  51. self.assertIn('http:', srcs[1])
  52. self.assertIn('cid:', srcs[2])
  53. cid = srcs[2][3:]
  54. self.assertEquals(email2.attachment_ids.name, encode_header_param(cid))