From ea9e2ece49feee245dbdfc7c8c09e2a2b8296b7e Mon Sep 17 00:00:00 2001 From: Mateusz Mandera Date: Thu, 30 Nov 2023 22:31:08 +0100 Subject: [PATCH] remote_billing: Extract RemoteBillingUserDict sub-dict. --- corporate/lib/remote_billing_util.py | 6 +++++- corporate/tests/test_remote_billing.py | 10 ++++++---- corporate/views/remote_billing_page.py | 18 +++++++++++++----- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/corporate/lib/remote_billing_util.py b/corporate/lib/remote_billing_util.py index 5627a68a55..38054e18cb 100644 --- a/corporate/lib/remote_billing_util.py +++ b/corporate/lib/remote_billing_util.py @@ -10,10 +10,14 @@ from zilencer.models import RemoteRealm, RemoteZulipServer billing_logger = logging.getLogger("corporate.stripe") -class RemoteBillingIdentityDict(TypedDict): +class RemoteBillingUserDict(TypedDict): user_uuid: str user_email: str user_full_name: str + + +class RemoteBillingIdentityDict(TypedDict): + user: RemoteBillingUserDict remote_server_uuid: str remote_realm_uuid: str diff --git a/corporate/tests/test_remote_billing.py b/corporate/tests/test_remote_billing.py index 67e5cbb909..80e95b5a98 100644 --- a/corporate/tests/test_remote_billing.py +++ b/corporate/tests/test_remote_billing.py @@ -4,7 +4,7 @@ from unittest import mock import responses from django.test import override_settings -from corporate.lib.remote_billing_util import RemoteBillingIdentityDict +from corporate.lib.remote_billing_util import RemoteBillingIdentityDict, RemoteBillingUserDict from zerver.lib.remote_server import send_realms_only_to_push_bouncer from zerver.lib.test_classes import BouncerTestCase from zerver.models import UserProfile @@ -34,9 +34,11 @@ class RemoteBillingAuthenticationTest(BouncerTestCase): # Verify the authed data that should have been stored in the session. identity_dict = RemoteBillingIdentityDict( - user_email=user.delivery_email, - user_uuid=str(user.uuid), - user_full_name=user.full_name, + user=RemoteBillingUserDict( + user_email=user.delivery_email, + user_uuid=str(user.uuid), + user_full_name=user.full_name, + ), remote_server_uuid=str(self.server.uuid), remote_realm_uuid=str(user.realm.uuid), next_page=next_page, diff --git a/corporate/views/remote_billing_page.py b/corporate/views/remote_billing_page.py index b5b5fe9fd7..fdbdd437a5 100644 --- a/corporate/views/remote_billing_page.py +++ b/corporate/views/remote_billing_page.py @@ -15,6 +15,7 @@ from corporate.lib.decorator import self_hosting_management_endpoint from corporate.lib.remote_billing_util import ( LegacyServerIdentityDict, RemoteBillingIdentityDict, + RemoteBillingUserDict, get_identity_dict_from_session, ) from zerver.lib.exceptions import JsonableError, MissingRemoteRealmError @@ -51,9 +52,9 @@ def remote_server_billing_entry( raise MissingRemoteRealmError identity_dict = RemoteBillingIdentityDict( - user_email=user.email, - user_uuid=str(user.uuid), - user_full_name=user.full_name, + user=RemoteBillingUserDict( + user_email=user.email, user_uuid=str(user.uuid), user_full_name=user.full_name + ), remote_server_uuid=str(remote_server.uuid), remote_realm_uuid=str(remote_realm.uuid), next_page=next_page, @@ -118,10 +119,17 @@ def render_tmp_remote_billing_page( # This key should be set in both RemoteRealm and legacy server # login flows. remote_server_uuid = identity_dict["remote_server_uuid"] - user_email = identity_dict.get("user_email") - user_full_name = identity_dict.get("user_full_name") + remote_realm_uuid = identity_dict.get("remote_realm_uuid") + user_dict = identity_dict.get("user", {}) + # Mypy recognizes user_dict as "object" otherwise. + # If not empty, this is actually a RemoteBillingUserDict. + assert isinstance(user_dict, dict) + + user_email = user_dict.get("user_email") + user_full_name = user_dict.get("user_full_name") + try: remote_server = RemoteZulipServer.objects.get(uuid=remote_server_uuid) except RemoteZulipServer.DoesNotExist: