Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Test clean_capture and clean_authorize

Pacu20895020be437
diff --git a/docs/guides/payments.rst b/docs/guides/payments.rst
index a362fc9167..c72f308ad1 100644
--- a/docs/guides/payments.rst
+++ b/docs/guides/payments.rst
@@ -18,8 +18,8 @@ Your changes should live under the
available from the API level. You will also need to integrate it into your
Frontend's workflow.
-get_client_token(**connection_params)
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+get_client_token(\*\*connection_params)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A client token is a signed data blob that includes configuration and
authorization information required by the payment gateway.
@@ -37,8 +37,8 @@ Example
client_token = gateway.client_token.generate()
return client_token
-authorize(payment, payment_token, **connection_params)
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+authorize(payment, payment_token, \*\*connection_params)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A process of reserving the amount of money against the customer's funding
source. Money does not change hands until the authorization is captured.
@@ -67,8 +67,8 @@ Example
return txn, response['error']
-refund(payment, amount, **connection_params)
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+refund(payment, amount, \*\*connection_params)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Full or partial return of captured funds to the customer.
@@ -100,8 +100,8 @@ Example
gateway_response=get_payment_gateway_response(response))
return txn, response['error']
-capture(payment, amount, **connection_params)
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+capture(payment, amount, \*\*connection_params)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A transfer of the money that was reserved during the authorization stage.
@@ -133,8 +133,8 @@ Example
gateway_response=get_payment_gateway_response(response))
return txn, response['error']
-void(payment, **connection_params)
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+void(payment, \*\*connection_params)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A cancellation of a pending authorization or capture.
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 1c46cc8c27..398d2c6931 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -94,7 +94,6 @@ def gateway_get_client_token(gateway_name: str):
def clean_capture(payment: Payment, amount: Decimal):
- # FIXME add tests
if amount <= 0:
raise PaymentError('Amount should be a positive number.')
if not payment.can_capture():
@@ -105,7 +104,6 @@ def clean_capture(payment: Payment, amount: Decimal):
def clean_authorize(payment: Payment):
- # FIXME add tests
if not payment.can_authorize():
raise PaymentError('Charged transactions cannot be authorized again.')
diff --git a/tests/test_payment.py b/tests/test_payment.py
index 91784ea8cc..f436d640f2 100644
--- a/tests/test_payment.py
+++ b/tests/test_payment.py
@@ -2,19 +2,20 @@ from decimal import Decimal
from unittest.mock import Mock, patch
import pytest
+
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.template.loader import get_template
from prices import Money
-
from saleor.order import OrderEvents, OrderEventsEmails
from saleor.order.views import PAYMENT_TEMPLATE
from saleor.payment import (
ChargeStatus, PaymentError, TransactionKind, get_payment_gateway)
from saleor.payment.utils import (
- create_payment, create_transaction, gateway_authorize, gateway_capture,
- gateway_get_client_token, gateway_refund, gateway_void, get_billing_data,
- handle_fully_paid_order, validate_payment)
+ clean_authorize, clean_capture, create_payment, create_transaction,
+ gateway_authorize, gateway_capture, gateway_get_client_token,
+ gateway_refund, gateway_void, get_billing_data, handle_fully_paid_order,
+ validate_payment)
NOT_ACTIVE_PAYMENT_ERROR = 'This payment is no longer active.'
EXAMPLE_ERROR = 'Example dummy error'
@@ -452,3 +453,42 @@ def test_payment_gateway_form_exists(gateway_name, payment_dummy):
payment_gateway, gateway_params = get_payment_gateway(
gateway_name)
payment_gateway.get_form_class()
+
+
+def test_clean_authorize():
+ payment = Mock(can_authorize=Mock(return_value=True))
+ clean_authorize(payment)
+
+ payment = Mock(can_authorize=Mock(return_value=False))
+ with pytest.raises(PaymentError):
+ clean_authorize(payment)
+
+
+def test_clean_capture():
+ # Amount should be a positive number
+ payment = Mock()
+ amount = Decimal('0.00')
+ with pytest.raises(PaymentError):
+ clean_capture(payment, amount)
+
+ # Payment cannot be captured
+ payment = Mock(can_capture=Mock(return_value=False))
+ amount = Decimal('1.00')
+ with pytest.raises(PaymentError):
+ clean_capture(payment, amount)
+
+ # Amount is larger than payment's total
+ payment = Mock(
+ can_capture=Mock(return_value=True),
+ total=Decimal('1.00'),
+ captured_amount=Decimal('0.00'))
+ amount = Decimal('2.00')
+ with pytest.raises(PaymentError):
+ clean_capture(payment, amount)
+
+ amount = Decimal('2.00')
+ payment = Mock(
+ can_capture=Mock(return_value=True),
+ total=amount,
+ captured_amount=Decimal('0.00'))
+ clean_capture(payment, amount)