2014-08-12 10:10:52 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-08-12 22:32:18 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
""" Tests REST events for /events paths."""
|
2018-07-19 06:03:33 -04:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from mock import Mock, NonCallableMock
|
2018-07-19 06:03:33 -04:00
|
|
|
from six import PY3
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2015-02-11 06:37:30 -05:00
|
|
|
from ....utils import MockHttpResource, setup_test_homeserver
|
2014-08-12 10:10:52 -04:00
|
|
|
from .utils import RestTestCase
|
|
|
|
|
2014-08-31 09:51:37 -04:00
|
|
|
PATH_PREFIX = "/_matrix/client/api/v1"
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
class EventStreamPermissionsTestCase(RestTestCase):
|
|
|
|
""" Tests event streaming (GET /events). """
|
|
|
|
|
2018-07-19 06:03:33 -04:00
|
|
|
if PY3:
|
|
|
|
skip = "Skip on Py3 until ported to use not V1 only register."
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def setUp(self):
|
2018-07-19 06:03:33 -04:00
|
|
|
import synapse.rest.client.v1.events
|
|
|
|
import synapse.rest.client.v1_only.register
|
|
|
|
import synapse.rest.client.v1.room
|
|
|
|
|
2014-08-18 09:03:07 -04:00
|
|
|
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2015-02-11 06:37:30 -05:00
|
|
|
hs = yield setup_test_homeserver(
|
2018-08-13 02:47:46 -04:00
|
|
|
self.addCleanup,
|
2014-08-12 10:10:52 -04:00
|
|
|
http_client=None,
|
2018-03-13 09:26:52 -04:00
|
|
|
federation_client=Mock(),
|
2018-08-10 09:54:09 -04:00
|
|
|
ratelimiter=NonCallableMock(spec_set=["send_message"]),
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
2014-09-03 04:15:22 -04:00
|
|
|
self.ratelimiter = hs.get_ratelimiter()
|
|
|
|
self.ratelimiter.send_message.return_value = (True, 0)
|
2014-09-06 02:41:18 -04:00
|
|
|
hs.config.enable_registration_captcha = False
|
2016-02-03 09:42:01 -05:00
|
|
|
hs.config.enable_registration = True
|
2018-03-14 11:45:37 -04:00
|
|
|
hs.config.auto_join_rooms = []
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-26 14:49:42 -04:00
|
|
|
hs.get_handlers().federation_handler = Mock()
|
|
|
|
|
2018-07-19 06:03:33 -04:00
|
|
|
synapse.rest.client.v1_only.register.register_servlets(hs, self.mock_resource)
|
2015-01-22 11:10:07 -05:00
|
|
|
synapse.rest.client.v1.events.register_servlets(hs, self.mock_resource)
|
|
|
|
synapse.rest.client.v1.room.register_servlets(hs, self.mock_resource)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
# register an account
|
|
|
|
self.user_id = "sid1"
|
|
|
|
response = yield self.register(self.user_id)
|
|
|
|
self.token = response["access_token"]
|
|
|
|
self.user_id = response["user_id"]
|
|
|
|
|
|
|
|
# register a 2nd account
|
|
|
|
self.other_user = "other1"
|
|
|
|
response = yield self.register(self.other_user)
|
|
|
|
self.other_token = response["access_token"]
|
|
|
|
self.other_user = response["user_id"]
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_stream_basic_permissions(self):
|
2018-04-30 15:58:30 -04: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.
|
|
|
|
# see issue #2602
|
2014-08-18 09:03:07 -04:00
|
|
|
(code, response) = yield self.mock_resource.trigger_get(
|
2018-08-10 09:54:09 -04:00
|
|
|
"/events?access_token=%s" % ("invalid" + self.token,)
|
2014-11-05 06:12:47 -05:00
|
|
|
)
|
2018-04-30 15:58:30 -04:00
|
|
|
self.assertEquals(401, code, msg=str(response))
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
# valid token, expect content
|
2014-08-18 09:03:07 -04:00
|
|
|
(code, response) = yield self.mock_resource.trigger_get(
|
2014-11-05 06:12:47 -05:00
|
|
|
"/events?access_token=%s&timeout=0" % (self.token,)
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
self.assertEquals(200, code, msg=str(response))
|
|
|
|
self.assertTrue("chunk" in response)
|
|
|
|
self.assertTrue("start" in response)
|
|
|
|
self.assertTrue("end" in response)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_stream_room_permissions(self):
|
2018-08-10 09:54:09 -04:00
|
|
|
room_id = yield self.create_room_as(self.other_user, tok=self.other_token)
|
2014-08-26 12:21:48 -04:00
|
|
|
yield self.send(room_id, tok=self.other_token)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
# invited to room (expect no content for room)
|
2014-11-05 06:12:47 -05:00
|
|
|
yield self.invite(
|
2018-08-10 09:54:09 -04:00
|
|
|
room_id, src=self.other_user, targ=self.user_id, tok=self.other_token
|
2014-11-05 06:12:47 -05:00
|
|
|
)
|
|
|
|
|
2014-08-18 09:03:07 -04:00
|
|
|
(code, response) = yield self.mock_resource.trigger_get(
|
2014-11-05 06:12:47 -05:00
|
|
|
"/events?access_token=%s&timeout=0" % (self.token,)
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
self.assertEquals(200, code, msg=str(response))
|
|
|
|
|
2015-07-07 11:18:36 -04:00
|
|
|
# We may get a presence event for ourselves down
|
|
|
|
self.assertEquals(
|
|
|
|
0,
|
2018-08-10 09:54:09 -04:00
|
|
|
len(
|
|
|
|
[
|
|
|
|
c
|
|
|
|
for c in response["chunk"]
|
|
|
|
if not (
|
|
|
|
c.get("type") == "m.presence"
|
|
|
|
and c["content"].get("user_id") == self.user_id
|
|
|
|
)
|
|
|
|
]
|
|
|
|
),
|
2015-07-07 11:18:36 -04:00
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
# joined room (expect all content for room)
|
|
|
|
yield self.join(room=room_id, user=self.user_id, tok=self.token)
|
|
|
|
|
|
|
|
# left to room (expect no content for room)
|
|
|
|
|
2014-11-05 06:12:47 -05:00
|
|
|
def TODO_test_stream_items(self):
|
2014-08-12 10: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
|