aboutsummaryrefslogblamecommitdiffstats
path: root/alphabetlearning/payments/tests/test_views.py
blob: aebb040fa02d320dc1d2e1df67d570432d163937 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
             
                                                  









                                               


                      
                                                      

                                                                                          
                                                                                

                              

                                      


































                                                                                                                              
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)