MatrixSynapse/tests/rest/test_events.py

230 lines
7.0 KiB
Python
Raw Normal View History

2014-08-12 16:10:52 +02:00
# -*- coding: utf-8 -*-
# Copyright 2014 OpenMarket Ltd
#
# 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 16:10:52 +02:00
""" Tests REST events for /events paths."""
from tests import unittest
2014-08-12 16:10:52 +02:00
# twisted imports
from twisted.internet import defer
import synapse.rest.events
import synapse.rest.register
import synapse.rest.room
from synapse.server import HomeServer
from ..utils import MockHttpResource, SQLiteMemoryDbPool, MockKey
2014-08-12 16:10:52 +02:00
from .utils import RestTestCase
2014-09-03 10:15:22 +02:00
from mock import Mock, NonCallableMock
2014-08-12 16:10:52 +02:00
PATH_PREFIX = "/_matrix/client/api/v1"
2014-08-12 16:10:52 +02:00
class EventStreamPaginationApiTestCase(unittest.TestCase):
""" Tests event streaming query parameters and start/end keys used in the
Pagination stream API. """
user_id = "sid1"
def setUp(self):
# configure stream and inject items
pass
def tearDown(self):
pass
2014-11-05 12:12:47 +01:00
def TODO_test_long_poll(self):
2014-08-12 16:10:52 +02:00
# stream from 'end' key, send (self+other) message, expect message.
# stream from 'END', send (self+other) message, expect message.
# stream from 'end' key, send (self+other) topic, expect topic.
# stream from 'END', send (self+other) topic, expect topic.
# stream from 'end' key, send (self+other) invite, expect invite.
# stream from 'END', send (self+other) invite, expect invite.
pass
2014-11-05 12:12:47 +01:00
def TODO_test_stream_forward(self):
2014-08-12 16:10:52 +02:00
# stream from START, expect injected items
# stream from 'start' key, expect same content
# stream from 'end' key, expect nothing
# stream from 'END', expect nothing
# The following is needed for cases where content is removed e.g. you
# left a room, so the token you're streaming from is > the one that
# would be returned naturally from START>END.
# stream from very new token (higher than end key), expect same token
# returned as end key
pass
2014-11-05 12:12:47 +01:00
def TODO_test_limits(self):
2014-08-12 16:10:52 +02:00
# stream from a key, expect limit_num items
# stream from START, expect limit_num items
pass
2014-11-05 12:12:47 +01:00
def TODO_test_range(self):
2014-08-12 16:10:52 +02:00
# stream from key to key, expect X items
# stream from key to END, expect X items
# stream from START to key, expect X items
# stream from START to END, expect all items
pass
2014-11-05 12:12:47 +01:00
def TODO_test_direction(self):
2014-08-12 16:10:52 +02:00
# stream from END to START and fwds, expect newest first
# stream from END to START and bwds, expect oldest first
# stream from START to END and fwds, expect oldest first
# stream from START to END and bwds, expect newest first
pass
class EventStreamPermissionsTestCase(RestTestCase):
""" Tests event streaming (GET /events). """
@defer.inlineCallbacks
def setUp(self):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
2014-08-12 16:10:52 +02:00
persistence_service = Mock(spec=["get_latest_pdus_in_context"])
persistence_service.get_latest_pdus_in_context.return_value = []
2014-10-16 01:09:48 +02:00
self.mock_config = NonCallableMock()
self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
2014-08-12 16:10:52 +02:00
hs = HomeServer(
"test",
db_pool=db_pool,
2014-08-12 16:10:52 +02:00
http_client=None,
replication_layer=Mock(),
persistence_service=persistence_service,
clock=Mock(spec=[
"call_later",
"cancel_call_later",
"time_msec",
2014-09-03 10:15:22 +02:00
"time"
2014-08-12 16:10:52 +02:00
]),
2014-09-03 10:15:22 +02:00
ratelimiter=NonCallableMock(spec_set=[
"send_message",
]),
2014-10-16 01:09:48 +02:00
config=self.mock_config,
2014-08-12 16:10:52 +02:00
)
2014-09-03 10:15:22 +02:00
self.ratelimiter = hs.get_ratelimiter()
self.ratelimiter.send_message.return_value = (True, 0)
2014-09-06 08:41:18 +02:00
hs.config.enable_registration_captcha = False
2014-08-12 16:10:52 +02:00
hs.get_handlers().federation_handler = Mock()
2014-08-12 16:10:52 +02:00
hs.get_clock().time_msec.return_value = 1000000
2014-11-05 12:12:47 +01:00
hs.get_clock().time.return_value = 1000
2014-08-12 16:10:52 +02:00
synapse.rest.register.register_servlets(hs, self.mock_resource)
synapse.rest.events.register_servlets(hs, self.mock_resource)
synapse.rest.room.register_servlets(hs, self.mock_resource)
2014-08-12 16:10:52 +02: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):
# invalid token, expect 403
(code, response) = yield self.mock_resource.trigger_get(
2014-11-05 12:12:47 +01:00
"/events?access_token=%s" % ("invalid" + self.token, )
)
2014-08-12 16:10:52 +02:00
self.assertEquals(403, code, msg=str(response))
# valid token, expect content
(code, response) = yield self.mock_resource.trigger_get(
2014-11-05 12:12:47 +01:00
"/events?access_token=%s&timeout=0" % (self.token,)
)
2014-08-12 16:10:52 +02: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):
2014-11-05 12:12:47 +01:00
room_id = yield self.create_room_as(
self.other_user,
tok=self.other_token
)
yield self.send(room_id, tok=self.other_token)
2014-08-12 16:10:52 +02:00
# invited to room (expect no content for room)
2014-11-05 12:12:47 +01:00
yield self.invite(
room_id,
src=self.other_user,
targ=self.user_id,
tok=self.other_token
)
(code, response) = yield self.mock_resource.trigger_get(
2014-11-05 12:12:47 +01:00
"/events?access_token=%s&timeout=0" % (self.token,)
)
2014-08-12 16:10:52 +02:00
self.assertEquals(200, code, msg=str(response))
self.assertEquals(0, len(response["chunk"]))
2014-08-12 16:10:52 +02: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 12:12:47 +01:00
def TODO_test_stream_items(self):
2014-08-12 16:10:52 +02: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