forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mixins.py
77 lines (61 loc) · 2.38 KB
/
test_mixins.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
"""
dj-stripe Mixin Tests.
"""
from copy import deepcopy
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test.client import RequestFactory
from django.test.testcases import TestCase
from djstripe.mixins import PaymentsContextMixin, SubscriptionMixin
from djstripe.models import Plan
from djstripe.settings import djstripe_settings
from . import FAKE_CUSTOMER, FAKE_PLAN, FAKE_PLAN_II, FAKE_PRODUCT
class TestPaymentsContextMixin(TestCase):
def test_get_context_data(self):
class TestSuperView(object):
def get_context_data(self):
return {}
class TestView(PaymentsContextMixin, TestSuperView):
pass
context = TestView().get_context_data()
self.assertIn(
"STRIPE_PUBLIC_KEY", context, "STRIPE_PUBLIC_KEY missing from context."
)
self.assertEqual(
context["STRIPE_PUBLIC_KEY"],
djstripe_settings.STRIPE_PUBLIC_KEY,
"Incorrect STRIPE_PUBLIC_KEY.",
)
self.assertIn("plans", context, "pans missing from context.")
self.assertEqual(
list(Plan.objects.all()), list(context["plans"]), "Incorrect plans."
)
class TestSubscriptionMixin(TestCase):
def setUp(self):
with patch(
"stripe.Product.retrieve",
return_value=deepcopy(FAKE_PRODUCT),
autospec=True,
):
Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN_II))
@patch(
"stripe.Customer.create", return_value=deepcopy(FAKE_CUSTOMER), autospec=True
)
def test_get_context_data(self, stripe_create_customer_mock):
class TestSuperView(object):
def get_context_data(self):
return {}
class TestView(SubscriptionMixin, TestSuperView):
pass
test_view = TestView()
test_view.request = RequestFactory()
test_view.request.user = get_user_model().objects.create(
username="x", email="[email protected]"
)
context = test_view.get_context_data()
self.assertIn(
"is_plans_plural", context, "is_plans_plural missing from context."
)
self.assertTrue(context["is_plans_plural"], "Incorrect is_plans_plural.")
self.assertIn("customer", context, "customer missing from context.")