Source code for example_checkout.tests.test_view_customer

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

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

from base.url_utils import url_with_querystring
from checkout.models import (
    Checkout,
    CheckoutAction,
    CheckoutError,
    CheckoutState,
    Customer,
    CustomerPayment,
)
from checkout.tests.factories import CustomerFactory
from login.tests.fixture import TEST_PASSWORD, UserFactory
from mail.models import Message
from mail.tests.factories import MailTemplateFactory


[docs]@pytest.mark.django_db def test_customer_charge(client, mocker): mocker.patch("stripe.Charge.create") obj = CustomerFactory() url = reverse("checkout.customer.charge", args=[obj.pk]) user = UserFactory(is_staff=True) assert client.login(username=user.username, password=TEST_PASSWORD) is True data = {"description": "Bread", "total": Decimal("678")} response = client.post(url, data) assert 302 == response.status_code assert "/checkout/customer/" in response.url assert Message.objects.first() is None payment = CustomerPayment.objects.get(description="Bread") checkout = Checkout.objects.audit_content_object(payment).first() assert checkout is not None assert Decimal("678") == checkout.total assert CheckoutAction.CHARGE == checkout.action.slug assert CheckoutState.SUCCESS == checkout.state.slug # check the message response = client.get(response.url) assert "messages" in response.context messages = response.context["messages"] assert 1 == len(messages) for msg in messages: # only one message assert "Charged" in msg.message assert "a total of 678.00 ref Bread" in msg.message
[docs]@pytest.mark.django_db def test_customer_charge_error(client): """The Stripe call will raise an exception - notify the user.""" with mock.patch("stripe.Charge.create") as mock_charge: mock_charge.side_effect = CheckoutError("mock exception for 'Charge'") obj = CustomerFactory() url = reverse("checkout.customer.charge", args=[obj.pk]) user = UserFactory(is_staff=True) assert ( client.login(username=user.username, password=TEST_PASSWORD) is True ) data = {"description": "Bread", "total": Decimal("678")} response = client.post(url, data) assert 302 == response.status_code assert "/checkout/customer/" in response.url assert Message.objects.first() is None payment = CustomerPayment.objects.get(description="Bread") checkout = Checkout.objects.audit_content_object(payment).first() assert checkout is not None assert Decimal("678") == checkout.total assert CheckoutAction.CHARGE == checkout.action.slug assert CheckoutState.FAIL == checkout.state.slug # check the message response = client.get(response.url) assert "messages" in response.context messages = response.context["messages"] assert 1 == len(messages) for msg in messages: # only one message assert "Error charging customer" in msg.message
[docs]@pytest.mark.django_db def test_customer_detail_view(client): obj = CustomerFactory() url = url_with_querystring(reverse("checkout.customer"), email=obj.email) user = UserFactory(is_staff=True) assert client.login(username=user.username, password=TEST_PASSWORD) is True response = client.get(url) assert 200 == response.status_code
[docs]@pytest.mark.django_db def test_customer_detail_view_invalid_email(client): url = url_with_querystring( reverse("checkout.customer"), email="does.not.exist@test.com" ) user = UserFactory(is_staff=True) assert client.login(username=user.username, password=TEST_PASSWORD) is True # with pytest.raises(Customer.DoesNotExist) as e: # client.get(url) # assert 'Cannot find a customer record' in str(e.value) response = client.get(url) assert 200 == response.status_code
[docs]@pytest.mark.django_db def test_customer_card_refresh_request(client): MailTemplateFactory(slug=Customer.MAIL_TEMPLATE_CARD_REFRESH_REQUEST) obj = CustomerFactory() url = reverse("checkout.customer.refresh.request", args=[obj.pk]) user = UserFactory(is_staff=True) assert client.login(username=user.username, password=TEST_PASSWORD) is True response = client.post(url) assert 302 == response.status_code assert "/checkout/customer/" in response.url message = Message.objects.first() assert message is not None mail = message.mail_set.first() assert mail is not None assert 2 == mail.mailfield_set.count() data = {f.key: f.value for f in mail.mailfield_set.all()} assert obj.name == data["name"] assert "/example/contact/card/refesh/" in data["url"]