Files
odoo_source/addons/test_mail/tests/common.py
T
Thibault Delavallée 6d166d6088 [IMP] base, mail_*: use MockSmtplibCase and improve tests tooling
mail_* = mail, test_mail, test_mail_full, test_mass_mailing

PURPOSE

Improve tooling of mail tests. Use SMTP mockup class available in base tests
in mail tests to have a better and simplified mock tool when dealing with
mail tests. Remove old fashioned code.

SPECIFICATIONS

Include ``MockSmtplibCase`` directly into ``MockEmail`` so that we re-use
existing components for mocking outgoing emails. Also make parameters for
gateway coherent between those two classes (use same test values).

Remove 'sim_error' weird parameter of Mail ``mock_mail_gateway`` and use
standard mock 'side_effect' instead in some specific tests, notably on
SMTP.connect() mock.

Add some assert / tools method and improve existing tools, used for creating
or improving test data. This will be used in future commits when adding new
tests.

Add outgoing mail servers data in mail tests, allowing to make tests closer
to real life use cases.

Remove unnecessary calls to ``_init_mail_gateway`` as it is automatically
done in ``MailCommon.setUpClass()``.

Try to consolidate starting test data, notably with countries, phone numbers
or email in mind to be sure test mail are deterministic.

Task-2673913 (TestMail: Cleanup and improve test coverage)

Part-of: odoo/odoo#86393
2022-03-24 13:57:35 +01:00

64 lines
2.2 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.addons.mail.tests.common import MailCommon
from odoo.tests.common import TransactionCase
class TestMailCommon(MailCommon):
""" Main entry point for functional tests. """
@classmethod
def _create_records_for_batch(cls, model, count, additional_values=None):
additional_values = additional_values or {}
records = cls.env[model]
partners = cls.env['res.partner']
country_id = cls.env.ref('base.be').id
base_values = [
{'name': 'Test_%s' % idx,
**additional_values,
} for idx in range(count)
]
if 'customer_id' in cls.env[model]:
partners = cls.env['res.partner'].with_context(**cls._test_context).create([{
'name': 'Partner_%s' % idx,
'email': '_test_partner_%s@example.com' % idx,
'country_id': country_id,
'mobile': '047500%02d%02d' % (idx, idx)
} for idx in range(count)])
for values, partner in zip(base_values, partners):
values['customer_id'] = partner.id
records = cls.env[model].with_context(**cls._test_context).create(base_values)
cls.records = cls._reset_mail_context(records)
cls.partners = partners
return cls.records, cls.partners
class TestRecipients(TransactionCase):
@classmethod
def setUpClass(cls):
super(TestRecipients, cls).setUpClass()
Partner = cls.env['res.partner'].with_context({
'mail_create_nolog': True,
'mail_create_nosubscribe': True,
'mail_notrack': True,
'no_reset_password': True,
})
cls.partner_1 = Partner.create({
'name': 'Valid Lelitre',
'email': 'valid.lelitre@agrolait.com',
'country_id': cls.env.ref('base.be').id,
'mobile': '0456001122',
})
cls.partner_2 = Partner.create({
'name': 'Valid Poilvache',
'email': 'valid.other@gmail.com',
'country_id': cls.env.ref('base.be').id,
'mobile': '+32 456 22 11 00',
})