Skip to content

Commit

Permalink
Resolves matrix-org#8947 - /_synapse/admin/v1/users/<user_id>/joined_…
Browse files Browse the repository at this point in the history
…rooms also works for remote users

As far as I can tell, the code simply filters off remote users but calls code
that works for both local and remote users.

Signed-off-by: David Teller <davidt@element.io>
  • Loading branch information
Yoric committed Jan 8, 2021
1 parent a03d71d commit 27bedec
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
1 change: 1 addition & 0 deletions changelog.d/8948.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update `/_synapse/admin/v1/users/<user_id>/joined_rooms` to work for both local and remote users.
4 changes: 4 additions & 0 deletions docs/admin_api/user_admin_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ A response body like the following is returned:
"total": 2
}
The server returns the list of rooms of which the user and the server
are member. If the user is local, that's all the rooms of which the
user is member.

**Parameters**

The following parameters should be set in the URL:
Expand Down
10 changes: 4 additions & 6 deletions synapse/rest/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,12 +714,10 @@ def __init__(self, hs):
async def on_GET(self, request, user_id):
await assert_requester_is_admin(self.auth, request)

if not self.is_mine(UserID.from_string(user_id)):
raise SynapseError(400, "Can only lookup local users")

user = await self.store.get_user_by_id(user_id)
if user is None:
raise NotFoundError("Unknown user")
if self.is_mine(UserID.from_string(user_id)):
# Let's get rid of unknown local users quickly.
if await self.store.get_user_by_id(user_id) 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)}
Expand Down
57 changes: 45 additions & 12 deletions tests/rest/admin/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import synapse.rest.admin
from synapse.api.constants import UserTypes
from synapse.api.errors import Codes, HttpResponseException, ResourceLimitError
from synapse.api.room_versions import RoomVersions
from synapse.rest.client.v1 import login, logout, profile, room
from synapse.rest.client.v2_alpha import devices, sync

Expand Down Expand Up @@ -166,7 +167,7 @@ def test_register_correct_nonce(self):
"mac": want_mac,
}
)
channel = self.make_request("POST", self.url, body.encode("utf8"))
request, channel = self.make_request("POST", self.url, body.encode("utf8"))

self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
self.assertEqual("@bob:test", channel.json_body["user_id"])
Expand Down Expand Up @@ -1244,17 +1245,6 @@ def test_user_does_not_exist(self):
self.assertEqual(404, channel.code, msg=channel.json_body)
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])

def test_user_is_not_local(self):
"""
Tests that a lookup for a user that is not a local returns a 400
"""
url = "/_synapse/admin/v1/users/@unknown_person:unknown_domain/joined_rooms"

channel = self.make_request("GET", url, access_token=self.admin_user_tok,)

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
Expand Down Expand Up @@ -1284,6 +1274,49 @@ def test_get_rooms(self):
self.assertEqual(number_rooms, channel.json_body["total"])
self.assertEqual(number_rooms, len(channel.json_body["joined_rooms"]))

def test_get_rooms_with_nonlocal_user(self):
"""
Tests that a normal lookup for rooms is successful with a non-local user
"""

other_user_tok = self.login("user", "pass")
event_builder_factory = self.hs.get_event_builder_factory()
event_creation_handler = self.hs.get_event_creation_handler()
storage = self.hs.get_storage()

# Create two rooms, one with a local user only and one with both a local
# and remote user.
self.helper.create_room_as(self.other_user, tok=other_user_tok)
local_and_remote_room_id = self.helper.create_room_as(
self.other_user, tok=other_user_tok
)

# Add a remote user to the room.
builder = event_builder_factory.for_room_version(
RoomVersions.V1,
{
"type": "m.room.member",
"sender": "@joiner:remote_hs",
"state_key": "@joiner:remote_hs",
"room_id": local_and_remote_room_id,
"content": {"membership": "join"},
},
)

event, context = self.get_success(
event_creation_handler.create_new_client_event(builder)
)

self.get_success(storage.persistence.persist_event(event, context))

# Now get rooms
url = "/_synapse/admin/v1/users/@joiner:remote_hs/joined_rooms"
channel = self.make_request("GET", url, access_token=self.admin_user_tok,)

self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual(1, channel.json_body["total"])
self.assertEqual([local_and_remote_room_id], channel.json_body["joined_rooms"])


class PushersRestTestCase(unittest.HomeserverTestCase):

Expand Down

0 comments on commit 27bedec

Please sign in to comment.