Skip to content

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

Added fixtures for order payments, refactored order form tests to use them

Rafał Pitoń83d0be43424b
diff --git a/tests/conftest.py b/tests/conftest.py
index 1481fba3f4..a1a798b2d3 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -9,6 +9,7 @@ from django.core.files import File
from django.core.files.uploadedfile import SimpleUploadedFile
from django.utils.encoding import smart_text
from PIL import Image
+from payments import FraudStatus, PaymentStatus
from prices import Money
from saleor.account.models import Address, User
@@ -500,6 +501,63 @@ def order_with_variant_from_different_stocks(order_with_lines_and_stock):
return order_with_lines_and_stock
+def payment_waiting(order_with_lines_and_stock):
+ return order_with_lines_and_stock.payments.create(
+ variant='default', status=PaymentStatus.WAITING,
+ fraud_status=FraudStatus.ACCEPT, currency='USD',
+ total=order_with_lines_and_stock.total_gross.amount)
+
+
+def payment_preauth(order_with_lines_and_stock):
+ return order_with_lines_and_stock.payments.create(
+ variant='default', status=PaymentStatus.PREAUTH,
+ fraud_status=FraudStatus.ACCEPT, currency='USD',
+ total=order_with_lines_and_stock.total_gross.amount)
+
+
+def payment_confirmed(order_with_lines_and_stock):
+ order_amount = order_with_lines_and_stock.total_gross.amount
+ return order_with_lines_and_stock.payments.create(
+ variant='default', status=PaymentStatus.CONFIRMED,
+ fraud_status=FraudStatus.ACCEPT, currency='USD',
+ total=order_amount, captured_amount=order_amount)
+
+
+def payment_rejected(order_with_lines_and_stock):
+ return order_with_lines_and_stock.payments.create(
+ variant='default', status=PaymentStatus.REJECTED,
+ fraud_status=FraudStatus.ACCEPT, currency='USD',
+ total=order_with_lines_and_stock.total_gross.amount)
+
+
+def payment_refunded(order_with_lines_and_stock):
+ return order_with_lines_and_stock.payments.create(
+ variant='default', status=PaymentStatus.REFUNDED,
+ fraud_status=FraudStatus.ACCEPT, currency='USD',
+ total=order_with_lines_and_stock.total_gross.amount)
+
+
+def payment_error(order_with_lines_and_stock):
+ return order_with_lines_and_stock.payments.create(
+ variant='default', status=PaymentStatus.ERROR,
+ fraud_status=FraudStatus.ACCEPT, currency='USD',
+ total=order_with_lines_and_stock.total_gross.amount)
+
+
+def payment_input(order_with_lines_and_stock):
+ return order_with_lines_and_stock.payments.create(
+ variant='default', status=PaymentStatus.INPUT,
+ fraud_status=FraudStatus.ACCEPT, currency='USD',
+ total=order_with_lines_and_stock.total_gross.amount)
+
+
@pytest.fixture()
def sale(db, default_category):
sale = Sale.objects.create(name="Sale", value=5)
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index 1ac5d8cad4..a8fa771a72 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -3,7 +3,7 @@ from unittest.mock import patch
import pytest
from django.urls import reverse
-from payments import FraudStatus, PaymentStatus
+from payments import PaymentStatus
from prices import Money, TaxedMoney
from tests.utils import get_redirect_location, get_url_path
@@ -18,111 +18,265 @@ from saleor.product.models import ProductVariant, Stock, StockLocation
@pytest.mark.integration
@pytest.mark.django_db
-def test_view_capture_order_payment(admin_client, order_with_lines_and_stock):
+def test_view_capture_order_payment_preauth(
+ admin_client, order_with_lines_and_stock, payment_preauth):
order = order_with_lines_and_stock
- payment = order.payments.create(
- variant='default', status=PaymentStatus.PREAUTH,
- fraud_status=FraudStatus.ACCEPT, currency='USD', total='100.0')
+ url = reverse(
+ 'dashboard:capture-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_preauth.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(
+ url, {
+ 'csrfmiddlewaretoken': 'hello',
+ 'amount': str(order.total.gross.amount)})
+ assert response.status_code == 302
+ assert order.payments.last().status == PaymentStatus.CONFIRMED
+ assert order.payments.last().get_captured_price() == order.total
+
+def test_view_capture_order_invalid_payment_waiting_status(
+ admin_client, order_with_lines_and_stock, payment_waiting):
+ order = order_with_lines_and_stock
url = reverse(
'dashboard:capture-payment', kwargs={
- 'order_pk': order.pk, 'payment_pk': payment.pk})
+ 'order_pk': order.pk, 'payment_pk': payment_waiting.pk})
response = admin_client.get(url)
assert response.status_code == 200
response = admin_client.post(
url, {'csrfmiddlewaretoken': 'hello', 'amount': '20.00'})
- assert response.status_code == 302
+ assert response.status_code == 400
+ assert order.payments.last().status == PaymentStatus.WAITING
+
+
+def test_view_capture_order_invalid_payment_confirmed_status(
+ admin_client, order_with_lines_and_stock, payment_confirmed):
+ order = order_with_lines_and_stock
+ url = reverse(
+ 'dashboard:capture-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_confirmed.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(
+ url, {'csrfmiddlewaretoken': 'hello', 'amount': '20.00'})
+ assert response.status_code == 400
assert order.payments.last().status == PaymentStatus.CONFIRMED
- assert order.payments.last().get_captured_price() == TaxedMoney(
- net=Money(20, 'USD'), gross=Money(20, 'USD'))
@pytest.mark.integration
@pytest.mark.django_db
- 'invalid_status', [
- PaymentStatus.WAITING, PaymentStatus.CONFIRMED, PaymentStatus.REJECTED,
- PaymentStatus.REFUNDED, PaymentStatus.ERROR, PaymentStatus.INPUT])
-def test_view_capture_order_payment_invalid_status(
- admin_client, order_with_lines_and_stock, invalid_status):
+def test_view_capture_order_invalid_payment_rejected_status(
+ admin_client, order_with_lines_and_stock, payment_rejected):
order = order_with_lines_and_stock
- payment = order.payments.create(
- variant='default', status=invalid_status,
- fraud_status=FraudStatus.ACCEPT, currency='USD', total='100.0')
+ url = reverse(
+ 'dashboard:capture-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_rejected.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(
+ url, {'csrfmiddlewaretoken': 'hello', 'amount': '20.00'})
+ assert response.status_code == 400
+ assert order.payments.last().status == PaymentStatus.REJECTED
+
+def test_view_capture_order_invalid_payment_refunded_status(
+ admin_client, order_with_lines_and_stock, payment_refunded):
+ order = order_with_lines_and_stock
url = reverse(
'dashboard:capture-payment', kwargs={
- 'order_pk': order.pk, 'payment_pk': payment.pk})
+ 'order_pk': order.pk, 'payment_pk': payment_refunded.pk})
response = admin_client.get(url)
assert response.status_code == 200
response = admin_client.post(
url, {'csrfmiddlewaretoken': 'hello', 'amount': '20.00'})
assert response.status_code == 400
- assert order.payments.last().status == invalid_status
+ assert order.payments.last().status == PaymentStatus.REFUNDED
@pytest.mark.integration
@pytest.mark.django_db
-def test_view_refund_order_payment(admin_client, order_with_lines_and_stock):
+def test_view_capture_order_invalid_payment_error_status(
+ admin_client, order_with_lines_and_stock, payment_error):
order = order_with_lines_and_stock
- payment = order.payments.create(
- variant='default', status=PaymentStatus.CONFIRMED,
- fraud_status=FraudStatus.ACCEPT, currency='USD', total='100.0',
- captured_amount='100.0')
+ url = reverse(
+ 'dashboard:capture-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_error.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+ response = admin_client.post(
+ url, {'csrfmiddlewaretoken': 'hello', 'amount': '20.00'})
+ assert response.status_code == 400
+ assert order.payments.last().status == PaymentStatus.ERROR
+
+
+def test_view_capture_order_invalid_payment_input_status(
+ admin_client, order_with_lines_and_stock, payment_input):
+ order = order_with_lines_and_stock
url = reverse(
- 'dashboard:refund-payment', kwargs={
- 'order_pk': order.pk, 'payment_pk': payment.pk})
+ 'dashboard:capture-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_input.pk})
response = admin_client.get(url)
assert response.status_code == 200
response = admin_client.post(
url, {'csrfmiddlewaretoken': 'hello', 'amount': '20.00'})
+ assert response.status_code == 400
+ assert order.payments.last().status == PaymentStatus.INPUT
+
+
+def test_view_refund_order_payment_confirmed(
+ admin_client, order_with_lines_and_stock, payment_confirmed):
+ order = order_with_lines_and_stock
+
+ url = reverse(
+ 'dashboard:refund-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_confirmed.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(
+ url, {
+ 'csrfmiddlewaretoken': 'hello',
+ 'amount': str(payment_confirmed.captured_amount)})
assert response.status_code == 302
- assert order.payments.last().status == PaymentStatus.CONFIRMED
+ assert order.payments.last().status == PaymentStatus.REFUNDED
assert order.payments.last().get_captured_price() == TaxedMoney(
- net=Money(80, 'USD'), gross=Money(80, 'USD'))
+ net=Money(0, 'USD'), gross=Money(0, 'USD'))
+
+
+def test_view_refund_order_invalid_payment_waiting_status(
+ admin_client, order_with_lines_and_stock, payment_waiting):
+ order = order_with_lines_and_stock
+
+ url = reverse(
+ 'dashboard:refund-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_waiting.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(
+ url, {'csrfmiddlewaretoken': 'hello', 'amount': '20.00'})
+ assert response.status_code == 400
+ assert order.payments.last().status == PaymentStatus.WAITING
+
+
+def test_view_refund_order_invalid_payment_preauth_status(
+ admin_client, order_with_lines_and_stock, payment_preauth):
+ order = order_with_lines_and_stock
+
+ url = reverse(
+ 'dashboard:refund-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_preauth.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(
+ url, {'csrfmiddlewaretoken': 'hello', 'amount': '20.00'})
+ assert response.status_code == 400
+ assert order.payments.last().status == PaymentStatus.PREAUTH
+
+
+def test_view_refund_order_invalid_payment_rejected_status(
+ admin_client, order_with_lines_and_stock, payment_rejected):
+ order = order_with_lines_and_stock
+
+ url = reverse(
+ 'dashboard:refund-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_rejected.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(
+ url, {'csrfmiddlewaretoken': 'hello', 'amount': '20.00'})
+ assert response.status_code == 400
+ assert order.payments.last().status == PaymentStatus.REJECTED
+
+
+def test_view_refund_order_invalid_payment_refunded_status(
+ admin_client, order_with_lines_and_stock, payment_refunded):
+ order = order_with_lines_and_stock
+
+ url = reverse(
+ 'dashboard:refund-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_refunded.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(
+ url, {'csrfmiddlewaretoken': 'hello', 'amount': '20.00'})
+ assert response.status_code == 400
+ assert order.payments.last().status == PaymentStatus.REFUNDED
+
+
+def test_view_refund_order_invalid_payment_error_status(
+ admin_client, order_with_lines_and_stock, payment_error):
+ order = order_with_lines_and_stock
+
+ url = reverse(
+ 'dashboard:refund-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_error.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(
+ url, {'csrfmiddlewaretoken': 'hello', 'amount': '20.00'})
+ assert response.status_code == 400
+ assert order.payments.last().status == PaymentStatus.ERROR
@pytest.mark.integration
@pytest.mark.django_db
- 'invalid_status', [
- PaymentStatus.WAITING, PaymentStatus.PREAUTH, PaymentStatus.REJECTED,
- PaymentStatus.REFUNDED, PaymentStatus.ERROR, PaymentStatus.INPUT])
-def test_view_refund_order_payment_invalid_status(
- admin_client, order_with_lines_and_stock, invalid_status):
+def test_view_refund_order_invalid_payment_input_status(
+ admin_client, order_with_lines_and_stock, payment_input):
order = order_with_lines_and_stock
- payment = order.payments.create(
- variant='default', status=invalid_status,
- fraud_status=FraudStatus.ACCEPT, currency='USD', total='100.0',
- captured_amount='100.0')
url = reverse(
'dashboard:refund-payment', kwargs={
- 'order_pk': order.pk, 'payment_pk': payment.pk})
+ 'order_pk': order.pk, 'payment_pk': payment_input.pk})
response = admin_client.get(url)
assert response.status_code == 200
response = admin_client.post(
url, {'csrfmiddlewaretoken': 'hello', 'amount': '20.00'})
assert response.status_code == 400
- assert order.payments.last().status == invalid_status
+ assert order.payments.last().status == PaymentStatus.INPUT
@pytest.mark.integration
@pytest.mark.django_db
-def test_view_release_order_payment(admin_client, order_with_lines_and_stock):
+def test_view_release_order_payment_preauth(
+ admin_client, order_with_lines_and_stock, payment_preauth):
order = order_with_lines_and_stock
- payment = order.payments.create(
- variant='default', status=PaymentStatus.PREAUTH,
- fraud_status=FraudStatus.ACCEPT, currency='USD', total='100.0')
url = reverse(
'dashboard:release-payment', kwargs={
- 'order_pk': order.pk, 'payment_pk': payment.pk})
+ 'order_pk': order.pk, 'payment_pk': payment_preauth.pk})
response = admin_client.get(url)
assert response.status_code == 200
@@ -136,27 +290,110 @@ def test_view_release_order_payment(admin_client, order_with_lines_and_stock):
@pytest.mark.integration
@pytest.mark.django_db
- 'invalid_status', [
- PaymentStatus.WAITING, PaymentStatus.CONFIRMED, PaymentStatus.REJECTED,
- PaymentStatus.REFUNDED, PaymentStatus.ERROR, PaymentStatus.INPUT])
-def test_view_release_order_payment_invalid_status(
- admin_client, order_with_lines_and_stock, invalid_status):
+def test_view_release_order_invalid_payment_waiting_status(
+ admin_client, order_with_lines_and_stock, payment_waiting):
+ order = order_with_lines_and_stock
+
+ url = reverse(
+ 'dashboard:release-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_waiting.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(url, {
+ 'csrfmiddlewaretoken': 'hello'})
+ assert response.status_code == 400
+ assert order.payments.last().status == PaymentStatus.WAITING
+
+
+def test_view_release_order_invalid_payment_confirmed_status(
+ admin_client, order_with_lines_and_stock, payment_confirmed):
+ order = order_with_lines_and_stock
+
+ url = reverse(
+ 'dashboard:release-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_confirmed.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(url, {
+ 'csrfmiddlewaretoken': 'hello'})
+ assert response.status_code == 400
+ assert order.payments.last().status == PaymentStatus.CONFIRMED
+
+
+def test_view_release_order_invalid_payment_rejected_status(
+ admin_client, order_with_lines_and_stock, payment_rejected):
+ order = order_with_lines_and_stock
+
+ url = reverse(
+ 'dashboard:release-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_rejected.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(url, {
+ 'csrfmiddlewaretoken': 'hello'})
+ assert response.status_code == 400
+ assert order.payments.last().status == PaymentStatus.REJECTED
+
+
+def test_view_release_order_invalid_payment_refunded_status(
+ admin_client, order_with_lines_and_stock, payment_refunded):
+ order = order_with_lines_and_stock
+
+ url = reverse(
+ 'dashboard:release-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_refunded.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(url, {
+ 'csrfmiddlewaretoken': 'hello'})
+ assert response.status_code == 400
+ assert order.payments.last().status == PaymentStatus.REFUNDED
+
+
+def test_view_release_order_invalid_payment_error_status(
+ admin_client, order_with_lines_and_stock, payment_error):
+ order = order_with_lines_and_stock
+
+ url = reverse(
+ 'dashboard:release-payment', kwargs={
+ 'order_pk': order.pk, 'payment_pk': payment_error.pk})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(url, {
+ 'csrfmiddlewaretoken': 'hello'})
+ assert response.status_code == 400
+ assert order.payments.last().status == PaymentStatus.ERROR
+
+
+def test_view_release_order_invalid_payment_input_status(
+ admin_client, order_with_lines_and_stock, payment_input):
order = order_with_lines_and_stock
- payment = order.payments.create(
- variant='default', status=invalid_status,
- fraud_status=FraudStatus.ACCEPT, currency='USD', total='100.0')
url = reverse(
'dashboard:release-payment', kwargs={
- 'order_pk': order.pk, 'payment_pk': payment.pk})
+ 'order_pk': order.pk, 'payment_pk': payment_input.pk})
response = admin_client.get(url)
assert response.status_code == 200
response = admin_client.post(url, {
'csrfmiddlewaretoken': 'hello'})
assert response.status_code == 400
- assert order.payments.last().status == invalid_status
+ assert order.payments.last().status == PaymentStatus.INPUT
@pytest.mark.integration