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`.
This commit is contained in:
Aman Agrawal 2021-10-06 17:41:48 +05:30 committed by Tim Abbott
parent 2a922409aa
commit 00d3b88257
4 changed files with 24 additions and 6 deletions

View File

@ -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

View File

@ -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")

View File

@ -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

View File

@ -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