2014-09-19 15:45:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-07 05:26:29 +01:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-09-19 15:45:21 +02: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.
|
|
|
|
|
|
|
|
|
|
|
|
from tests import unittest
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2014-12-16 12:29:05 +01:00
|
|
|
from synapse.api.constants import EventTypes, Membership
|
2015-01-23 12:55:12 +01:00
|
|
|
from synapse.types import UserID, RoomID
|
2015-09-22 13:57:40 +02:00
|
|
|
from tests.storage.event_injector import EventInjector
|
2014-09-19 15:45:21 +02:00
|
|
|
|
2015-02-11 12:37:30 +01:00
|
|
|
from tests.utils import setup_test_homeserver
|
2014-12-10 19:00:57 +01:00
|
|
|
|
|
|
|
from mock import Mock
|
2014-09-19 15:45:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
class StreamStoreTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def setUp(self):
|
2015-02-11 12:37:30 +01:00
|
|
|
hs = yield setup_test_homeserver(
|
2014-12-10 19:00:57 +01:00
|
|
|
resource_for_federation=Mock(),
|
|
|
|
http_client=None,
|
2014-09-19 15:45:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
self.store = hs.get_datastore()
|
2014-12-10 19:00:57 +01:00
|
|
|
self.event_builder_factory = hs.get_event_builder_factory()
|
2015-09-22 13:57:40 +02:00
|
|
|
self.event_injector = EventInjector(hs)
|
2014-12-10 19:00:57 +01:00
|
|
|
self.handlers = hs.get_handlers()
|
|
|
|
self.message_handler = self.handlers.message_handler
|
2014-09-19 15:45:21 +02:00
|
|
|
|
2015-01-23 12:47:15 +01:00
|
|
|
self.u_alice = UserID.from_string("@alice:test")
|
|
|
|
self.u_bob = UserID.from_string("@bob:test")
|
2014-09-19 15:45:21 +02:00
|
|
|
|
2015-01-23 12:55:12 +01:00
|
|
|
self.room1 = RoomID.from_string("!abc123:test")
|
|
|
|
self.room2 = RoomID.from_string("!xyx987:test")
|
2014-09-19 15:45:21 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_event_stream_get_other(self):
|
|
|
|
# Both bob and alice joins the room
|
2015-09-22 13:57:40 +02:00
|
|
|
yield self.event_injector.inject_room_member(
|
2014-09-19 17:38:38 +02:00
|
|
|
self.room1, self.u_alice, Membership.JOIN
|
|
|
|
)
|
2015-09-22 13:57:40 +02:00
|
|
|
yield self.event_injector.inject_room_member(
|
2014-09-19 17:38:38 +02:00
|
|
|
self.room1, self.u_bob, Membership.JOIN
|
|
|
|
)
|
2014-09-19 15:45:21 +02:00
|
|
|
|
|
|
|
# Initial stream key:
|
|
|
|
start = yield self.store.get_room_events_max_id()
|
|
|
|
|
2015-09-22 13:57:40 +02:00
|
|
|
yield self.event_injector.inject_message(self.room1, self.u_alice, u"test")
|
2014-09-19 15:45:21 +02:00
|
|
|
|
|
|
|
end = yield self.store.get_room_events_max_id()
|
|
|
|
|
|
|
|
results, _ = yield self.store.get_room_events_stream(
|
|
|
|
self.u_bob.to_string(),
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(1, len(results))
|
|
|
|
|
|
|
|
event = results[0]
|
|
|
|
|
|
|
|
self.assertObjectHasAttributes(
|
|
|
|
{
|
2014-12-16 12:29:05 +01:00
|
|
|
"type": EventTypes.Message,
|
2014-09-19 15:45:21 +02:00
|
|
|
"user_id": self.u_alice.to_string(),
|
|
|
|
"content": {"body": "test", "msgtype": "message"},
|
|
|
|
},
|
|
|
|
event,
|
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_event_stream_get_own(self):
|
|
|
|
# Both bob and alice joins the room
|
2015-09-22 13:57:40 +02:00
|
|
|
yield self.event_injector.inject_room_member(
|
2014-09-19 17:38:38 +02:00
|
|
|
self.room1, self.u_alice, Membership.JOIN
|
|
|
|
)
|
2015-09-22 13:57:40 +02:00
|
|
|
yield self.event_injector.inject_room_member(
|
2014-09-19 17:38:38 +02:00
|
|
|
self.room1, self.u_bob, Membership.JOIN
|
|
|
|
)
|
2014-09-19 15:45:21 +02:00
|
|
|
|
|
|
|
# Initial stream key:
|
|
|
|
start = yield self.store.get_room_events_max_id()
|
|
|
|
|
2015-09-22 13:57:40 +02:00
|
|
|
yield self.event_injector.inject_message(self.room1, self.u_alice, u"test")
|
2014-09-19 15:45:21 +02:00
|
|
|
|
|
|
|
end = yield self.store.get_room_events_max_id()
|
|
|
|
|
|
|
|
results, _ = yield self.store.get_room_events_stream(
|
|
|
|
self.u_alice.to_string(),
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(1, len(results))
|
|
|
|
|
|
|
|
event = results[0]
|
|
|
|
|
|
|
|
self.assertObjectHasAttributes(
|
|
|
|
{
|
2014-12-16 12:29:05 +01:00
|
|
|
"type": EventTypes.Message,
|
2014-09-19 15:45:21 +02:00
|
|
|
"user_id": self.u_alice.to_string(),
|
|
|
|
"content": {"body": "test", "msgtype": "message"},
|
|
|
|
},
|
|
|
|
event,
|
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_event_stream_join_leave(self):
|
|
|
|
# Both bob and alice joins the room
|
2015-09-22 13:57:40 +02:00
|
|
|
yield self.event_injector.inject_room_member(
|
2014-09-19 17:38:38 +02:00
|
|
|
self.room1, self.u_alice, Membership.JOIN
|
|
|
|
)
|
2015-09-22 13:57:40 +02:00
|
|
|
yield self.event_injector.inject_room_member(
|
2014-09-19 17:38:38 +02:00
|
|
|
self.room1, self.u_bob, Membership.JOIN
|
|
|
|
)
|
2014-09-19 15:45:21 +02:00
|
|
|
|
|
|
|
# Then bob leaves again.
|
2015-09-22 13:57:40 +02:00
|
|
|
yield self.event_injector.inject_room_member(
|
2014-09-19 17:38:38 +02:00
|
|
|
self.room1, self.u_bob, Membership.LEAVE
|
|
|
|
)
|
2014-09-19 15:45:21 +02:00
|
|
|
|
|
|
|
# Initial stream key:
|
|
|
|
start = yield self.store.get_room_events_max_id()
|
|
|
|
|
2015-09-22 13:57:40 +02:00
|
|
|
yield self.event_injector.inject_message(self.room1, self.u_alice, u"test")
|
2014-09-19 15:45:21 +02:00
|
|
|
|
|
|
|
end = yield self.store.get_room_events_max_id()
|
|
|
|
|
|
|
|
results, _ = yield self.store.get_room_events_stream(
|
|
|
|
self.u_bob.to_string(),
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
)
|
|
|
|
|
|
|
|
# We should not get the message, as it happened *after* bob left.
|
|
|
|
self.assertEqual(0, len(results))
|
2014-09-19 17:38:38 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_event_stream_prev_content(self):
|
2015-09-22 13:57:40 +02:00
|
|
|
yield self.event_injector.inject_room_member(
|
2014-09-19 17:38:38 +02:00
|
|
|
self.room1, self.u_bob, Membership.JOIN
|
|
|
|
)
|
|
|
|
|
2015-09-22 13:57:40 +02:00
|
|
|
event1 = yield self.event_injector.inject_room_member(
|
2014-09-19 17:38:38 +02:00
|
|
|
self.room1, self.u_alice, Membership.JOIN
|
|
|
|
)
|
|
|
|
|
|
|
|
start = yield self.store.get_room_events_max_id()
|
|
|
|
|
2015-09-22 13:57:40 +02:00
|
|
|
event2 = yield self.event_injector.inject_room_member(
|
2014-09-19 17:38:38 +02:00
|
|
|
self.room1, self.u_alice, Membership.JOIN,
|
|
|
|
)
|
|
|
|
|
|
|
|
end = yield self.store.get_room_events_max_id()
|
|
|
|
|
|
|
|
results, _ = yield self.store.get_room_events_stream(
|
|
|
|
self.u_bob.to_string(),
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
)
|
|
|
|
|
|
|
|
# We should not get the message, as it happened *after* bob left.
|
|
|
|
self.assertEqual(1, len(results))
|
|
|
|
|
|
|
|
event = results[0]
|
|
|
|
|
2014-11-10 16:29:19 +01:00
|
|
|
self.assertTrue(
|
2014-12-11 16:16:55 +01:00
|
|
|
"prev_content" in event.unsigned,
|
2014-11-10 16:29:19 +01:00
|
|
|
msg="No prev_content key"
|
|
|
|
)
|