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.
68 lines
2.5 KiB
68 lines
2.5 KiB
# -*- coding: utf-8 -*-
|
|
# Copyright 2019 Therp BV <https://therp.nl>
|
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
|
from odoo.tests.common import TransactionCase
|
|
from mock import patch
|
|
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
|
|
|
|
@patch(
|
|
'odoo.fields.html_sanitize',
|
|
side_effect=lambda *args, **kwargs: args[0],
|
|
)
|
|
def test_mail_embed_image(self, html_sanitize):
|
|
""" 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_html': body1,
|
|
'email_from': 'test@example.com',
|
|
'email_to': 'test@example.com',
|
|
})
|
|
email1.send()
|
|
self.assertEquals(email1.body_html, 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_html': body2,
|
|
'email_from': 'test@example.com',
|
|
'email_to': 'test@example.com',
|
|
})
|
|
email2.send()
|
|
# TODO the below will not work, assert build_email called and return
|
|
# value (remove the tests above as well, they are foolish)
|
|
body2final = fromstring(email2.body_html)
|
|
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))
|