1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from unittest.mock import patch
from werkzeug.exceptions import Forbidden
from odoo.exceptions import UserError
from odoo.tests import tagged
from odoo.tools import mute_logger
from odoo.addons.payment import utils as payment_utils
from odoo.addons.payment.tests.http_common import PaymentHttpCommon
from odoo.addons.payment_adyen import utils as adyen_utils
from odoo.addons.payment_adyen.controllers.main import AdyenController
from odoo.addons.payment_adyen.tests.common import AdyenCommon
@tagged('post_install', '-at_install')
class AdyenTest(AdyenCommon, PaymentHttpCommon):
def test_processing_values(self):
tx = self._create_transaction(flow='direct')
with mute_logger('odoo.addons.payment.models.payment_transaction'), \
patch(
'odoo.addons.payment.utils.generate_access_token',
new=self._generate_test_access_token
):
processing_values = tx._get_processing_values()
converted_amount = 111111
self.assertEqual(
payment_utils.to_minor_currency_units(self.amount, self.currency),
converted_amount,
)
self.assertEqual(processing_values['converted_amount'], converted_amount)
with patch(
'odoo.addons.payment.utils.generate_access_token', new=self._generate_test_access_token
):
self.assertTrue(payment_utils.check_access_token(
processing_values['access_token'], self.reference, converted_amount, self.currency.id, self.partner.id
))
@mute_logger('odoo.addons.payment_adyen.models.payment_transaction')
def test_send_refund_request(self):
self.provider.support_refund = 'full_only' # Should simply not be False
tx = self._create_transaction(
'redirect', state='done', provider_reference='source_reference'
)
tx._post_process() # Create the payment
# Send the refund request
with patch(
'odoo.addons.payment_adyen.models.payment_provider.PaymentProvider._adyen_make_request',
new=lambda *args, **kwargs: {'pspReference': "refund_reference", 'status': "received"}
):
tx._send_refund_request()
refund_tx = self.env['payment.transaction'].search([('source_transaction_id', '=', tx.id)])
self.assertTrue(
refund_tx,
msg="Refunding an Adyen transaction should always create a refund transaction."
)
self.assertTrue(
refund_tx.state == 'draft',
msg="A refund request as been made, but the state of the refund tx stays as 'draft' "
"until a success notification is sent"
)
self.assertNotEqual(
refund_tx.provider_reference,
tx.provider_reference,
msg="The provider reference of the refund transaction should be different from that of "
"the source transaction."
)
def test_get_tx_from_notification_data_returns_refund_tx(self):
source_tx = self._create_transaction(
'direct', state='done', provider_reference=self.original_reference
)
refund_tx = self._create_transaction(
'direct',
reference='RefundTx',
provider_reference=self.psp_reference,
amount=-source_tx.amount,
operation='refund',
source_transaction_id=source_tx.id
)
data = dict(
self.webhook_notification_payload,
amount={
'currency': 'USD',
'value': payment_utils.to_minor_currency_units(
-source_tx.amount, refund_tx.currency_id
)
},
eventCode='REFUND',
)
returned_tx = self.env['payment.transaction']._get_tx_from_notification_data('adyen', data)
self.assertEqual(returned_tx, refund_tx, msg="The existing refund tx is the one returned")
def test_get_tx_from_notification_data_creates_refund_tx_when_missing(self):
source_tx = self._create_transaction(
'direct', state='done', provider_reference=self.original_reference
)
data = dict(
self.webhook_notification_payload,
amount={'currency': 'USD', 'value': payment_utils.to_minor_currency_units(
-self.amount, source_tx.currency_id
)},
eventCode='REFUND',
)
refund_tx = self.env['payment.transaction']._get_tx_from_notification_data('adyen', data)
self.assertTrue(
refund_tx,
msg="If no refund tx is found with received refund data, a refund tx should be created"
)
self.assertNotEqual(refund_tx, source_tx)
self.assertEqual(refund_tx.source_transaction_id, source_tx)
def test_get_tx_from_notification_data_returns_partial_capture_child_tx(self):
self.provider.capture_manually = True
source_tx = self._create_transaction(
'direct', state='authorized', provider_reference=self.original_reference
)
capture_tx = self._create_transaction(
'direct',
reference='CaptureTx',
provider_reference=self.psp_reference,
amount=source_tx.amount-10,
operation=source_tx.operation,
source_transaction_id=source_tx.id,
)
data = dict(
self.webhook_notification_payload,
amount={
'currency': 'USD',
'value': payment_utils.to_minor_currency_units(
source_tx.amount-10, capture_tx.currency_id
),
},
eventCode='CAPTURE',
)
returned_tx = self.env['payment.transaction']._get_tx_from_notification_data('adyen', data)
self.assertEqual(returned_tx, capture_tx, msg="The existing capture tx is the one returned")
def test_get_tx_from_notification_data_creates_capture_tx_when_missing(self):
self.provider.capture_manually = True
source_tx = self._create_transaction(
'direct', state='authorized', provider_reference=self.original_reference
)
data = dict(
self.webhook_notification_payload,
amount={'currency': 'USD', 'value': payment_utils.to_minor_currency_units(
self.amount - 10, source_tx.currency_id
)},
eventCode='CAPTURE',
)
capture_tx = self.env['payment.transaction']._get_tx_from_notification_data('adyen', data)
self.assertTrue(
capture_tx,
msg="If no child tx is found with received capture data, a child tx should be created.",
)
self.assertNotEqual(capture_tx, source_tx)
self.assertEqual(capture_tx.source_transaction_id, source_tx)
def test_get_tx_from_notification_data_returns_void_tx(self):
self.provider.capture_manually = True
source_tx = self._create_transaction(
'direct', state='authorized', provider_reference=self.original_reference
)
cancel_tx = self._create_transaction(
'direct',
reference='CancelTx',
provider_reference=self.psp_reference,
amount=source_tx.amount - 10,
operation=source_tx.operation,
source_transaction_id=source_tx.id,
)
data = dict(
self.webhook_notification_payload,
amount={
'currency': 'USD',
'value': payment_utils.to_minor_currency_units(
source_tx.amount - 10, cancel_tx.currency_id
),
},
eventCode='CANCELLATION',
)
returned_tx = self.env['payment.transaction']._get_tx_from_notification_data('adyen', data)
self.assertEqual(returned_tx, cancel_tx, msg="The existing void tx is the one returned")
def test_get_tx_from_notification_data_creates_void_tx_when_missing(self):
self.provider.capture_manually = True
source_tx = self._create_transaction(
'direct', state='authorized', provider_reference=self.original_reference
)
data = dict(
self.webhook_notification_payload,
amount={'currency': 'USD', 'value': payment_utils.to_minor_currency_units(
self.amount - 10, source_tx.currency_id
)},
eventCode='CANCELLATION',
)
void_tx = self.env['payment.transaction']._get_tx_from_notification_data('adyen', data)
self.assertTrue(
void_tx,
msg="If no child tx is found with received void data, a child tx should be created."
)
self.assertNotEqual(void_tx, source_tx)
self.assertEqual(void_tx.source_transaction_id, source_tx)
def test_send_full_capture_request_does_not_create_capture_tx(self):
self.provider.capture_manually = True
source_tx = self._create_transaction(flow='direct', state='authorized')
with patch(
'odoo.addons.payment_adyen.models.payment_provider.PaymentProvider._adyen_make_request',
return_value={'status': 'received', 'pspreference': 'dummy ref'},
):
child_tx = source_tx._send_capture_request()
self.assertFalse(
child_tx, msg="Full capture should not create a child transaction."
)
def test_send_partial_capture_request_creates_capture_tx(self):
self.provider.capture_manually = True
source_tx = self._create_transaction(flow='direct', state='authorized')
with patch(
'odoo.addons.payment_adyen.models.payment_provider.PaymentProvider._adyen_make_request',
return_value={'status': 'received', 'pspreference': 'dummy ref'},
):
child_tx = source_tx._send_capture_request(amount_to_capture=10)
self.assertTrue(
source_tx.child_transaction_ids,
msg="Partial capture should create a child transaction and linked it to the source tx.",
)
self.assertEqual(
child_tx.amount, 10, msg="Child transaction should have the requested amount."
)
self.assertEqual(
child_tx.state, 'draft', msg="Child transaction are created in draft until confirmed."
)
@mute_logger('odoo.addons.payment_adyen.models.payment_transaction')
def test_tx_state_after_send_full_capture_request(self):
self.provider.capture_manually = True
tx = self._create_transaction('direct', state='authorized')
with patch(
'odoo.addons.payment_adyen.models.payment_provider.PaymentProvider._adyen_make_request',
return_value={'status': 'received'},
):
tx._send_capture_request()
self.assertEqual(
tx.state,
'authorized',
msg="A capture request as been made, but the state of the transaction stays as "
"'authorized' until a success notification is sent",
)
@mute_logger('odoo.addons.payment_adyen.models.payment_transaction')
def test_tx_state_after_send_partial_capture_request(self):
self.provider.capture_manually = True
tx = self._create_transaction('direct', state='authorized')
with patch(
'odoo.addons.payment_adyen.models.payment_provider.PaymentProvider._adyen_make_request',
return_value={'status': 'received'},
):
tx._send_capture_request(amount_to_capture=10)
self.assertEqual(
tx.state,
'authorized',
msg="A partial capture request as been made, but the state of the source transaction "
"stays as 'authorized' until the full amount is either done or canceled.",
)
self.assertEqual(
tx.child_transaction_ids[0].state,
'draft',
msg="A partial capture request as been made, but the state of the child transaction "
"stays as 'draft' until a success notification is sent.",
)
def test_send_full_void_request_does_not_create_void_tx(self):
self.provider.capture_manually = True
source_tx = self._create_transaction(flow='direct', state='authorized')
with patch(
'odoo.addons.payment_adyen.models.payment_provider.PaymentProvider._adyen_make_request',
return_value={'status': 'received', 'pspreference': 'dummy ref'},
):
child_tx = source_tx._send_void_request()
self.assertFalse(
child_tx, msg="Full void should not create a child transaction."
)
def test_send_partial_void_request_creates_void_tx(self):
self.provider.capture_manually = True
source_tx = self._create_transaction(flow='direct', state='authorized')
with patch(
'odoo.addons.payment_adyen.models.payment_provider.PaymentProvider._adyen_make_request',
return_value={'status': 'received', 'pspreference': 'dummy ref'},
):
child_tx = source_tx._send_void_request(amount_to_void=10)
self.assertTrue(
source_tx.child_transaction_ids,
msg="Partial void should create a child transaction and linked it to the source tx.",
)
self.assertEqual(
child_tx.amount, 10, msg="Child transaction should have the requested amount."
)
self.assertEqual(
child_tx.state, 'draft', msg="Child transaction are created in draft until confirmed."
)
@mute_logger('odoo.addons.payment_adyen.models.payment_transaction')
def test_tx_state_after_send_void_request(self):
self.provider.capture_manually = True
tx = self._create_transaction('direct', state='authorized')
with patch(
'odoo.addons.payment_adyen.models.payment_provider.PaymentProvider._adyen_make_request',
return_value={'status': 'received'},
):
tx._send_void_request()
self.assertEqual(
tx.state,
'authorized',
msg="A void request as been made, but the state of the transaction stays as"
" 'authorized' until a success notification is sent",
)
def test_webhook_notification_confirms_transaction(self):
tx = self._create_transaction('direct')
self._webhook_notification_flow(self.webhook_notification_batch_data)
self.assertEqual(tx.state, 'done')
def test_webhook_notification_authorizes_transaction(self):
self.provider.capture_manually = True
tx = self._create_transaction('direct')
self._webhook_notification_flow(self.webhook_notification_batch_data)
self.assertEqual(
tx.state,
'authorized',
msg="The authorization succeeded, the manual capture is enabled, the tx state should be"
" 'authorized'.",
)
def test_webhook_notification_captures_transaction(self):
self.provider.capture_manually = True
tx = self._create_transaction(
'direct', state='authorized', provider_reference=self.original_reference, amount=9.99
)
payload = dict(self.webhook_notification_batch_data, notificationItems=[{
'NotificationRequestItem': dict(self.webhook_notification_payload, eventCode='CAPTURE')
}])
self._webhook_notification_flow(payload)
self.assertEqual(
tx.state, 'done',
msg="The capture succeeded, the tx state should be 'done'.",
)
def test_webhook_notification_cancels_transaction(self):
tx = self._create_transaction(
'direct', state='pending', provider_reference=self.original_reference, amount=9.99
)
payload = dict(self.webhook_notification_batch_data, notificationItems=[{
'NotificationRequestItem': dict(
self.webhook_notification_payload, eventCode='CANCELLATION'
)
}])
self._webhook_notification_flow(payload)
self.assertEqual(
tx.state,
'cancel',
msg="The cancellation succeeded, the tx state should be 'cancel'.",
)
def test_webhook_notification_refunds_transaction(self):
source_tx = self._create_transaction(
'direct', state='done', provider_reference=self.original_reference
)
payload = dict(self.webhook_notification_batch_data, notificationItems=[{
'NotificationRequestItem': dict(
self.webhook_notification_payload,
amount={'currency': 'USD', 'value': payment_utils.to_minor_currency_units(
-self.amount, source_tx.currency_id
)},
eventCode='REFUND',
)
}])
self._webhook_notification_flow(payload)
refund_tx = self.env['payment.transaction'].search(
[('source_transaction_id', '=', source_tx.id)]
)
self.assertEqual(
refund_tx.state,
'done',
msg="After a successful refund notification, the refund state should be in 'done'.",
)
def test_failed_webhook_authorization_notification_leaves_transaction_in_draft(self):
tx = self._create_transaction('direct')
payload = dict(self.webhook_notification_batch_data, notificationItems=[
{'NotificationRequestItem': dict(self.webhook_notification_payload, success='false')}
])
self._webhook_notification_flow(payload)
self.assertEqual(
tx.state, 'draft',
msg="The authorization failed, as we don't support failed authorization, the tx state "
"should still be 'draft'.",
)
def test_failed_webhook_capture_notification_leaves_transaction_authorized(self):
tx = self._create_transaction(
'direct', state='authorized', provider_reference=self.original_reference
)
payload = dict(self.webhook_notification_batch_data, notificationItems=[{
'NotificationRequestItem': dict(
self.webhook_notification_payload, eventCode='CAPTURE', success='false'
)
}])
self._webhook_notification_flow(payload)
self.assertEqual(
tx.state, 'authorized',
msg="The capture failed, the tx state should still be 'authorized'.",
)
def test_failed_webhook_cancellation_notification_leaves_transaction_authorized(self):
tx = self._create_transaction('direct', state='authorized')
payload = dict(self.webhook_notification_batch_data, notificationItems=[{
'NotificationRequestItem': dict(
self.webhook_notification_payload, eventCode='CANCELLATION', success='false'
)
}])
self._webhook_notification_flow(payload)
self.assertEqual(
tx.state, 'authorized',
msg="The cancellation failed, the tx state should still be 'authorized'.",
)
def test_failed_webhook_refund_notification_sets_refund_transaction_in_error(self):
source_tx = self._create_transaction(
'direct', state='done', provider_reference=self.original_reference
)
payload = dict(self.webhook_notification_batch_data, notificationItems=[{
'NotificationRequestItem': dict(
self.webhook_notification_payload,
amount={'currency': 'USD', 'value': payment_utils.to_minor_currency_units(
-self.amount, source_tx.currency_id
)},
eventCode='REFUND',
success='false',
)
}])
self._webhook_notification_flow(payload)
refund_tx = self.env['payment.transaction'].search([
('source_transaction_id', '=', source_tx.id)]
)
self.assertEqual(
refund_tx.state,
'error',
msg="After a failed refund notification, the refund state should be in 'error'.",
)
@mute_logger('odoo.addons.payment_adyen.controllers.main')
@mute_logger('odoo.addons.payment_adyen.models.payment_transaction')
def _webhook_notification_flow(self, payload):
""" Send a notification to the webhook, ignore the signature, and check the response. """
url = self._build_url(AdyenController._webhook_url)
with patch(
'odoo.addons.payment_adyen.controllers.main.AdyenController'
'._verify_notification_signature'
):
response = self._make_json_request(url, data=payload).json()
self.assertEqual(
response, '[accepted]', msg="The webhook should always respond '[accepted]'",
)
@mute_logger('odoo.addons.payment_adyen.controllers.main')
def test_webhook_notification_triggers_signature_check(self):
""" Test that receiving a webhook notification triggers a signature check. """
self._create_transaction('direct')
url = self._build_url(AdyenController._webhook_url)
with patch(
'odoo.addons.payment_adyen.controllers.main.AdyenController'
'._verify_notification_signature'
) as signature_check_mock, patch(
'odoo.addons.payment.models.payment_transaction.PaymentTransaction'
'._handle_notification_data'
):
self._make_json_request(url, data=self.webhook_notification_batch_data)
self.assertEqual(signature_check_mock.call_count, 1)
def test_accept_webhook_notification_with_valid_signature(self):
""" Test the verification of a webhook notification with a valid signature. """
tx = self._create_transaction('direct')
self._assert_does_not_raise(
Forbidden,
AdyenController._verify_notification_signature,
self.webhook_notification_payload,
tx,
)
@mute_logger('odoo.addons.payment_adyen.controllers.main')
def test_reject_webhook_notification_with_missing_signature(self):
""" Test the verification of a webhook notification with a missing signature. """
payload = dict(self.webhook_notification_payload, additionalData={'hmacSignature': None})
tx = self._create_transaction('direct')
self.assertRaises(Forbidden, AdyenController._verify_notification_signature, payload, tx)
@mute_logger('odoo.addons.payment_adyen.controllers.main')
def test_reject_webhook_notification_with_invalid_signature(self):
""" Test the verification of a webhook notification with an invalid signature. """
payload = dict(self.webhook_notification_payload, additionalData={'hmacSignature': 'dummy'})
tx = self._create_transaction('direct')
self.assertRaises(Forbidden, AdyenController._verify_notification_signature, payload, tx)
@mute_logger('odoo.addons.payment_adyen.models.payment_transaction')
def test_no_information_missing_from_partner_address(self):
test_partner = self.env['res.partner'].create({
'name': 'Dummy Partner',
'email': 'norbert.buyer@example.com',
'phone': '0032 12 34 56 78',
})
test_address = adyen_utils.format_partner_address(test_partner)
for key in ('city', 'country', 'stateOrProvince', 'street',):
self.assertTrue(test_address.get(key))
|