# -*- coding: utf-8 -*- # Copyright 2019 Therp BV # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). from odoo.tests.common import TransactionCase from requests import get from base64 import b64encode from lxml.html.soupparser import fromstring from odoo.addons.base.ir.ir_mail_server import encode_header_param class TestMailEmbedImage(TransactionCase): post_install = True at_install = False def test_mail_embed_image(self): """ The following are tested here: 1) Create an email with no image, send it. 2) Create an email with three images, one base64 content, one http full url and one of the format `/web/image/.*` We assert that nothing has changed on 1) and only the `/web/image/.*` has changed in 2) """ model_mail_mail = self.env['mail.mail'] base_url = self.env['ir.config_parameter'].get_param('web.base.url') image_url = base_url + '/mail_embed_image/static/description/icon.png' image = get(image_url).content body1 = '
this is an email
' email1 = model_mail_mail.create({'body': body1}) email1.send() message_body = self.env.cr.execute( "SELECT body FROM mail_message WHERE message_id = %s LIMIT 1", (email1.message_id, )) self.assertEquals(message_body, body1) body2 = """
this is an email
""" % ( b64encode(image), image_url, '/web/image/res.partner/1/image', ) email2 = model_mail_mail.create({'body': body2}) email2.send() message_body = self.env.cr.execute( "SELECT body FROM mail_message WHERE message_id = %s LIMIT 1", (email1.message_id, )) body2final = fromstring(message_body) self.assertEquals(len(body2final.xpath('//img')), 3) srcs = [x.get('src') for x in body2final.xpath('//img')] self.assertIn('base64:', srcs[0]) self.assertIn('http:', srcs[1]) self.assertIn('cid:', srcs[2]) cid = srcs[2][3:] self.assertEquals(email2.attachment_ids.name, encode_header_param(cid))