From 00d3b88257988283fd45b06d3de0b24f988c1c03 Mon Sep 17 00:00:00 2001 From: Aman Agrawal Date: Wed, 6 Oct 2021 17:41:48 +0530 Subject: [PATCH] realm: Enable spectator access by default in development. Enable spectator access for test `zulip` realm in developement setup. Add option in `do_create_realm` to configure `enable_spectator_access` field of `Realm`. --- zerver/lib/actions.py | 3 +++ zerver/tests/test_home.py | 19 +++++++++++++------ zerver/tests/test_realm.py | 7 +++++++ zilencer/management/commands/populate_db.py | 1 + 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index b9476a3f0e..48b2ea79f7 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -5067,6 +5067,7 @@ def do_create_realm( org_type: Optional[int] = None, date_created: Optional[datetime.datetime] = None, is_demo_organization: Optional[bool] = False, + enable_spectator_access: Optional[bool] = False, ) -> Realm: if string_id == settings.SOCIAL_AUTH_SUBDOMAIN: raise AssertionError("Creating a realm on SOCIAL_AUTH_SUBDOMAIN is not allowed!") @@ -5089,6 +5090,8 @@ def do_create_realm( kwargs["plan_type"] = plan_type if org_type is not None: kwargs["org_type"] = org_type + if enable_spectator_access is not None: + kwargs["enable_spectator_access"] = enable_spectator_access if date_created is not None: # The date_created parameter is intended only for use by test diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py index 6529cb0cb0..0a819c790d 100644 --- a/zerver/tests/test_home.py +++ b/zerver/tests/test_home.py @@ -313,11 +313,21 @@ class HomeTest(ZulipTestCase): self.assertEqual(result.url, "/login/") # Tell server that user wants to login anonymously - # Redirects to load webapp. Since Realm.enable_spectator_access - # is False, the login should fail. + # Redirects to load webapp. realm = get_realm("zulip") result = self.client_post("/", {"prefers_web_public_view": "true"}) - self.assertEqual(self.client.session.get("prefers_web_public_view"), None) + self.assertEqual(self.client.session.get("prefers_web_public_view"), True) + self.assertEqual(realm.enable_spectator_access, True) + self.assertEqual(result.status_code, 302) + self.assertEqual(result.url, "http://zulip.testserver") + + # Disable spectator login. Since Realm.enable_spectator_access + # is False, the login should fail. + realm.enable_spectator_access = False + realm.save() + + result = self.client_post("/", {"prefers_web_public_view": "true"}) + self.assertEqual(self.client.session.get("prefers_web_public_view"), True) self.assertEqual(realm.enable_spectator_access, False) self.assertEqual(result.status_code, 302) self.assertEqual(result.url, "/login/") @@ -352,9 +362,6 @@ class HomeTest(ZulipTestCase): self.client_get("/") self.assertEqual(self.client.session.get("prefers_web_public_view"), None) - realm.enable_spectator_access = False - realm.save() - def test_home_under_2fa_without_otp_device(self) -> None: with self.settings(TWO_FACTOR_AUTHENTICATION_ENABLED=True): self.login("iago") diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py index 564da1cb2f..a7dcf182e5 100644 --- a/zerver/tests/test_realm.py +++ b/zerver/tests/test_realm.py @@ -93,6 +93,13 @@ class RealmTest(ZulipTestCase): self.assertEqual(realm.user_group_edit_policy, Realm.POLICY_MODERATORS_ONLY) self.assertEqual(realm.invite_to_stream_policy, Realm.POLICY_MODERATORS_ONLY) + def test_realm_enable_spectator_access(self) -> None: + realm = do_create_realm("test_web_public_true", "Foo", enable_spectator_access=True) + self.assertEqual(realm.enable_spectator_access, True) + + realm = do_create_realm("test_web_public_false", "Boo", enable_spectator_access=False) + self.assertEqual(realm.enable_spectator_access, False) + def test_do_set_realm_name_caching(self) -> None: """The main complicated thing about setting realm names is fighting the cache, and we start by populating the cache for Hamlet, and we end diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index 2fea0e1584..1ad08b59ec 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -314,6 +314,7 @@ class Command(BaseCommand): invite_required=False, plan_type=Realm.PLAN_TYPE_SELF_HOSTED, org_type=Realm.ORG_TYPES["business"]["id"], + enable_spectator_access=True, ) RealmDomain.objects.create(realm=zulip_realm, domain="zulip.com") assert zulip_realm.notifications_stream is not None