From f684d36710e6bdeadac2daddb52818e0c93d85d5 Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Tue, 21 Mar 2023 18:41:44 +0530 Subject: [PATCH] test_classes: Add submit_realm_creation_form helper. This commit adds a new helper submit_realm_creation_form, similar to existing submit_reg_form_for_user, to avoid duplicate code for creating realms in tests. --- analytics/tests/test_support_views.py | 10 +- zerver/lib/test_classes.py | 22 +++ zerver/tests/test_external.py | 22 +-- zerver/tests/test_signup.py | 236 +++++--------------------- 4 files changed, 76 insertions(+), 214 deletions(-) diff --git a/analytics/tests/test_support_views.py b/analytics/tests/test_support_views.py index 11fac2de3d..74b457b4c4 100644 --- a/analytics/tests/test_support_views.py +++ b/analytics/tests/test_support_views.py @@ -302,14 +302,8 @@ class TestSupportEndpoint(ZulipTestCase): check_zulip_realm_query_result(result) email = self.nonreg_email("alice") - self.client_post( - "/new/", - { - "email": email, - "realm_name": "Zulip test", - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": "zuliptest", - }, + self.submit_realm_creation_form( + email, realm_subdomain="zuliptest", realm_name="Zulip test" ) result = get_check_query_result(email, 1) check_realm_creation_query_result(result, email) diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index bfdd1d9f54..12b691b2c5 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -802,6 +802,28 @@ Output: **extra, ) + def submit_realm_creation_form( + self, + email: str, + *, + realm_subdomain: str, + realm_name: str, + realm_type: int = Realm.ORG_TYPES["business"]["id"], + realm_in_root_domain: Optional[str] = None, + ) -> "TestHttpResponse": + payload = { + "email": email, + "realm_name": realm_name, + "realm_type": realm_type, + "realm_subdomain": realm_subdomain, + } + if realm_in_root_domain is not None: + payload["realm_in_root_domain"] = realm_in_root_domain + return self.client_post( + "/new/", + payload, + ) + def get_confirmation_url_from_outbox( self, email_address: str, diff --git a/zerver/tests/test_external.py b/zerver/tests/test_external.py index 1f35de2ea9..655f5f2cc6 100644 --- a/zerver/tests/test_external.py +++ b/zerver/tests/test_external.py @@ -24,7 +24,7 @@ from zerver.lib.rate_limiter import ( ) from zerver.lib.test_classes import ZulipTestCase from zerver.lib.zephyr import compute_mit_user_fullname -from zerver.models import PushDeviceToken, Realm, UserProfile +from zerver.models import PushDeviceToken, UserProfile if settings.ZILENCER_ENABLED: from zilencer.models import RateLimitedRemoteZulipServer, RemoteZulipServer @@ -223,14 +223,8 @@ class RateLimitTests(ZulipTestCase): def test_create_realm_rate_limiting(self) -> None: with self.settings(OPEN_REALM_CREATION=True): self.do_test_hit_ratelimits( - lambda: self.client_post( - "/new/", - { - "email": "new@zulip.com", - "realm_name": "Zulip test", - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": "zuliptest", - }, + lambda: self.submit_realm_creation_form( + email="new@zulip.com", realm_subdomain="zuliptest", realm_name="Zulip test" ), is_json=False, ) @@ -296,14 +290,8 @@ class RateLimitTests(ZulipTestCase): nonlocal request_count request_count += 1 if request_count % 2 == 1: - return self.client_post( - "/new/", - { - "email": "new@zulip.com", - "realm_name": "Zulip test", - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": "zuliptest", - }, + return self.submit_realm_creation_form( + email="new@zulip.com", realm_subdomain="zuliptest", realm_name="Zulip test" ) else: return self.client_post("/accounts/find/", {"emails": "new@zulip.com"}) diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index 0097ceaeb5..22eb069271 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -1259,15 +1259,10 @@ class RealmCreationTest(ZulipTestCase): get_realm(string_id) # Create new realm with the email - result = self.client_post( - "/new/", - { - "email": email, - "realm_name": org_name, - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": string_id, - }, + result = self.submit_realm_creation_form( + email, realm_subdomain=string_id, realm_name=org_name ) + self.assertEqual(result.status_code, 302) self.assertTrue( result["Location"].endswith( @@ -1367,14 +1362,8 @@ class RealmCreationTest(ZulipTestCase): ) def test_create_realm_as_system_bot(self) -> None: - result = self.client_post( - "/new/", - { - "email": "notification-bot@zulip.com", - "realm_name": "Zulip test", - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": "zuliptest", - }, + result = self.submit_realm_creation_form( + email="notification-bot@zulip.com", realm_subdomain="zuliptest", realm_name="Zulip test" ) self.assertEqual(result.status_code, 200) self.assert_in_response("notification-bot@zulip.com is reserved for system bots", result) @@ -1388,14 +1377,8 @@ class RealmCreationTest(ZulipTestCase): with self.settings(OPEN_REALM_CREATION=False): # Create new realm with the email, but no creation key. - result = self.client_post( - "/new/", - { - "email": email, - "realm_name": "Zulip test", - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": "zuliptest", - }, + result = self.submit_realm_creation_form( + email, realm_subdomain="zuliptest", realm_name="Zulip test" ) self.assertEqual(result.status_code, 200) self.assert_in_response("Organization creation link required", result) @@ -1412,14 +1395,8 @@ class RealmCreationTest(ZulipTestCase): get_realm(string_id) # Create new realm with the email - result = self.client_post( - "/new/", - { - "email": email, - "realm_name": realm_name, - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": string_id, - }, + result = self.submit_realm_creation_form( + email, realm_subdomain=string_id, realm_name=realm_name ) self.assertEqual(result.status_code, 302) self.assertTrue( @@ -1464,14 +1441,8 @@ class RealmCreationTest(ZulipTestCase): get_realm(string_id) # Create new realm with the email - result = self.client_post( - "/new/", - { - "email": email, - "realm_name": realm_name, - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": string_id, - }, + result = self.submit_realm_creation_form( + email, realm_subdomain=string_id, realm_name=realm_name ) self.assertEqual(result.status_code, 302) self.assertTrue( @@ -1519,14 +1490,8 @@ class RealmCreationTest(ZulipTestCase): get_realm(string_id) # Create new realm with the email - result = self.client_post( - "/new/", - { - "email": email, - "realm_name": realm_name, - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": string_id, - }, + result = self.submit_realm_creation_form( + email, realm_subdomain=string_id, realm_name=realm_name ) self.assertEqual(result.status_code, 302) self.assertTrue( @@ -1585,14 +1550,8 @@ class RealmCreationTest(ZulipTestCase): get_realm(string_id) # Create new realm with the email - result = self.client_post( - "/new/", - { - "email": email, - "realm_name": "Zulip test", - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": string_id, - }, + result = self.submit_realm_creation_form( + email, realm_subdomain=string_id, realm_name="Zulip test" ) self.assertEqual(result.status_code, 302) self.assertTrue( @@ -1636,14 +1595,8 @@ class RealmCreationTest(ZulipTestCase): realm_name = "Test" # Create new realm with the email - result = self.client_post( - "/new/", - { - "email": email, - "realm_name": realm_name, - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": string_id, - }, + result = self.submit_realm_creation_form( + email, realm_subdomain=string_id, realm_name=realm_name ) self.assertEqual(result.status_code, 302) self.assertTrue( @@ -1683,14 +1636,8 @@ class RealmCreationTest(ZulipTestCase): realm_name = "Test" # Create new realm with the email - result = self.client_post( - "/new/", - { - "email": email, - "realm_name": realm_name, - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": string_id, - }, + result = self.submit_realm_creation_form( + email, realm_subdomain=string_id, realm_name=realm_name ) self.assertEqual(result.status_code, 302) self.assertTrue( @@ -1733,15 +1680,10 @@ class RealmCreationTest(ZulipTestCase): with self.assertRaises(Realm.DoesNotExist): get_realm(string_id) - result = self.client_post( - "/new/", - { - "email": email, - "realm_name": realm_name, - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": string_id, - }, + result = self.submit_realm_creation_form( + email, realm_subdomain=string_id, realm_name=realm_name ) + self.assertEqual(result.status_code, 302) self.assertTrue( result["Location"].endswith( @@ -1793,14 +1735,8 @@ class RealmCreationTest(ZulipTestCase): get_realm(second_string_id) # Now we pre-generate two realm creation links - result = self.client_post( - "/new/", - { - "email": email, - "realm_name": first_realm_name, - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": first_string_id, - }, + result = self.submit_realm_creation_form( + email, realm_subdomain=first_string_id, realm_name=first_realm_name ) self.assertEqual(result.status_code, 302) self.assertTrue( @@ -1813,15 +1749,8 @@ class RealmCreationTest(ZulipTestCase): first_confirmation_url = self.get_confirmation_url_from_outbox(email) self.assertEqual(PreregistrationRealm.objects.filter(email=email, status=0).count(), 1) - # Get a second realm creation link. - result = self.client_post( - "/new/", - { - "email": email, - "realm_name": second_realm_name, - "realm_type": Realm.ORG_TYPES["business"]["id"], - "realm_subdomain": second_string_id, - }, + result = self.submit_realm_creation_form( + email, realm_subdomain=second_string_id, realm_name=second_realm_name ) self.assertEqual(result.status_code, 302) self.assertTrue( @@ -1876,25 +1805,13 @@ class RealmCreationTest(ZulipTestCase): @override_settings(OPEN_REALM_CREATION=True) def test_invalid_email_signup(self) -> None: - result = self.client_post( - "/new/", - { - "email": "