Skip to content
Snippets Groups Projects
Commit a7574396 authored by Jairo Llopis's avatar Jairo Llopis Committed by Thibault Delavallée
Browse files

[IMP] test_mail: test models with type do not mess with attachment types

Purpose of this commit is to highlight an issue that may happens easily with
`crm` that is made generic here within `test_mail`.

`crm` alters the context when creating a new record adding in this case
`default_type` to it][1]. The returned record contains that altered context.
his results in other records created from it trying to assign that same default
value for `type`. This is a very common name for fields, and happens to exist
in `ir.attachment` too.

If you create an alias for incoming leads in your DB with default values
`{"type": "lead"}` (something very common) and then an email comes to that
alias that contains an inlined base64 image, the attachment creation process
would simply fail.

Obtained error is ``ValueError: Wrong value for ir.attachment.type: 'lead'`` .

[1]: https://github.com/odoo/odoo/blob/272602193f5647f7f2270ed6ec68777625a139dd/addons/crm/models/crm_lead.py#L310-L311



Co-Authored-By: default avatarThibault Delavallee <tde@odoo.com>
parent b45e65a9
No related branches found
No related tags found
No related merge requests found
......@@ -43,6 +43,50 @@ Content-Transfer-Encoding: quoted-printable
------=_Part_4200734_24778174.1344608186754--
"""
MAIL_TEMPLATE_EXTRA_HTML = """Return-Path: <whatever-2a840@postmaster.twitter.com>
To: {to}
cc: {cc}
Received: by mail1.openerp.com (Postfix, from userid 10002)
id 5DF9ABFB2A; Fri, 10 Aug 2012 16:16:39 +0200 (CEST)
From: {email_from}
Subject: {subject}
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_Part_4200734_24778174.1344608186754"
Date: Fri, 10 Aug 2012 14:16:26 +0000
Message-ID: {msg_id}
{extra}
------=_Part_4200734_24778174.1344608186754
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Please call me as soon as possible this afternoon!
--
Sylvie
------=_Part_4200734_24778174.1344608186754
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>=20
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8" />
</head>=20
<body style=3D"margin: 0; padding: 0; background: #ffffff;-webkit-text-size-adjust: 100%;">=20
<p>Please call me as soon as possible this afternoon!</p>
{extra_html}
<p>--<br/>
Sylvie
<p>
</body>
</html>
------=_Part_4200734_24778174.1344608186754--
"""
MAIL_TEMPLATE_PLAINTEXT = """Return-Path: <whatever-2a840@postmaster.twitter.com>
To: {to}
Received: by mail1.openerp.com (Postfix, from userid 10002)
......
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import test_mail_corner_case_models
from . import test_mail_models
from . import test_mail_models
\ No newline at end of file
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
class MailTestFieldType(models.Model):
""" Test default values, notably type, messing through models during gateway
processing (i.e. lead.type versus attachment.type). """
_description = 'Test Field Type'
_name = 'mail.test.field.type'
_inherit = ['mail.thread']
name = fields.Char()
email_from = fields.Char()
datetime = fields.Datetime(default=fields.Datetime.now)
customer_id = fields.Many2one('res.partner', 'Customer')
type = fields.Selection([('first', 'First'), ('second', 'Second')])
user_id = fields.Many2one('res.users', 'Responsible', track_visibility='onchange')
@api.model_create_multi
def create(self, vals_list):
# Emulate an addon that alters the creation context, such as `crm`
if not self._context.get('default_type'):
self = self.with_context(default_type='first')
return super(MailTestFieldType, self).create(vals_list)
......@@ -6,6 +6,8 @@ access_mail_test_track_portal,mail.test.track.portal,model_mail_test_track,base.
access_mail_test_track_user,mail.test.track.user.employee,model_mail_test_track,base.group_user,1,1,1,1
access_mail_test_activity_portal,mail.test.activity.portal,model_mail_test_activity,base.group_portal,1,0,0,0
access_mail_test_activity_user,mail.test.activity.user,model_mail_test_activity,base.group_user,1,1,1,1
access_mail_test_field_type_portal,mail.test.field.type.portal,model_mail_test_field_type,base.group_portal,0,0,0,0
access_mail_test_field_type_user,mail.test.field.type.user,model_mail_test_field_type,base.group_user,1,1,1,1
access_mail_test_full_portal,mail.test.full.portal,model_mail_test_full,base.group_portal,0,0,0,0
access_mail_test_full_user,mail.test.full.user,model_mail_test_full,base.group_user,1,1,1,1
access_mail_test_portal,mail.test.portal,model_mail_test,base.group_portal,1,1,0,0
......
......@@ -251,15 +251,15 @@ class MockEmails(common.SingleTransactionCase):
def format(self, template, to='groups@example.com, other@gmail.com', subject='Frogs',
extra='', email_from='Sylvie Lelitre <test.sylvie.lelitre@agrolait.com>',
cc='', msg_id='<1198923581.41972151344608186760.JavaMail@agrolait.com>'):
return template.format(to=to, subject=subject, cc=cc, extra=extra, email_from=email_from, msg_id=msg_id)
cc='', msg_id='<1198923581.41972151344608186760.JavaMail@agrolait.com>', **kwargs):
return template.format(to=to, subject=subject, cc=cc, extra=extra, email_from=email_from, msg_id=msg_id, **kwargs)
def format_and_process(self, template, to='groups@example.com, other@gmail.com', subject='Frogs',
extra='', email_from='Sylvie Lelitre <test.sylvie.lelitre@agrolait.com>',
cc='', msg_id='<1198923581.41972151344608186760.JavaMail@agrolait.com>',
model=None, target_model='mail.test.simple', target_field='name'):
model=None, target_model='mail.test.simple', target_field='name', **kwargs):
self.assertFalse(self.env[target_model].search([(target_field, '=', subject)]))
mail = self.format(template, to=to, subject=subject, cc=cc, extra=extra, email_from=email_from, msg_id=msg_id)
mail = self.format(template, to=to, subject=subject, cc=cc, extra=extra, email_from=email_from, msg_id=msg_id, **kwargs)
self.env['mail.thread'].with_context(mail_channel_noautofollow=True).message_process(model, mail)
return self.env[target_model].search([(target_field, '=', subject)])
......
......@@ -6,12 +6,14 @@ import socket
from odoo.addons.test_mail.data.test_mail_data import \
MAIL_TEMPLATE, MAIL_TEMPLATE_PLAINTEXT, MAIL_MULTIPART_MIXED, MAIL_MULTIPART_MIXED_TWO, \
MAIL_MULTIPART_IMAGE, MAIL_SINGLE_BINARY, MAIL_EML_ATTACHMENT, MAIL_ATTACHMENT_BAD_ENCODING, \
MAIL_XHTML
MAIL_XHTML, MAIL_TEMPLATE_EXTRA_HTML
from odoo.addons.test_mail.tests.common import BaseFunctionalTest, MockEmails
from odoo.addons.test_mail.tests.common import mail_new_test_user
from odoo.tests import tagged
from odoo.tools import mute_logger, formataddr
@tagged('mail_gateway')
class TestEmailParsing(BaseFunctionalTest, MockEmails):
@mute_logger('odoo.addons.mail.models.mail_thread')
......@@ -51,6 +53,7 @@ class TestEmailParsing(BaseFunctionalTest, MockEmails):
self.assertEqual(res['attachments'][0][0], 'thetruth.pdf')
@tagged('mail_gateway')
class TestMailgateway(BaseFunctionalTest, MockEmails):
@classmethod
......@@ -692,3 +695,52 @@ class TestMailgateway(BaseFunctionalTest, MockEmails):
self.assertEqual(msg_fw.model, 'mail.test.simple')
self.assertFalse(msg_fw.parent_id)
self.assertTrue(msg_fw.res_id == new_record.id)
# --------------------------------------------------
# Gateway / Record synchronization
# --------------------------------------------------
@mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models')
def test_gateway_values_base64_image(self):
"""New record with mail that contains base64 inline image."""
target_model = "mail.test.field.type"
alias = self.env["mail.alias"].create({
"alias_name": "base64-lover",
"alias_model_id": self.env["ir.model"]._get(target_model).id,
"alias_defaults": "{}",
"alias_contact": "everyone",
})
record = self.format_and_process(
MAIL_TEMPLATE_EXTRA_HTML,
to='%s@%s' % (alias.alias_name, self.catchall_domain),
subject='base64 image to alias',
target_model=target_model,
extra_html='<img src="data:image/png;base64,iV/+OkI=">',
)
self.assertEqual(record.type, "first")
self.assertEqual(len(record.message_ids[0].attachment_ids), 1)
self.assertEqual(record.message_ids[0].attachment_ids[0].name, "image0")
self.assertEqual(record.message_ids[0].attachment_ids[0].type, "binary")
@mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models')
def test_gateway_values_base64_image_walias(self):
"""New record with mail that contains base64 inline image + default values
coming from alias."""
target_model = "mail.test.field.type"
alias = self.env["mail.alias"].create({
"alias_name": "base64-lover",
"alias_model_id": self.env["ir.model"]._get(target_model).id,
"alias_defaults": "{'type': 'second'}",
"alias_contact": "everyone",
})
record = self.format_and_process(
MAIL_TEMPLATE_EXTRA_HTML,
to='%s@%s' % (alias.alias_name, self.catchall_domain),
subject='base64 image to alias',
target_model=target_model,
extra_html='<img src="data:image/png;base64,iV/+OkI=">',
)
self.assertEqual(record.type, "second")
self.assertEqual(len(record.message_ids[0].attachment_ids), 1)
self.assertEqual(record.message_ids[0].attachment_ids[0].name, "image0")
self.assertEqual(record.message_ids[0].attachment_ids[0].type, "binary")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment