aboutsummaryrefslogtreecommitdiffstats
path: root/pyblackbird_cc/payments/tests/test_models.py
blob: fd26fea1b2267bbf6f3d4aeed1316dce0ecaae07 (plain) (blame)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import pytest
from allauth.account.signals import user_signed_up
from django.contrib.auth import get_user_model
from django.db import IntegrityError
from django.test import RequestFactory

from pyblackbird_cc.payments.models import ShoppingCart, Subscription
from pyblackbird_cc.payments.models import SubscriptionPlan

User = get_user_model()


@pytest.fixture
def user_data():
    return {"email": "testuser@example.com", "password": "testpassword123"}


@pytest.mark.django_db
def test_subscription_user_unique():
    # Ensure the free plan exists
    free_plan, _ = SubscriptionPlan.objects.get_or_create(
        name="Free Plan",
        defaults={
            "price": 0,
            "description": "Free plan description",
            "allowed_downloads": 10,
        },
    )

    # Create a new user
    user_data = {"email": "testuser@example.com", "password": "testpassword123"}
    user = User.objects.create_user(**user_data)  # type: ignore

    # Create a subscription for the user
    Subscription.objects.create(user=user, plan=free_plan)

    # Try to create another subscription for the same user
    with pytest.raises(IntegrityError):
        Subscription.objects.create(user=user, plan=free_plan)


@pytest.mark.django_db
def test_user_signup_assigns_free_subscription(user_data):
    # Ensure the free plan exists
    free_plan, _ = SubscriptionPlan.objects.get_or_create(
        name="Free Plan",
        defaults={
            "price": 0,
            "description": "Free plan description",
            "allowed_downloads": 10,
        },
    )  # Create a new user
    user = User.objects.create_user(**user_data)  # type: ignore
    # Manually trigger the user_signed_up signal
    request = RequestFactory().get("/")
    user_signed_up.send(sender=user.__class__, request=request, user=user)

    # Check if a SubscriptionPlan was created for the user
    subscription = user.subscription
    assert subscription is not None

    # Check if the assigned plan is the free plan
    assert subscription.plan == free_plan

    # Additional assertions can be added here to check other properties
    # of the SubscriptionPlan or Subscription as needed


@pytest.mark.django_db
def test_shopping_cart_is_created_when_user_is_created(user_data):
    user = User.objects.create_user(**user_data)  # type: ignore
    request = RequestFactory().get("/")
    user_signed_up.send(sender=user.__class__, request=request, user=user)
    shopping_cart = ShoppingCart.objects.get(user=user)
    assert shopping_cart is not None


# When the user adds a `Resource` to their cart, create a new `CartItem` instance and associate it with the user's `ShoppingCart` and the selected `Resource`.


@pytest.mark.django_db
def test_cart_item_is_created_when_resource_is_added_to_cart(user_data, resource):
    user = User.objects.create_user(**user_data)  # type: ignore
    request = RequestFactory().get("/")
    user_signed_up.send(sender=user.__class__, request=request, user=user)
    users_cart = ShoppingCart.objects.get(user=user)
    users_cart.add_resource(resource)
    assert users_cart.items.count() == 1  # type: ignore