Skip to content

Commit 66e1efd

Browse files
authored
ref: Remove references to plan and stripe consts (#1130)
1 parent ae8f192 commit 66e1efd

File tree

13 files changed

+318
-481
lines changed

13 files changed

+318
-481
lines changed

api/internal/owner/serializers.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import Any, Dict
44

55
from dateutil.relativedelta import relativedelta
6-
from django.conf import settings
76
from rest_framework import serializers
87
from rest_framework.exceptions import PermissionDenied
98
from shared.plan.constants import (
@@ -217,11 +216,7 @@ class StripeScheduledPhaseSerializer(serializers.Serializer):
217216

218217
def get_plan(self, phase: Dict[str, Any]) -> str:
219218
plan_id = phase["items"][0]["plan"]
220-
stripe_plan_dict = settings.STRIPE_PLAN_IDS
221-
plan_name = list(stripe_plan_dict.keys())[
222-
list(stripe_plan_dict.values()).index(plan_id)
223-
]
224-
marketing_plan_name = Plan.objects.get(name=plan_name).marketing_name
219+
marketing_plan_name = Plan.objects.get(stripe_id=plan_id).marketing_name
225220
return marketing_plan_name
226221

227222
def get_quantity(self, phase: Dict[str, Any]) -> int:
@@ -344,11 +339,13 @@ def update(self, instance: Owner, validated_data: Dict[str, Any]) -> object:
344339
instance, desired_plan
345340
)
346341

347-
sentry_plans = Plan.objects.filter(
348-
tier__tier_name=TierName.SENTRY.value, is_active=True
349-
).values_list("name", flat=True)
342+
plan = (
343+
Plan.objects.select_related("tier")
344+
.filter(name=desired_plan["value"])
345+
.first()
346+
)
350347

351-
if desired_plan["value"] in sentry_plans:
348+
if plan and plan.tier.tier_name == TierName.SENTRY.value:
352349
current_owner = self.context["view"].request.current_owner
353350
send_sentry_webhook(current_owner, instance)
354351

api/internal/tests/views/test_account_viewset.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def test_retrieve_account_gets_account_fields_when_there_are_scheduled_details(
234234
schedule_params = {
235235
"id": 123,
236236
"start_date": 123689126736,
237-
"stripe_plan_id": "plan_H6P3KZXwmAbqPS",
237+
"stripe_plan_id": "plan_pro_yearly",
238238
"quantity": 6,
239239
}
240240
phases = [
@@ -330,7 +330,7 @@ def test_retrieve_account_returns_last_phase_when_more_than_one_scheduled_phases
330330
schedule_params = {
331331
"id": 123,
332332
"start_date": 123689126736,
333-
"stripe_plan_id": "plan_H6P3KZXwmAbqPS",
333+
"stripe_plan_id": "plan_pro_yearly",
334334
"quantity": 6,
335335
}
336336
phases = [

billing/helpers.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def get_all_admins_for_owners(owners: QuerySet[Owner]):
2727

2828

2929
def mock_all_plans_and_tiers():
30+
TierFactory(tier_name=TierName.BASIC.value)
31+
3032
trial_tier = TierFactory(tier_name=TierName.TRIAL.value)
3133
PlanFactory(
3234
tier=trial_tier,
@@ -39,18 +41,7 @@ def mock_all_plans_and_tiers():
3941
"Unlimited private repositories",
4042
"Priority Support",
4143
],
42-
)
43-
44-
basic_tier = TierFactory(tier_name=TierName.BASIC.value)
45-
PlanFactory(
46-
name=PlanName.FREE_PLAN_NAME.value,
47-
tier=basic_tier,
48-
marketing_name="Developer",
49-
benefits=[
50-
"Up to 1 user",
51-
"Unlimited public repositories",
52-
"Unlimited private repositories",
53-
],
44+
stripe_id="plan_trial",
5445
)
5546

5647
pro_tier = TierFactory(tier_name=TierName.PRO.value)
@@ -67,6 +58,7 @@ def mock_all_plans_and_tiers():
6758
billing_rate=BillingRate.MONTHLY.value,
6859
base_unit_price=PlanPrice.MONTHLY.value,
6960
paid_plan=True,
61+
stripe_id="plan_pro",
7062
)
7163
PlanFactory(
7264
name=PlanName.CODECOV_PRO_YEARLY.value,
@@ -81,6 +73,7 @@ def mock_all_plans_and_tiers():
8173
billing_rate=BillingRate.ANNUALLY.value,
8274
base_unit_price=PlanPrice.YEARLY.value,
8375
paid_plan=True,
76+
stripe_id="plan_pro_yearly",
8477
)
8578

8679
team_tier = TierFactory(tier_name=TierName.TEAM.value)
@@ -98,6 +91,7 @@ def mock_all_plans_and_tiers():
9891
base_unit_price=PlanPrice.TEAM_MONTHLY.value,
9992
monthly_uploads_limit=2500,
10093
paid_plan=True,
94+
stripe_id="plan_team_monthly",
10195
)
10296
PlanFactory(
10397
name=PlanName.TEAM_YEARLY.value,
@@ -113,6 +107,7 @@ def mock_all_plans_and_tiers():
113107
base_unit_price=PlanPrice.TEAM_YEARLY.value,
114108
monthly_uploads_limit=2500,
115109
paid_plan=True,
110+
stripe_id="plan_team_yearly",
116111
)
117112

118113
sentry_tier = TierFactory(tier_name=TierName.SENTRY.value)
@@ -130,6 +125,7 @@ def mock_all_plans_and_tiers():
130125
"Unlimited private repositories",
131126
"Priority Support",
132127
],
128+
stripe_id="plan_sentry_monthly",
133129
)
134130
PlanFactory(
135131
name=PlanName.SENTRY_YEARLY.value,
@@ -145,6 +141,7 @@ def mock_all_plans_and_tiers():
145141
"Unlimited private repositories",
146142
"Priority Support",
147143
],
144+
stripe_id="plan_sentry_yearly",
148145
)
149146

