diff options
author | Dirk Klimpel <5740567+dklimpel@users.noreply.github.com> | 2020-10-26 17:25:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-26 12:25:48 -0400 |
commit | 4ac3a8c5dcec00e8aa7a796b5bf05402133b49f0 (patch) | |
tree | 1a9fcefd6252f39b476fce5637e0580bd7a352ca | |
parent | Merge tag 'v1.22.0rc2' into develop (diff) | |
download | synapse-4ac3a8c5dcec00e8aa7a796b5bf05402133b49f0.tar.xz |
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.
-rw-r--r-- | changelog.d/8643.bugfix | 1 | ||||
-rw-r--r-- | synapse/rest/admin/users.py | 7 | ||||
-rw-r--r-- | tests/rest/admin/test_user.py | 16 |
3 files changed, 20 insertions, 4 deletions
diff --git a/changelog.d/8643.bugfix b/changelog.d/8643.bugfix new file mode 100644 index 0000000000..fcda1ca871 --- /dev/null +++ b/changelog.d/8643.bugfix @@ -0,0 +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. diff --git a/synapse/rest/admin/users.py b/synapse/rest/admin/users.py index 8efefbc0a0..e71d9b0e1c 100644 --- a/synapse/rest/admin/users.py +++ b/synapse/rest/admin/users.py @@ -702,9 +702,10 @@ class UserMembershipRestServlet(RestServlet): if not self.is_mine(UserID.from_string(user_id)): raise SynapseError(400, "Can only lookup local users") - room_ids = await self.store.get_rooms_for_user(user_id) - if not room_ids: - raise NotFoundError("User not found") + user = await self.store.get_user_by_id(user_id) + if user is None: + raise NotFoundError("Unknown user") + room_ids = await self.store.get_rooms_for_user(user_id) ret = {"joined_rooms": list(room_ids), "total": len(room_ids)} return 200, ret diff --git a/tests/rest/admin/test_user.py b/tests/rest/admin/test_user.py index 98d0623734..d4b7ae21d1 100644 --- a/tests/rest/admin/test_user.py +++ b/tests/rest/admin/test_user.py @@ -1016,7 +1016,6 @@ class UserMembershipRestTestCase(unittest.HomeserverTestCase): servlets = [ synapse.rest.admin.register_servlets, login.register_servlets, - sync.register_servlets, room.register_servlets, ] @@ -1082,6 +1081,21 @@ class UserMembershipRestTestCase(unittest.HomeserverTestCase): self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual("Can only lookup local users", channel.json_body["error"]) + def test_no_memberships(self): + """ + Tests that a normal lookup for rooms is successfully + if user has no memberships + """ + # Get rooms + request, channel = self.make_request( + "GET", self.url, access_token=self.admin_user_tok, + ) + self.render(request) + + self.assertEqual(200, channel.code, msg=channel.json_body) + self.assertEqual(0, channel.json_body["total"]) + self.assertEqual(0, len(channel.json_body["joined_rooms"])) + def test_get_rooms(self): """ Tests that a normal lookup for rooms is successfully |