|
| 1 | +from twisted.internet.testing import MemoryReactor |
| 2 | + |
| 3 | +import synapse.rest.admin |
| 4 | +from synapse.api.errors import Codes |
| 5 | +from synapse.rest.client import login, room |
| 6 | +from synapse.server import HomeServer |
| 7 | +from synapse.util.clock import Clock |
| 8 | + |
| 9 | +from tests import unittest |
| 10 | + |
| 11 | + |
| 12 | +class FetchEventTestCase(unittest.HomeserverTestCase): |
| 13 | + servlets = [ |
| 14 | + synapse.rest.admin.register_servlets, |
| 15 | + login.register_servlets, |
| 16 | + room.register_servlets, |
| 17 | + ] |
| 18 | + |
| 19 | + def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None: |
| 20 | + self.admin_user = self.register_user("admin", "pass", admin=True) |
| 21 | + self.admin_user_tok = self.login("admin", "pass") |
| 22 | + |
| 23 | + self.other_user = self.register_user("user", "pass") |
| 24 | + self.other_user_tok = self.login("user", "pass") |
| 25 | + |
| 26 | + self.room_id1 = self.helper.create_room_as( |
| 27 | + self.other_user, tok=self.other_user_tok, is_public=True |
| 28 | + ) |
| 29 | + resp = self.helper.send(self.room_id1, body="Hey now", tok=self.other_user_tok) |
| 30 | + self.event_id = resp["event_id"] |
| 31 | + |
| 32 | + def test_no_auth(self) -> None: |
| 33 | + """ |
| 34 | + Try to get an event without authentication. |
| 35 | + """ |
| 36 | + channel = self.make_request( |
| 37 | + "GET", |
| 38 | + f"/_synapse/admin/v1/fetch_event/{self.event_id}", |
| 39 | + ) |
| 40 | + |
| 41 | + self.assertEqual(401, channel.code, msg=channel.json_body) |
| 42 | + self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) |
| 43 | + |
| 44 | + def test_requester_is_not_admin(self) -> None: |
| 45 | + """ |
| 46 | + If the user is not a server admin, an error 403 is returned. |
| 47 | + """ |
| 48 | + |
| 49 | + channel = self.make_request( |
| 50 | + "GET", |
| 51 | + f"/_synapse/admin/v1/fetch_event/{self.event_id}", |
| 52 | + access_token=self.other_user_tok, |
| 53 | + ) |
| 54 | + |
| 55 | + self.assertEqual(403, channel.code, msg=channel.json_body) |
| 56 | + self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) |
| 57 | + |
| 58 | + def test_fetch_event(self) -> None: |
| 59 | + """ |
| 60 | + Test that we can successfully fetch an event |
| 61 | + """ |
| 62 | + channel = self.make_request( |
| 63 | + "GET", |
| 64 | + f"/_synapse/admin/v1/fetch_event/{self.event_id}", |
| 65 | + access_token=self.admin_user_tok, |
| 66 | + ) |
| 67 | + self.assertEqual(200, channel.code, msg=channel.json_body) |
| 68 | + self.assertEqual( |
| 69 | + channel.json_body["event"]["content"], |
| 70 | + {"body": "Hey now", "msgtype": "m.text"}, |
| 71 | + ) |
| 72 | + self.assertEqual(channel.json_body["event"]["event_id"], self.event_id) |
| 73 | + self.assertEqual(channel.json_body["event"]["type"], "m.room.message") |
| 74 | + self.assertEqual(channel.json_body["event"]["sender"], self.other_user) |
0 commit comments