150147
enterprise_tier = TierFactory(tier_name=TierName.ENTERPRISE.value)
@@ -161,6 +158,7 @@ def mock_all_plans_and_tiers():
161158
"Unlimited private repositories",
162159
"Priority Support",
163160
],
161+
stripe_id="plan_enterprise_cloud_monthly",
164162
)
165163
PlanFactory(
166164
name=PlanName.ENTERPRISE_CLOUD_YEARLY.value,
@@ -175,6 +173,7 @@ def mock_all_plans_and_tiers():
175173
"Unlimited private repositories",
176174
"Priority Support",
177175
],
176+
stripe_id="plan_enterprise_cloud_yearly",
178177
)
179178

180179
PlanFactory(
@@ -190,4 +189,5 @@ def mock_all_plans_and_tiers():
190189
"Unlimited public repositories",
191190
"Unlimited private repositories",
192191
],
192+
stripe_id="plan_default_free",
193193
)

billing/tests/test_views.py

+25-24
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from billing.helpers import mock_all_plans_and_tiers
1515
from billing.views import StripeWebhookHandler
16+
from codecov_auth.models import Plan
1617

1718
from ..constants import StripeHTTPHeaders
1819

@@ -721,7 +722,7 @@ def test_customer_subscription_created_early_returns_if_unverified_payment(
721722
"object": {
722723
"id": "sub_123",
723724
"customer": "cus_123",
724-
"plan": {"id": "plan_H6P16wij3lUuxg"},
725+
"plan": {"id": "plan_pro_yearly"},
725726
"metadata": {"obo_organization": self.owner.ownerid},
726727
"quantity": 20,
727728
}
@@ -776,7 +777,7 @@ def test_customer_subscription_created_does_nothing_if_plan_not_paid_user_plan(
776777
"object": {
777778
"id": "FOEKDCDEQ",
778779
"customer": "sdo050493",
779-
"plan": {"id": "?"},
780+
"plan": {"id": "plan_free"},
780781
"metadata": {"obo_organization": self.owner.ownerid},
781782
"quantity": 20,
782783
}
@@ -809,7 +810,7 @@ def test_customer_subscription_created_sets_plan_info(
809810
"object": {
810811
"id": stripe_subscription_id,
811812
"customer": stripe_customer_id,
812-
"plan": {"id": "plan_H6P16wij3lUuxg"},
813+
"plan": {"id": "plan_pro_yearly"},
813814
"metadata": {"obo_organization": self.owner.ownerid},
814815
"quantity": quantity,
815816
"status": "active",
@@ -849,7 +850,7 @@ def test_customer_subscription_created_can_trigger_trial_expiration(
849850
"object": {
850851
"id": stripe_subscription_id,
851852
"customer": stripe_customer_id,
852-
"plan": {"id": "plan_H6P16wij3lUuxg"},
853+
"plan": {"id": "plan_pro_yearly"},
853854
"metadata": {"obo_organization": self.owner.ownerid},
854855
"quantity": quantity,
855856
"default_payment_method": "blabla",
@@ -882,7 +883,7 @@ def test_customer_subscription_updated_does_not_change_subscription_if_not_paid_
882883
"object": {
883884
"id": self.owner.stripe_subscription_id,
884885
"customer": self.owner.stripe_customer_id,
885-
"plan": {"id": "?"},
886+
"plan": {"id": "plan_free"},
886887
"metadata": {"obo_organization": self.owner.ownerid},
887888
"quantity": 20,
888889
"status": "active",
@@ -929,7 +930,7 @@ def test_customer_subscription_updated_does_not_change_subscription_if_there_is_
929930
"object": {
930931
"id": self.owner.stripe_subscription_id,
931932
"customer": self.owner.stripe_customer_id,
932-
"plan": {"id": "plan_H6P16wij3lUuxg"},
933+
"plan": {"id": "plan_pro_yearly"},
933934
"metadata": {"obo_organization": self.owner.ownerid},
934935
"quantity": 20,
935936
"status": "active",
@@ -985,7 +986,7 @@ def test_customer_subscription_updated_sets_free_and_deactivates_all_repos_if_in
985986
"id": self.owner.stripe_subscription_id,
986987
"customer": self.owner.stripe_customer_id,
987988
"plan": {
988-
"id": "plan_H6P16wij3lUuxg",
989+
"id": "plan_pro_yearly",
989990
},
990991
"metadata": {"obo_organization": self.owner.ownerid},
991992
"quantity": 20,
@@ -1088,7 +1089,7 @@ def test_customer_subscription_updated_sets_free_and_deactivates_all_repos_if_in
10881089
"id": self.owner.stripe_subscription_id,
10891090
"customer": self.owner.stripe_customer_id,
10901091
"plan": {
1091-
"id": "plan_H6P16wij3lUuxg",
1092+
"id": "plan_pro_yearly",
10921093
},
10931094
"metadata": {"obo_organization": self.owner.ownerid},
10941095
"quantity": 20,
@@ -1149,7 +1150,7 @@ def test_customer_subscription_updated_sets_fields_on_success(
11491150
"object": {
11501151
"id": self.owner.stripe_subscription_id,
11511152
"customer": self.owner.stripe_customer_id,
1152-
"plan": {"id": "plan_H6P16wij3lUuxg"},
1153+
"plan": {"id": "plan_pro_yearly"},
11531154
"metadata": {"obo_organization": self.owner.ownerid},
11541155
"quantity": quantity,
11551156
"status": "active",
@@ -1200,7 +1201,7 @@ def test_customer_subscription_updated_sets_fields_on_success_multiple_owner(
12001201
"object": {
12011202
"id": self.owner.stripe_subscription_id,
12021203
"customer": self.owner.stripe_customer_id,
1203-
"plan": {"id": "plan_H6P16wij3lUuxg"},
1204+
"plan": {"id": "plan_pro_yearly"},
12041205
"metadata": {"obo_organization": self.owner.ownerid},
12051206
"quantity": quantity,
12061207
"status": "active",
@@ -1238,7 +1239,7 @@ def test_customer_subscription_updated_logs_error_if_no_matching_owners(
12381239
"object": {
12391240
"id": "sub_notexist",
12401241
"customer": "cus_notexist",
1241-
"plan": {"id": "plan_H6P16wij3lUuxg"},
1242+
"plan": {"id": "plan_pro_yearly"},
12421243
"metadata": {"obo_organization": 1},
12431244
"quantity": 8,
12441245
"status": "active",
@@ -1254,7 +1255,7 @@ def test_customer_subscription_updated_logs_error_if_no_matching_owners(
12541255
extra={
12551256
"stripe_subscription_id": "sub_notexist",
12561257
"stripe_customer_id": "cus_notexist",
1257-
"plan_id": "plan_H6P16wij3lUuxg",
1258+
"plan_id": "plan_pro_yearly",
12581259
},
12591260
)
12601261

@@ -1267,7 +1268,7 @@ def test_subscription_schedule_released_updates_owner_with_existing_subscription
12671268
self.owner.save()
12681269

12691270
self.new_params = {
1270-
"new_plan": "plan_H6P3KZXwmAbqPS",
1271+
"new_plan": "plan_pro_yearly",
12711272
"new_quantity": 7,
12721273
"subscription_id": "sub_123",
12731274
}
@@ -1288,7 +1289,8 @@ def test_subscription_schedule_released_updates_owner_with_existing_subscription
12881289
)
12891290

12901291
self.owner.refresh_from_db()
1291-
assert self.owner.plan == settings.STRIPE_PLAN_VALS[self.new_params["new_plan"]]
1292+
plan = Plan.objects.get(stripe_id=self.new_params["new_plan"])
1293+
assert self.owner.plan == plan.name
12921294
assert self.owner.plan_user_count == self.new_params["new_quantity"]
12931295

12941296
@patch("services.billing.stripe.Subscription.retrieve")
@@ -1304,7 +1306,7 @@ def test_subscription_schedule_released_updates_multiple_owners_with_existing_su
13041306
self.other_owner.save()
13051307

13061308
self.new_params = {
1307-
"new_plan": "plan_H6P3KZXwmAbqPS",
1309+
"new_plan": "plan_pro_yearly",
13081310
"new_quantity": 7,
13091311
"subscription_id": "sub_123",
13101312
}
@@ -1326,12 +1328,11 @@ def test_subscription_schedule_released_updates_multiple_owners_with_existing_su
13261328

13271329
self.owner.refresh_from_db()
13281330
self.other_owner.refresh_from_db()
1329-
assert self.owner.plan == settings.STRIPE_PLAN_VALS[self.new_params["new_plan"]]
1331+
1332+
plan = Plan.objects.get(stripe_id=self.new_params["new_plan"])
1333+
assert self.owner.plan == plan.name
13301334
assert self.owner.plan_user_count == self.new_params["new_quantity"]
1331-
assert (
1332-
self.other_owner.plan
1333-
== settings.STRIPE_PLAN_VALS[self.new_params["new_plan"]]
1334-
)
1335+
assert self.other_owner.plan == plan.name
13351336
assert self.other_owner.plan_user_count == self.new_params["new_quantity"]
13361337

13371338
@patch("logging.Logger.error")
@@ -1342,7 +1343,7 @@ def test_subscription_schedule_released_logs_error_if_owner_does_not_exist(
13421343
log_error_mock,
13431344
):
13441345
self.new_params = {
1345-
"new_plan": "plan_H6P3KZXwmAbqPS",
1346+
"new_plan": "plan_pro_yearly",
13461347
"new_quantity": 7,
13471348
"subscription_id": "sub_notexist",
13481349
}
@@ -1367,7 +1368,7 @@ def test_subscription_schedule_released_logs_error_if_owner_does_not_exist(
13671368
extra={
13681369
"stripe_subscription_id": "sub_notexist",
13691370
"stripe_customer_id": "cus_123",
1370-
"plan_id": "plan_H6P3KZXwmAbqPS",
1371+
"plan_id": "plan_pro_yearly",
13711372
},
13721373
)
13731374

@@ -1383,7 +1384,7 @@ def test_subscription_schedule_created_logs_a_new_schedule(
13831384
self.owner.save()
13841385

13851386
self.params = {
1386-
"new_plan": "plan_H6P3KZXwmAbqPS",
1387+
"new_plan": "plan_pro_yearly",
13871388
"new_quantity": 7,
13881389
"subscription_id": subscription_id,
13891390
}
@@ -1410,7 +1411,7 @@ def test_subscription_schedule_updated_logs_changes_to_schedule(
14101411
original_plan = "users-pr-inappy"
14111412
original_quantity = 10
14121413
subscription_id = "sub_1K8xfkGlVGuVgOrkxvroyZdH"
1413-
new_plan = "plan_H6P3KZXwmAbqPS"
1414+
new_plan = "plan_pro_yearly"
14141415
new_quantity = 7
14151416
self.owner.plan = original_plan
14161417
self.owner.plan_user_count = original_quantity

0 commit comments

Comments
 (0)