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
|
import pytest
from alphabetlearning.payments.models import Price
from django.urls import reverse
@pytest.mark.django_db
def test_cart_view(client, user):
url = reverse("payments:cart_detail")
client.force_login(user)
response = client.get(url)
assert response.status_code == 200
assert "My basket" in str(response.content)
@pytest.mark.django_db
def test_add_resource_to_cart(client, resource, user):
price = Price.objects.create(resource=resource, price=1000, stripe_price_id="price_1")
resource.price_obj.add(price)
url = reverse("payments:add_to_basket", kwargs={"resource_id": resource.id})
client.force_login(user)
response = client.get(url)
# resdirects to the shopping cart
assert response.status_code == 302
@pytest.mark.django_db # Marks the test function as utilizing the Django database.
def test_cart_contains_resource_with_correct_price(client, resource, user):
# Create a Price object associated with the given resource, with a specified price and Stripe price ID.
# strip works with cents, so 2000 is 2.00
price = Price.objects.create(resource=resource, price=2000, stripe_price_id="price_1")
# Add the created price object to the resource's price objects.
# This is a many-to-many relationship, so we use the add method to add the price object to the resource's price_obj field.
resource.price_obj.add(price)
# Generate the URL for adding the resource to the shopping basket using a reverse lookup.
addurl = reverse("payments:add_to_basket", kwargs={"resource_id": resource.id})
# Generate the URL for viewing the shopping cart.
carturl = reverse("payments:cart_detail")
# Log in the user for the test client to simulate an authenticated user session.
client.force_login(user)
# Send a GET request to the addurl to attempt to add the resource to the basket and store the response.
addresponse = client.get(addurl)
# Check that the response status code indicates a successful redirection (302).
assert addresponse.status_code == 302
# Send a GET request to the carturl to retrieve the cart's contents and store the response.
cartresponse = client.get(carturl)
# Assert that the resource's name is included in the cart response content, confirming it was added.
assert resource.name in str(cartresponse.content)
# Assert that the correct price (formatted as '2.00') is displayed in the cart response content.
assert "2.00" in str(cartresponse.content)
|