Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
remote_billing: Remove unnecessary hop from the first-time-login flow.
Mateusz Mandera0424191af54f
diff --git a/corporate/tests/test_remote_billing.py b/corporate/tests/test_remote_billing.pyindex 9b7ee41d66f..b0399e6ef6a 100644--- a/corporate/tests/test_remote_billing.py+++ b/corporate/tests/test_remote_billing.py@@ -130,14 +130,6 @@ class RemoteRealmBillingTestCase(BouncerTestCase):self.assertEqual(prereg_user.created_user, remote_billing_user)self.assertEqual(remote_billing_user.date_joined, now)- # Now we should be redirected again to the /remote-billing-login/ endpoint- # with a new signed_access_token. Now that the email has been confirmed,- # and we have a RemoteRealmBillingUser entry, we'll be in the same position- # as the case where first_time_login=False.- self.assertEqual(result.status_code, 302)- self.assertTrue(result["Location"].startswith("/remote-billing-login/"))- result = self.client_get(result["Location"], subdomain="selfhosting")-# Final confirmation page - just confirm your details, possibly# agreeing to ToS if needed and an authenticated session will be granted:self.assertEqual(result.status_code, 200)@@ -691,8 +683,8 @@ class RemoteBillingAuthenticationTest(RemoteRealmBillingTestCase):# Click the first confirmation link.with time_machine.travel(now, tick=False):result = self.client_get(first_confirmation_url, subdomain="selfhosting")- self.assertEqual(result.status_code, 302)- self.assertTrue(result["Location"].startswith("/remote-billing-login/"))+ self.assertEqual(result.status_code, 200)+ self.assert_in_success_response(["Log in to Zulip plan management"], result)# This created the RemoteRealmBillingUser entry.remote_billing_user = RemoteRealmBillingUser.objects.latest("id")@@ -703,15 +695,15 @@ class RemoteBillingAuthenticationTest(RemoteRealmBillingTestCase):self.assertEqual(first_prereg_user.created_user, remote_billing_user)# Now click the second confirmation link. The RemoteRealmBillingUser entry- # stays the same, since it's already been created, and the user is redirected+ # stays the same, since it's already been created, and the user proceeds# normally further through the flow, while we log this event.with (time_machine.travel(now + timedelta(seconds=1), tick=False),self.assertLogs("corporate.stripe", "INFO") as mock_logger,):result = self.client_get(second_confirmation_url, subdomain="selfhosting")- self.assertEqual(result.status_code, 302)- self.assertTrue(result["Location"].startswith("/remote-billing-login/"))+ self.assertEqual(result.status_code, 200)+ self.assert_in_success_response(["Log in to Zulip plan management"], result)# The RemoteRealmBillingUser entry stays the same.self.assertEqual(RemoteRealmBillingUser.objects.latest("id"), remote_billing_user)diff --git a/corporate/views/remote_billing_page.py b/corporate/views/remote_billing_page.pyindex 9eee2817091..cf033121de0 100644--- a/corporate/views/remote_billing_page.py+++ b/corporate/views/remote_billing_page.py@@ -32,6 +32,7 @@ from corporate.lib.remote_billing_util import (RemoteBillingUserDict,get_remote_server_and_user_from_session,)+from corporate.lib.stripe import BILLING_SUPPORT_EMAIL, RemoteRealmBillingSessionfrom corporate.models.customers import get_customer_by_remote_serverfrom corporate.models.plans import CustomerPlan, get_current_plan_by_customerfrom zerver.lib.exceptions import (@@ -160,11 +161,9 @@ def remote_realm_billing_finalize_login(This is the endpoint accessed via the billing_access_url, generated byremote_realm_billing_entry entry."""- from corporate.lib.stripe import BILLING_SUPPORT_EMAIL, RemoteRealmBillingSessionif request.method not in ["GET", "POST"]:return HttpResponseNotAllowed(["GET", "POST"])- tos_consent_given = tos_consent == "true"# Sanity assert, because otherwise these make no sense.assert (@@ -173,7 +172,25 @@ def remote_realm_billing_finalize_login()identity_dict = get_identity_dict_from_signed_access_token(signed_billing_access_token)+ return remote_realm_billing_finalize_login_core(+ request,+ identity_dict=identity_dict,+ full_name=full_name,+ tos_consent_given=tos_consent == "true",+ enable_major_release_emails=enable_major_release_emails == "true",+ enable_maintenance_release_emails=enable_maintenance_release_emails == "true",+ )++def remote_realm_billing_finalize_login_core(+ request: HttpRequest,+ *,+ identity_dict: RemoteBillingIdentityDict,+ full_name: str | None = None,+ tos_consent_given: bool = False,+ enable_major_release_emails: bool = False,+ enable_maintenance_release_emails: bool = False,+) -> HttpResponse:# Now we want to fetch the RemoteRealmBillingUser object implied# by the IdentityDict. We'll use this:# (1) If the user came here via just GET, we want to show them a confirmation@@ -248,6 +265,7 @@ def remote_realm_billing_finalize_login(tos_consent_needed = Trueif request.method == "GET":+ signed_billing_access_token = signing.dumps(identity_dict)if remote_user is not None:# Render a template where the user will just confirm their info,# possibly accept ToS if needed, POST back here and will get fully@@ -301,8 +319,8 @@ def remote_realm_billing_finalize_login(# only when the user first logs in.if full_name is not None:remote_user.full_name = full_name- remote_user.enable_major_release_emails = enable_major_release_emails == "true"- remote_user.enable_maintenance_release_emails = enable_maintenance_release_emails == "true"+ remote_user.enable_major_release_emails = enable_major_release_emails+ remote_user.enable_maintenance_release_emails = enable_maintenance_release_emailsremote_user.tos_version = settings.TERMS_OF_SERVICE_VERSIONremote_user.last_login = timezone_now()@@ -484,11 +502,7 @@ def remote_realm_billing_from_login_confirmation_link(next_page=next_page,)- signed_identity_dict = signing.dumps(identity_dict)-- return HttpResponseRedirect(- reverse(remote_realm_billing_finalize_login, args=[signed_identity_dict])- )+ return remote_realm_billing_finalize_login_core(request, identity_dict=identity_dict)def create_remote_billing_confirmation_link(