From cfe9172884eec368af202ef9aacad6963615b5ce Mon Sep 17 00:00:00 2001 From: Victor Feyens Date: Mon, 1 Aug 2022 10:19:58 +0000 Subject: [PATCH] [ADD] base: standard test commons * Consistent currency used for tests * Consistent base data (partner & users) Part-of: odoo/odoo#96791 --- odoo/addons/base/tests/common.py | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/odoo/addons/base/tests/common.py b/odoo/addons/base/tests/common.py index c4b8e412e23..f5b57da1003 100644 --- a/odoo/addons/base/tests/common.py +++ b/odoo/addons/base/tests/common.py @@ -7,6 +7,74 @@ from unittest.mock import patch from odoo.tests.common import TransactionCase, HttpCase from odoo import Command +DISABLED_MAIL_CONTEXT = { + 'tracking_disable': True, + 'mail_create_nolog': True, + 'mail_create_nosubscribe': True, + 'mail_notrack': True, + 'no_reset_password': True, +} + + +class BaseCommon(TransactionCase): + + @classmethod + def setUpClass(cls): + super().setUpClass() + + # Enforce the use of USD as main currency unless modified in inherited class(es) + cls._use_currency('USD') + + # Mail logic won't be tested by default in other modules. + # Mail API overrides should be tested with dedicated tests on purpose + # Hack to use with_context and avoid manual context dict modification + cls.env = cls.env['base'].with_context(**DISABLED_MAIL_CONTEXT).env + + cls.partner = cls.env['res.partner'].create({ + 'name': 'Test Partner', + }) + + @classmethod + def _use_currency(cls, currency_code): + # Enforce constant currency + currency = cls._enable_currency(currency_code) + if not cls.env.company.currency_id == currency: + cls.env.company.currency_id = currency + + @classmethod + def _enable_currency(cls, currency_code): + currency = cls.env['res.currency'].with_context(active_test=False).search( + [('name', '=', currency_code.upper())] + ) + currency.action_unarchive() + return currency + + +class BaseUsersCommon(BaseCommon): + + @classmethod + def setUpClass(cls): + super().setUpClass() + + cls.group_portal = cls.env.ref('base.group_portal') + cls.group_user = cls.env.ref('base.group_user') + + cls.user_portal = cls.env['res.users'].create({ + 'name': 'Test Portal User', + 'login': 'portal_user', + 'password': 'portal_user', + 'email': 'portal_user@gladys.portal', + 'groups_id': [Command.set([cls.group_portal.id])], + }) + + cls.user_internal = cls.env['res.users'].create({ + 'name': 'Test Internal User', + 'login': 'internal_user', + 'password': 'internal_user', + 'email': 'mark.brown23@example.com', + 'groups_id': [Command.set([cls.group_user.id])], + }) + class TransactionCaseWithUserDemo(TransactionCase):