Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

remote_billing: Handle two confirmation links for same user correctly.

Mateusz Mandera63254f18ec06
diff --git a/corporate/tests/test_remote_billing.py b/corporate/tests/test_remote_billing.py
index 1a199255e69..4b87bb43ff7 100644
--- a/corporate/tests/test_remote_billing.py
+++ b/corporate/tests/test_remote_billing.py
@@ -466,6 +466,88 @@ class RemoteBillingAuthenticationTest(RemoteRealmBillingTestCase):
# we can't do a nice redirect back to their original server.
self.assertEqual(result.status_code, 401)
+ @responses.activate
+ def test_remote_billing_authentication_flow_generate_two_confirmation_links_before_confirming(
+ self,
+ ) -> None:
+ self.login("desdemona")
+ desdemona = self.example_user("desdemona")
+
+ self.add_mock_response()
+
+ result = self.execute_remote_billing_authentication_flow(
+ desdemona,
+ expect_tos=True,
+ confirm_tos=False,
+ first_time_login=True,
+ return_without_clicking_confirmation_link=True,
+ )
+ self.assertEqual(result.status_code, 200)
+ first_confirmation_url = self.get_confirmation_url_from_outbox(
+ desdemona.delivery_email,
+ url_pattern=(
+ f"{settings.SELF_HOSTING_MANAGEMENT_SUBDOMAIN}.{settings.EXTERNAL_HOST}" + r"(\S+)"
+ ),
+ )
+ first_prereg_user = PreregistrationRemoteRealmBillingUser.objects.latest("id")
+
+ result = self.execute_remote_billing_authentication_flow(
+ desdemona,
+ expect_tos=True,
+ confirm_tos=False,
+ first_time_login=True,
+ return_without_clicking_confirmation_link=True,
+ )
+ self.assertEqual(result.status_code, 200)
+ second_confirmation_url = self.get_confirmation_url_from_outbox(
+ desdemona.delivery_email,
+ url_pattern=(
+ f"{settings.SELF_HOSTING_MANAGEMENT_SUBDOMAIN}.{settings.EXTERNAL_HOST}" + r"(\S+)"
+ ),
+ )
+ second_prereg_user = PreregistrationRemoteRealmBillingUser.objects.latest("id")
+
+ self.assertNotEqual(first_confirmation_url, second_confirmation_url)
+ self.assertNotEqual(first_prereg_user.id, second_prereg_user.id)
+
+ now = timezone_now()
+ # 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/"))
+
+ # This created the RemoteRealmBillingUser entry.
+ remote_billing_user = RemoteRealmBillingUser.objects.latest("id")
+ self.assertEqual(remote_billing_user.user_uuid, desdemona.uuid)
+ self.assertEqual(remote_billing_user.email, desdemona.delivery_email)
+
+ first_prereg_user.refresh_from_db()
+ 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
+ # 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/"))
+
+ # The RemoteRealmBillingUser entry stays the same.
+ self.assertEqual(RemoteRealmBillingUser.objects.latest("id"), remote_billing_user)
+ # The second prereg user is unused, since it wasn't needed.
+ self.assertEqual(second_prereg_user.created_user, None)
+
+ self.assertEqual(
+ mock_logger.output,
+ [
+ "INFO:corporate.stripe:Matching RemoteRealmBillingUser already exists for "
+ f"PreregistrationRemoteRealmBillingUser {second_prereg_user.id}"
+ ],
+ )
+
@responses.activate
def test_transfer_legacy_plan_from_server_to_all_realms(self) -> None:
self.login("desdemona")
diff --git a/corporate/views/remote_billing_page.py b/corporate/views/remote_billing_page.py
index 4a517615806..aa479ccaed6 100644
--- a/corporate/views/remote_billing_page.py
+++ b/corporate/views/remote_billing_page.py
@@ -448,11 +448,18 @@ def remote_realm_billing_from_login_confirmation_link(
# Mypy is not satisfied by the above assert, so we need to cast.
uri_scheme = cast(Literal["http://", "https://"], uri_scheme)
- remote_billing_user = RemoteRealmBillingUser.objects.create(
- email=prereg_object.email,
+ remote_billing_user, created = RemoteRealmBillingUser.objects.get_or_create(
remote_realm=remote_realm,
user_uuid=prereg_object.user_uuid,
+ defaults={"email": prereg_object.email},
)
+ if not created:
+ billing_logger.info(
+ "Matching RemoteRealmBillingUser already exists for "
+ "PreregistrationRemoteRealmBillingUser %s",
+ prereg_object.id,
+ )
+
prereg_object.created_user = remote_billing_user
prereg_object.save(update_fields=["created_user"])