Source code for example_checkout.tests.test_view_checkout_mixin

# -*- encoding: utf-8 -*-
import pytest

from collections import namedtuple
from decimal import Decimal
from django.urls import reverse
from unittest import mock

from checkout.models import CheckoutError, Checkout, CheckoutAction
from checkout.tests.factories import CheckoutSettingsFactory
from checkout.views import CONTENT_OBJECT_PK
from example_checkout.tests.factories import SalesLedgerFactory
from mail.models import Message
from mail.tests.factories import NotifyFactory
from stock.tests.factories import ProductFactory


def _set_session(client, pk):
    session = client.session
    session[CONTENT_OBJECT_PK] = pk
    session.save()


[docs]@pytest.mark.django_db def test_get(client): CheckoutSettingsFactory() obj = SalesLedgerFactory() _set_session(client, obj.pk) url = reverse("example.sales.ledger.checkout", args=[obj.pk]) response = client.get(url) assert 200 == response.status_code
[docs]@pytest.mark.django_db def test_post_card_payment(client): with mock.patch("stripe.Charge.create"), mock.patch( "stripe.Customer.create" ) as mock_customer_create: # mock return data = {"id": "xyz"} return_value = namedtuple("ReturnValue", data.keys())(**data) mock_customer_create.return_value = return_value # factories NotifyFactory() obj = SalesLedgerFactory() _set_session(client, obj.pk) url = reverse("example.sales.ledger.checkout", args=[obj.pk]) data = {"action": CheckoutAction.PAYMENT, "token": "my-testing-token"} response = client.post(url, data) assert 302 == response.status_code assert 1 == Checkout.objects.count() checkout = Checkout.objects.first() assert CheckoutAction.PAYMENT == checkout.action.slug assert Decimal("0") < checkout.total # check email notification assert 1 == Message.objects.count() message = Message.objects.first() assert "SUCCESS - Payment" in message.subject assert "SUCCESS - Payment from" in message.description
[docs]@pytest.mark.django_db def test_post_card_payment_can_charge(client): """Check if the object can be charged when we post to the form.""" with mock.patch("stripe.Charge.create"), mock.patch( "stripe.Customer.create" ) as mock_customer_create, mock.patch( "example_checkout.models.SalesLedger.checkout_can_charge", new_callable=mock.PropertyMock, ) as mock_checkout_can_charge: # mock return data = {"id": "xyz"} return_value = namedtuple("ReturnValue", data.keys())(**data) mock_customer_create.return_value = return_value mock_checkout_can_charge.return_value = False # factories NotifyFactory() obj = SalesLedgerFactory() _set_session(client, obj.pk) url = reverse("example.sales.ledger.checkout", args=[obj.pk]) data = {"action": CheckoutAction.PAYMENT, "token": "my-testing-token"} with pytest.raises(CheckoutError) as e: client.post(url, data) assert "Cannot charge 'SalesLedger'" in str(e.value)
[docs]@pytest.mark.django_db def test_post_card_payment_fail(client): with mock.patch("stripe.Charge.create") as mock_charge, mock.patch( "stripe.Customer.create" ) as mock_customer_create: # mock return mock_charge.side_effect = CheckoutError("Mock") data = {"id": "xyz"} return_value = namedtuple("ReturnValue", data.keys())(**data) mock_customer_create.return_value = return_value # factories NotifyFactory() obj = SalesLedgerFactory() _set_session(client, obj.pk) url = reverse("example.sales.ledger.checkout", args=[obj.pk]) data = {"action": CheckoutAction.PAYMENT, "token": "my-testing-token"} response = client.post(url, data) assert 302 == response.status_code assert 1 == Checkout.objects.count() checkout = Checkout.objects.first() assert CheckoutAction.PAYMENT == checkout.action.slug assert Decimal("0") < checkout.total # check email notification assert 1 == Message.objects.count() message = Message.objects.first() assert "FAIL - Payment" in message.subject assert "FAIL - Payment from" in message.description
[docs]@pytest.mark.django_db def test_post_card_payment_plan(client): with mock.patch("stripe.Customer.create") as mock_customer_create: # mock return data = {"id": "xyz"} return_value = namedtuple("ReturnValue", data.keys())(**data) mock_customer_create.return_value = return_value # factories CheckoutSettingsFactory() NotifyFactory() product = ProductFactory(price=Decimal("12.34")) obj = SalesLedgerFactory(product=product) _set_session(client, obj.pk) url = reverse("example.sales.ledger.checkout", args=[obj.pk]) data = { "action": CheckoutAction.PAYMENT_PLAN, "token": "my-testing-token", } response = client.post(url, data) assert 302 == response.status_code assert 1 == Checkout.objects.count() checkout = Checkout.objects.first() assert CheckoutAction.PAYMENT_PLAN == checkout.action.slug assert Decimal("12.34") == checkout.total # check email notification assert 1 == Message.objects.count() message = Message.objects.first() assert "SUCCESS - Payment plan" in message.subject assert "SUCCESS - Payment Plan from" in message.description
[docs]@pytest.mark.django_db def test_post_card_refresh(client): with mock.patch("stripe.Customer.create") as mock_customer_create: # mock return data = {"id": "xyz"} return_value = namedtuple("ReturnValue", data.keys())(**data) mock_customer_create.return_value = return_value # factories NotifyFactory() obj = SalesLedgerFactory() _set_session(client, obj.pk) url = reverse("example.sales.ledger.checkout", args=[obj.pk]) data = { "action": CheckoutAction.CARD_REFRESH, "token": "my-testing-token", } response = client.post(url, data) assert 302 == response.status_code assert 1 == Checkout.objects.count() checkout = Checkout.objects.first() assert CheckoutAction.CARD_REFRESH == checkout.action.slug assert checkout.total is None # check email notification assert 1 == Message.objects.count() message = Message.objects.first() assert "SUCCESS - Card refresh" in message.subject assert "SUCCESS - Card Refresh from" in message.description
[docs]@pytest.mark.django_db def test_post_invoice(client): NotifyFactory() obj = SalesLedgerFactory() _set_session(client, obj.pk) url = reverse("example.sales.ledger.checkout", args=[obj.pk]) data = { "action": CheckoutAction.INVOICE, "company_name": "KB", "address_1": "My Address", "town": "Hatherleigh", "county": "Devon", "postcode": "EX20", "country": "UK", "contact_name": "Patrick", "email": "test@test.com", } response = client.post(url, data) assert 302 == response.status_code assert 1 == Checkout.objects.count() checkout = Checkout.objects.first() assert CheckoutAction.INVOICE == checkout.action.slug invoice = checkout.checkoutadditional assert "test@test.com" == invoice.email assert Decimal("0") < checkout.total # check email notification assert 1 == Message.objects.count() message = Message.objects.first() assert "SUCCESS - Invoice" in message.subject assert "SUCCESS - Invoice" in message.description