|
| 1 | +import pytest as pytest |
1 | 2 | from django.core import management |
2 | 3 | from django_pagarme import facade as pagarme_facade |
3 | 4 | from django_pagarme.models import PagarmePayment, PagarmeItemConfig, PagarmeNotification |
4 | 5 | from model_bakery import baker |
| 6 | +from rolepermissions.roles import assign_role |
5 | 7 |
|
6 | 8 | from pythonpro.core.models import User |
7 | 9 | from pythonpro.memberkit.models import SubscriptionType, PaymentItemConfigToSubscriptionType, Subscription |
@@ -75,3 +77,91 @@ def _make_subscriptions_with_payment(payment_config, status): |
75 | 77 | payment.items.set([payment_config]) |
76 | 78 | baker.make(PagarmeNotification, payment=payment, status=status) |
77 | 79 | return baker.make(Subscription, payment=payment, status=Subscription.Status.INACTIVE) |
| 80 | + |
| 81 | + |
| 82 | +@pytest.fixture |
| 83 | +def subscription_types(db): |
| 84 | + role_to_subscription_type_dct = { |
| 85 | + 'data_scientist': 4456, |
| 86 | + 'webdev': 4426, |
| 87 | + 'member': 4424, |
| 88 | + 'bootcamper': 4423, |
| 89 | + 'client': 4420, |
| 90 | + } |
| 91 | + return [ |
| 92 | + baker.make(SubscriptionType, id=v, name=k) |
| 93 | + for k, v in role_to_subscription_type_dct.items() |
| 94 | + ] |
| 95 | + |
| 96 | + |
| 97 | +def test_subscription_not_created_for_lead(django_user_model): |
| 98 | + lead = baker.make(django_user_model) |
| 99 | + assign_role(lead, 'lead') |
| 100 | + management.call_command('create_subscriptions_for_roles') |
| 101 | + assert not Subscription.objects.exists() |
| 102 | + |
| 103 | + |
| 104 | +role_to_subscription = pytest.mark.parametrize( |
| 105 | + 'role,subscription_type_id', |
| 106 | + [ |
| 107 | + ('data_scientist', 4456), |
| 108 | + ('client', 4420), |
| 109 | + ('webdev', 4426), |
| 110 | + ('member', 4424), |
| 111 | + ('pythonista', 4423), |
| 112 | + ('bootcamper', 4423), |
| 113 | + ] |
| 114 | +) |
| 115 | + |
| 116 | + |
| 117 | +@role_to_subscription |
| 118 | +def test_subscription_created(role, subscription_type_id, subscription_types, django_user_model): |
| 119 | + user_with_role = baker.make(django_user_model) |
| 120 | + assign_role(user_with_role, role) |
| 121 | + management.call_command('create_subscriptions_for_roles') |
| 122 | + subscription = Subscription.objects.first() |
| 123 | + assert subscription.subscriber == user_with_role |
| 124 | + assert subscription.status == Subscription.Status.INACTIVE |
| 125 | + assert subscription.subscription_types.first().id == subscription_type_id |
| 126 | + |
| 127 | + |
| 128 | +@role_to_subscription |
| 129 | +def test_subscription_creation_idempotence(role, subscription_type_id, subscription_types, django_user_model): |
| 130 | + user_with_role = baker.make(django_user_model) |
| 131 | + assign_role(user_with_role, role) |
| 132 | + previous_subscription = baker.make(Subscription, subscriber=user_with_role) |
| 133 | + previous_subscription.subscription_types.add(subscription_type_id) |
| 134 | + management.call_command('create_subscriptions_for_roles') |
| 135 | + management.call_command('create_subscriptions_for_roles') |
| 136 | + assert Subscription.objects.count() == 1, 'Subscription should not be created' |
| 137 | + |
| 138 | + |
| 139 | +@pytest.mark.parametrize( |
| 140 | + 'role,subscription_type_id', |
| 141 | + [ |
| 142 | + ('data_scientist', 4424), |
| 143 | + ('client', 4424), |
| 144 | + ('webdev', 4424), |
| 145 | + ('member', 4420), |
| 146 | + ('pythonista', 4424), |
| 147 | + ('bootcamper', 4424), |
| 148 | + ] |
| 149 | +) |
| 150 | +def test_subscription_creation_another_subscription_type(role, subscription_type_id, subscription_types, |
| 151 | + django_user_model): |
| 152 | + """ |
| 153 | + Check all ids from parametrize area different from role_to_subscription |
| 154 | + :param role: |
| 155 | + :param subscription_type_id: |
| 156 | + :param subscription_types: |
| 157 | + :param django_user_model: |
| 158 | + :return: |
| 159 | + """ |
| 160 | + user_with_role = baker.make(django_user_model) |
| 161 | + assign_role(user_with_role, role) |
| 162 | + previous_subscription = baker.make(Subscription, subscriber=user_with_role) |
| 163 | + previous_subscription.subscription_types.add(subscription_type_id) |
| 164 | + management.call_command('create_subscriptions_for_roles') |
| 165 | + assert Subscription.objects.count() == 2, 'New Subscription should be created' |
| 166 | + management.call_command('create_subscriptions_for_roles') |
| 167 | + assert Subscription.objects.count() == 2, 'New Subscription should be created only once' |
0 commit comments