[IMP] payment: simplify test commons

During the payment pocalypse, new tests common were introduced. Those
commons were split into different classes to be as modular as possible
but we noticed during the following months (/year) that they weren't so
easy to understand and use.

Therefore, this commit aims to simplify those commons by removing the
core PaymentTestUtils common, integrating it in the base PaymentCommon,
and making the HttpCommon depend on the core PaymentCommon, instead of
using only the utils.

This doesn't require much changes in the tests since they all used
either the PaymentCommon or both PaymentCommon and PaymentHttpCommon.

Task - 2848326

Part-of: odoo/odoo#90716
This commit is contained in:
Victor Feyens
2022-05-31 19:17:19 +02:00
parent ac00d19c8d
commit 26976477c4
5 changed files with 69 additions and 83 deletions
+65 -2
View File
@@ -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
+2 -5
View File
@@ -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 #
+1 -2
View File
@@ -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.
-72
View File
@@ -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
+1 -2
View File
@@ -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):