aboutsummaryrefslogtreecommitdiffstats
path: root/alphabetlearning/payments/tests/test_views.py
diff options
context:
space:
mode:
Diffstat (limited to 'alphabetlearning/payments/tests/test_views.py')
-rw-r--r--alphabetlearning/payments/tests/test_views.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/alphabetlearning/payments/tests/test_views.py b/alphabetlearning/payments/tests/test_views.py
index 24bb19a..aebb040 100644
--- a/alphabetlearning/payments/tests/test_views.py
+++ b/alphabetlearning/payments/tests/test_views.py
@@ -21,3 +21,38 @@ def test_add_resource_to_cart(client, resource, 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)