~linuxgoose/bocpress

ref: 244924e12944adc844b3b7b76cb3340b97d5add4 bocpress/main/tests/test_billing.py -rw-r--r-- 10.0 KiB
244924e1Jordan Robinson fix allow tags input on post form to be blank 2 months ago
                                                                                
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from datetime import datetime, timedelta
from unittest.mock import patch

import stripe
from django.test import TestCase
from django.urls import reverse

from main import models
from main.views import billing


class BillingCannotChangeIsPremiumTestCase(TestCase):
    """Test user cannot change their is_premium flag without going through billing."""

    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.client.force_login(self.user)

    def test_update_billing_settings(self):
        data = {
            "username": "alice",
            "is_premium": True,
        }
        self.client.post(reverse("user_update"), data)
        self.assertFalse(models.User.objects.get(id=self.user.id).is_premium)


class BillingIndexGrandfatherTestCase(TestCase):
    """Test billing pages work accordingly for grandathered user."""

    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.user.is_grandfathered = True
        self.user.save()
        self.client.force_login(self.user)

    def test_index(self):
        response = self.client.get(reverse("billing_index"))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, b"Grandfather Plan")

    def test_cannot_subscribe(self):
        response = self.client.post(reverse("billing_subscription"))
        self.assertEqual(response.status_code, 302)
        self.assertRedirects(response, reverse("dashboard"))

    def test_cannot_cancel_get(self):
        response = self.client.get(reverse("billing_subscription_cancel"))
        self.assertEqual(response.status_code, 302)
        self.assertRedirects(response, reverse("dashboard"))


class BillingIndexFreeTestCase(TestCase):
    """Test billing index works for free user."""

    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.user.save()
        self.client.force_login(self.user)

    def test_index(self):
        with (
            patch.object(
                stripe.Customer, "create", return_value={"id": "cus_123abcdefg"}
            ),
            patch.object(billing, "_get_stripe_subscription", return_value=None),
            patch.object(
                billing,
                "_get_payment_methods",
            ),
            patch.object(billing, "_get_invoices"),
        ):
            response = self.client.get(reverse("billing_index"))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, b"Free Plan")


class BillingIndexPremiumTestCase(TestCase):
    """Test billing index works for premium user."""

    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.user.is_premium = True
        self.user.save()
        self.client.force_login(self.user)

    def test_index(self):
        one_year_later = datetime.now() + timedelta(days=365)
        subscription = {
            "current_period_end": one_year_later.timestamp(),
            "current_period_start": datetime.now().timestamp(),
        }
        with (
            patch.object(
                stripe.Customer, "create", return_value={"id": "cus_123abcdefg"}
            ),
            patch.object(
                billing,
                "_get_stripe_subscription",
                return_value=subscription,
            ),
            patch.object(billing, "_get_payment_methods"),
            patch.object(billing, "_get_invoices"),
        ):
            response = self.client.get(reverse("billing_index"))

        self.assertEqual(response.status_code, 200)
        self.assertContains(response, b"Premium Plan")


class BillingCardAddTestCase(TestCase):
    """Test billing card add functionality."""

    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.user.is_premium = True
        self.user.save()
        self.client.force_login(self.user)

    def test_card_add_get(self):
        with patch.object(
            stripe.SetupIntent, "create", return_value={"client_secret": "seti_123abc"}
        ):
            response = self.client.get(reverse("billing_card"))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, b"Add card")

    def test_card_add_post(self):
        one_year_later = datetime.now() + timedelta(days=365)
        subscription = {
            "current_period_end": one_year_later.timestamp(),
            "current_period_start": datetime.now().timestamp(),
        }
        with (
            patch.object(
                stripe.Customer, "create", return_value={"id": "cus_123abcdefg"}
            ),
            patch.object(
                billing,
                "_get_stripe_subscription",
                return_value=subscription,
            ),
            patch.object(billing, "_get_payment_methods"),
            patch.object(billing, "_get_invoices"),
        ):
            response = self.client.post(
                reverse("billing_card"),
                data={"card_token": "tok_123"},
                follow=True,
            )

        self.assertEqual(response.status_code, 200)
        self.assertContains(response, b"Premium Plan")


