test_decorators: Call mock_home via patched zulip_login_required.

This avoids a weird issue that when mocking both
zulip_login_required and home_real, home_realm doesn't stay
patched.
This commit is contained in:
Aman Agrawal 2021-10-03 17:40:56 +05:30 committed by Tim Abbott
parent 16d35f5b4e
commit 28488d9939

View File

@ -2079,27 +2079,31 @@ class TestRequestNotes(ZulipTestCase):
return inner
zulip_realm = get_realm("zulip")
with mock.patch("zerver.views.home.home_real", new=mock_home(zulip_realm)):
# We don't need to test if user is logged in here, so we patch zulip_login_required.
with mock.patch("zerver.views.home.zulip_login_required", lambda f: mock_home(zulip_realm)):
result = self.client_get("/", subdomain="zulip")
self.assertEqual(result.status_code, 200)
# When a request is made to the root subdomain and there is no realm on it,
# no realm can be set on the request notes.
with mock.patch("zerver.views.home.home_real", new=mock_home(None)):
with mock.patch("zerver.views.home.zulip_login_required", lambda f: mock_home(None)):
result = self.client_get("/", subdomain="")
self.assertEqual(result.status_code, 200)
root_subdomain_realm = do_create_realm("", "Root Domain")
# Now test that that realm does get set, if it exists, for requests
# to the root subdomain.
with mock.patch("zerver.views.home.home_real", new=mock_home(root_subdomain_realm)):
with mock.patch(
"zerver.views.home.zulip_login_required", lambda f: mock_home(root_subdomain_realm)
):
result = self.client_get("/", subdomain="")
self.assertEqual(result.status_code, 200)
# Only the root subdomain allows requests to it without having a realm.
# Requests to non-root subdomains get stopped by the middleware and
# an error page is returned before the request hits the view.
with mock.patch("zerver.views.home.home_real") as mock_home_real:
with mock.patch("zerver.views.home.zulip_login_required") as mock_home_real:
result = self.client_get("/", subdomain="invalid")
self.assertEqual(result.status_code, 404)
self.assert_in_response(