Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit d318691

Browse files
authored
chore: Move worker billing functionality to shared (#436)
* chore: Move worker billing functionality to shared * why the log? * update the tests * update the tests 2 * Refactor tests * Update to fn based tests, easier and simpler to read
1 parent c481846 commit d318691

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

shared/billing/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
from enum import Enum
22

3+
from django.conf import settings
4+
5+
from shared.license import get_current_license
6+
37

48
class BillingPlan(Enum):
9+
users_ghm = "users"
510
users_monthly = "users-inappm"
611
users_yearly = "users-inappy"
712
users_free = "users-free"
813
users_basic = "users-basic"
14+
users_trial = "users-trial"
915
pr_monthly = "users-pr-inappm"
1016
pr_yearly = "users-pr-inappy"
1117
enterprise_cloud_yearly = "users-enterprisey"
1218
enterprise_cloud_monthly = "users-enterprisem"
19+
team_monthly = "users-teamm"
20+
team_yearly = "users-teamy"
1321

1422
def __init__(self, db_name):
1523
self.db_name = db_name
@@ -26,3 +34,21 @@ def is_enterprise_cloud_plan(plan: BillingPlan) -> bool:
2634
BillingPlan.enterprise_cloud_monthly,
2735
BillingPlan.enterprise_cloud_yearly,
2836
]
37+
38+
39+
def is_pr_billing_plan(plan: str) -> bool:
40+
if not settings.IS_ENTERPRISE:
41+
return plan in [
42+
BillingPlan.pr_monthly.value,
43+
BillingPlan.pr_yearly.value,
44+
BillingPlan.users_free.value,
45+
BillingPlan.users_basic.value,
46+
BillingPlan.users_trial.value,
47+
BillingPlan.enterprise_cloud_monthly.value,
48+
BillingPlan.enterprise_cloud_yearly.value,
49+
BillingPlan.team_monthly.value,
50+
BillingPlan.team_yearly.value,
51+
BillingPlan.users_ghm.value,
52+
]
53+
else:
54+
return get_current_license().is_pr_billing

tests/unit/billing/test_enum_definitions.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,64 @@
1-
from shared.billing import BillingPlan, is_enterprise_cloud_plan
1+
import pytest
2+
from django.test import override_settings
3+
4+
from shared.billing import BillingPlan, is_enterprise_cloud_plan, is_pr_billing_plan
5+
from shared.django_apps.codecov_auth.tests.factories import OwnerFactory
6+
7+
8+
@pytest.fixture
9+
def dbsession(db):
10+
return db
11+
12+
13+
@override_settings(IS_ENTERPRISE=False)
14+
def test_pr_author_plan_check(dbsession, mock_configuration):
15+
owner = OwnerFactory(service="github", plan="users-pr-inappm")
16+
if dbsession is not None:
17+
dbsession.add(owner)
18+
dbsession.flush()
19+
assert is_pr_billing_plan(owner.plan)
20+
21+
22+
@override_settings(IS_ENTERPRISE=True)
23+
def test_pr_author_enterprise_plan_check(dbsession, mock_configuration):
24+
owner = OwnerFactory(service="github")
25+
if dbsession is not None:
26+
dbsession.add(owner)
27+
dbsession.flush()
28+
29+
encrypted_license = "wxWEJyYgIcFpi6nBSyKQZQeaQ9Eqpo3SXyUomAqQOzOFjdYB3A8fFM1rm+kOt2ehy9w95AzrQqrqfxi9HJIb2zLOMOB9tSy52OykVCzFtKPBNsXU/y5pQKOfV7iI3w9CHFh3tDwSwgjg8UsMXwQPOhrpvl2GdHpwEhFdaM2O3vY7iElFgZfk5D9E7qEnp+WysQwHKxDeKLI7jWCnBCBJLDjBJRSz0H7AfU55RQDqtTrnR+rsLDHOzJ80/VxwVYhb"
30+
mock_configuration.params["setup"]["enterprise_license"] = encrypted_license
31+
mock_configuration.params["setup"]["codecov_dashboard_url"] = (
32+
"https://codecov.mysite.com"
33+
)
34+
35+
assert is_pr_billing_plan(owner.plan)
36+
37+
38+
@override_settings(IS_ENTERPRISE=False)
39+
def test_plan_not_pr_author(dbsession, mock_configuration):
40+
owner = OwnerFactory(service="github", plan=BillingPlan.users_monthly.value)
41+
if dbsession is not None:
42+
dbsession.add(owner)
43+
dbsession.flush()
44+
45+
assert not is_pr_billing_plan(owner.plan)
46+
47+
48+
@override_settings(IS_ENTERPRISE=True)
49+
def test_pr_author_enterprise_plan_check_non_pr_plan(dbsession, mock_configuration):
50+
owner = OwnerFactory(service="github")
51+
if dbsession is not None:
52+
dbsession.add(owner)
53+
dbsession.flush()
54+
55+
encrypted_license = "0dRbhbzp8TVFQp7P4e2ES9lSfyQlTo8J7LQ"
56+
mock_configuration.params["setup"]["enterprise_license"] = encrypted_license
57+
mock_configuration.params["setup"]["codecov_dashboard_url"] = (
58+
"https://codeov.mysite.com"
59+
)
60+
61+
assert not is_pr_billing_plan(owner.plan)
262

363

464
def test_billing_enums():
@@ -10,6 +70,8 @@ def test_billing_enums():
1070
assert BillingPlan.pr_yearly.db_name == "users-pr-inappy"
1171
assert BillingPlan.enterprise_cloud_yearly.db_name == "users-enterprisey"
1272
assert BillingPlan.enterprise_cloud_monthly.db_name == "users-enterprisem"
73+
assert BillingPlan.team_monthly.db_name == "users-teamm"
74+
assert BillingPlan.team_yearly.db_name == "users-teamy"
1375

1476

1577
def test_get_from_string():
@@ -26,6 +88,8 @@ def test_get_from_string():
2688
BillingPlan.from_str("users-enterprisem")
2789
== BillingPlan.enterprise_cloud_monthly
2890
)
91+
assert BillingPlan.from_str("users-teamm") == BillingPlan.team_monthly
92+
assert BillingPlan.from_str("users-teamy") == BillingPlan.team_yearly
2993

3094

3195
def test_is_enterprise_cloud_plan():

0 commit comments

Comments
 (0)