Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
billing: Require billing access to change payment methods.
Aman Agrawalbf28c82dc9b1
diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex f2c34521b7a..ad61988ddaf 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -5520,8 +5520,8 @@ class RequiresBillingAccessTest(StripeTestCase):)check_users_cant_access(- [guest],- "Must be an organization member",+ [guest, member],+ "Insufficient permission","/json/upgrade/session/start_card_update_session","POST",{},@@ -5567,6 +5567,45 @@ class RequiresBillingAccessTest(StripeTestCase):response = self.client_get("/billing/")self.assert_in_success_response(["You do not have permission to view this page."], response)+ def test_start_card_update_stripe_session_requires_billing_access(self) -> None:+ # Verify that only users with billing access can update card.+ guest = self.example_user("polonius")+ member = self.example_user("othello")+ realm_owner = self.example_user("desdemona")+ realm_owner.role = UserProfile.ROLE_REALM_OWNER+ realm_owner.save(update_fields=["role"])++ # Guest users should not have access+ self.login_user(guest)+ response = self.client_post(+ "/json/billing/session/start_card_update_session",+ {},+ content_type="application/json",+ )+ self.assert_json_error_contains(response, "Insufficient permission")++ # Regular members should not have access+ self.login_user(member)+ response = self.client_post(+ "/json/billing/session/start_card_update_session",+ {},+ content_type="application/json",+ )+ self.assert_json_error_contains(response, "Insufficient permission")++ # Realm owner should have access (they have billing access)+ self.login_user(realm_owner)+ with patch(+ "corporate.lib.stripe.RealmBillingSession.create_card_update_session",+ return_value={"stripe_session_id": "cs_test_session_id"},+ ):+ response = self.client_post(+ "/json/billing/session/start_card_update_session",+ {},+ content_type="application/json",+ )+ self.assert_json_success(response)+class BillingHelpersTest(ZulipTestCase):def test_next_month(self) -> None:diff --git a/corporate/views/session.py b/corporate/views/session.pyindex 75a4e5adb40..34011aeea40 100644--- a/corporate/views/session.py+++ b/corporate/views/session.py@@ -8,7 +8,7 @@ from corporate.lib.decorator import (authenticated_remote_realm_management_endpoint,authenticated_remote_server_management_endpoint,)-from zerver.decorator import require_billing_access, require_organization_member+from zerver.decorator import require_billing_accessfrom zerver.lib.response import json_successfrom zerver.lib.typed_endpoint import typed_endpointfrom zerver.models import UserProfile@@ -53,7 +53,7 @@ def start_card_update_stripe_session_for_remote_server()-@require_organization_member+@require_billing_access@typed_endpointdef start_card_update_stripe_session_for_realm_upgrade(request: HttpRequest,