corporate: Use check_int_in in change_plan_status.

This commit is contained in:
Vishnu KS 2021-04-14 19:20:40 +05:30 committed by Tim Abbott
parent 752fd2e2d1
commit d88f1103b7
2 changed files with 25 additions and 8 deletions

View File

@ -2299,6 +2299,17 @@ class StripeTest(StripeTestCase):
self.assertEqual(old_plan.next_invoice_date, None)
self.assertEqual(old_plan.status, CustomerPlan.ENDED)
def test_update_plan_with_invalid_status(self, *mocks: Mock) -> None:
with patch("corporate.lib.stripe.timezone_now", return_value=self.now):
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, "token")
self.login_user(self.example_user("hamlet"))
response = self.client_post(
"/json/billing/plan/change",
{"status": CustomerPlan.NEVER_STARTED},
)
self.assert_json_error_contains(response, "Invalid status")
def test_deactivate_realm(self) -> None:
user = self.example_user("hamlet")
with patch("corporate.lib.stripe.timezone_now", return_value=self.now):

View File

@ -46,7 +46,7 @@ from zerver.decorator import (
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success
from zerver.lib.send_email import FromAddress, send_email
from zerver.lib.validator import check_int, check_string_in
from zerver.lib.validator import check_int, check_int_in, check_string_in
from zerver.models import UserProfile, get_realm
billing_logger = logging.getLogger("corporate.stripe")
@ -357,14 +357,20 @@ def billing_home(request: HttpRequest) -> HttpResponse:
@require_billing_access
@has_request_variables
def change_plan_status(
request: HttpRequest, user: UserProfile, status: int = REQ("status", json_validator=check_int)
request: HttpRequest,
user: UserProfile,
status: int = REQ(
"status",
json_validator=check_int_in(
[
CustomerPlan.ACTIVE,
CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE,
CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE,
CustomerPlan.ENDED,
]
),
),
) -> HttpResponse:
assert status in [
CustomerPlan.ACTIVE,
CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE,
CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE,
CustomerPlan.ENDED,
]
plan = get_current_plan_by_realm(user.realm)
assert plan is not None # for mypy