Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit e0804ef

Browse files
authored
Improve the synapse.api.auth.Auth mock used in unit tests. (#13809)
To return the proper type (`Requester`) instead of a `dict`.
1 parent a35842c commit e0804ef

File tree

2 files changed

+11
-26
lines changed

2 files changed

+11
-26
lines changed

changelog.d/13809.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve the `synapse.api.auth.Auth` mock used in unit tests.

tests/unittest.py

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -300,47 +300,31 @@ def setUp(self) -> None:
300300
if hasattr(self, "user_id"):
301301
if self.hijack_auth:
302302
assert self.helper.auth_user_id is not None
303+
token = "some_fake_token"
303304

304305
# We need a valid token ID to satisfy foreign key constraints.
305306
token_id = self.get_success(
306307
self.hs.get_datastores().main.add_access_token_to_user(
307308
self.helper.auth_user_id,
308-
"some_fake_token",
309+
token,
309310
None,
310311
None,
311312
)
312313
)
313314

314-
async def get_user_by_access_token(
315-
token: Optional[str] = None, allow_guest: bool = False
316-
) -> JsonDict:
317-
assert self.helper.auth_user_id is not None
318-
return {
319-
"user": UserID.from_string(self.helper.auth_user_id),
320-
"token_id": token_id,
321-
"is_guest": False,
322-
}
323-
324-
async def get_user_by_req(
325-
request: SynapseRequest,
326-
allow_guest: bool = False,
327-
allow_expired: bool = False,
328-
) -> Requester:
315+
# This has to be a function and not just a Mock, because
316+
# `self.helper.auth_user_id` is temporarily reassigned in some tests
317+
async def get_requester(*args, **kwargs) -> Requester:
329318
assert self.helper.auth_user_id is not None
330319
return create_requester(
331-
UserID.from_string(self.helper.auth_user_id),
332-
token_id,
333-
False,
334-
False,
335-
None,
320+
user_id=UserID.from_string(self.helper.auth_user_id),
321+
access_token_id=token_id,
336322
)
337323

338324
# Type ignore: mypy doesn't like us assigning to methods.
339-
self.hs.get_auth().get_user_by_req = get_user_by_req # type: ignore[assignment]
340-
self.hs.get_auth().get_user_by_access_token = get_user_by_access_token # type: ignore[assignment]
341-
self.hs.get_auth().get_access_token_from_request = Mock( # type: ignore[assignment]
342-
return_value="1234"
343-
)
325+
self.hs.get_auth().get_user_by_req = get_requester # type: ignore[assignment]
326+
self.hs.get_auth().get_user_by_access_token = get_requester # type: ignore[assignment]
327+
self.hs.get_auth().get_access_token_from_request = Mock(return_value=token) # type: ignore[assignment]
344328

345329
if self.needs_threadpool:
346330
self.reactor.threadpool = ThreadPool() # type: ignore[assignment]

0 commit comments

Comments
 (0)