Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions apps/api/test_user_settings_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.contrib.auth.models import User
from django.test import TestCase

from apps.core.choices import ProfileStates


class UserSettingsApiTestCase(TestCase):
def setUp(self):
self.user = User.objects.create_user(
username="settings-user",
email="settings@example.com",
password="pass123",
)
self.client.force_login(self.user)

def test_user_settings_returns_false_when_user_has_no_active_subscription(self):
response = self.client.get("/api/user/settings")

self.assertEqual(response.status_code, 200)
self.assertEqual(
response.json(),
{"profile": {"has_pro_subscription": False}},
)

def test_user_settings_returns_true_when_user_is_subscribed(self):
profile = self.user.profile
profile.state = ProfileStates.SUBSCRIBED
profile.save(update_fields=["state", "updated_at"])

response = self.client.get("/api/user/settings")

self.assertEqual(response.status_code, 200)
self.assertEqual(
response.json(),
{"profile": {"has_pro_subscription": True}},
)
10 changes: 9 additions & 1 deletion apps/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ def current_state(self):
latest_transition = self.state_transitions.latest("created_at")
return latest_transition.to_state


@property
def has_active_subscription(self):
return self.state in [
ProfileStates.TRIAL_STARTED,
ProfileStates.SUBSCRIBED,
ProfileStates.CANCELLED,
] or (self.user.is_superuser and settings.ENVIRONMENT == "prod")


class ProfileStateTransition(BaseModel):
profile = models.ForeignKey(
Profile,
Expand Down