diff --git a/mail_embed_image/models/ir_mail_server.py b/mail_embed_image/models/ir_mail_server.py index c9d125e9..dc44cdab 100644 --- a/mail_embed_image/models/ir_mail_server.py +++ b/mail_embed_image/models/ir_mail_server.py @@ -6,7 +6,6 @@ import uuid from odoo import models from odoo.addons.base.ir.ir_mail_server import encode_header_param from lxml.html.soupparser import fromstring -from lxml import etree from lxml.etree import tostring from base64 import b64encode from email.mime.image import MIMEImage diff --git a/mail_embed_image/tests/test_mail_embed_image.py b/mail_embed_image/tests/test_mail_embed_image.py index 153a40d8..810fd69a 100644 --- a/mail_embed_image/tests/test_mail_embed_image.py +++ b/mail_embed_image/tests/test_mail_embed_image.py @@ -1,8 +1,11 @@ # -*- coding: utf-8 -*- # Copyright 2019 Therp BV # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from openerp.tests.common import TransactionCase -from lxml import etree +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): @@ -11,15 +14,46 @@ class TestMailEmbedImage(TransactionCase): at_install = False def test_mail_embed_image(self): - server = self.env['ir.mail_server'] - root = etree.Element('div') - etree.SubElement(root, 'img') - etree.SubElement(root, 'img') - body = etree.tostring(root) - message = server.build_email( - 'test@example.com', - 'test@example.com', - 'subject', - body, - ) - # TODO assertions + """ 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))