diff --git a/addons/payment/tests/common.py b/addons/payment/tests/common.py index 2f673b50dda..507f823e916 100644 --- a/addons/payment/tests/common.py +++ b/addons/payment/tests/common.py @@ -3,15 +3,19 @@ import logging from unittest.mock import patch +from lxml import objectify +from werkzeug import urls + from odoo.fields import Command +from odoo.tools.misc import hmac as hmac_tool from odoo.addons.account.models.account_payment_method import AccountPaymentMethod -from odoo.addons.payment.tests.utils import PaymentTestUtils +from odoo.addons.account.tests.common import AccountTestInvoicingCommon _logger = logging.getLogger(__name__) -class PaymentCommon(PaymentTestUtils): +class PaymentCommon(AccountTestInvoicingCommon): @classmethod def setUpClass(cls, chart_template_ref=None): @@ -220,3 +224,62 @@ class PaymentCommon(PaymentTestUtils): 'invoice_id': self.invoice.id, 'flow': flow, } + + def _generate_test_access_token(self, *values): + """ Generate an access token based on the provided values for testing purposes. + + This methods returns a token identical to that generated by + payment.utils.generate_access_token but uses the test class environment rather than the + environment of odoo.http.request. + + See payment.utils.generate_access_token for additional details. + + :param list values: The values to use for the generation of the token + :return: The generated access token + :rtype: str + """ + token_str = '|'.join(str(val) for val in values) + access_token = hmac_tool(self.env(su=True), 'generate_access_token', token_str) + return access_token + + def _build_url(self, route): + return urls.url_join(self.base_url(), route) + + def _extract_values_from_html_form(self, html_form): + """ Extract the transaction rendering values from an HTML form. + + :param str html_form: The HTML form + :return: The extracted information (action & inputs) + :rtype: dict[str:str] + """ + html_tree = objectify.fromstring(html_form) + if hasattr(html_tree, 'input'): + inputs = {input_.get('name'): input_.get('value') for input_ in html_tree.input} + else: + inputs = {} + return { + 'action': html_tree.get('action'), + 'method': html_tree.get('method'), + 'inputs': inputs, + } + + def _assert_does_not_raise(self, exception_class, func, *args, **kwargs): + """ Fail if an exception of the provided class is raised when calling the function. + + If an exception of any other class is raised, it is caught and silently ignored. + + This method cannot be used with functions that make requests. Any exception raised in the + scope of the new request will not be caught and will make the test fail. + + :param class exception_class: The class of the exception to monitor + :param function fun: The function to call when monitoring for exceptions + :param list args: The positional arguments passed as-is to the called function + :param dict kwargs: The keyword arguments passed as-is to the called function + :return: None + """ + try: + func(*args, **kwargs) + except exception_class: + self.fail(f"{func.__name__} should not raise error of class {exception_class.__name__}") + except Exception: + pass # Any exception whose class is not monitored is caught and ignored diff --git a/addons/payment/tests/http_common.py b/addons/payment/tests/http_common.py index 4a3533f7a15..5c6f44d3e3f 100644 --- a/addons/payment/tests/http_common.py +++ b/addons/payment/tests/http_common.py @@ -2,21 +2,18 @@ import json from uuid import uuid4 - from lxml import etree, objectify from odoo.tests import HttpCase -from odoo.addons.payment.tests.utils import PaymentTestUtils +from odoo.addons.payment.tests.common import PaymentCommon -class PaymentHttpCommon(PaymentTestUtils, HttpCase): +class PaymentHttpCommon(PaymentCommon, HttpCase): """ HttpCase common to build and simulate requests going through payment controllers. Only use if you effectively want to test controllers. If you only want to test 'models' code, the PaymentCommon should be sufficient. - - Note: This Common is expected to be used in parallel with the main PaymentCommon. """ # Helpers # diff --git a/addons/payment/tests/test_flows.py b/addons/payment/tests/test_flows.py index 735daa98927..89d7649e5b5 100644 --- a/addons/payment/tests/test_flows.py +++ b/addons/payment/tests/test_flows.py @@ -9,12 +9,11 @@ from odoo.tests import tagged from odoo.tools import mute_logger from odoo.addons.payment.controllers.portal import PaymentPortal -from odoo.addons.payment.tests.common import PaymentCommon from odoo.addons.payment.tests.http_common import PaymentHttpCommon @tagged('post_install', '-at_install') -class TestFlows(PaymentCommon, PaymentHttpCommon): +class TestFlows(PaymentHttpCommon): def _test_flow(self, flow): """ Simulate the given online payment flow and tests the tx values at each step. diff --git a/addons/payment/tests/utils.py b/addons/payment/tests/utils.py deleted file mode 100644 index 6b3c71fa4bb..00000000000 --- a/addons/payment/tests/utils.py +++ /dev/null @@ -1,72 +0,0 @@ -# Part of Odoo. See LICENSE file for full copyright and licensing details. - -import logging - -from lxml import objectify -from werkzeug import urls - -from odoo.addons.account.tests.common import AccountTestInvoicingCommon -from odoo.tools.misc import hmac as hmac_tool - -_logger = logging.getLogger(__name__) - - -class PaymentTestUtils(AccountTestInvoicingCommon): - def _generate_test_access_token(self, *values): - """ Generate an access token based on the provided values for testing purposes. - - This methods returns a token identical to that generated by - payment.utils.generate_access_token but uses the test class environment rather than the - environment of odoo.http.request. - - See payment.utils.generate_access_token for additional details. - - :param list values: The values to use for the generation of the token - :return: The generated access token - :rtype: str - """ - token_str = '|'.join(str(val) for val in values) - access_token = hmac_tool(self.env(su=True), 'generate_access_token', token_str) - return access_token - - def _build_url(self, route): - return urls.url_join(self.base_url(), route) - - def _extract_values_from_html_form(self, html_form): - """ Extract the transaction rendering values from an HTML form. - - :param str html_form: The HTML form - :return: The extracted information (action & inputs) - :rtype: dict[str:str] - """ - html_tree = objectify.fromstring(html_form) - if hasattr(html_tree, 'input'): - inputs = {input_.get('name'): input_.get('value') for input_ in html_tree.input} - else: - inputs = {} - return { - 'action': html_tree.get('action'), - 'method': html_tree.get('method'), - 'inputs': inputs, - } - - def _assert_does_not_raise(self, exception_class, func, *args, **kwargs): - """ Fail if an exception of the provided class is raised when calling the function. - - If an exception of any other class is raised, it is caught and silently ignored. - - This method cannot be used with functions that make requests. Any exception raised in the - scope of the new request will not be caught and will make the test fail. - - :param class exception_class: The class of the exception to monitor - :param function fun: The function to call when monitoring for exceptions - :param list args: The positional arguments passed as-is to the called function - :param dict kwargs: The keyword arguments passed as-is to the called function - :return: None - """ - try: - func(*args, **kwargs) - except exception_class: - self.fail(f"{func.__name__} should not raise error of class {exception_class.__name__}") - except Exception: - pass # Any exception whose class is not monitored is caught and ignored diff --git a/addons/sale/tests/test_payment_flow.py b/addons/sale/tests/test_payment_flow.py index 3a4d12d0df8..d0f762f6e84 100644 --- a/addons/sale/tests/test_payment_flow.py +++ b/addons/sale/tests/test_payment_flow.py @@ -5,12 +5,11 @@ from odoo.fields import Command from odoo.tests import tagged from odoo.tools import mute_logger -from odoo.addons.payment.tests.common import PaymentCommon from odoo.addons.payment.tests.http_common import PaymentHttpCommon @tagged('-at_install', 'post_install') -class TestSalePayment(PaymentCommon, PaymentHttpCommon): +class TestSalePayment(PaymentHttpCommon): @classmethod def setUpClass(cls):