|
|
@ -1,8 +1,11 @@ |
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
# Copyright 2019 Therp BV <https://therp.nl> |
|
|
|
# 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 = '<div>this is an email</div>' |
|
|
|
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 = """ |
|
|
|
<div> |
|
|
|
this is an email |
|
|
|
<img src="base64: %s"></img> |
|
|
|
<img src="%s"></img> |
|
|
|
<img src="%s"></img> |
|
|
|
</div>""" % ( |
|
|
|
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)) |