[IMP] payment: improve send_payment_request test
We split the test in three to allow easier debugging at no cost in line of codes and by extracting a part that could be used for future tests. task-2659750 closes odoo/odoo#85162 X-original-commit: 8502d4ec895f2d4a2ddb1b305a9f6f119da750fa Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com> Signed-off-by: Morgane Demesmaeker <edm@odoo.com>
This commit is contained in:
@@ -194,3 +194,28 @@ class PaymentCommon(PaymentTestUtils):
|
||||
return self.env['payment.transaction'].sudo().search([
|
||||
('reference', '=', reference),
|
||||
])
|
||||
|
||||
def _prepare_transaction_values(self, payment_option_id, flow):
|
||||
""" Prepare the basic payment/transaction route values.
|
||||
|
||||
:param int payment_option_id: The payment option handling the transaction, as a
|
||||
`payment.acquirer` id or a `payment.token` id
|
||||
:param str flow: The payment flow
|
||||
:return: The route values
|
||||
:rtype: dict
|
||||
"""
|
||||
return {
|
||||
'amount': self.amount,
|
||||
'currency_id': self.currency.id,
|
||||
'partner_id': self.partner.id,
|
||||
'access_token': self._generate_test_access_token(
|
||||
self.partner.id, self.amount, self.currency.id
|
||||
),
|
||||
'payment_option_id': payment_option_id,
|
||||
'reference_prefix': 'test',
|
||||
'tokenization_requested': True,
|
||||
'landing_route': 'Test',
|
||||
'is_validation': False,
|
||||
'invoice_id': self.invoice.id,
|
||||
'flow': flow,
|
||||
}
|
||||
|
||||
@@ -202,8 +202,10 @@ class PaymentHttpCommon(PaymentTestUtils, HttpCase):
|
||||
"""
|
||||
uri = '/payment/transaction'
|
||||
url = self._build_url(uri)
|
||||
response = self._make_json_rpc_request(url, route_kwargs)
|
||||
self.assertEqual(response.status_code, 200) # Check the request went through.
|
||||
|
||||
return self._make_json_rpc_request(url, route_kwargs)
|
||||
return response
|
||||
|
||||
def get_processing_values(self, **route_kwargs):
|
||||
response = self.portal_transaction(**route_kwargs)
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from freezegun import freeze_time
|
||||
from unittest.mock import patch
|
||||
|
||||
from freezegun import freeze_time
|
||||
|
||||
from odoo.tests import tagged
|
||||
from odoo.tools import mute_logger
|
||||
|
||||
@@ -360,41 +361,44 @@ class TestFlows(PaymentCommon, PaymentHttpCommon):
|
||||
self.assertEqual(manage_context['acquirer_ids'], [acquirer_b.id])
|
||||
self.assertEqual(manage_context['token_ids'], [])
|
||||
|
||||
def test_number_of_payment_request_from_portal(self):
|
||||
@mute_logger('odoo.addons.payment.models.payment_transaction')
|
||||
def test_direct_payment_triggers_no_payment_request(self):
|
||||
self.authenticate(self.portal_user.login, self.portal_user.login)
|
||||
self.partner = self.portal_partner
|
||||
self.user = self.portal_user
|
||||
self._test_number_of_payment_request_from_portal('direct', 0)
|
||||
self._test_number_of_payment_request_from_portal('redirect', 0)
|
||||
self._test_number_of_payment_request_from_portal('token', 1)
|
||||
|
||||
@mute_logger('odoo.addons.payment.models.payment_transaction')
|
||||
def _test_number_of_payment_request_from_portal(self, flow, calls):
|
||||
""" Test the number of payment requests made from the portal for a given online payment flow
|
||||
|
||||
:param str flow: The online payment flow to test ('direct', 'redirect', or 'token')
|
||||
:param int calls: The expected number of call to `_send_payment_request`
|
||||
"""
|
||||
payment_option_id = self.create_token().id if flow == 'token' else self.acquirer.id
|
||||
data = {
|
||||
'amount': self.amount,
|
||||
'currency_id': self.currency.id,
|
||||
'partner_id': self.partner.id,
|
||||
'access_token': self._generate_test_access_token(
|
||||
self.partner.id, self.amount, self.currency.id
|
||||
),
|
||||
'payment_option_id': payment_option_id,
|
||||
'reference_prefix': 'test',
|
||||
'tokenization_requested': True,
|
||||
'landing_route': 'Test',
|
||||
'is_validation': False,
|
||||
'invoice_id': self.invoice.id,
|
||||
'flow': flow,
|
||||
}
|
||||
|
||||
with patch(
|
||||
'odoo.addons.payment.models.payment_transaction.PaymentTransaction'
|
||||
'._send_payment_request'
|
||||
) as patched:
|
||||
self.get_processing_values(**data)
|
||||
self.assertEqual(patched.call_count, calls)
|
||||
self.portal_transaction(
|
||||
**self._prepare_transaction_values(self.acquirer.id, 'direct')
|
||||
)
|
||||
self.assertEqual(patched.call_count, 0)
|
||||
|
||||
@mute_logger('odoo.addons.payment.models.payment_transaction')
|
||||
def test_payment_with_redirect_triggers_no_payment_request(self):
|
||||
self.authenticate(self.portal_user.login, self.portal_user.login)
|
||||
self.partner = self.portal_partner
|
||||
self.user = self.portal_user
|
||||
with patch(
|
||||
'odoo.addons.payment.models.payment_transaction.PaymentTransaction'
|
||||
'._send_payment_request'
|
||||
) as patched:
|
||||
self.portal_transaction(
|
||||
**self._prepare_transaction_values(self.acquirer.id, 'redirect')
|
||||
)
|
||||
self.assertEqual(patched.call_count, 0)
|
||||
|
||||
@mute_logger('odoo.addons.payment.models.payment_transaction')
|
||||
def test_payment_by_token_triggers_exactly_one_payment_request(self):
|
||||
self.authenticate(self.portal_user.login, self.portal_user.login)
|
||||
self.partner = self.portal_partner
|
||||
self.user = self.portal_user
|
||||
with patch(
|
||||
'odoo.addons.payment.models.payment_transaction.PaymentTransaction'
|
||||
'._send_payment_request'
|
||||
) as patched:
|
||||
self.portal_transaction(
|
||||
**self._prepare_transaction_values(self.create_token().id, 'token')
|
||||
)
|
||||
self.assertEqual(patched.call_count, 1)
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo.tests import tagged
|
||||
from unittest.mock import patch
|
||||
|
||||
from odoo.tests import tagged
|
||||
|
||||
from odoo.addons.payment.tests.common import PaymentCommon
|
||||
|
||||
|
||||
@@ -117,7 +118,7 @@ class TestPayments(PaymentCommon):
|
||||
msg="The refunds count should only consider transactions with operation 'refund'."
|
||||
)
|
||||
|
||||
def test_action_post_call_send_payment_request_only_once(self):
|
||||
def test_action_post_calls_send_payment_request_only_once(self):
|
||||
payment_token = self.create_token()
|
||||
payment_without_token = self.env['account.payment'].create({
|
||||
'payment_type': 'inbound',
|
||||
|
||||
Reference in New Issue
Block a user