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

Commit 4ac3a8c

Browse files
authored
Fix a bug in the joined_rooms admin API (#8643)
If the user was not in any rooms then the API returned the same error as if the user did not exist.
1 parent cf9a17a commit 4ac3a8c

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

changelog.d/8643.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug in the `joined_rooms` admin API if the user has never joined any rooms. The bug was introduced, along with the API, in v1.21.0.

synapse/rest/admin/users.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,10 @@ async def on_GET(self, request, user_id):
702702
if not self.is_mine(UserID.from_string(user_id)):
703703
raise SynapseError(400, "Can only lookup local users")
704704

705-
room_ids = await self.store.get_rooms_for_user(user_id)
706-
if not room_ids:
707-
raise NotFoundError("User not found")
705+
user = await self.store.get_user_by_id(user_id)
706+
if user is None:
707+
raise NotFoundError("Unknown user")
708708

709+
room_ids = await self.store.get_rooms_for_user(user_id)
709710
ret = {"joined_rooms": list(room_ids), "total": len(room_ids)}
710711
return 200, ret

tests/rest/admin/test_user.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,6 @@ class UserMembershipRestTestCase(unittest.HomeserverTestCase):
10161016
servlets = [
10171017
synapse.rest.admin.register_servlets,
10181018
login.register_servlets,
1019-
sync.register_servlets,
10201019
room.register_servlets,
10211020
]
10221021

@@ -1082,6 +1081,21 @@ def test_user_is_not_local(self):
10821081
self.assertEqual(400, channel.code, msg=channel.json_body)
10831082
self.assertEqual("Can only lookup local users", channel.json_body["error"])
10841083

1084+
def test_no_memberships(self):
1085+
"""
1086+
Tests that a normal lookup for rooms is successfully
1087+
if user has no memberships
1088+
"""
1089+
# Get rooms
1090+
request, channel = self.make_request(
1091+
"GET", self.url, access_token=self.admin_user_tok,
1092+
)
1093+
self.render(request)
1094+
1095+
self.assertEqual(200, channel.code, msg=channel.json_body)
1096+
self.assertEqual(0, channel.json_body["total"])
1097+
self.assertEqual(0, len(channel.json_body["joined_rooms"]))
1098+
10851099
def test_get_rooms(self):
10861100
"""
10871101
Tests that a normal lookup for rooms is successfully

0 commit comments

Comments
 (0)