2014-08-13 06:32:18 +04:00
|
|
|
#
|
2023-11-21 23:29:58 +03:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 14:26:48 +03:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2023-11-21 23:29:58 +03:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2014-08-13 06:32:18 +04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2014-08-12 18:10:52 +04:00
|
|
|
""" Tests REST events for /events paths."""
|
2018-07-19 13:03:33 +03:00
|
|
|
|
2021-04-09 20:44:38 +03:00
|
|
|
from unittest.mock import Mock
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2022-02-28 21:59:00 +03:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
|
|
|
|
2019-05-01 17:32:38 +03:00
|
|
|
import synapse.rest.admin
|
2022-05-27 14:14:36 +03:00
|
|
|
from synapse.api.constants import EduTypes
|
2021-08-17 14:57:58 +03:00
|
|
|
from synapse.rest.client import events, login, room
|
2022-02-28 21:59:00 +03:00
|
|
|
from synapse.server import HomeServer
|
|
|
|
from synapse.util import Clock
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2018-12-29 15:12:30 +03:00
|
|
|
from tests import unittest
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
|
2018-12-29 15:12:30 +03:00
|
|
|
class EventStreamPermissionsTestCase(unittest.HomeserverTestCase):
|
2021-06-17 17:20:06 +03:00
|
|
|
"""Tests event streaming (GET /events)."""
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2018-12-29 15:12:30 +03:00
|
|
|
servlets = [
|
|
|
|
events.register_servlets,
|
|
|
|
room.register_servlets,
|
2019-05-02 13:59:16 +03:00
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
2018-12-29 15:12:30 +03:00
|
|
|
login.register_servlets,
|
|
|
|
]
|
2018-07-19 13:03:33 +03:00
|
|
|
|
2022-02-28 21:59:00 +03:00
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
2018-12-29 15:12:30 +03:00
|
|
|
config = self.default_config()
|
2019-05-13 23:01:14 +03:00
|
|
|
config["enable_registration_captcha"] = False
|
|
|
|
config["enable_registration"] = True
|
|
|
|
config["auto_join_rooms"] = []
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2020-06-05 12:47:20 +03:00
|
|
|
hs = self.setup_test_homeserver(config=config)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2023-08-29 17:38:56 +03:00
|
|
|
hs.get_federation_handler = Mock() # type: ignore[method-assign]
|
2014-08-26 22:49:42 +04:00
|
|
|
|
2018-12-29 15:12:30 +03:00
|
|
|
return hs
|
|
|
|
|
2022-02-28 21:59:00 +03:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2014-08-12 18:10:52 +04:00
|
|
|
# register an account
|
2018-12-29 15:12:30 +03:00
|
|
|
self.user_id = self.register_user("sid1", "pass")
|
|
|
|
self.token = self.login(self.user_id, "pass")
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
# register a 2nd account
|
2018-12-29 15:12:30 +03:00
|
|
|
self.other_user = self.register_user("other2", "pass")
|
|
|
|
self.other_token = self.login(self.other_user, "pass")
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2022-02-28 21:59:00 +03:00
|
|
|
def test_stream_basic_permissions(self) -> None:
|
2018-04-30 22:58:30 +03:00
|
|
|
# invalid token, expect 401
|
|
|
|
# note: this is in violation of the original v1 spec, which expected
|
|
|
|
# 403. However, since the v1 spec no longer exists and the v1
|
|
|
|
# implementation is now part of the r0 implementation, the newer
|
|
|
|
# behaviour is used instead to be consistent with the r0 spec.
|
2023-11-15 16:02:11 +03:00
|
|
|
# see issue https://github.com/matrix-org/synapse/issues/2602
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2018-12-29 15:12:30 +03:00
|
|
|
"GET", "/events?access_token=%s" % ("invalid" + self.token,)
|
2014-11-05 14:12:47 +03:00
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(channel.code, 401, msg=channel.result)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
# valid token, expect content
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2018-12-29 15:12:30 +03:00
|
|
|
"GET", "/events?access_token=%s&timeout=0" % (self.token,)
|
2014-11-05 14:12:47 +03:00
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(channel.code, 200, msg=channel.result)
|
2018-12-29 15:12:30 +03:00
|
|
|
self.assertTrue("chunk" in channel.json_body)
|
|
|
|
self.assertTrue("start" in channel.json_body)
|
|
|
|
self.assertTrue("end" in channel.json_body)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2022-02-28 21:59:00 +03:00
|
|
|
def test_stream_room_permissions(self) -> None:
|
2018-12-29 15:12:30 +03:00
|
|
|
room_id = self.helper.create_room_as(self.other_user, tok=self.other_token)
|
|
|
|
self.helper.send(room_id, tok=self.other_token)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
# invited to room (expect no content for room)
|
2018-12-29 15:12:30 +03:00
|
|
|
self.helper.invite(
|
2014-11-05 14:12:47 +03:00
|
|
|
room_id, src=self.other_user, targ=self.user_id, tok=self.other_token
|
|
|
|
)
|
|
|
|
|
2018-12-29 15:12:30 +03:00
|
|
|
# valid token, expect content
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2018-12-29 15:12:30 +03:00
|
|
|
"GET", "/events?access_token=%s&timeout=0" % (self.token,)
|
2014-11-05 14:12:47 +03:00
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(channel.code, 200, msg=channel.result)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2015-07-07 18:18:36 +03:00
|
|
|
# We may get a presence event for ourselves down
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(
|
2015-07-07 18:18:36 +03:00
|
|
|
0,
|
|
|
|
len(
|
|
|
|
[
|
|
|
|
c
|
2018-12-29 15:12:30 +03:00
|
|
|
for c in channel.json_body["chunk"]
|
2015-07-07 18:18:36 +03:00
|
|
|
if not (
|
2022-05-27 14:14:36 +03:00
|
|
|
c.get("type") == EduTypes.PRESENCE
|
2015-07-07 18:18:36 +03:00
|
|
|
and c["content"].get("user_id") == self.user_id
|
|
|
|
)
|
|
|
|
]
|
2018-08-10 16:54:09 +03:00
|
|
|
),
|
2015-07-07 18:18:36 +03:00
|
|
|
)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
# joined room (expect all content for room)
|
2018-12-29 15:12:30 +03:00
|
|
|
self.helper.join(room=room_id, user=self.user_id, tok=self.token)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
# left to room (expect no content for room)
|
|
|
|
|
2022-02-28 21:59:00 +03:00
|
|
|
def TODO_test_stream_items(self) -> None:
|
2014-08-12 18:10:52 +04:00
|
|
|
# new user, no content
|
|
|
|
|
|
|
|
# join room, expect 1 item (join)
|
|
|
|
|
|
|
|
# send message, expect 2 items (join,send)
|
|
|
|
|
|
|
|
# set topic, expect 3 items (join,send,topic)
|
|
|
|
|
|
|
|
# someone else join room, expect 4 (join,send,topic,join)
|
|
|
|
|
|
|
|
# someone else send message, expect 5 (join,send.topic,join,send)
|
|
|
|
|
|
|
|
# someone else set topic, expect 6 (join,send,topic,join,send,topic)
|
|
|
|
pass
|
2020-01-20 20:38:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
class GetEventsTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
events.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
2022-02-28 21:59:00 +03:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2020-01-20 20:38:09 +03:00
|
|
|
# register an account
|
|
|
|
self.user_id = self.register_user("sid1", "pass")
|
|
|
|
self.token = self.login(self.user_id, "pass")
|
|
|
|
|
|
|
|
self.room_id = self.helper.create_room_as(self.user_id, tok=self.token)
|
|
|
|
|
2022-02-28 21:59:00 +03:00
|
|
|
def test_get_event_via_events(self) -> None:
|
2020-01-20 20:38:09 +03:00
|
|
|
resp = self.helper.send(self.room_id, tok=self.token)
|
|
|
|
event_id = resp["event_id"]
|
|
|
|
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2020-01-20 20:38:09 +03:00
|
|
|
"GET",
|
|
|
|
"/events/" + event_id,
|
|
|
|
access_token=self.token,
|
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(channel.code, 200, msg=channel.result)
|