mirror of
https://github.com/zulip/zulip.git
synced 2025-11-14 10:57:58 +00:00
test_helpers: Default user_profile to AnonymousUser.
A request that has went through the auth middleware shouldn't have `.user` being `None`. We should use `AnonymousUser` by default to represent unauthenticated users. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
committed by
Tim Abbott
parent
d8f1794698
commit
0056becd04
@@ -300,7 +300,7 @@ class HostRequestMock(HttpRequest):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
post_data: Dict[str, Any] = {},
|
post_data: Dict[str, Any] = {},
|
||||||
user_profile: Optional[Union[UserProfile, AnonymousUser, RemoteZulipServer]] = None,
|
user_profile: Union[UserProfile, RemoteZulipServer, None] = None,
|
||||||
host: str = settings.EXTERNAL_HOST,
|
host: str = settings.EXTERNAL_HOST,
|
||||||
client_name: Optional[str] = None,
|
client_name: Optional[str] = None,
|
||||||
meta_data: Optional[Dict[str, Any]] = None,
|
meta_data: Optional[Dict[str, Any]] = None,
|
||||||
@@ -325,7 +325,7 @@ class HostRequestMock(HttpRequest):
|
|||||||
else:
|
else:
|
||||||
self.META = meta_data
|
self.META = meta_data
|
||||||
self.path = path
|
self.path = path
|
||||||
self.user = user_profile
|
self.user = user_profile or AnonymousUser()
|
||||||
self._body = b""
|
self._body = b""
|
||||||
self.content_type = ""
|
self.content_type = ""
|
||||||
BaseNotes[str, str].get_notes
|
BaseNotes[str, str].get_notes
|
||||||
|
|||||||
@@ -650,9 +650,8 @@ class RateLimitTestCase(ZulipTestCase):
|
|||||||
|
|
||||||
def test_internal_local_clients_skip_rate_limiting(self) -> None:
|
def test_internal_local_clients_skip_rate_limiting(self) -> None:
|
||||||
META = {"REMOTE_ADDR": "127.0.0.1"}
|
META = {"REMOTE_ADDR": "127.0.0.1"}
|
||||||
user = AnonymousUser()
|
|
||||||
|
|
||||||
request = HostRequestMock(client_name="internal", user_profile=user, meta_data=META)
|
request = HostRequestMock(client_name="internal", meta_data=META)
|
||||||
|
|
||||||
f = self.get_ratelimited_view()
|
f = self.get_ratelimited_view()
|
||||||
|
|
||||||
@@ -668,9 +667,8 @@ class RateLimitTestCase(ZulipTestCase):
|
|||||||
|
|
||||||
def test_debug_clients_skip_rate_limiting(self) -> None:
|
def test_debug_clients_skip_rate_limiting(self) -> None:
|
||||||
META = {"REMOTE_ADDR": "3.3.3.3"}
|
META = {"REMOTE_ADDR": "3.3.3.3"}
|
||||||
user = AnonymousUser()
|
|
||||||
|
|
||||||
req = HostRequestMock(client_name="internal", user_profile=user, meta_data=META)
|
req = HostRequestMock(client_name="internal", meta_data=META)
|
||||||
|
|
||||||
f = self.get_ratelimited_view()
|
f = self.get_ratelimited_view()
|
||||||
|
|
||||||
@@ -742,9 +740,8 @@ class RateLimitTestCase(ZulipTestCase):
|
|||||||
|
|
||||||
def test_rate_limiting_happens_by_ip_if_unauthed(self) -> None:
|
def test_rate_limiting_happens_by_ip_if_unauthed(self) -> None:
|
||||||
META = {"REMOTE_ADDR": "3.3.3.3"}
|
META = {"REMOTE_ADDR": "3.3.3.3"}
|
||||||
user = AnonymousUser()
|
|
||||||
|
|
||||||
req = HostRequestMock(client_name="external", user_profile=user, meta_data=META)
|
req = HostRequestMock(client_name="external", meta_data=META)
|
||||||
|
|
||||||
f = self.get_ratelimited_view()
|
f = self.get_ratelimited_view()
|
||||||
|
|
||||||
|
|||||||
@@ -204,7 +204,7 @@ class EventsEndpointTest(ZulipTestCase):
|
|||||||
),
|
),
|
||||||
).decode(),
|
).decode(),
|
||||||
)
|
)
|
||||||
req = HostRequestMock(post_data, user_profile=None)
|
req = HostRequestMock(post_data)
|
||||||
req.META["REMOTE_ADDR"] = "127.0.0.1"
|
req.META["REMOTE_ADDR"] = "127.0.0.1"
|
||||||
with self.assertRaises(AccessDeniedError) as context:
|
with self.assertRaises(AccessDeniedError) as context:
|
||||||
result = self.client_post_request("/notify_tornado", req)
|
result = self.client_post_request("/notify_tornado", req)
|
||||||
@@ -212,7 +212,7 @@ class EventsEndpointTest(ZulipTestCase):
|
|||||||
self.assertEqual(context.exception.http_status_code, 403)
|
self.assertEqual(context.exception.http_status_code, 403)
|
||||||
|
|
||||||
post_data["secret"] = settings.SHARED_SECRET
|
post_data["secret"] = settings.SHARED_SECRET
|
||||||
req = HostRequestMock(post_data, user_profile=None, tornado_handler=dummy_handler)
|
req = HostRequestMock(post_data, tornado_handler=dummy_handler)
|
||||||
req.META["REMOTE_ADDR"] = "127.0.0.1"
|
req.META["REMOTE_ADDR"] = "127.0.0.1"
|
||||||
result = self.client_post_request("/notify_tornado", req)
|
result = self.client_post_request("/notify_tornado", req)
|
||||||
self.assert_json_success(result)
|
self.assert_json_success(result)
|
||||||
|
|||||||
Reference in New Issue
Block a user