From 231f3fecc73a58b0152120ec46de2681d31ac994 Mon Sep 17 00:00:00 2001 From: Adrian Date: Tue, 12 Nov 2024 13:46:43 -0700 Subject: [PATCH] Revert "Temporarily disable trials starting for paid orgs (#972)" This reverts commit 2f4a6969e3c8d1a428ef2f26a655fbc10e6650a0. --- codecov_auth/tests/test_admin.py | 41 ++++++++++++++++---------------- plan/service.py | 13 +++++----- plan/tests/test_plan.py | 29 +++++++++++----------- 3 files changed, 40 insertions(+), 43 deletions(-) diff --git a/codecov_auth/tests/test_admin.py b/codecov_auth/tests/test_admin.py index 6f906bf07b..6b4316b787 100644 --- a/codecov_auth/tests/test_admin.py +++ b/codecov_auth/tests/test_admin.py @@ -25,7 +25,7 @@ ) from shared.django_apps.core.tests.factories import PullFactory, RepositoryFactory -# from codecov.commands.exceptions import ValidationError +from codecov.commands.exceptions import ValidationError from codecov_auth.admin import ( AccountAdmin, InvoiceBillingAdmin, @@ -337,26 +337,25 @@ def test_extend_trial_action(self, mock_start_trial_service): assert mock_start_trial_service.called assert mock_start_trial_service.call_args.kwargs == {"is_extension": True} - # Temporarily comment for the time being - # @patch("plan.service.PlanService.start_trial_manually") - # def test_start_trial_paid_plan(self, mock_start_trial_service): - # mock_start_trial_service.side_effect = ValidationError( - # "Cannot trial from a paid plan" - # ) - - # org_to_be_trialed = OwnerFactory() - - # res = self.client.post( - # reverse("admin:codecov_auth_owner_changelist"), - # { - # "action": "extend_trial", - # ACTION_CHECKBOX_NAME: [org_to_be_trialed.pk], - # "end_date": "2024-01-01 01:02:03", - # "extend_trial": True, - # }, - # ) - # assert res.status_code == 302 - # assert mock_start_trial_service.called + @patch("plan.service.PlanService.start_trial_manually") + def test_start_trial_paid_plan(self, mock_start_trial_service): + mock_start_trial_service.side_effect = ValidationError( + "Cannot trial from a paid plan" + ) + + org_to_be_trialed = OwnerFactory() + + res = self.client.post( + reverse("admin:codecov_auth_owner_changelist"), + { + "action": "extend_trial", + ACTION_CHECKBOX_NAME: [org_to_be_trialed.pk], + "end_date": "2024-01-01 01:02:03", + "extend_trial": True, + }, + ) + assert res.status_code == 302 + assert mock_start_trial_service.called def test_account_widget(self): owner = OwnerFactory(user=UserFactory(), plan="users-enterprisey") diff --git a/plan/service.py b/plan/service.py index c1c09d8291..0da4f2add3 100644 --- a/plan/service.py +++ b/plan/service.py @@ -219,16 +219,15 @@ def start_trial_manually(self, current_owner: Owner, end_date: datetime) -> None Returns: No value """ + # Start a new trial plan for free users currently not on trial + if self.plan_name in FREE_PLAN_REPRESENTATIONS: + self._start_trial_helper(current_owner, end_date, is_extension=False) # Extend an existing trial plan for users currently on trial - if self.plan_name in TRIAL_PLAN_REPRESENTATION: + elif self.plan_name in TRIAL_PLAN_REPRESENTATION: self._start_trial_helper(current_owner, end_date, is_extension=True) - # Start a new trial plan for any users, used to be free but temporarily doing it for anyone - # as long as the person extending the trial is responsible to manually returning the users - # the way they were before - else: - self._start_trial_helper(current_owner, end_date, is_extension=False) # Paying users cannot start a trial - # raise ValidationError("Cannot trial from a paid plan") + else: + raise ValidationError("Cannot trial from a paid plan") def cancel_trial(self) -> None: if not self.is_org_trialing: diff --git a/plan/tests/test_plan.py b/plan/tests/test_plan.py index b07d4dc6ae..70b6becf7a 100644 --- a/plan/tests/test_plan.py +++ b/plan/tests/test_plan.py @@ -226,21 +226,20 @@ def test_plan_service_start_trial_manually(self): assert current_org.plan_auto_activate == True assert current_org.trial_fired_by == current_owner.ownerid - # Temporarily comment this - # def test_plan_service_start_trial_manually_already_on_paid_plan(self): - # current_org = OwnerFactory( - # plan=PlanName.CODECOV_PRO_MONTHLY.value, - # trial_start_date=None, - # trial_end_date=None, - # trial_status=TrialStatus.NOT_STARTED.value, - # ) - # plan_service = PlanService(current_org=current_org) - # current_owner = OwnerFactory() - - # with self.assertRaises(ValidationError): - # plan_service.start_trial_manually( - # current_owner=current_owner, end_date="2024-01-01 00:00:00" - # ) + def test_plan_service_start_trial_manually_already_on_paid_plan(self): + current_org = OwnerFactory( + plan=PlanName.CODECOV_PRO_MONTHLY.value, + trial_start_date=None, + trial_end_date=None, + trial_status=TrialStatus.NOT_STARTED.value, + ) + plan_service = PlanService(current_org=current_org) + current_owner = OwnerFactory() + + with self.assertRaises(ValidationError): + plan_service.start_trial_manually( + current_owner=current_owner, end_date="2024-01-01 00:00:00" + ) def test_plan_service_returns_plan_data_for_non_trial_basic_plan(self): trial_start_date = None