class BillingCancelSubscriptionTestCase(TestCase):
    """Test billing cancel subscription."""

    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.user.is_premium = True
        self.user.stripe_customer_id = "cus_123abcdefg"
        self.user.save()
        self.client.force_login(self.user)

    def test_cancel_subscription_get(self):
        one_year_later = datetime.now() + timedelta(days=365)
        subscription = {
            "current_period_end": one_year_later.timestamp(),
            "current_period_start": datetime.now().timestamp(),
        }
        with patch.object(
            billing,
            "_get_stripe_subscription",
            return_value=subscription,
        ):
            response = self.client.get(reverse("billing_subscription_cancel"))

        self.assertEqual(response.status_code, 200)
        self.assertContains(response, b"Cancel Premium")

    def test_cancel_subscription_post(self):
        with (
            patch.object(stripe.Subscription, "delete"),
            patch.object(
                billing,
                "_get_stripe_subscription",
                return_value={"id": "sub_123"},
            ),
        ):
            response = self.client.post(reverse("billing_subscription_cancel"))

        self.assertEqual(response.status_code, 302)
        self.assertFalse(models.User.objects.get(id=self.user.id).is_premium)


class BillingCancelSubscriptionTwiceTestCase(TestCase):
    """Test billing cancel subscription when already canceled."""

    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.user.stripe_customer_id = "cus_123abcdefg"
        self.user.save()
        self.client.force_login(self.user)

    def test_cancel_subscription_get(self):
        with (
            patch.object(billing, "_get_stripe_subscription", return_value=None),
            patch.object(
                stripe.Customer, "create", return_value={"id": "cus_123abcdefg"}
            ),
            patch.object(
                billing,
                "_get_payment_methods",
            ),
            patch.object(billing, "_get_invoices"),
        ):
            response = self.client.get(reverse("billing_subscription_cancel"))

            # need to check inside with context because billing_index needs
            # __get_stripe_subscription patch
            self.assertRedirects(response, reverse("billing_index"))

    def test_cancel_subscription_post(self):
        with (
            patch.object(stripe.Subscription, "delete"),
            patch.object(
                billing,
                "_get_stripe_subscription",
                return_value=None,
            ),
            patch.object(
                stripe.Customer, "create", return_value={"id": "cus_123abcdefg"}
            ),
            patch.object(
                billing,
                "_get_payment_methods",
            ),
            patch.object(billing, "_get_invoices"),
        ):
            response = self.client.post(reverse("billing_subscription_cancel"))

            self.assertRedirects(response, reverse("billing_index"))
            self.assertFalse(models.User.objects.get(id=self.user.id).is_premium)


class BillingReenableSubscriptionTestCase(TestCase):
    """Test re-enabling subscription after cancelation."""

    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.user.stripe_customer_id = "cus_123abcdefg"
        self.user.save()
        self.client.force_login(self.user)

    def test_reenable_subscription_post(self):
        one_year_later = datetime.now() + timedelta(days=365)
        subscription = {
            "current_period_end": one_year_later.timestamp(),
            "current_period_start": datetime.now().timestamp(),
        }
        created_subscription = {
            "id": "sub_456abcdefg",
            "latest_invoice": {
                "payment_intent": {
                    "client_secret": "seti_123abc",
                },
            },
        }
        with (
            patch.object(stripe.Subscription, "delete"),
            patch.object(
                billing,
                "_get_stripe_subscription",
                return_value=subscription,
            ),
            patch.object(
                stripe.Customer, "create", return_value={"id": "cus_123abcdefg"}
            ),
            patch.object(
                stripe.Subscription,
                "create",
                return_value=created_subscription,
            ),
            patch.object(
                billing,
                "_get_payment_methods",
            ),
            patch.object(billing, "_get_invoices"),
        ):
            response = self.client.post(reverse("billing_subscription"))

            self.assertRedirects(response, reverse("billing_index"))
            self.assertTrue(models.User.objects.get(id=self.user.id).is_premium)