From 0e6b459a3059c83cc5b6d177241c4b3a76de836c Mon Sep 17 00:00:00 2001 From: dklimpel <5740567+dklimpel@users.noreply.github.com> Date: Sat, 24 Oct 2020 16:00:07 +0200 Subject: [PATCH 1/4] Bugfix in joined_rooms admin API if user has no memberships --- synapse/rest/admin/users.py | 7 ++++--- tests/rest/admin/test_user.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/synapse/rest/admin/users.py b/synapse/rest/admin/users.py index 8efefbc0a03a..f9b3f477cd7f 100644 --- a/synapse/rest/admin/users.py +++ b/synapse/rest/admin/users.py @@ -702,9 +702,10 @@ async def on_GET(self, request, user_id): 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") + u = await self.store.get_user_by_id(user_id) + if u 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 98d062373497..81f9ad7b3645 100644 --- a/tests/rest/admin/test_user.py +++ b/tests/rest/admin/test_user.py @@ -1082,6 +1082,21 @@ def test_user_is_not_local(self): 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 From 50cedf88ae3eb56d76598342d080b3ba54f96cad Mon Sep 17 00:00:00 2001 From: dklimpel <5740567+dklimpel@users.noreply.github.com> Date: Sat, 24 Oct 2020 16:05:52 +0200 Subject: [PATCH 2/4] add newsfile --- changelog.d/8643.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/8643.bugfix diff --git a/changelog.d/8643.bugfix b/changelog.d/8643.bugfix new file mode 100644 index 000000000000..5e3433df837d --- /dev/null +++ b/changelog.d/8643.bugfix @@ -0,0 +1 @@ +Fix a bug in joined_rooms admin API if user has no memberships. \ No newline at end of file From dbea2d98eb7f8d8e0b82a933709cb512d8e9c12a Mon Sep 17 00:00:00 2001 From: dklimpel <5740567+dklimpel@users.noreply.github.com> Date: Sun, 25 Oct 2020 10:03:54 +0100 Subject: [PATCH 3/4] Remove not needed servlet in tests --- tests/rest/admin/test_user.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/rest/admin/test_user.py b/tests/rest/admin/test_user.py index 81f9ad7b3645..d4b7ae21d190 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, ] From 571996257e745fac335dc4b90dc0381593b3813e Mon Sep 17 00:00:00 2001 From: Dirk Klimpel <5740567+dklimpel@users.noreply.github.com> Date: Mon, 26 Oct 2020 16:24:55 +0100 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Patrick Cloke --- changelog.d/8643.bugfix | 2 +- synapse/rest/admin/users.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/changelog.d/8643.bugfix b/changelog.d/8643.bugfix index 5e3433df837d..fcda1ca87160 100644 --- a/changelog.d/8643.bugfix +++ b/changelog.d/8643.bugfix @@ -1 +1 @@ -Fix a bug in joined_rooms admin API if user has no memberships. \ No newline at end of file +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 f9b3f477cd7f..e71d9b0e1c41 100644 --- a/synapse/rest/admin/users.py +++ b/synapse/rest/admin/users.py @@ -702,8 +702,8 @@ async def on_GET(self, request, user_id): if not self.is_mine(UserID.from_string(user_id)): raise SynapseError(400, "Can only lookup local users") - u = await self.store.get_user_by_id(user_id) - if u is None: + 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)