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

Make /_synapse/admin/v1/users/<user_id>/joined_rooms also work for remote users #8948

Merged
merged 5 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
clokep marked this conversation as resolved.
Show resolved Hide resolved

**Parameters**

The following parameters should be set in the URL:
Expand Down
7 changes: 0 additions & 7 deletions synapse/rest/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,13 +714,6 @@ 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")

room_ids = await self.store.get_rooms_for_user(user_id)
ret = {"joined_rooms": list(room_ids), "total": len(room_ids)}
return 200, ret
Expand Down
58 changes: 52 additions & 6 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 @@ -1236,24 +1237,26 @@ def test_requester_is_no_admin(self):

def test_user_does_not_exist(self):
"""
Tests that a lookup for a user that does not exist returns a 404
Tests that a lookup for a user that does not exist returns an empty list
"""
url = "/_synapse/admin/v1/users/@unknown_person:test/joined_rooms"
channel = self.make_request("GET", url, access_token=self.admin_user_tok,)

self.assertEqual(404, channel.code, msg=channel.json_body)
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
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_user_is_not_local(self):
"""
Tests that a lookup for a user that is not a local returns a 400
Tests that a lookup for a user that is not a local and participates in no conversation returns an empty list
"""
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"])
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_no_memberships(self):
"""
Expand Down Expand Up @@ -1284,6 +1287,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):
clokep marked this conversation as resolved.
Show resolved Hide resolved
"""
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