| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from unittest.mock import patch |
| 4 |
from urllib.parse import quote as url_quote |
| 5 |
|
| 6 |
from odoo.tests import tagged |
| 7 |
from odoo.tools import mute_logger |
| 8 |
|
| 9 |
from odoo.addons.payment.tests.http_common import PaymentHttpCommon |
| 10 |
from odoo.addons.payment_mercado_pago.tests.common import MercadoPagoCommon |
| 11 |
|
| 12 |
|
| 13 |
@tagged('post_install', '-at_install') |
| 14 |
class TestPaymentTransaction(MercadoPagoCommon, PaymentHttpCommon): |
| 15 |
|
| 16 |
def test_no_item_missing_from_preference_request_payload(self): |
| 17 |
""" Test that the request values are conform to the transaction fields. """ |
| 18 |
tx = self._create_transaction(flow='redirect') |
| 19 |
request_payload = tx._mercado_pago_prepare_preference_request_payload() |
| 20 |
self.maxDiff = 10000 # Allow comparing large dicts. |
| 21 |
return_url = self._build_url('/payment/mercado_pago/return') |
| 22 |
webhook_url = self._build_url('/payment/mercado_pago/webhook') |
| 23 |
sanitized_reference = url_quote(tx.reference) |
| 24 |
self.assertDictEqual(request_payload, { |
| 25 |
'external_reference': tx.reference, |
| 26 |
'notification_url': f'{webhook_url}/{sanitized_reference}', |
| 27 |
'auto_return': 'all', |
| 28 |
'back_urls': { |
| 29 |
'failure': return_url, |
| 30 |
'pending': return_url, |
| 31 |
'success': return_url, |
| 32 |
}, |
| 33 |
'items': [{ |
| 34 |
'currency_id': tx.currency_id.name, |
| 35 |
'quantity': 1, |
| 36 |
'title': tx.reference, |
| 37 |
'unit_price': tx.amount, |
| 38 |
}], |
| 39 |
'payer': { |
| 40 |
'name': tx.partner_name, |
| 41 |
'email': tx.partner_email, |
| 42 |
'phone': {'number': tx.partner_phone}, |
| 43 |
'address': {'street_name': tx.partner_address, 'zip_code': tx.partner_zip}, |
| 44 |
}, |
| 45 |
}) |
| 46 |
|
| 47 |
@mute_logger('odoo.addons.payment.models.payment_transaction') |
| 48 |
def test_no_input_missing_from_redirect_form(self): |
| 49 |
""" Test that the `api_url` key is not omitted from the rendering values. """ |
| 50 |
tx = self._create_transaction(flow='redirect') |
| 51 |
with patch( |
| 52 |
'odoo.addons.payment_mercado_pago.models.payment_transaction.PaymentTransaction' |
| 53 |
'._get_specific_rendering_values', return_value={'api_url': 'https://dummy.com'} |
| 54 |
): |
| 55 |
processing_values = tx._get_processing_values() |
| 56 |
form_info = self._extract_values_from_html_form(processing_values['redirect_form_html']) |
| 57 |
self.assertEqual(form_info['action'], 'https://dummy.com') |
| 58 |
self.assertEqual(form_info['method'], 'get') |
| 59 |
self.assertDictEqual(form_info['inputs'], {}) |
| 60 |
|
| 61 |
def test_apply_updates_confirms_transaction(self): |
| 62 |
""" Test that the transaction state is set to 'done' when the payment data indicate a |
| 63 |
successful payment. """ |
| 64 |
tx = self._create_transaction(flow='redirect') |
| 65 |
tx._apply_updates(self.verification_data) |
| 66 |
self.assertEqual(tx.state, 'done') |
| 67 |
|
| 68 |
@mute_logger('odoo.addons.payment_mercado_pago.models.payment_transaction') |
| 69 |
def test_apply_updates_rejects_transaction(self): |
| 70 |
""" Test that the transaction state is set to 'error' when the payment data indicate a status of |
| 71 |
404 error payment. """ |
| 72 |
tx = self._create_transaction(flow='redirect') |
| 73 |
tx._apply_updates(self.verification_data_for_error_state) |
| 74 |
self.assertEqual(tx.state, 'error') |
| 75 |
|
| 76 |
def test_cop_currency_rounding(self): |
| 77 |
"""Ensure COP payments get sent as integer amounts to Mercado Pago.""" |
| 78 |
self.currency = self.env['res.currency'].with_context(active_test=False).search( |
| 79 |
[('name', '=', 'COP')], |
| 80 |
limit=1, |
| 81 |
) |
| 82 |
self.amount = 999.99 |
| 83 |
tx = self._create_transaction(flow='redirect') |
| 84 |
request_payload = tx._mercado_pago_prepare_preference_request_payload() |
| 85 |
self.assertEqual( |
| 86 |
request_payload['items'][0]['unit_price'], |
| 87 |
999, |
| 88 |
"COP payment amounts should be rounded down in the payload sent to Mercado Pago", |
| 89 |
) |