forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_views.py
77 lines (57 loc) · 3.08 KB
/
test_views.py
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
"""
.. module:: dj-stripe.tests.test_views
:synopsis: dj-stripe View Tests.
.. moduleauthor:: Daniel Greenfeld (@pydanny)
.. moduleauthor:: Alex Kavanaugh (@kavdev)
"""
from copy import deepcopy
from django.contrib.auth import get_user, get_user_model
from django.test.testcases import TestCase
from django.urls import reverse
from mock import patch
from djstripe.models import Customer, Subscription, Plan
from tests import (
FAKE_CUSTOMER, FAKE_PLAN, FAKE_SUBSCRIPTION,
FAKE_SUBSCRIPTION_CANCELED, FAKE_SUBSCRIPTION_CANCELED_AT_PERIOD_END
)
class CancelSubscriptionViewTest(TestCase):
def setUp(self):
self.plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
self.url = reverse("djstripe:cancel_subscription")
self.user = get_user_model().objects.create_user(
username="pydanny",
email="[email protected]",
password="password"
)
self.assertTrue(self.client.login(username="pydanny", password="password"))
@patch("djstripe.stripe_objects.StripeSubscription.cancel", return_value=FAKE_SUBSCRIPTION_CANCELED)
def test_cancel(self, cancel_subscription_mock):
Customer.objects.create(subscriber=self.user, stripe_id=FAKE_CUSTOMER["id"], livemode=False)
Subscription.sync_from_stripe_data(FAKE_SUBSCRIPTION)
response = self.client.post(self.url)
cancel_subscription_mock.assert_called_once_with(at_period_end=True)
self.assertRedirects(response, reverse("home"))
self.assertTrue(self.user.is_authenticated)
@patch("djstripe.stripe_objects.StripeSubscription.cancel", return_value=FAKE_SUBSCRIPTION_CANCELED_AT_PERIOD_END)
def test_cancel_at_period_end(self, cancel_subscription_mock):
Customer.objects.create(subscriber=self.user, stripe_id=FAKE_CUSTOMER["id"], livemode=False)
Subscription.sync_from_stripe_data(FAKE_SUBSCRIPTION)
response = self.client.post(self.url)
cancel_subscription_mock.assert_called_once_with(at_period_end=True)
self.assertRedirects(response, reverse("home"))
self.assertTrue(self.user.is_authenticated)
@patch("djstripe.stripe_objects.StripeSubscription.cancel", return_value=FAKE_SUBSCRIPTION_CANCELED)
def test_cancel_next_url(self, cancel_subscription_mock):
Customer.objects.create(subscriber=self.user, stripe_id=FAKE_CUSTOMER["id"], livemode=False)
Subscription.sync_from_stripe_data(FAKE_SUBSCRIPTION)
response = self.client.post(self.url + "?next=/test")
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/test")
self.assertTrue(get_user(self.client).is_anonymous)
@patch("djstripe.stripe_objects.StripeSubscription.cancel")
def test_cancel_no_subscription(self, cancel_subscription_mock):
Customer.objects.create(subscriber=self.user, stripe_id=FAKE_CUSTOMER["id"], livemode=False)
response = self.client.post(self.url)
cancel_subscription_mock.assert_not_called()
self.assertEqual(response.status_code, 302)
self.assertTrue(get_user(self.client).is_anonymous)