diff options
Diffstat (limited to 'tests')
26 files changed, 4818 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000000..40a96afc6f --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/tests/events/__init__.py b/tests/events/__init__.py new file mode 100644 index 0000000000..40a96afc6f --- /dev/null +++ b/tests/events/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/tests/events/test_events.py b/tests/events/test_events.py new file mode 100644 index 0000000000..11d3d09c96 --- /dev/null +++ b/tests/events/test_events.py @@ -0,0 +1,172 @@ +# -*- coding: utf-8 -*- +from synapse.api.events import SynapseEvent + +import unittest + + +class SynapseTemplateCheckTestCase(unittest.TestCase): + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_top_level_keys(self): + template = { + "person": {}, + "friends": ["string"] + } + + content = { + "person": {"name": "bob"}, + "friends": ["jill", "mike"] + } + + event = MockSynapseEvent(template) + self.assertTrue(event.check_json(content, raises=False)) + + content = { + "person": {"name": "bob"}, + "friends": ["jill"], + "enemies": ["mike"] + } + event = MockSynapseEvent(template) + self.assertTrue(event.check_json(content, raises=False)) + + content = { + "person": {"name": "bob"}, + # missing friends + "enemies": ["mike", "jill"] + } + self.assertFalse(event.check_json(content, raises=False)) + + def test_lists(self): + template = { + "person": {}, + "friends": [{"name":"string"}] + } + + content = { + "person": {"name": "bob"}, + "friends": ["jill", "mike"] # should be in objects + } + + event = MockSynapseEvent(template) + self.assertFalse(event.check_json(content, raises=False)) + + content = { + "person": {"name": "bob"}, + "friends": [{"name": "jill"}, {"name": "mike"}] + } + self.assertTrue(event.check_json(content, raises=False)) + + def test_nested_lists(self): + template = { + "results": { + "families": [ + { + "name": "string", + "members": [ + {} + ] + } + ] + } + } + + content = { + "results": { + "families": [ + { + "name": "Smith", + "members": [ + "Alice", "Bob" # wrong types + ] + } + ] + } + } + + event = MockSynapseEvent(template) + self.assertFalse(event.check_json(content, raises=False)) + + content = { + "results": { + "families": [ + { + "name": "Smith", + "members": [ + {"name": "Alice"}, {"name": "Bob"} + ] + } + ] + } + } + self.assertTrue(event.check_json(content, raises=False)) + + def test_nested_keys(self): + template = { + "person": { + "attributes": { + "hair": "string", + "eye": "string" + }, + "age": 0, + "fav_books": ["string"] + } + } + event = MockSynapseEvent(template) + + content = { + "person": { + "attributes": { + "hair": "brown", + "eye": "green", + "skin": "purple" + }, + "age": 33, + "fav_books": ["lotr", "hobbit"], + "fav_music": ["abba", "beatles"] + } + } + + self.assertTrue(event.check_json(content, raises=False)) + + content = { + "person": { + "attributes": { + "hair": "brown" + # missing eye + }, + "age": 33, + "fav_books": ["lotr", "hobbit"], + "fav_music": ["abba", "beatles"] + } + } + + self.assertFalse(event.check_json(content, raises=False)) + + content = { + "person": { + "attributes": { + "hair": "brown", + "eye": "green", + "skin": "purple" + }, + "age": 33, + "fav_books": "nothing", # should be a list + } + } + + self.assertFalse(event.check_json(content, raises=False)) + + +class MockSynapseEvent(SynapseEvent): + + def __init__(self, template): + self.template = template + + def get_content_template(self): + return self.template + diff --git a/tests/federation/__init__.py b/tests/federation/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/federation/__init__.py diff --git a/tests/federation/test_federation.py b/tests/federation/test_federation.py new file mode 100644 index 0000000000..1792f9de56 --- /dev/null +++ b/tests/federation/test_federation.py @@ -0,0 +1,240 @@ +# trial imports +from twisted.internet import defer +from twisted.trial import unittest + +# python imports +from mock import Mock +import logging + +from ..utils import MockHttpServer + +from synapse.server import HomeServer +from synapse.federation import initialize_http_replication +from synapse.federation.units import Pdu +from synapse.storage.pdu import PduTuple, PduEntry + + +logging.getLogger().addHandler(logging.NullHandler()) + + +def make_pdu(prev_pdus=[], **kwargs): + """Provide some default fields for making a PduTuple.""" + pdu_fields = { + "is_state": False, + "unrecognized_keys": [], + "outlier": False, + "have_processed": True, + "state_key": None, + "power_level": None, + "prev_state_id": None, + "prev_state_origin": None, + } + pdu_fields.update(kwargs) + + return PduTuple(PduEntry(**pdu_fields), prev_pdus) + + +class MockClock(object): + now = 1000 + + def time(self): + return self.now + + def time_msec(self): + return self.time() * 1000 + + +class FederationTestCase(unittest.TestCase): + def setUp(self): + self.mock_http_server = MockHttpServer() + self.mock_http_client = Mock(spec=[ + "put_json", + ]) + self.mock_persistence = Mock(spec=[ + "get_current_state_for_context", + "get_pdu", + "persist_pdu", + "update_min_depth_for_context", + "prep_send_transaction", + "delivered_txn", + "get_received_txn_response", + "set_received_txn_response", + ]) + self.mock_persistence.get_received_txn_response.return_value = ( + defer.succeed(None) + ) + self.clock = MockClock() + hs = HomeServer("test", + http_server=self.mock_http_server, + http_client=self.mock_http_client, + db_pool=None, + datastore=self.mock_persistence, + clock=self.clock, + ) + self.federation = initialize_http_replication(hs) + self.distributor = hs.get_distributor() + + @defer.inlineCallbacks + def test_get_state(self): + self.mock_persistence.get_current_state_for_context.return_value = ( + defer.succeed([]) + ) + + # Empty context initially + (code, response) = yield self.mock_http_server.trigger("GET", + "/state/my-context/", None) + self.assertEquals(200, code) + self.assertFalse(response["pdus"]) + + # Now lets give the context some state + self.mock_persistence.get_current_state_for_context.return_value = ( + defer.succeed([ + make_pdu( + pdu_id="the-pdu-id", + origin="red", + context="my-context", + pdu_type="m.topic", + ts=123456789000, + depth=1, + is_state=True, + content_json='{"topic":"The topic"}', + state_key="", + power_level=1000, + prev_state_id="last-pdu-id", + prev_state_origin="blue", + ), + ]) + ) + + (code, response) = yield self.mock_http_server.trigger("GET", + "/state/my-context/", None) + self.assertEquals(200, code) + self.assertEquals(1, len(response["pdus"])) + + @defer.inlineCallbacks + def test_get_pdu(self): + self.mock_persistence.get_pdu.return_value = ( + defer.succeed(None) + ) + + (code, response) = yield self.mock_http_server.trigger("GET", + "/pdu/red/abc123def456/", None) + self.assertEquals(404, code) + + # Now insert such a PDU + self.mock_persistence.get_pdu.return_value = ( + defer.succeed( + make_pdu( + pdu_id="abc123def456", + origin="red", + context="my-context", + pdu_type="m.text", + ts=123456789001, + depth=1, + content_json='{"text":"Here is the message"}', + ) + ) + ) + + (code, response) = yield self.mock_http_server.trigger("GET", + "/pdu/red/abc123def456/", None) + self.assertEquals(200, code) + self.assertEquals(1, len(response["pdus"])) + self.assertEquals("m.text", response["pdus"][0]["pdu_type"]) + + @defer.inlineCallbacks + def test_send_pdu(self): + self.mock_http_client.put_json.return_value = defer.succeed( + (200, "OK") + ) + + pdu = Pdu( + pdu_id="abc123def456", + origin="red", + destinations=["remote"], + context="my-context", + ts=123456789002, + pdu_type="m.test", + content={"testing": "content here"}, + depth=1, + ) + + yield self.federation.send_pdu(pdu) + + self.mock_http_client.put_json.assert_called_with( + "remote", + path="/send/1000000/", + data={ + "ts": 1000000, + "origin": "test", + "pdus": [ + { + "origin": "red", + "pdu_id": "abc123def456", + "prev_pdus": [], + "ts": 123456789002, + "context": "my-context", + "pdu_type": "m.test", + "is_state": False, + "content": {"testing": "content here"}, + "depth": 1, + }, + ] + } + ) + + @defer.inlineCallbacks + def test_send_edu(self): + self.mock_http_client.put_json.return_value = defer.succeed( + (200, "OK") + ) + + yield self.federation.send_edu( + destination="remote", + edu_type="m.test", + content={"testing": "content here"}, + ) + + # MockClock ensures we can guess these timestamps + self.mock_http_client.put_json.assert_called_with( + "remote", + path="/send/1000000/", + data={ + "origin": "test", + "ts": 1000000, + "pdus": [], + "edus": [ + { + "origin": "test", + "destination": "remote", + "edu_type": "m.test", + "content": {"testing": "content here"}, + } + ], + }) + + @defer.inlineCallbacks + def test_recv_edu(self): + recv_observer = Mock() + recv_observer.return_value = defer.succeed(()) + + self.federation.register_edu_handler("m.test", recv_observer) + + yield self.mock_http_server.trigger("PUT", "/send/1001000/", + """{ + "origin": "remote", + "ts": 1001000, + "pdus": [], + "edus": [ + { + "origin": "remote", + "destination": "test", + "edu_type": "m.test", + "content": {"testing": "reply here"} + } + ] + }""") + + recv_observer.assert_called_with( + "remote", {"testing": "reply here"} + ) diff --git a/tests/federation/test_pdu_codec.py b/tests/federation/test_pdu_codec.py new file mode 100644 index 0000000000..688182fa5b --- /dev/null +++ b/tests/federation/test_pdu_codec.py @@ -0,0 +1,146 @@ +# -*- coding: utf-8 -*- +from twisted.trial import unittest + +from synapse.federation.pdu_codec import ( + PduCodec, encode_event_id, decode_event_id +) +from synapse.federation.units import Pdu +#from synapse.api.events.room import MessageEvent + +from synapse.server import HomeServer + +from mock import Mock + + +class PduCodecTestCase(unittest.TestCase): + def setUp(self): + self.hs = HomeServer("blargle.net") + self.event_factory = self.hs.get_event_factory() + + self.codec = PduCodec(self.hs) + + def test_decode_event_id(self): + self.assertEquals( + ("foo", "bar.com"), + decode_event_id("foo@bar.com", "A") + ) + + self.assertEquals( + ("foo", "bar.com"), + decode_event_id("foo", "bar.com") + ) + + def test_encode_event_id(self): + self.assertEquals("A@B", encode_event_id("A", "B")) + + def test_codec_event_id(self): + event_id = "aa@bb.com" + + self.assertEquals( + event_id, + encode_event_id(*decode_event_id(event_id, None)) + ) + + pdu_id = ("aa", "bb.com") + + self.assertEquals( + pdu_id, + decode_event_id(encode_event_id(*pdu_id), None) + ) + + def test_event_from_pdu(self): + pdu = Pdu( + pdu_id="foo", + context="rooooom", + pdu_type="m.room.message", + origin="bar.com", + ts=12345, + depth=5, + prev_pdus=[("alice", "bob.com")], + is_state=False, + content={"msgtype": u"test"}, + ) + + event = self.codec.event_from_pdu(pdu) + + self.assertEquals("foo@bar.com", event.event_id) + self.assertEquals(pdu.context, event.room_id) + self.assertEquals(pdu.is_state, event.is_state) + self.assertEquals(pdu.depth, event.depth) + self.assertEquals(["alice@bob.com"], event.prev_events) + self.assertEquals(pdu.content, event.content) + + def test_pdu_from_event(self): + event = self.event_factory.create_event( + etype="m.room.message", + event_id="gargh_id", + room_id="rooom", + user_id="sender", + content={"msgtype": u"test"}, + ) + + pdu = self.codec.pdu_from_event(event) + + self.assertEquals(event.event_id, pdu.pdu_id) + self.assertEquals(self.hs.hostname, pdu.origin) + self.assertEquals(event.room_id, pdu.context) + self.assertEquals(event.content, pdu.content) + self.assertEquals(event.type, pdu.pdu_type) + + event = self.event_factory.create_event( + etype="m.room.message", + event_id="gargh_id@bob.com", + room_id="rooom", + user_id="sender", + content={"msgtype": u"test"}, + ) + + pdu = self.codec.pdu_from_event(event) + + self.assertEquals("gargh_id", pdu.pdu_id) + self.assertEquals("bob.com", pdu.origin) + self.assertEquals(event.room_id, pdu.context) + self.assertEquals(event.content, pdu.content) + self.assertEquals(event.type, pdu.pdu_type) + + def test_event_from_state_pdu(self): + pdu = Pdu( + pdu_id="foo", + context="rooooom", + pdu_type="m.room.topic", + origin="bar.com", + ts=12345, + depth=5, + prev_pdus=[("alice", "bob.com")], + is_state=True, + content={"topic": u"test"}, + state_key="", + ) + + event = self.codec.event_from_pdu(pdu) + + self.assertEquals("foo@bar.com", event.event_id) + self.assertEquals(pdu.context, event.room_id) + self.assertEquals(pdu.is_state, event.is_state) + self.assertEquals(pdu.depth, event.depth) + self.assertEquals(["alice@bob.com"], event.prev_events) + self.assertEquals(pdu.content, event.content) + self.assertEquals(pdu.state_key, event.state_key) + + def test_pdu_from_state_event(self): + event = self.event_factory.create_event( + etype="m.room.topic", + event_id="gargh_id", + room_id="rooom", + user_id="sender", + content={"topic": u"test"}, + ) + + pdu = self.codec.pdu_from_event(event) + + self.assertEquals(event.event_id, pdu.pdu_id) + self.assertEquals(self.hs.hostname, pdu.origin) + self.assertEquals(event.room_id, pdu.context) + self.assertEquals(event.content, pdu.content) + self.assertEquals(event.type, pdu.pdu_type) + self.assertEquals(event.state_key, pdu.state_key) diff --git a/tests/handlers/__init__.py b/tests/handlers/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/handlers/__init__.py diff --git a/tests/handlers/test_federation.py b/tests/handlers/test_federation.py new file mode 100644 index 0000000000..880cfb47b8 --- /dev/null +++ b/tests/handlers/test_federation.py @@ -0,0 +1,107 @@ +# -*- coding: utf-8 -*- + +from twisted.internet import defer +from twisted.trial import unittest + +from synapse.api.events.room import ( + InviteJoinEvent, MessageEvent, RoomMemberEvent +) +from synapse.api.constants import Membership +from synapse.handlers.federation import FederationHandler +from synapse.server import HomeServer + +from mock import NonCallableMock + +import logging + +logging.getLogger().addHandler(logging.NullHandler()) + + +class FederationTestCase(unittest.TestCase): + + def setUp(self): + self.hostname = "test" + hs = HomeServer( + self.hostname, + db_pool=None, + datastore=NonCallableMock(spec_set=[ + "persist_event", + "store_room", + ]), + http_server=NonCallableMock(), + http_client=NonCallableMock(spec_set=[]), + notifier=NonCallableMock(spec_set=["on_new_room_event"]), + handlers=NonCallableMock(spec_set=[ + "room_member_handler", + "federation_handler", + ]), + ) + + self.datastore = hs.get_datastore() + self.handlers = hs.get_handlers() + self.notifier = hs.get_notifier() + self.hs = hs + + self.handlers.federation_handler = FederationHandler(self.hs) + + @defer.inlineCallbacks + def test_msg(self): + event = self.hs.get_event_factory().create_event( + etype=MessageEvent.TYPE, + msg_id="bob", + room_id="foo", + content={"msgtype": u"fooo"}, + ) + + store_id = "ASD" + self.datastore.persist_event.return_value = defer.succeed(store_id) + + yield self.handlers.federation_handler.on_receive(event, False) + + self.datastore.persist_event.assert_called_once_with(event) + self.notifier.on_new_room_event.assert_called_once_with( + event, store_id) + + @defer.inlineCallbacks + def test_invite_join_target_this(self): + room_id = "foo" + user_id = "@bob:red" + + event = self.hs.get_event_factory().create_event( + etype=InviteJoinEvent.TYPE, + user_id=user_id, + target_host=self.hostname, + room_id=room_id, + content={}, + ) + + yield self.handlers.federation_handler.on_receive(event, False) + + mem_handler = self.handlers.room_member_handler + self.assertEquals(1, mem_handler.change_membership.call_count) + self.assertEquals(True, mem_handler.change_membership.call_args[0][1]) + + new_event = mem_handler.change_membership.call_args[0][0] + self.assertEquals(RoomMemberEvent.TYPE, new_event.type) + self.assertEquals(room_id, new_event.room_id) + self.assertEquals(user_id, new_event.target_user_id) + self.assertEquals(user_id, new_event.state_key) + self.assertEquals(Membership.JOIN, new_event.membership) + + @defer.inlineCallbacks + def test_invite_join_target_other(self): + room_id = "foo" + user_id = "@bob:red" + + event = self.hs.get_event_factory().create_event( + etype=InviteJoinEvent.TYPE, + user_id=user_id, + target_user_id="@red:not%s" % self.hostname, + room_id=room_id, + content={}, + ) + + yield self.handlers.federation_handler.on_receive(event, False) + + mem_handler = self.handlers.room_member_handler + self.assertEquals(0, mem_handler.change_membership.call_count) diff --git a/tests/handlers/test_presence.py b/tests/handlers/test_presence.py new file mode 100644 index 0000000000..e814357520 --- /dev/null +++ b/tests/handlers/test_presence.py @@ -0,0 +1,884 @@ +# -*- coding: utf-8 -*- + +from twisted.trial import unittest +from twisted.internet import defer + +from mock import Mock, call, ANY +import logging + +from synapse.server import HomeServer +from synapse.api.constants import PresenceState +from synapse.api.errors import SynapseError +from synapse.handlers.presence import PresenceHandler, UserPresenceCache + + +OFFLINE = PresenceState.OFFLINE +BUSY = PresenceState.BUSY +ONLINE = PresenceState.ONLINE + + +logging.getLogger().addHandler(logging.NullHandler()) + + +class MockReplication(object): + def __init__(self): + self.edu_handlers = {} + + def register_edu_handler(self, edu_type, handler): + self.edu_handlers[edu_type] = handler + + def received_edu(self, origin, edu_type, content): + self.edu_handlers[edu_type](origin, content) + + +class JustPresenceHandlers(object): + def __init__(self, hs): + self.presence_handler = PresenceHandler(hs) + + +class PresenceStateTestCase(unittest.TestCase): + """ Tests presence management. """ + + def setUp(self): + hs = HomeServer("test", + db_pool=None, + datastore=Mock(spec=[ + "get_presence_state", + "set_presence_state", + "add_presence_list_pending", + "set_presence_list_accepted", + ]), + handlers=None, + http_server=Mock(), + http_client=None, + ) + hs.handlers = JustPresenceHandlers(hs) + + self.datastore = hs.get_datastore() + + def is_presence_visible(observed_localpart, observer_userid): + allow = (observed_localpart == "apple" and + observer_userid == "@banana:test" + ) + return defer.succeed(allow) + self.datastore.is_presence_visible = is_presence_visible + + # Some local users to test with + self.u_apple = hs.parse_userid("@apple:test") + self.u_banana = hs.parse_userid("@banana:test") + self.u_clementine = hs.parse_userid("@clementine:test") + + self.handler = hs.get_handlers().presence_handler + + hs.handlers.room_member_handler = Mock(spec=[ + "get_rooms_for_user", + ]) + hs.handlers.room_member_handler.get_rooms_for_user = ( + lambda u: defer.succeed([])) + + self.mock_start = Mock() + self.mock_stop = Mock() + + self.handler.start_polling_presence = self.mock_start + self.handler.stop_polling_presence = self.mock_stop + + @defer.inlineCallbacks + def test_get_my_state(self): + mocked_get = self.datastore.get_presence_state + mocked_get.return_value = defer.succeed( + {"state": ONLINE, "status_msg": "Online"} + ) + + state = yield self.handler.get_state( + target_user=self.u_apple, auth_user=self.u_apple + ) + + self.assertEquals({"state": ONLINE, "status_msg": "Online"}, + state + ) + mocked_get.assert_called_with("apple") + + @defer.inlineCallbacks + def test_get_allowed_state(self): + mocked_get = self.datastore.get_presence_state + mocked_get.return_value = defer.succeed( + {"state": ONLINE, "status_msg": "Online"} + ) + + state = yield self.handler.get_state( + target_user=self.u_apple, auth_user=self.u_banana + ) + + self.assertEquals({"state": ONLINE, "status_msg": "Online"}, + state + ) + mocked_get.assert_called_with("apple") + + @defer.inlineCallbacks + def test_get_disallowed_state(self): + mocked_get = self.datastore.get_presence_state + mocked_get.return_value = defer.succeed( + {"state": ONLINE, "status_msg": "Online"} + ) + + yield self.assertFailure( + self.handler.get_state( + target_user=self.u_apple, auth_user=self.u_clementine + ), + SynapseError + ) + + @defer.inlineCallbacks + def test_set_my_state(self): + mocked_set = self.datastore.set_presence_state + mocked_set.return_value = defer.succeed({"state": OFFLINE}) + + yield self.handler.set_state( + target_user=self.u_apple, auth_user=self.u_apple, + state={"state": BUSY, "status_msg": "Away"}) + + mocked_set.assert_called_with("apple", + {"state": 1, "status_msg": "Away"}) + self.mock_start.assert_called_with(self.u_apple, + state={"state": 1, "status_msg": "Away"}) + + yield self.handler.set_state( + target_user=self.u_apple, auth_user=self.u_apple, + state={"state": OFFLINE}) + + self.mock_stop.assert_called_with(self.u_apple) + + +class PresenceInvitesTestCase(unittest.TestCase): + """ Tests presence management. """ + + def setUp(self): + self.replication = MockReplication() + self.replication.send_edu = Mock() + + hs = HomeServer("test", + db_pool=None, + datastore=Mock(spec=[ + "has_presence_state", + "allow_presence_visible", + "add_presence_list_pending", + "set_presence_list_accepted", + "get_presence_list", + "del_presence_list", + ]), + handlers=None, + http_server=Mock(), + http_client=None, + replication_layer=self.replication + ) + hs.handlers = JustPresenceHandlers(hs) + + self.datastore = hs.get_datastore() + + def has_presence_state(user_localpart): + return defer.succeed( + user_localpart in ("apple", "banana")) + self.datastore.has_presence_state = has_presence_state + + # Some local users to test with + self.u_apple = hs.parse_userid("@apple:test") + self.u_banana = hs.parse_userid("@banana:test") + # ID of a local user that does not exist + self.u_durian = hs.parse_userid("@durian:test") + + # A remote user + self.u_cabbage = hs.parse_userid("@cabbage:elsewhere") + + self.handler = hs.get_handlers().presence_handler + + self.mock_start = Mock() + self.mock_stop = Mock() + + self.handler.start_polling_presence = self.mock_start + self.handler.stop_polling_presence = self.mock_stop + + @defer.inlineCallbacks + def test_invite_local(self): + # TODO(paul): This test will likely break if/when real auth permissions + # are added; for now the HS will always accept any invite + + yield self.handler.send_invite( + observer_user=self.u_apple, observed_user=self.u_banana) + + self.datastore.add_presence_list_pending.assert_called_with( + "apple", "@banana:test") + self.datastore.allow_presence_visible.assert_called_with( + "banana", "@apple:test") + self.datastore.set_presence_list_accepted.assert_called_with( + "apple", "@banana:test") + + self.mock_start.assert_called_with( + self.u_apple, target_user=self.u_banana) + + @defer.inlineCallbacks + def test_invite_local_nonexistant(self): + yield self.handler.send_invite( + observer_user=self.u_apple, observed_user=self.u_durian) + + self.datastore.add_presence_list_pending.assert_called_with( + "apple", "@durian:test") + self.datastore.del_presence_list.assert_called_with( + "apple", "@durian:test") + + @defer.inlineCallbacks + def test_invite_remote(self): + self.replication.send_edu.return_value = defer.succeed((200, "OK")) + + yield self.handler.send_invite( + observer_user=self.u_apple, observed_user=self.u_cabbage) + + self.datastore.add_presence_list_pending.assert_called_with( + "apple", "@cabbage:elsewhere") + + self.replication.send_edu.assert_called_with( + destination="elsewhere", + edu_type="m.presence_invite", + content={ + "observer_user": "@apple:test", + "observed_user": "@cabbage:elsewhere", + } + ) + + @defer.inlineCallbacks + def test_accept_remote(self): + # TODO(paul): This test will likely break if/when real auth permissions + # are added; for now the HS will always accept any invite + self.replication.send_edu.return_value = defer.succeed((200, "OK")) + + yield self.replication.received_edu( + "elsewhere", "m.presence_invite", { + "observer_user": "@cabbage:elsewhere", + "observed_user": "@apple:test", + } + ) + + self.datastore.allow_presence_visible.assert_called_with( + "apple", "@cabbage:elsewhere") + + self.replication.send_edu.assert_called_with( + destination="elsewhere", + edu_type="m.presence_accept", + content={ + "observer_user": "@cabbage:elsewhere", + "observed_user": "@apple:test", + } + ) + + @defer.inlineCallbacks + def test_invited_remote_nonexistant(self): + self.replication.send_edu.return_value = defer.succeed((200, "OK")) + + yield self.replication.received_edu( + "elsewhere", "m.presence_invite", { + "observer_user": "@cabbage:elsewhere", + "observed_user": "@durian:test", + } + ) + + self.replication.send_edu.assert_called_with( + destination="elsewhere", + edu_type="m.presence_deny", + content={ + "observer_user": "@cabbage:elsewhere", + "observed_user": "@durian:test", + } + ) + + @defer.inlineCallbacks + def test_accepted_remote(self): + yield self.replication.received_edu( + "elsewhere", "m.presence_accept", { + "observer_user": "@apple:test", + "observed_user": "@cabbage:elsewhere", + } + ) + + self.datastore.set_presence_list_accepted.assert_called_with( + "apple", "@cabbage:elsewhere") + + self.mock_start.assert_called_with( + self.u_apple, target_user=self.u_cabbage) + + @defer.inlineCallbacks + def test_denied_remote(self): + yield self.replication.received_edu( + "elsewhere", "m.presence_deny", { + "observer_user": "@apple:test", + "observed_user": "@eggplant:elsewhere", + } + ) + + self.datastore.del_presence_list.assert_called_with( + "apple", "@eggplant:elsewhere") + + @defer.inlineCallbacks + def test_drop_local(self): + yield self.handler.drop( + observer_user=self.u_apple, observed_user=self.u_banana) + + self.datastore.del_presence_list.assert_called_with( + "apple", "@banana:test") + + self.mock_stop.assert_called_with( + self.u_apple, target_user=self.u_banana) + + @defer.inlineCallbacks + def test_get_presence_list(self): + self.datastore.get_presence_list.return_value = defer.succeed( + [{"observed_user_id": "@banana:test"}] + ) + + presence = yield self.handler.get_presence_list( + observer_user=self.u_apple) + + self.assertEquals([{"observed_user": self.u_banana, + "state": OFFLINE}], presence) + + self.datastore.get_presence_list.assert_called_with("apple", + accepted=None) + + + self.datastore.get_presence_list.return_value = defer.succeed( + [{"observed_user_id": "@banana:test"}] + ) + + presence = yield self.handler.get_presence_list( + observer_user=self.u_apple, accepted=True) + + self.assertEquals([{"observed_user": self.u_banana, + "state": OFFLINE}], presence) + + self.datastore.get_presence_list.assert_called_with("apple", + accepted=True) + + +class PresencePushTestCase(unittest.TestCase): + """ Tests steady-state presence status updates. + + They assert that presence state update messages are pushed around the place + when users change state, presuming that the watches are all established. + + These tests are MASSIVELY fragile currently as they poke internals of the + presence handler; namely the _local_pushmap and _remote_recvmap. + BE WARNED... + """ + def setUp(self): + self.replication = MockReplication() + self.replication.send_edu = Mock() + self.replication.send_edu.return_value = defer.succeed((200, "OK")) + + hs = HomeServer("test", + db_pool=None, + datastore=Mock(spec=[ + "set_presence_state", + ]), + handlers=None, + http_server=Mock(), + http_client=None, + replication_layer=self.replication, + ) + hs.handlers = JustPresenceHandlers(hs) + + self.mock_update_client = Mock() + self.mock_update_client.return_value = defer.succeed(None) + + self.datastore = hs.get_datastore() + self.handler = hs.get_handlers().presence_handler + self.handler.push_update_to_clients = self.mock_update_client + + # Mock the RoomMemberHandler + hs.handlers.room_member_handler = Mock(spec=[ + "get_rooms_for_user", + "get_room_members", + ]) + self.room_member_handler = hs.handlers.room_member_handler + + self.room_members = [] + + def get_rooms_for_user(user): + if user in self.room_members: + return defer.succeed(["a-room"]) + else: + return defer.succeed([]) + self.room_member_handler.get_rooms_for_user = get_rooms_for_user + + def get_room_members(room_id): + if room_id == "a-room": + return defer.succeed(self.room_members) + else: + return defer.succeed([]) + self.room_member_handler.get_room_members = get_room_members + + @defer.inlineCallbacks + def fetch_room_distributions_into(room_id, localusers=None, + remotedomains=None, ignore_user=None): + + members = yield get_room_members(room_id) + for member in members: + if ignore_user is not None and member == ignore_user: + continue + + if member.is_mine: + if localusers is not None: + localusers.add(member) + else: + if remotedomains is not None: + remotedomains.add(member.domain) + self.room_member_handler.fetch_room_distributions_into = ( + fetch_room_distributions_into) + + def get_presence_list(user_localpart, accepted=None): + if user_localpart == "apple": + return defer.succeed([ + {"observed_user_id": "@banana:test"}, + {"observed_user_id": "@clementine:test"}, + ]) + else: + return defer.succeed([]) + self.datastore.get_presence_list = get_presence_list + + def is_presence_visible(observer_userid, observed_localpart): + if (observed_localpart == "clementine" and + observer_userid == "@banana:test"): + return False + return False + self.datastore.is_presence_visible = is_presence_visible + + self.distributor = hs.get_distributor() + self.distributor.declare("user_joined_room") + + # Some local users to test with + self.u_apple = hs.parse_userid("@apple:test") + self.u_banana = hs.parse_userid("@banana:test") + self.u_clementine = hs.parse_userid("@clementine:test") + self.u_elderberry = hs.parse_userid("@elderberry:test") + + # Remote user + self.u_onion = hs.parse_userid("@onion:farm") + self.u_potato = hs.parse_userid("@potato:remote") + + @defer.inlineCallbacks + def test_push_local(self): + self.room_members = [self.u_apple, self.u_elderberry] + + self.datastore.set_presence_state.return_value = defer.succeed( + {"state": ONLINE}) + + # TODO(paul): Gut-wrenching + self.handler._user_cachemap[self.u_apple] = UserPresenceCache() + apple_set = self.handler._local_pushmap.setdefault("apple", set()) + apple_set.add(self.u_banana) + apple_set.add(self.u_clementine) + + yield self.handler.set_state(self.u_apple, self.u_apple, + {"state": ONLINE}) + + self.mock_update_client.assert_has_calls([ + call(observer_user=self.u_apple, + observed_user=self.u_apple, + statuscache=ANY), # self-reflection + call(observer_user=self.u_banana, + observed_user=self.u_apple, + statuscache=ANY), + call(observer_user=self.u_clementine, + observed_user=self.u_apple, + statuscache=ANY), + call(observer_user=self.u_elderberry, + observed_user=self.u_apple, + statuscache=ANY), + ], any_order=True) + self.mock_update_client.reset_mock() + + presence = yield self.handler.get_presence_list( + observer_user=self.u_apple, accepted=True) + + self.assertEquals([ + {"observed_user": self.u_banana, "state": OFFLINE}, + {"observed_user": self.u_clementine, "state": OFFLINE}], + presence) + + yield self.handler.set_state(self.u_banana, self.u_banana, + {"state": ONLINE}) + + presence = yield self.handler.get_presence_list( + observer_user=self.u_apple, accepted=True) + + self.assertEquals([ + {"observed_user": self.u_banana, "state": ONLINE}, + {"observed_user": self.u_clementine, "state": OFFLINE}], + presence) + + self.mock_update_client.assert_has_calls([ + call(observer_user=self.u_banana, + observed_user=self.u_banana, + statuscache=ANY), # self-reflection + ]) # and no others... + + @defer.inlineCallbacks + def test_push_remote(self): + self.room_members = [self.u_apple, self.u_onion] + + self.datastore.set_presence_state.return_value = defer.succeed( + {"state": ONLINE}) + + # TODO(paul): Gut-wrenching + self.handler._user_cachemap[self.u_apple] = UserPresenceCache() + apple_set = self.handler._remote_sendmap.setdefault("apple", set()) + apple_set.add(self.u_potato.domain) + + yield self.handler.set_state(self.u_apple, self.u_apple, + {"state": ONLINE}) + + self.replication.send_edu.assert_has_calls([ + call( + destination="remote", + edu_type="m.presence", + content={ + "push": [ + {"user_id": "@apple:test", + "state": 2}, + ], + }), + call( + destination="farm", + edu_type="m.presence", + content={ + "push": [ + {"user_id": "@apple:test", + "state": 2}, + ], + }) + ], any_order=True) + + @defer.inlineCallbacks + def test_recv_remote(self): + # TODO(paul): Gut-wrenching + potato_set = self.handler._remote_recvmap.setdefault(self.u_potato, + set()) + potato_set.add(self.u_apple) + + self.room_members = [self.u_banana, self.u_potato] + + yield self.replication.received_edu( + "remote", "m.presence", { + "push": [ + {"user_id": "@potato:remote", + "state": 2}, + ], + } + ) + + self.mock_update_client.assert_has_calls([ + call(observer_user=self.u_apple, + observed_user=self.u_potato, + statuscache=ANY), + call(observer_user=self.u_banana, + observed_user=self.u_potato, + statuscache=ANY), + ], any_order=True) + + state = yield self.handler.get_state(self.u_potato, self.u_apple) + + self.assertEquals({"state": ONLINE}, state) + + @defer.inlineCallbacks + def test_join_room_local(self): + self.room_members = [self.u_apple, self.u_banana] + + yield self.distributor.fire("user_joined_room", self.u_elderberry, + "a-room" + ) + + self.mock_update_client.assert_has_calls([ + # Apple and Elderberry see each other + call(observer_user=self.u_apple, + observed_user=self.u_elderberry, + statuscache=ANY), + call(observer_user=self.u_elderberry, + observed_user=self.u_apple, + statuscache=ANY), + # Banana and Elderberry see each other + call(observer_user=self.u_banana, + observed_user=self.u_elderberry, + statuscache=ANY), + call(observer_user=self.u_elderberry, + observed_user=self.u_banana, + statuscache=ANY), + ], any_order=True) + + @defer.inlineCallbacks + def test_join_room_remote(self): + ## Sending local user state to a newly-joined remote user + + # TODO(paul): Gut-wrenching + self.handler._user_cachemap[self.u_apple] = UserPresenceCache() + self.handler._user_cachemap[self.u_apple].update( + {"state": PresenceState.ONLINE}, self.u_apple) + self.room_members = [self.u_apple, self.u_banana] + + yield self.distributor.fire("user_joined_room", self.u_potato, + "a-room" + ) + + self.replication.send_edu.assert_has_calls([ + call( + destination="remote", + edu_type="m.presence", + content={ + "push": [ + {"user_id": "@apple:test", + "state": 2}, + ], + }), + call( + destination="remote", + edu_type="m.presence", + content={ + "push": [ + {"user_id": "@banana:test", + "state": 0}, + ], + }), + ], any_order=True) + + self.replication.send_edu.reset_mock() + + ## Sending newly-joined local user state to remote users + + self.handler._user_cachemap[self.u_clementine] = UserPresenceCache() + self.handler._user_cachemap[self.u_clementine].update( + {"state": PresenceState.ONLINE}, self.u_clementine) + self.room_members.append(self.u_potato) + + yield self.distributor.fire("user_joined_room", self.u_clementine, + "a-room" + ) + + self.replication.send_edu.assert_has_calls( + call( + destination="remote", + edu_type="m.presence", + content={ + "push": [ + {"user_id": "@clementine:test", + "state": 2}, + ], + }), + ) + + +class PresencePollingTestCase(unittest.TestCase): + """ Tests presence status polling. """ + + # For this test, we have three local users; apple is watching and is + # watched by the other two, but the others don't watch each other. + # Additionally clementine is watching a remote user. + PRESENCE_LIST = { + 'apple': [ "@banana:test", "@clementine:test" ], + 'banana': [ "@apple:test" ], + 'clementine': [ "@apple:test", "@potato:remote" ], + } + + + def setUp(self): + self.replication = MockReplication() + self.replication.send_edu = Mock() + + hs = HomeServer("test", + db_pool=None, + datastore=Mock(spec=[]), + handlers=None, + http_server=Mock(), + http_client=None, + replication_layer=self.replication, + ) + hs.handlers = JustPresenceHandlers(hs) + + self.datastore = hs.get_datastore() + + self.mock_update_client = Mock() + self.mock_update_client.return_value = defer.succeed(None) + + self.handler = hs.get_handlers().presence_handler + self.handler.push_update_to_clients = self.mock_update_client + + hs.handlers.room_member_handler = Mock(spec=[ + "get_rooms_for_user", + ]) + # For this test no users are ever in rooms + def get_rooms_for_user(user): + return defer.succeed([]) + hs.handlers.room_member_handler.get_rooms_for_user = get_rooms_for_user + + # Mocked database state + # Local users always start offline + self.current_user_state = { + "apple": OFFLINE, + "banana": OFFLINE, + "clementine": OFFLINE, + } + + def get_presence_state(user_localpart): + return defer.succeed( + {"state": self.current_user_state[user_localpart], + "status_msg": None} + ) + self.datastore.get_presence_state = get_presence_state + + def set_presence_state(user_localpart, new_state): + was = self.current_user_state[user_localpart] + self.current_user_state[user_localpart] = new_state["state"] + return defer.succeed({"state": was}) + self.datastore.set_presence_state = set_presence_state + + def get_presence_list(user_localpart, accepted): + return defer.succeed([ + {"observed_user_id": u} for u in + self.PRESENCE_LIST[user_localpart]]) + self.datastore.get_presence_list = get_presence_list + + def is_presence_visible(observed_localpart, observer_userid): + return True + self.datastore.is_presence_visible = is_presence_visible + + # Local users + self.u_apple = hs.parse_userid("@apple:test") + self.u_banana = hs.parse_userid("@banana:test") + self.u_clementine = hs.parse_userid("@clementine:test") + + # Remote users + self.u_potato = hs.parse_userid("@potato:remote") + + @defer.inlineCallbacks + def test_push_local(self): + # apple goes online + yield self.handler.set_state( + target_user=self.u_apple, auth_user=self.u_apple, + state={"state": ONLINE}) + + # apple should see both banana and clementine currently offline + self.mock_update_client.assert_has_calls([ + call(observer_user=self.u_apple, + observed_user=self.u_banana, + statuscache=ANY), + call(observer_user=self.u_apple, + observed_user=self.u_clementine, + statuscache=ANY), + ], any_order=True) + + # Gut-wrenching tests + self.assertTrue("banana" in self.handler._local_pushmap) + self.assertTrue(self.u_apple in self.handler._local_pushmap["banana"]) + self.assertTrue("clementine" in self.handler._local_pushmap) + self.assertTrue(self.u_apple in self.handler._local_pushmap["clementine"]) + + self.mock_update_client.reset_mock() + + # banana goes online + yield self.handler.set_state( + target_user=self.u_banana, auth_user=self.u_banana, + state={"state": ONLINE}) + + # apple and banana should now both see each other online + self.mock_update_client.assert_has_calls([ + call(observer_user=self.u_apple, + observed_user=self.u_banana, + statuscache=ANY), + call(observer_user=self.u_banana, + observed_user=self.u_apple, + statuscache=ANY), + ], any_order=True) + + self.assertTrue("apple" in self.handler._local_pushmap) + self.assertTrue(self.u_banana in self.handler._local_pushmap["apple"]) + + self.mock_update_client.reset_mock() + + # apple goes offline + yield self.handler.set_state( + target_user=self.u_apple, auth_user=self.u_apple, + state={"state": OFFLINE}) + + # banana should now be told apple is offline + self.mock_update_client.assert_has_calls([ + call(observer_user=self.u_banana, + observed_user=self.u_apple, + statuscache=ANY), + ], any_order=True) + + self.assertFalse("banana" in self.handler._local_pushmap) + self.assertFalse("clementine" in self.handler._local_pushmap) + + @defer.inlineCallbacks + def test_remote_poll_send(self): + # clementine goes online + yield self.handler.set_state( + target_user=self.u_clementine, auth_user=self.u_clementine, + state={"state": ONLINE}) + + self.replication.send_edu.assert_called_with( + destination="remote", + edu_type="m.presence", + content={ + "poll": [ "@potato:remote" ], + }, + ) + + # Gut-wrenching tests + self.assertTrue(self.u_potato in self.handler._remote_recvmap) + self.assertTrue(self.u_clementine in + self.handler._remote_recvmap[self.u_potato]) + + self.replication.send_edu.reset_mock() + + # clementine goes offline + yield self.handler.set_state( + target_user=self.u_clementine, auth_user=self.u_clementine, + state={"state": OFFLINE}) + + self.replication.send_edu.assert_called_with( + destination="remote", + edu_type="m.presence", + content={ + "unpoll": [ "@potato:remote" ], + }, + ) + + self.assertFalse(self.u_potato in self.handler._remote_recvmap) + + @defer.inlineCallbacks + def test_remote_poll_receive(self): + yield self.replication.received_edu( + "remote", "m.presence", { + "poll": [ "@banana:test" ], + } + ) + + # Gut-wrenching tests + self.assertTrue(self.u_banana in self.handler._remote_sendmap) + + self.replication.send_edu.assert_called_with( + destination="remote", + edu_type="m.presence", + content={ + "push": [ + {"user_id": "@banana:test", + "state": 0, + "status_msg": None}, + ], + }, + ) + + yield self.replication.received_edu( + "remote", "m.presence", { + "unpoll": [ "@banana:test" ], + } + ) + + # Gut-wrenching tests + self.assertFalse(self.u_banana in self.handler._remote_sendmap) diff --git a/tests/handlers/test_presencelike.py b/tests/handlers/test_presencelike.py new file mode 100644 index 0000000000..c194e4dd72 --- /dev/null +++ b/tests/handlers/test_presencelike.py @@ -0,0 +1,250 @@ +# -*- coding: utf-8 -*- +"""This file contains tests of the "presence-like" data that is shared between +presence and profiles; namely, the displayname and avatar_url.""" + +from twisted.trial import unittest +from twisted.internet import defer + +from mock import Mock, call, ANY +import logging + +from synapse.server import HomeServer +from synapse.api.constants import PresenceState +from synapse.handlers.presence import PresenceHandler +from synapse.handlers.profile import ProfileHandler + + +OFFLINE = PresenceState.OFFLINE +BUSY = PresenceState.BUSY +ONLINE = PresenceState.ONLINE + + +logging.getLogger().addHandler(logging.NullHandler()) + + +class MockReplication(object): + def __init__(self): + self.edu_handlers = {} + + def register_edu_handler(self, edu_type, handler): + self.edu_handlers[edu_type] = handler + + def received_edu(self, origin, edu_type, content): + self.edu_handlers[edu_type](origin, content) + + +class PresenceAndProfileHandlers(object): + def __init__(self, hs): + self.presence_handler = PresenceHandler(hs) + self.profile_handler = ProfileHandler(hs) + + +class PresenceProfilelikeDataTestCase(unittest.TestCase): + + def setUp(self): + hs = HomeServer("test", + db_pool=None, + datastore=Mock(spec=[ + "set_presence_state", + + "set_profile_displayname", + ]), + handlers=None, + http_server=Mock(), + http_client=None, + replication_layer=MockReplication(), + ) + hs.handlers = PresenceAndProfileHandlers(hs) + + self.datastore = hs.get_datastore() + + self.replication = hs.get_replication_layer() + self.replication.send_edu = Mock() + self.replication.send_edu.return_value = defer.succeed((200, "OK")) + + def get_profile_displayname(user_localpart): + return defer.succeed("Frank") + self.datastore.get_profile_displayname = get_profile_displayname + + def get_profile_avatar_url(user_localpart): + return defer.succeed("http://foo") + self.datastore.get_profile_avatar_url = get_profile_avatar_url + + def get_presence_list(user_localpart, accepted=None): + return defer.succeed([ + {"observed_user_id": "@banana:test"}, + {"observed_user_id": "@clementine:test"}, + ]) + self.datastore.get_presence_list = get_presence_list + + self.handlers = hs.get_handlers() + + self.mock_start = Mock() + self.mock_stop = Mock() + + self.mock_update_client = Mock() + self.mock_update_client.return_value = defer.succeed(None) + + self.handlers.presence_handler.start_polling_presence = self.mock_start + self.handlers.presence_handler.stop_polling_presence = self.mock_stop + self.handlers.presence_handler.push_update_to_clients = ( + self.mock_update_client) + + hs.handlers.room_member_handler = Mock(spec=[ + "get_rooms_for_user", + ]) + hs.handlers.room_member_handler.get_rooms_for_user = ( + lambda u: defer.succeed([])) + + # Some local users to test with + self.u_apple = hs.parse_userid("@apple:test") + self.u_banana = hs.parse_userid("@banana:test") + self.u_clementine = hs.parse_userid("@clementine:test") + + # Remote user + self.u_potato = hs.parse_userid("@potato:remote") + + @defer.inlineCallbacks + def test_set_my_state(self): + mocked_set = self.datastore.set_presence_state + mocked_set.return_value = defer.succeed({"state": OFFLINE}) + + yield self.handlers.presence_handler.set_state( + target_user=self.u_apple, auth_user=self.u_apple, + state={"state": BUSY, "status_msg": "Away"}) + + mocked_set.assert_called_with("apple", + {"state": 1, "status_msg": "Away"}) + self.mock_start.assert_called_with(self.u_apple, + state={"state": 1, "status_msg": "Away", + "displayname": "Frank", + "avatar_url": "http://foo"}) + + @defer.inlineCallbacks + def test_push_local(self): + self.datastore.set_presence_state.return_value = defer.succeed( + {"state": ONLINE}) + + # TODO(paul): Gut-wrenching + from synapse.handlers.presence import UserPresenceCache + self.handlers.presence_handler._user_cachemap[self.u_apple] = ( + UserPresenceCache()) + apple_set = self.handlers.presence_handler._local_pushmap.setdefault( + "apple", set()) + apple_set.add(self.u_banana) + apple_set.add(self.u_clementine) + + yield self.handlers.presence_handler.set_state(self.u_apple, + self.u_apple, {"state": ONLINE}) + yield self.handlers.presence_handler.set_state(self.u_banana, + self.u_banana, {"state": ONLINE}) + + presence = yield self.handlers.presence_handler.get_presence_list( + observer_user=self.u_apple, accepted=True) + + self.assertEquals([ + {"observed_user": self.u_banana, "state": ONLINE, + "displayname": "Frank", "avatar_url": "http://foo"}, + {"observed_user": self.u_clementine, "state": OFFLINE}], + presence) + + self.mock_update_client.assert_has_calls([ + call(observer_user=self.u_apple, + observed_user=self.u_apple, + statuscache=ANY), # self-reflection + call(observer_user=self.u_banana, + observed_user=self.u_apple, + statuscache=ANY), + ], any_order=True) + + statuscache = self.mock_update_client.call_args[1]["statuscache"] + self.assertEquals({"state": ONLINE, + "displayname": "Frank", + "avatar_url": "http://foo"}, statuscache.state) + + self.mock_update_client.reset_mock() + + self.datastore.set_profile_displayname.return_value = defer.succeed( + None) + + yield self.handlers.profile_handler.set_displayname(self.u_apple, + self.u_apple, "I am an Apple") + + self.mock_update_client.assert_has_calls([ + call(observer_user=self.u_apple, + observed_user=self.u_apple, + statuscache=ANY), # self-reflection + call(observer_user=self.u_banana, + observed_user=self.u_apple, + statuscache=ANY), + ], any_order=True) + + statuscache = self.mock_update_client.call_args[1]["statuscache"] + self.assertEquals({"state": ONLINE, + "displayname": "I am an Apple", + "avatar_url": "http://foo"}, statuscache.state) + + @defer.inlineCallbacks + def test_push_remote(self): + self.datastore.set_presence_state.return_value = defer.succeed( + {"state": ONLINE}) + + # TODO(paul): Gut-wrenching + from synapse.handlers.presence import UserPresenceCache + self.handlers.presence_handler._user_cachemap[self.u_apple] = ( + UserPresenceCache()) + apple_set = self.handlers.presence_handler._remote_sendmap.setdefault( + "apple", set()) + apple_set.add(self.u_potato.domain) + + yield self.handlers.presence_handler.set_state(self.u_apple, + self.u_apple, {"state": ONLINE}) + + self.replication.send_edu.assert_called_with( + destination="remote", + edu_type="m.presence", + content={ + "push": [ + {"user_id": "@apple:test", + "state": 2, + "displayname": "Frank", + "avatar_url": "http://foo"}, + ], + }, + ) + + @defer.inlineCallbacks + def test_recv_remote(self): + # TODO(paul): Gut-wrenching + potato_set = self.handlers.presence_handler._remote_recvmap.setdefault( + self.u_potato, set()) + potato_set.add(self.u_apple) + + yield self.replication.received_edu( + "remote", "m.presence", { + "push": [ + {"user_id": "@potato:remote", + "state": 2, + "displayname": "Frank", + "avatar_url": "http://foo"}, + ], + } + ) + + self.mock_update_client.assert_called_with( + observer_user=self.u_apple, + observed_user=self.u_potato, + statuscache=ANY) + + statuscache = self.mock_update_client.call_args[1]["statuscache"] + self.assertEquals({"state": ONLINE, + "displayname": "Frank", + "avatar_url": "http://foo"}, statuscache.state) + + state = yield self.handlers.presence_handler.get_state(self.u_potato, + self.u_apple) + + self.assertEquals({"state": ONLINE, + "displayname": "Frank", + "avatar_url": "http://foo"}, + state) diff --git a/tests/handlers/test_profile.py b/tests/handlers/test_profile.py new file mode 100644 index 0000000000..a4408e9fd3 --- /dev/null +++ b/tests/handlers/test_profile.py @@ -0,0 +1,112 @@ +# -*- coding: utf-8 -*- + +from twisted.trial import unittest +from twisted.internet import defer + +from mock import Mock +import logging + +from synapse.api.errors import AuthError +from synapse.server import HomeServer +from synapse.handlers.profile import ProfileHandler + + +logging.getLogger().addHandler(logging.NullHandler()) + + +class ProfileHandlers(object): + def __init__(self, hs): + self.profile_handler = ProfileHandler(hs) + + +class ProfileTestCase(unittest.TestCase): + """ Tests profile management. """ + + def setUp(self): + self.mock_client = Mock(spec=[ + "get_json", + ]) + + hs = HomeServer("test", + db_pool=None, + http_client=self.mock_client, + datastore=Mock(spec=[ + "get_profile_displayname", + "set_profile_displayname", + "get_profile_avatar_url", + "set_profile_avatar_url", + ]), + handlers=None, + http_server=Mock(), + ) + hs.handlers = ProfileHandlers(hs) + + self.datastore = hs.get_datastore() + + self.frank = hs.parse_userid("@1234ABCD:test") + self.bob = hs.parse_userid("@4567:test") + self.alice = hs.parse_userid("@alice:remote") + + self.handler = hs.get_handlers().profile_handler + + # TODO(paul): Icky signal declarings.. booo + hs.get_distributor().declare("changed_presencelike_data") + + @defer.inlineCallbacks + def test_get_my_name(self): + mocked_get = self.datastore.get_profile_displayname + mocked_get.return_value = defer.succeed("Frank") + + displayname = yield self.handler.get_displayname(self.frank) + + self.assertEquals("Frank", displayname) + mocked_get.assert_called_with("1234ABCD") + + @defer.inlineCallbacks + def test_set_my_name(self): + mocked_set = self.datastore.set_profile_displayname + mocked_set.return_value = defer.succeed(()) + + yield self.handler.set_displayname(self.frank, self.frank, "Frank Jr.") + + mocked_set.assert_called_with("1234ABCD", "Frank Jr.") + + @defer.inlineCallbacks + def test_set_my_name_noauth(self): + d = self.handler.set_displayname(self.frank, self.bob, "Frank Jr.") + + yield self.assertFailure(d, AuthError) + + @defer.inlineCallbacks + def test_get_other_name(self): + self.mock_client.get_json.return_value = defer.succeed( + {"displayname": "Alice"}) + + displayname = yield self.handler.get_displayname(self.alice) + + self.assertEquals(displayname, "Alice") + self.mock_client.get_json.assert_called_with( + destination="remote", + path="/matrix/client/api/v1/profile/@alice:remote/displayname" + "?local_only=1" + ) + + @defer.inlineCallbacks + def test_get_my_avatar(self): + mocked_get = self.datastore.get_profile_avatar_url + mocked_get.return_value = defer.succeed("http://my.server/me.png") + + avatar_url = yield self.handler.get_avatar_url(self.frank) + + self.assertEquals("http://my.server/me.png", avatar_url) + mocked_get.assert_called_with("1234ABCD") + + @defer.inlineCallbacks + def test_set_my_avatar(self): + mocked_set = self.datastore.set_profile_avatar_url + mocked_set.return_value = defer.succeed(()) + + yield self.handler.set_avatar_url(self.frank, self.frank, + "http://my.server/pic.gif") + + mocked_set.assert_called_with("1234ABCD", "http://my.server/pic.gif") diff --git a/tests/handlers/test_room.py b/tests/handlers/test_room.py new file mode 100644 index 0000000000..26b233bccd --- /dev/null +++ b/tests/handlers/test_room.py @@ -0,0 +1,363 @@ +# -*- coding: utf-8 -*- + +from twisted.internet import defer +from twisted.trial import unittest + +from synapse.api.events.room import ( + InviteJoinEvent, RoomMemberEvent, RoomConfigEvent +) +from synapse.api.constants import Membership +from synapse.handlers.room import RoomMemberHandler, RoomCreationHandler +from synapse.handlers.profile import ProfileHandler +from synapse.server import HomeServer + +from mock import Mock, NonCallableMock + +import logging + +logging.getLogger().addHandler(logging.NullHandler()) + + +class RoomMemberHandlerTestCase(unittest.TestCase): + + def setUp(self): + self.hostname = "red" + hs = HomeServer( + self.hostname, + db_pool=None, + datastore=NonCallableMock(spec_set=[ + "store_room_member", + "get_joined_hosts_for_room", + "get_room_member", + "get_room", + "store_room", + ]), + http_server=NonCallableMock(), + http_client=NonCallableMock(spec_set=[]), + notifier=NonCallableMock(spec_set=["on_new_room_event"]), + handlers=NonCallableMock(spec_set=[ + "room_member_handler", + "profile_handler", + ]), + auth=NonCallableMock(spec_set=["check"]), + federation=NonCallableMock(spec_set=[ + "handle_new_event", + "get_state_for_room", + ]), + state_handler=NonCallableMock(spec_set=["handle_new_event"]), + ) + + self.datastore = hs.get_datastore() + self.handlers = hs.get_handlers() + self.notifier = hs.get_notifier() + self.federation = hs.get_federation() + self.state_handler = hs.get_state_handler() + self.distributor = hs.get_distributor() + self.hs = hs + + self.handlers.room_member_handler = RoomMemberHandler(self.hs) + self.handlers.profile_handler = ProfileHandler(self.hs) + self.room_member_handler = self.handlers.room_member_handler + + @defer.inlineCallbacks + def test_invite(self): + room_id = "!foo:red" + user_id = "@bob:red" + target_user_id = "@red:blue" + content = {"membership": Membership.INVITE} + + event = self.hs.get_event_factory().create_event( + etype=RoomMemberEvent.TYPE, + user_id=user_id, + target_user_id=target_user_id, + room_id=room_id, + membership=Membership.INVITE, + content=content, + ) + + joined = ["red", "green"] + + self.state_handler.handle_new_event.return_value = defer.succeed(True) + self.datastore.get_joined_hosts_for_room.return_value = ( + defer.succeed(joined) + ) + + store_id = "store_id_fooo" + self.datastore.store_room_member.return_value = defer.succeed(store_id) + + # Actual invocation + yield self.room_member_handler.change_membership(event) + + self.state_handler.handle_new_event.assert_called_once_with(event) + self.federation.handle_new_event.assert_called_once_with(event) + + self.assertEquals( + set(["blue", "red", "green"]), + set(event.destinations) + ) + + self.datastore.store_room_member.assert_called_once_with( + user_id=target_user_id, + sender=user_id, + room_id=room_id, + content=content, + membership=Membership.INVITE, + ) + self.notifier.on_new_room_event.assert_called_once_with( + event, store_id) + + self.assertFalse(self.datastore.get_room.called) + self.assertFalse(self.datastore.store_room.called) + self.assertFalse(self.federation.get_state_for_room.called) + + @defer.inlineCallbacks + def test_simple_join(self): + room_id = "!foo:red" + user_id = "@bob:red" + user = self.hs.parse_userid(user_id) + target_user_id = "@bob:red" + content = {"membership": Membership.JOIN} + + event = self.hs.get_event_factory().create_event( + etype=RoomMemberEvent.TYPE, + user_id=user_id, + target_user_id=target_user_id, + room_id=room_id, + membership=Membership.JOIN, + content=content, + ) + + joined = ["red", "green"] + + self.state_handler.handle_new_event.return_value = defer.succeed(True) + self.datastore.get_joined_hosts_for_room.return_value = ( + defer.succeed(joined) + ) + + store_id = "store_id_fooo" + self.datastore.store_room_member.return_value = defer.succeed(store_id) + self.datastore.get_room.return_value = defer.succeed(1) # Not None. + + prev_state = NonCallableMock() + prev_state.membership = Membership.INVITE + prev_state.sender = "@foo:red" + self.datastore.get_room_member.return_value = defer.succeed(prev_state) + + join_signal_observer = Mock() + self.distributor.observe("user_joined_room", join_signal_observer) + + # Actual invocation + yield self.room_member_handler.change_membership(event) + + self.state_handler.handle_new_event.assert_called_once_with(event) + self.federation.handle_new_event.assert_called_once_with(event) + + self.assertEquals( + set(["red", "green"]), + set(event.destinations) + ) + + self.datastore.store_room_member.assert_called_once_with( + user_id=target_user_id, + sender=user_id, + room_id=room_id, + content=content, + membership=Membership.JOIN, + ) + self.notifier.on_new_room_event.assert_called_once_with( + event, store_id) + + join_signal_observer.assert_called_with( + user=user, room_id=room_id) + + @defer.inlineCallbacks + def STALE_test_invite_join(self): + room_id = "foo" + user_id = "@bob:red" + target_user_id = "@bob:red" + content = {"membership": Membership.JOIN} + + event = self.hs.get_event_factory().create_event( + etype=RoomMemberEvent.TYPE, + user_id=user_id, + target_user_id=target_user_id, + room_id=room_id, + membership=Membership.JOIN, + content=content, + ) + + joined = ["red", "blue", "green"] + + self.state_handler.handle_new_event.return_value = defer.succeed(True) + self.datastore.get_joined_hosts_for_room.return_value = ( + defer.succeed(joined) + ) + + store_id = "store_id_fooo" + self.datastore.store_room_member.return_value = defer.succeed(store_id) + self.datastore.get_room.return_value = defer.succeed(None) + + prev_state = NonCallableMock(name="prev_state") + prev_state.membership = Membership.INVITE + prev_state.sender = "@foo:blue" + self.datastore.get_room_member.return_value = defer.succeed(prev_state) + + # Actual invocation + yield self.room_member_handler.change_membership(event) + + self.datastore.get_room_member.assert_called_once_with( + target_user_id, room_id + ) + + self.assertTrue(self.federation.handle_new_event.called) + args = self.federation.handle_new_event.call_args[0] + invite_join_event = args[0] + + self.assertTrue(InviteJoinEvent.TYPE, invite_join_event.TYPE) + self.assertTrue("blue", invite_join_event.target_host) + self.assertTrue(room_id, invite_join_event.room_id) + self.assertTrue(user_id, invite_join_event.user_id) + self.assertFalse(hasattr(invite_join_event, "state_key")) + + self.assertEquals( + set(["blue"]), + set(invite_join_event.destinations) + ) + + self.federation.get_state_for_room.assert_called_once_with( + "blue", room_id + ) + + self.assertFalse(self.datastore.store_room_member.called) + + self.assertFalse(self.notifier.on_new_room_event.called) + self.assertFalse(self.state_handler.handle_new_event.called) + + @defer.inlineCallbacks + def STALE_test_invite_join_public(self): + room_id = "#foo:blue" + user_id = "@bob:red" + target_user_id = "@bob:red" + content = {"membership": Membership.JOIN} + + event = self.hs.get_event_factory().create_event( + etype=RoomMemberEvent.TYPE, + user_id=user_id, + target_user_id=target_user_id, + room_id=room_id, + membership=Membership.JOIN, + content=content, + ) + + joined = ["red", "blue", "green"] + + self.state_handler.handle_new_event.return_value = defer.succeed(True) + self.datastore.get_joined_hosts_for_room.return_value = ( + defer.succeed(joined) + ) + + store_id = "store_id_fooo" + self.datastore.store_room_member.return_value = defer.succeed(store_id) + self.datastore.get_room.return_value = defer.succeed(None) + + prev_state = NonCallableMock(name="prev_state") + prev_state.membership = Membership.INVITE + prev_state.sender = "@foo:blue" + self.datastore.get_room_member.return_value = defer.succeed(prev_state) + + # Actual invocation + yield self.room_member_handler.change_membership(event) + + self.assertTrue(self.federation.handle_new_event.called) + args = self.federation.handle_new_event.call_args[0] + invite_join_event = args[0] + + self.assertTrue(InviteJoinEvent.TYPE, invite_join_event.TYPE) + self.assertTrue("blue", invite_join_event.target_host) + self.assertTrue("foo", invite_join_event.room_id) + self.assertTrue(user_id, invite_join_event.user_id) + self.assertFalse(hasattr(invite_join_event, "state_key")) + + self.assertEquals( + set(["blue"]), + set(invite_join_event.destinations) + ) + + self.federation.get_state_for_room.assert_called_once_with( + "blue", "foo" + ) + + self.assertFalse(self.datastore.store_room_member.called) + + self.assertFalse(self.notifier.on_new_room_event.called) + self.assertFalse(self.state_handler.handle_new_event.called) + + +class RoomCreationTest(unittest.TestCase): + + def setUp(self): + self.hostname = "red" + hs = HomeServer( + self.hostname, + db_pool=None, + datastore=NonCallableMock(spec_set=[ + "store_room", + ]), + http_server=NonCallableMock(), + http_client=NonCallableMock(spec_set=[]), + notifier=NonCallableMock(spec_set=["on_new_room_event"]), + handlers=NonCallableMock(spec_set=[ + "room_creation_handler", + "room_member_handler", + ]), + auth=NonCallableMock(spec_set=["check"]), + federation=NonCallableMock(spec_set=[ + "handle_new_event", + ]), + state_handler=NonCallableMock(spec_set=["handle_new_event"]), + ) + + self.datastore = hs.get_datastore() + self.handlers = hs.get_handlers() + self.notifier = hs.get_notifier() + self.federation = hs.get_federation() + self.state_handler = hs.get_state_handler() + self.hs = hs + + self.handlers.room_creation_handler = RoomCreationHandler(self.hs) + self.room_creation_handler = self.handlers.room_creation_handler + + self.handlers.room_member_handler = NonCallableMock(spec_set=[ + "change_membership" + ]) + self.room_member_handler = self.handlers.room_member_handler + + @defer.inlineCallbacks + def test_room_creation(self): + user_id = "@foo:red" + room_id = "!bobs_room:red" + config = {"visibility": "private"} + + yield self.room_creation_handler.create_room( + user_id=user_id, + room_id=room_id, + config=config, + ) + + self.assertTrue(self.room_member_handler.change_membership.called) + join_event = self.room_member_handler.change_membership.call_args[0][0] + + self.assertEquals(RoomMemberEvent.TYPE, join_event.type) + self.assertEquals(room_id, join_event.room_id) + self.assertEquals(user_id, join_event.user_id) + self.assertEquals(user_id, join_event.target_user_id) + + self.assertTrue(self.state_handler.handle_new_event.called) + + self.assertTrue(self.federation.handle_new_event.called) + config_event = self.federation.handle_new_event.call_args[0][0] + + self.assertEquals(RoomConfigEvent.TYPE, config_event.type) + self.assertEquals(room_id, config_event.room_id) + self.assertEquals(user_id, config_event.user_id) + self.assertEquals(config, config_event.content) diff --git a/tests/rest/__init__.py b/tests/rest/__init__.py new file mode 100644 index 0000000000..40a96afc6f --- /dev/null +++ b/tests/rest/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/tests/rest/test_events.py b/tests/rest/test_events.py new file mode 100644 index 0000000000..fa40e049ea --- /dev/null +++ b/tests/rest/test_events.py @@ -0,0 +1,202 @@ +# -*- coding: utf-8 -*- +""" Tests REST events for /events paths.""" +from twisted.trial import unittest + +# twisted imports +from twisted.internet import defer + +import synapse.rest.events +import synapse.rest.register +import synapse.rest.room + +from synapse.server import HomeServer + +# python imports +import json +import logging + +from ..utils import MockHttpServer, MemoryDataStore +from .utils import RestTestCase + +from mock import Mock + +logging.getLogger().addHandler(logging.NullHandler()) + +PATH_PREFIX = "/matrix/client/api/v1" + + +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 + + def test_long_poll(self): + # 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 + + def test_stream_forward(self): + # 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 + + def test_limits(self): + # stream from a key, expect limit_num items + + # stream from START, expect limit_num items + + pass + + def test_range(self): + # 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 + + def test_direction(self): + # 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_server = MockHttpServer(prefix=PATH_PREFIX) + + state_handler = Mock(spec=["handle_new_event"]) + state_handler.handle_new_event.return_value = True + + persistence_service = Mock(spec=["get_latest_pdus_in_context"]) + persistence_service.get_latest_pdus_in_context.return_value = [] + + hs = HomeServer( + "test", + db_pool=None, + http_client=None, + federation=Mock(), + replication_layer=Mock(), + state_handler=state_handler, + persistence_service=persistence_service, + clock=Mock(spec=[ + "call_later", + "cancel_call_later", + "time_msec", + ]), + ) + + hs.get_clock().time_msec.return_value = 1000000 + + hs.datastore = MemoryDataStore() + synapse.rest.register.register_servlets(hs, self.mock_server) + synapse.rest.events.register_servlets(hs, self.mock_server) + synapse.rest.room.register_servlets(hs, self.mock_server) + + # 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_server.trigger_get( + "/events?access_token=%s" % ("invalid" + self.token)) + self.assertEquals(403, code, msg=str(response)) + + # valid token, expect content + (code, response) = yield self.mock_server.trigger_get( + "/events?access_token=%s&timeout=0" % (self.token)) + 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): + room_id = "!rid1:test" + yield self.create_room_as(room_id, self.other_user, + tok=self.other_token) + yield self.send(room_id, self.other_user, tok=self.other_token) + + # invited to room (expect no content for room) + yield self.invite(room_id, src=self.other_user, targ=self.user_id, + tok=self.other_token) + (code, response) = yield self.mock_server.trigger_get( + "/events?access_token=%s&timeout=0" % (self.token)) + self.assertEquals(200, code, msg=str(response)) + + # First message is a reflection of my own presence status change + self.assertEquals(1, len(response["chunk"])) + self.assertEquals("m.presence", response["chunk"][0]["type"]) + + # 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) + + def test_stream_items(self): + # 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 diff --git a/tests/rest/test_presence.py b/tests/rest/test_presence.py new file mode 100644 index 0000000000..3a2e86e5c4 --- /dev/null +++ b/tests/rest/test_presence.py @@ -0,0 +1,241 @@ +# -*- coding: utf-8 -*- +"""Tests REST events for /presence paths.""" + +from twisted.trial import unittest +from twisted.internet import defer + +from mock import Mock +import logging + +from ..utils import MockHttpServer + +from synapse.api.constants import PresenceState +from synapse.server import HomeServer + + +logging.getLogger().addHandler(logging.NullHandler()) + + +OFFLINE = PresenceState.OFFLINE +BUSY = PresenceState.BUSY +ONLINE = PresenceState.ONLINE + + +myid = "@apple:test" +PATH_PREFIX = "/matrix/client/api/v1" + + +class PresenceStateTestCase(unittest.TestCase): + + def setUp(self): + self.mock_server = MockHttpServer(prefix=PATH_PREFIX) + self.mock_handler = Mock(spec=[ + "get_state", + "set_state", + ]) + + hs = HomeServer("test", + db_pool=None, + http_client=None, + http_server=self.mock_server, + ) + + def _get_user_by_token(token=None): + return hs.parse_userid(myid) + + hs.get_auth().get_user_by_token = _get_user_by_token + + hs.get_handlers().presence_handler = self.mock_handler + + hs.register_servlets() + + self.u_apple = hs.parse_userid(myid) + + @defer.inlineCallbacks + def test_get_my_status(self): + mocked_get = self.mock_handler.get_state + mocked_get.return_value = defer.succeed( + {"state": 2, "status_msg": "Available"}) + + (code, response) = yield self.mock_server.trigger("GET", + "/presence/%s/status" % (myid), None) + + self.assertEquals(200, code) + self.assertEquals({"state": ONLINE, "status_msg": "Available"}, + response) + mocked_get.assert_called_with(target_user=self.u_apple, + auth_user=self.u_apple) + + @defer.inlineCallbacks + def test_set_my_status(self): + mocked_set = self.mock_handler.set_state + mocked_set.return_value = defer.succeed(()) + + (code, response) = yield self.mock_server.trigger("PUT", + "/presence/%s/status" % (myid), + '{"state": 1, "status_msg": "Away"}') + + self.assertEquals(200, code) + mocked_set.assert_called_with(target_user=self.u_apple, + auth_user=self.u_apple, + state={"state": 1, "status_msg": "Away"}) + + +class PresenceListTestCase(unittest.TestCase): + + def setUp(self): + self.mock_server = MockHttpServer(prefix=PATH_PREFIX) + self.mock_handler = Mock(spec=[ + "get_presence_list", + "send_invite", + "drop", + ]) + + hs = HomeServer("test", + db_pool=None, + http_client=None, + http_server=self.mock_server, + ) + + def _get_user_by_token(token=None): + return hs.parse_userid(myid) + + hs.get_auth().get_user_by_token = _get_user_by_token + + hs.get_handlers().presence_handler = self.mock_handler + + hs.register_servlets() + + self.u_apple = hs.parse_userid("@apple:test") + self.u_banana = hs.parse_userid("@banana:test") + + @defer.inlineCallbacks + def test_get_my_list(self): + self.mock_handler.get_presence_list.return_value = defer.succeed( + [{"observed_user": self.u_banana}] + ) + + (code, response) = yield self.mock_server.trigger("GET", + "/presence_list/%s" % (myid), None) + + self.assertEquals(200, code) + self.assertEquals([{"user_id": "@banana:test"}], response) + + @defer.inlineCallbacks + def test_invite(self): + self.mock_handler.send_invite.return_value = defer.succeed(()) + + (code, response) = yield self.mock_server.trigger("POST", + "/presence_list/%s" % (myid), + """{ + "invite": ["@banana:test"] + }""") + + self.assertEquals(200, code) + + self.mock_handler.send_invite.assert_called_with( + observer_user=self.u_apple, observed_user=self.u_banana) + + @defer.inlineCallbacks + def test_drop(self): + self.mock_handler.drop.return_value = defer.succeed(()) + + (code, response) = yield self.mock_server.trigger("POST", + "/presence_list/%s" % (myid), + """{ + "drop": ["@banana:test"] + }""") + + self.assertEquals(200, code) + + self.mock_handler.drop.assert_called_with( + observer_user=self.u_apple, observed_user=self.u_banana) + + +class PresenceEventStreamTestCase(unittest.TestCase): + def setUp(self): + self.mock_server = MockHttpServer(prefix=PATH_PREFIX) + + # TODO: mocked data store + + # HIDEOUS HACKERY + # TODO(paul): This should be injected in via the HomeServer DI system + from synapse.handlers.events import EventStreamHandler + from synapse.handlers.presence import PresenceStreamData + EventStreamHandler.stream_data_classes = [ + PresenceStreamData + ] + + hs = HomeServer("test", + db_pool=None, + http_client=None, + http_server=self.mock_server, + datastore=Mock(spec=[ + "set_presence_state", + "get_presence_list", + ]), + clock=Mock(spec=[ + "call_later", + "cancel_call_later", + "time_msec", + ]), + ) + + hs.get_clock().time_msec.return_value = 1000000 + + def _get_user_by_req(req=None): + return hs.parse_userid(myid) + + hs.get_auth().get_user_by_req = _get_user_by_req + + hs.register_servlets() + + hs.handlers.room_member_handler = Mock(spec=[ + "get_rooms_for_user", + ]) + hs.handlers.room_member_handler.get_rooms_for_user = ( + lambda u: defer.succeed([])) + + self.mock_datastore = hs.get_datastore() + self.presence = hs.get_handlers().presence_handler + + self.u_apple = hs.parse_userid("@apple:test") + self.u_banana = hs.parse_userid("@banana:test") + + @defer.inlineCallbacks + def test_shortpoll(self): + self.mock_datastore.set_presence_state.return_value = defer.succeed( + {"state": ONLINE}) + self.mock_datastore.get_presence_list.return_value = defer.succeed( + []) + + (code, response) = yield self.mock_server.trigger("GET", + "/events?timeout=0", None) + + self.assertEquals(200, code) + + # We've forced there to be only one data stream so the tokens will + # all be ours + + # I'll already get my own presence state change + self.assertEquals({"start": "0", "end": "1", "chunk": [ + {"type": "m.presence", + "content": {"user_id": "@apple:test", "state": 2}}, + ]}, response) + + self.mock_datastore.set_presence_state.return_value = defer.succeed( + {"state": ONLINE}) + self.mock_datastore.get_presence_list.return_value = defer.succeed( + []) + + yield self.presence.set_state(self.u_banana, self.u_banana, + state={"state": ONLINE}) + + (code, response) = yield self.mock_server.trigger("GET", + "/events?from=1&timeout=0", None) + + self.assertEquals(200, code) + self.assertEquals({"start": "1", "end": "2", "chunk": [ + {"type": "m.presence", + "content": {"user_id": "@banana:test", "state": 2}}, + ]}, response) diff --git a/tests/rest/test_profile.py b/tests/rest/test_profile.py new file mode 100644 index 0000000000..13342b61e5 --- /dev/null +++ b/tests/rest/test_profile.py @@ -0,0 +1,130 @@ +# -*- coding: utf-8 -*- +"""Tests REST events for /profile paths.""" + +from twisted.trial import unittest +from twisted.internet import defer + +from mock import Mock + +from ..utils import MockHttpServer + +from synapse.api.errors import SynapseError, AuthError +from synapse.server import HomeServer + +myid = "@1234ABCD:test" +PATH_PREFIX = "/matrix/client/api/v1" + +class ProfileTestCase(unittest.TestCase): + """ Tests profile management. """ + + def setUp(self): + self.mock_server = MockHttpServer(prefix=PATH_PREFIX) + self.mock_handler = Mock(spec=[ + "get_displayname", + "set_displayname", + "get_avatar_url", + "set_avatar_url", + ]) + + hs = HomeServer("test", + db_pool=None, + http_client=None, + http_server=self.mock_server, + federation=Mock(), + replication_layer=Mock(), + ) + + def _get_user_by_token(token=None): + return hs.parse_userid(myid) + + hs.get_auth().get_user_by_token = _get_user_by_token + + hs.get_handlers().profile_handler = self.mock_handler + + hs.register_servlets() + + @defer.inlineCallbacks + def test_get_my_name(self): + mocked_get = self.mock_handler.get_displayname + mocked_get.return_value = defer.succeed("Frank") + + (code, response) = yield self.mock_server.trigger("GET", + "/profile/%s/displayname" % (myid), None) + + self.assertEquals(200, code) + self.assertEquals({"displayname": "Frank"}, response) + self.assertEquals(mocked_get.call_args[0][0].localpart, "1234ABCD") + + @defer.inlineCallbacks + def test_set_my_name(self): + mocked_set = self.mock_handler.set_displayname + mocked_set.return_value = defer.succeed(()) + + (code, response) = yield self.mock_server.trigger("PUT", + "/profile/%s/displayname" % (myid), + '{"displayname": "Frank Jr."}') + + self.assertEquals(200, code) + self.assertEquals(mocked_set.call_args[0][0].localpart, "1234ABCD") + self.assertEquals(mocked_set.call_args[0][1].localpart, "1234ABCD") + self.assertEquals(mocked_set.call_args[0][2], "Frank Jr.") + + @defer.inlineCallbacks + def test_set_my_name_noauth(self): + mocked_set = self.mock_handler.set_displayname + mocked_set.side_effect = AuthError(400, "message") + + (code, response) = yield self.mock_server.trigger("PUT", + "/profile/%s/displayname" % ("@4567:test"), '"Frank Jr."') + + self.assertTrue(400 <= code < 499, + msg="code %d is in the 4xx range" % (code)) + + @defer.inlineCallbacks + def test_get_other_name(self): + mocked_get = self.mock_handler.get_displayname + mocked_get.return_value = defer.succeed("Bob") + + (code, response) = yield self.mock_server.trigger("GET", + "/profile/%s/displayname" % ("@opaque:elsewhere"), None) + + self.assertEquals(200, code) + self.assertEquals({"displayname": "Bob"}, response) + + @defer.inlineCallbacks + def test_set_other_name(self): + mocked_set = self.mock_handler.set_displayname + mocked_set.side_effect = SynapseError(400, "message") + + (code, response) = yield self.mock_server.trigger("PUT", + "/profile/%s/displayname" % ("@opaque:elsewhere"), None) + + self.assertTrue(400 <= code <= 499, + msg="code %d is in the 4xx range" % (code)) + + @defer.inlineCallbacks + def test_get_my_avatar(self): + mocked_get = self.mock_handler.get_avatar_url + mocked_get.return_value = defer.succeed("http://my.server/me.png") + + (code, response) = yield self.mock_server.trigger("GET", + "/profile/%s/avatar_url" % (myid), None) + + self.assertEquals(200, code) + self.assertEquals({"avatar_url": "http://my.server/me.png"}, response) + self.assertEquals(mocked_get.call_args[0][0].localpart, "1234ABCD") + + @defer.inlineCallbacks + def test_set_my_avatar(self): + mocked_set = self.mock_handler.set_avatar_url + mocked_set.return_value = defer.succeed(()) + + (code, response) = yield self.mock_server.trigger("PUT", + "/profile/%s/avatar_url" % (myid), + '{"avatar_url": "http://my.server/pic.gif"}') + + self.assertEquals(200, code) + self.assertEquals(mocked_set.call_args[0][0].localpart, "1234ABCD") + self.assertEquals(mocked_set.call_args[0][1].localpart, "1234ABCD") + self.assertEquals(mocked_set.call_args[0][2], + "http://my.server/pic.gif") diff --git a/tests/rest/test_rooms.py b/tests/rest/test_rooms.py new file mode 100644 index 0000000000..29e82fc13c --- /dev/null +++ b/tests/rest/test_rooms.py @@ -0,0 +1,924 @@ +# -*- coding: utf-8 -*- +"""Tests REST events for /rooms paths.""" + +# twisted imports +from twisted.internet import defer + +import synapse.rest.room +from synapse.api.constants import Membership + +from synapse.server import HomeServer + +# python imports +import json +import urllib + +from ..utils import MockHttpServer, MemoryDataStore +from .utils import RestTestCase + +from mock import Mock + +PATH_PREFIX = "/matrix/client/api/v1" + + +class RoomPermissionsTestCase(RestTestCase): + """ Tests room permissions. """ + user_id = "@sid1:red" + rmcreator_id = "@notme:red" + + @defer.inlineCallbacks + def setUp(self): + self.mock_server = MockHttpServer(prefix=PATH_PREFIX) + + state_handler = Mock(spec=["handle_new_event"]) + state_handler.handle_new_event.return_value = True + + persistence_service = Mock(spec=["get_latest_pdus_in_context"]) + persistence_service.get_latest_pdus_in_context.return_value = [] + + hs = HomeServer( + "test", + db_pool=None, + http_client=None, + federation=Mock(), + datastore=MemoryDataStore(), + replication_layer=Mock(), + state_handler=state_handler, + persistence_service=persistence_service, + ) + + def _get_user_by_token(token=None): + return hs.parse_userid(self.auth_user_id) + hs.get_auth().get_user_by_token = _get_user_by_token + + self.auth_user_id = self.rmcreator_id + + synapse.rest.room.register_servlets(hs, self.mock_server) + + self.auth = hs.get_auth() + + # create some rooms under the name rmcreator_id + self.uncreated_rmid = "!aa:test" + + self.created_rmid = "!abc:test" + yield self.create_room_as(self.created_rmid, self.rmcreator_id, + is_public=False) + + self.created_public_rmid = "!def1234ghi:test" + yield self.create_room_as(self.created_public_rmid, self.rmcreator_id, + is_public=True) + + # send a message in one of the rooms + self.created_rmid_msg_path = ("/rooms/%s/messages/%s/midaaa1" % + (self.created_rmid, self.rmcreator_id)) + (code, response) = yield self.mock_server.trigger( + "PUT", + self.created_rmid_msg_path, + '{"msgtype":"m.text","body":"test msg"}') + self.assertEquals(200, code, msg=str(response)) + + # set topic for public room + (code, response) = yield self.mock_server.trigger( + "PUT", + "/rooms/%s/topic" % self.created_public_rmid, + '{"topic":"Public Room Topic"}') + self.assertEquals(200, code, msg=str(response)) + + # auth as user_id now + self.auth_user_id = self.user_id + + def tearDown(self): + pass + + @defer.inlineCallbacks + def test_get_message(self): + # get message in uncreated room, expect 403 + (code, response) = yield self.mock_server.trigger_get( + "/rooms/noroom/messages/someid/m1") + self.assertEquals(403, code, msg=str(response)) + + # get message in created room not joined (no state), expect 403 + (code, response) = yield self.mock_server.trigger_get( + self.created_rmid_msg_path) + self.assertEquals(403, code, msg=str(response)) + + # get message in created room and invited, expect 403 + yield self.invite(room=self.created_rmid, src=self.rmcreator_id, + targ=self.user_id) + (code, response) = yield self.mock_server.trigger_get( + self.created_rmid_msg_path) + self.assertEquals(403, code, msg=str(response)) + + # get message in created room and joined, expect 200 + yield self.join(room=self.created_rmid, user=self.user_id) + (code, response) = yield self.mock_server.trigger_get( + self.created_rmid_msg_path) + self.assertEquals(200, code, msg=str(response)) + + # get message in created room and left, expect 403 + yield self.leave(room=self.created_rmid, user=self.user_id) + (code, response) = yield self.mock_server.trigger_get( + self.created_rmid_msg_path) + self.assertEquals(403, code, msg=str(response)) + + @defer.inlineCallbacks + def test_send_message(self): + msg_content = '{"msgtype":"m.text","body":"hello"}' + send_msg_path = ("/rooms/%s/messages/%s/mid1" % + (self.created_rmid, self.user_id)) + + # send message in uncreated room, expect 403 + (code, response) = yield self.mock_server.trigger( + "PUT", + "/rooms/%s/messages/%s/mid1" % + (self.uncreated_rmid, self.user_id), msg_content) + self.assertEquals(403, code, msg=str(response)) + + # send message in created room not joined (no state), expect 403 + (code, response) = yield self.mock_server.trigger( + "PUT", send_msg_path, msg_content) + self.assertEquals(403, code, msg=str(response)) + + # send message in created room and invited, expect 403 + yield self.invite(room=self.created_rmid, src=self.rmcreator_id, + targ=self.user_id) + (code, response) = yield self.mock_server.trigger( + "PUT", send_msg_path, msg_content) + self.assertEquals(403, code, msg=str(response)) + + # send message in created room and joined, expect 200 + yield self.join(room=self.created_rmid, user=self.user_id) + (code, response) = yield self.mock_server.trigger( + "PUT", send_msg_path, msg_content) + self.assertEquals(200, code, msg=str(response)) + + # send message in created room and left, expect 403 + yield self.leave(room=self.created_rmid, user=self.user_id) + (code, response) = yield self.mock_server.trigger( + "PUT", send_msg_path, msg_content) + self.assertEquals(403, code, msg=str(response)) + + @defer.inlineCallbacks + def test_topic_perms(self): + topic_content = '{"topic":"My Topic Name"}' + topic_path = "/rooms/%s/topic" % self.created_rmid + + # set/get topic in uncreated room, expect 403 + (code, response) = yield self.mock_server.trigger( + "PUT", "/rooms/%s/topic" % self.uncreated_rmid, + topic_content) + self.assertEquals(403, code, msg=str(response)) + (code, response) = yield self.mock_server.trigger_get( + "/rooms/%s/topic" % self.uncreated_rmid) + self.assertEquals(403, code, msg=str(response)) + + # set/get topic in created PRIVATE room not joined, expect 403 + (code, response) = yield self.mock_server.trigger( + "PUT", topic_path, topic_content) + self.assertEquals(403, code, msg=str(response)) + (code, response) = yield self.mock_server.trigger_get(topic_path) + self.assertEquals(403, code, msg=str(response)) + + # set topic in created PRIVATE room and invited, expect 403 + yield self.invite(room=self.created_rmid, src=self.rmcreator_id, + targ=self.user_id) + (code, response) = yield self.mock_server.trigger( + "PUT", topic_path, topic_content) + self.assertEquals(403, code, msg=str(response)) + + # get topic in created PRIVATE room and invited, expect 200 (or 404) + (code, response) = yield self.mock_server.trigger_get(topic_path) + self.assertEquals(404, code, msg=str(response)) + + # set/get topic in created PRIVATE room and joined, expect 200 + yield self.join(room=self.created_rmid, user=self.user_id) + (code, response) = yield self.mock_server.trigger( + "PUT", topic_path, topic_content) + self.assertEquals(200, code, msg=str(response)) + (code, response) = yield self.mock_server.trigger_get(topic_path) + self.assertEquals(200, code, msg=str(response)) + self.assert_dict(json.loads(topic_content), response) + + # set/get topic in created PRIVATE room and left, expect 403 + yield self.leave(room=self.created_rmid, user=self.user_id) + (code, response) = yield self.mock_server.trigger( + "PUT", topic_path, topic_content) + self.assertEquals(403, code, msg=str(response)) + (code, response) = yield self.mock_server.trigger_get(topic_path) + self.assertEquals(403, code, msg=str(response)) + + # get topic in PUBLIC room, not joined, expect 200 (or 404) + (code, response) = yield self.mock_server.trigger_get( + "/rooms/%s/topic" % self.created_public_rmid) + self.assertEquals(200, code, msg=str(response)) + + # set topic in PUBLIC room, not joined, expect 403 + (code, response) = yield self.mock_server.trigger( + "PUT", + "/rooms/%s/topic" % self.created_public_rmid, + topic_content) + self.assertEquals(403, code, msg=str(response)) + + @defer.inlineCallbacks + def _test_get_membership(self, room=None, members=[], expect_code=None): + path = "/rooms/%s/members/%s/state" + for member in members: + (code, response) = yield self.mock_server.trigger_get( + path % + (room, member)) + self.assertEquals(expect_code, code) + + @defer.inlineCallbacks + def test_membership_basic_room_perms(self): + # === room does not exist === + room = self.uncreated_rmid + # get membership of self, get membership of other, uncreated room + # expect all 403s + yield self._test_get_membership( + members=[self.user_id, self.rmcreator_id], + room=room, expect_code=403) + + # trying to invite people to this room should 403 + yield self.invite(room=room, src=self.user_id, targ=self.rmcreator_id, + expect_code=403) + + # set [invite/join/left] of self, set [invite/join/left] of other, + # expect all 403s + for usr in [self.user_id, self.rmcreator_id]: + yield self.join(room=room, user=usr, expect_code=403) + yield self.leave(room=room, user=usr, expect_code=403) + + @defer.inlineCallbacks + def test_membership_private_room_perms(self): + room = self.created_rmid + # get membership of self, get membership of other, private room + invite + # expect all 403s + yield self.invite(room=room, src=self.rmcreator_id, + targ=self.user_id) + yield self._test_get_membership( + members=[self.user_id, self.rmcreator_id], + room=room, expect_code=403) + + # get membership of self, get membership of other, private room + joined + # expect all 200s + yield self.join(room=room, user=self.user_id) + yield self._test_get_membership( + members=[self.user_id, self.rmcreator_id], + room=room, expect_code=200) + + # get membership of self, get membership of other, private room + left + # expect all 403s + yield self.leave(room=room, user=self.user_id) + yield self._test_get_membership( + members=[self.user_id, self.rmcreator_id], + room=room, expect_code=403) + + @defer.inlineCallbacks + def test_membership_public_room_perms(self): + room = self.created_public_rmid + # get membership of self, get membership of other, public room + invite + # expect all 403s + yield self.invite(room=room, src=self.rmcreator_id, + targ=self.user_id) + yield self._test_get_membership( + members=[self.user_id, self.rmcreator_id], + room=room, expect_code=403) + + # get membership of self, get membership of other, public room + joined + # expect all 200s + yield self.join(room=room, user=self.user_id) + yield self._test_get_membership( + members=[self.user_id, self.rmcreator_id], + room=room, expect_code=200) + + # get membership of self, get membership of other, public room + left + # expect all 403s + yield self.leave(room=room, user=self.user_id) + yield self._test_get_membership( + members=[self.user_id, self.rmcreator_id], + room=room, expect_code=403) + + @defer.inlineCallbacks + def test_invited_permissions(self): + room = self.created_rmid + yield self.invite(room=room, src=self.rmcreator_id, targ=self.user_id) + + # set [invite/join/left] of other user, expect 403s + yield self.invite(room=room, src=self.user_id, targ=self.rmcreator_id, + expect_code=403) + yield self.change_membership(room=room, src=self.user_id, + targ=self.rmcreator_id, + membership=Membership.JOIN, + expect_code=403) + yield self.change_membership(room=room, src=self.user_id, + targ=self.rmcreator_id, + membership=Membership.LEAVE, + expect_code=403) + + @defer.inlineCallbacks + def test_joined_permissions(self): + room = self.created_rmid + yield self.invite(room=room, src=self.rmcreator_id, targ=self.user_id) + yield self.join(room=room, user=self.user_id) + + # set invited of self, expect 403 + yield self.invite(room=room, src=self.user_id, targ=self.user_id, + expect_code=403) + + # set joined of self, expect 200 (NOOP) + yield self.join(room=room, user=self.user_id) + + other = "@burgundy:red" + # set invited of other, expect 200 + yield self.invite(room=room, src=self.user_id, targ=other, + expect_code=200) + + # set joined of other, expect 403 + yield self.change_membership(room=room, src=self.user_id, + targ=other, + membership=Membership.JOIN, + expect_code=403) + + # set left of other, expect 403 + yield self.change_membership(room=room, src=self.user_id, + targ=other, + membership=Membership.LEAVE, + expect_code=403) + + # set left of self, expect 200 + yield self.leave(room=room, user=self.user_id) + + @defer.inlineCallbacks + def test_leave_permissions(self): + room = self.created_rmid + yield self.invite(room=room, src=self.rmcreator_id, targ=self.user_id) + yield self.join(room=room, user=self.user_id) + yield self.leave(room=room, user=self.user_id) + + # set [invite/join/left] of self, set [invite/join/left] of other, + # expect all 403s + for usr in [self.user_id, self.rmcreator_id]: + yield self.change_membership(room=room, src=self.user_id, + targ=usr, + membership=Membership.INVITE, + expect_code=403) + yield self.change_membership(room=room, src=self.user_id, + targ=usr, + membership=Membership.JOIN, + expect_code=403) + yield self.change_membership(room=room, src=self.user_id, + targ=usr, + membership=Membership.LEAVE, + expect_code=403) + + +class RoomsMemberListTestCase(RestTestCase): + """ Tests /rooms/$room_id/members/list REST events.""" + user_id = "@sid1:red" + + def setUp(self): + self.mock_server = MockHttpServer(prefix=PATH_PREFIX) + + state_handler = Mock(spec=["handle_new_event"]) + state_handler.handle_new_event.return_value = True + + persistence_service = Mock(spec=["get_latest_pdus_in_context"]) + persistence_service.get_latest_pdus_in_context.return_value = [] + + hs = HomeServer( + "test", + db_pool=None, + http_client=None, + federation=Mock(), + datastore=MemoryDataStore(), + replication_layer=Mock(), + state_handler=state_handler, + persistence_service=persistence_service, + ) + + self.auth_user_id = self.user_id + + def _get_user_by_token(token=None): + return hs.parse_userid(self.auth_user_id) + hs.get_auth().get_user_by_token = _get_user_by_token + + synapse.rest.room.register_servlets(hs, self.mock_server) + + def tearDown(self): + pass + + @defer.inlineCallbacks + def test_get_member_list(self): + room_id = "!aa:test" + yield self.create_room_as(room_id, self.user_id) + (code, response) = yield self.mock_server.trigger_get( + "/rooms/%s/members/list" % room_id) + self.assertEquals(200, code, msg=str(response)) + + @defer.inlineCallbacks + def test_get_member_list_no_room(self): + (code, response) = yield self.mock_server.trigger_get( + "/rooms/roomdoesnotexist/members/list") + self.assertEquals(403, code, msg=str(response)) + + @defer.inlineCallbacks + def test_get_member_list_no_permission(self): + room_id = "!bb:test" + yield self.create_room_as(room_id, "@some_other_guy:red") + (code, response) = yield self.mock_server.trigger_get( + "/rooms/%s/members/list" % room_id) + self.assertEquals(403, code, msg=str(response)) + + @defer.inlineCallbacks + def test_get_member_list_mixed_memberships(self): + room_id = "!bb:test" + room_creator = "@some_other_guy:blue" + room_path = "/rooms/%s/members/list" % room_id + yield self.create_room_as(room_id, room_creator) + yield self.invite(room=room_id, src=room_creator, + targ=self.user_id) + # can't see list if you're just invited. + (code, response) = yield self.mock_server.trigger_get(room_path) + self.assertEquals(403, code, msg=str(response)) + + yield self.join(room=room_id, user=self.user_id) + # can see list now joined + (code, response) = yield self.mock_server.trigger_get(room_path) + self.assertEquals(200, code, msg=str(response)) + + yield self.leave(room=room_id, user=self.user_id) + # can no longer see list, you've left. + (code, response) = yield self.mock_server.trigger_get(room_path) + self.assertEquals(403, code, msg=str(response)) + + +class RoomsCreateTestCase(RestTestCase): + """ Tests /rooms and /rooms/$room_id REST events. """ + user_id = "@sid1:red" + + def setUp(self): + self.mock_server = MockHttpServer(prefix=PATH_PREFIX) + self.auth_user_id = self.user_id + + state_handler = Mock(spec=["handle_new_event"]) + state_handler.handle_new_event.return_value = True + + persistence_service = Mock(spec=["get_latest_pdus_in_context"]) + persistence_service.get_latest_pdus_in_context.return_value = [] + + hs = HomeServer( + "test", + db_pool=None, + http_client=None, + federation=Mock(), + datastore=MemoryDataStore(), + replication_layer=Mock(), + state_handler=state_handler, + persistence_service=persistence_service, + ) + + def _get_user_by_token(token=None): + return hs.parse_userid(self.auth_user_id) + hs.get_auth().get_user_by_token = _get_user_by_token + + synapse.rest.room.register_servlets(hs, self.mock_server) + + def tearDown(self): + pass + + @defer.inlineCallbacks + def test_post_room_no_keys(self): + # POST with no config keys, expect new room id + (code, response) = yield self.mock_server.trigger("POST", "/rooms", + "{}") + self.assertEquals(200, code, response) + self.assertTrue("room_id" in response) + + @defer.inlineCallbacks + def test_post_room_visibility_key(self): + # POST with visibility config key, expect new room id + (code, response) = yield self.mock_server.trigger("POST", "/rooms", + '{"visibility":"private"}') + self.assertEquals(200, code) + self.assertTrue("room_id" in response) + + @defer.inlineCallbacks + def test_post_room_custom_key(self): + # POST with custom config keys, expect new room id + (code, response) = yield self.mock_server.trigger("POST", "/rooms", + '{"custom":"stuff"}') + self.assertEquals(200, code) + self.assertTrue("room_id" in response) + + @defer.inlineCallbacks + def test_post_room_known_and_unknown_keys(self): + # POST with custom + known config keys, expect new room id + (code, response) = yield self.mock_server.trigger("POST", "/rooms", + '{"visibility":"private","custom":"things"}') + self.assertEquals(200, code) + self.assertTrue("room_id" in response) + + @defer.inlineCallbacks + def test_post_room_invalid_content(self): + # POST with invalid content / paths, expect 400 + (code, response) = yield self.mock_server.trigger("POST", "/rooms", + '{"visibili') + self.assertEquals(400, code) + + (code, response) = yield self.mock_server.trigger("POST", "/rooms", + '["hello"]') + self.assertEquals(400, code) + + @defer.inlineCallbacks + def test_put_room_no_keys(self): + # PUT with no config keys, expect new room id + (code, response) = yield self.mock_server.trigger( + "PUT", "/rooms/%21aa%3Atest", "{}" + ) + self.assertEquals(200, code) + self.assertTrue("room_id" in response) + + @defer.inlineCallbacks + def test_put_room_visibility_key(self): + # PUT with known config keys, expect new room id + (code, response) = yield self.mock_server.trigger( + "PUT", "/rooms/%21bb%3Atest", '{"visibility":"private"}' + ) + self.assertEquals(200, code) + self.assertTrue("room_id" in response) + + @defer.inlineCallbacks + def test_put_room_custom_key(self): + # PUT with custom config keys, expect new room id + (code, response) = yield self.mock_server.trigger( + "PUT", "/rooms/%21cc%3Atest", '{"custom":"stuff"}' + ) + self.assertEquals(200, code) + self.assertTrue("room_id" in response) + + @defer.inlineCallbacks + def test_put_room_known_and_unknown_keys(self): + # PUT with custom + known config keys, expect new room id + (code, response) = yield self.mock_server.trigger( + "PUT", "/rooms/%21dd%3Atest", + '{"visibility":"private","custom":"things"}' + ) + self.assertEquals(200, code) + self.assertTrue("room_id" in response) + + @defer.inlineCallbacks + def test_put_room_invalid_content(self): + # PUT with invalid content / room names, expect 400 + + (code, response) = yield self.mock_server.trigger( + "PUT", "/rooms/ee", '{"sdf"' + ) + self.assertEquals(400, code) + + (code, response) = yield self.mock_server.trigger( + "PUT", "/rooms/ee", '["hello"]' + ) + self.assertEquals(400, code) + + @defer.inlineCallbacks + def test_put_room_conflict(self): + yield self.create_room_as("!aa:test", self.user_id) + + # PUT with conflicting room ID, expect 409 + (code, response) = yield self.mock_server.trigger( + "PUT", "/rooms/%21aa%3Atest", "{}" + ) + self.assertEquals(409, code) + + +class RoomTopicTestCase(RestTestCase): + """ Tests /rooms/$room_id/topic REST events. """ + user_id = "@sid1:red" + + @defer.inlineCallbacks + def setUp(self): + self.mock_server = MockHttpServer(prefix=PATH_PREFIX) + self.auth_user_id = self.user_id + self.room_id = "!rid1:test" + self.path = "/rooms/%s/topic" % self.room_id + + state_handler = Mock(spec=["handle_new_event"]) + state_handler.handle_new_event.return_value = True + + persistence_service = Mock(spec=["get_latest_pdus_in_context"]) + persistence_service.get_latest_pdus_in_context.return_value = [] + + hs = HomeServer( + "test", + db_pool=None, + http_client=None, + federation=Mock(), + datastore=MemoryDataStore(), + replication_layer=Mock(), + state_handler=state_handler, + persistence_service=persistence_service, + ) + + def _get_user_by_token(token=None): + return hs.parse_userid(self.auth_user_id) + hs.get_auth().get_user_by_token = _get_user_by_token + + synapse.rest.room.register_servlets(hs, self.mock_server) + + # create the room + yield self.create_room_as(self.room_id, self.user_id) + + def tearDown(self): + pass + + @defer.inlineCallbacks + def test_invalid_puts(self): + # missing keys or invalid json + (code, response) = yield self.mock_server.trigger("PUT", + self.path, '{}') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + self.path, '{"_name":"bob"}') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + self.path, '{"nao') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + self.path, '[{"_name":"bob"},{"_name":"jill"}]') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + self.path, 'text only') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + self.path, '') + self.assertEquals(400, code, msg=str(response)) + + # valid key, wrong type + content = '{"topic":["Topic name"]}' + (code, response) = yield self.mock_server.trigger("PUT", + self.path, content) + self.assertEquals(400, code, msg=str(response)) + + @defer.inlineCallbacks + def test_rooms_topic(self): + # nothing should be there + (code, response) = yield self.mock_server.trigger_get(self.path) + self.assertEquals(404, code, msg=str(response)) + + # valid put + content = '{"topic":"Topic name"}' + (code, response) = yield self.mock_server.trigger("PUT", + self.path, content) + self.assertEquals(200, code, msg=str(response)) + + # valid get + (code, response) = yield self.mock_server.trigger_get(self.path) + self.assertEquals(200, code, msg=str(response)) + self.assert_dict(json.loads(content), response) + + @defer.inlineCallbacks + def test_rooms_topic_with_extra_keys(self): + # valid put with extra keys + content = '{"topic":"Seasons","subtopic":"Summer"}' + (code, response) = yield self.mock_server.trigger("PUT", + self.path, content) + self.assertEquals(200, code, msg=str(response)) + + # valid get + (code, response) = yield self.mock_server.trigger_get(self.path) + self.assertEquals(200, code, msg=str(response)) + self.assert_dict(json.loads(content), response) + + +class RoomMemberStateTestCase(RestTestCase): + """ Tests /rooms/$room_id/members/$user_id/state REST events. """ + user_id = "@sid1:red" + + @defer.inlineCallbacks + def setUp(self): + self.mock_server = MockHttpServer(prefix=PATH_PREFIX) + self.auth_user_id = self.user_id + self.room_id = "!rid1:test" + + state_handler = Mock(spec=["handle_new_event"]) + state_handler.handle_new_event.return_value = True + + persistence_service = Mock(spec=["get_latest_pdus_in_context"]) + persistence_service.get_latest_pdus_in_context.return_value = [] + + hs = HomeServer( + "test", + db_pool=None, + http_client=None, + federation=Mock(), + datastore=MemoryDataStore(), + replication_layer=Mock(), + state_handler=state_handler, + persistence_service=persistence_service, + ) + + def _get_user_by_token(token=None): + return hs.parse_userid(self.auth_user_id) + hs.get_auth().get_user_by_token = _get_user_by_token + + synapse.rest.room.register_servlets(hs, self.mock_server) + + yield self.create_room_as(self.room_id, self.user_id) + + def tearDown(self): + pass + + @defer.inlineCallbacks + def test_invalid_puts(self): + path = "/rooms/%s/members/%s/state" % (self.room_id, self.user_id) + # missing keys or invalid json + (code, response) = yield self.mock_server.trigger("PUT", + path, '{}') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + path, '{"_name":"bob"}') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + path, '{"nao') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + path, '[{"_name":"bob"},{"_name":"jill"}]') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + path, 'text only') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + path, '') + self.assertEquals(400, code, msg=str(response)) + + # valid keys, wrong types + content = ('{"membership":["%s","%s","%s"]}' % + (Membership.INVITE, Membership.JOIN, Membership.LEAVE)) + (code, response) = yield self.mock_server.trigger("PUT", path, content) + self.assertEquals(400, code, msg=str(response)) + + @defer.inlineCallbacks + def test_rooms_members_self(self): + path = "/rooms/%s/members/%s/state" % ( + urllib.quote(self.room_id), self.user_id + ) + + # valid join message (NOOP since we made the room) + content = '{"membership":"%s"}' % Membership.JOIN + (code, response) = yield self.mock_server.trigger("PUT", path, content) + self.assertEquals(200, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("GET", path, None) + self.assertEquals(200, code, msg=str(response)) + self.assertEquals(json.loads(content), response) + + @defer.inlineCallbacks + def test_rooms_members_other(self): + self.other_id = "@zzsid1:red" + path = "/rooms/%s/members/%s/state" % ( + urllib.quote(self.room_id), self.other_id + ) + + # valid invite message + content = '{"membership":"%s"}' % Membership.INVITE + (code, response) = yield self.mock_server.trigger("PUT", path, content) + self.assertEquals(200, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("GET", path, None) + self.assertEquals(200, code, msg=str(response)) + self.assertEquals(json.loads(content), response) + + @defer.inlineCallbacks + def test_rooms_members_other_custom_keys(self): + self.other_id = "@zzsid1:red" + path = "/rooms/%s/members/%s/state" % ( + urllib.quote(self.room_id), self.other_id + ) + + # valid invite message with custom key + content = ('{"membership":"%s","invite_text":"%s"}' % + (Membership.INVITE, "Join us!")) + (code, response) = yield self.mock_server.trigger("PUT", path, content) + self.assertEquals(200, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("GET", path, None) + self.assertEquals(200, code, msg=str(response)) + self.assertEquals(json.loads(content), response) + + +class RoomMessagesTestCase(RestTestCase): + """ Tests /rooms/$room_id/messages/$user_id/$msg_id REST events. """ + user_id = "@sid1:red" + + @defer.inlineCallbacks + def setUp(self): + self.mock_server = MockHttpServer(prefix=PATH_PREFIX) + self.auth_user_id = self.user_id + self.room_id = "!rid1:test" + + state_handler = Mock(spec=["handle_new_event"]) + state_handler.handle_new_event.return_value = True + + persistence_service = Mock(spec=["get_latest_pdus_in_context"]) + persistence_service.get_latest_pdus_in_context.return_value = [] + + hs = HomeServer( + "test", + db_pool=None, + http_client=None, + federation=Mock(), + datastore=MemoryDataStore(), + replication_layer=Mock(), + state_handler=state_handler, + persistence_service=persistence_service, + ) + + def _get_user_by_token(token=None): + return hs.parse_userid(self.auth_user_id) + hs.get_auth().get_user_by_token = _get_user_by_token + + synapse.rest.room.register_servlets(hs, self.mock_server) + + yield self.create_room_as(self.room_id, self.user_id) + + def tearDown(self): + pass + + @defer.inlineCallbacks + def test_invalid_puts(self): + path = "/rooms/%s/messages/%s/mid1" % ( + urllib.quote(self.room_id), self.user_id + ) + # missing keys or invalid json + (code, response) = yield self.mock_server.trigger("PUT", + path, '{}') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + path, '{"_name":"bob"}') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + path, '{"nao') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + path, '[{"_name":"bob"},{"_name":"jill"}]') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + path, 'text only') + self.assertEquals(400, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("PUT", + path, '') + self.assertEquals(400, code, msg=str(response)) + + @defer.inlineCallbacks + def test_rooms_messages_sent(self): + path = "/rooms/%s/messages/%s/mid1" % ( + urllib.quote(self.room_id), self.user_id + ) + + content = '{"body":"test","msgtype":{"type":"a"}}' + (code, response) = yield self.mock_server.trigger("PUT", path, content) + self.assertEquals(400, code, msg=str(response)) + + # custom message types + content = '{"body":"test","msgtype":"test.custom.text"}' + (code, response) = yield self.mock_server.trigger("PUT", path, content) + self.assertEquals(200, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("GET", path, None) + self.assertEquals(200, code, msg=str(response)) + self.assert_dict(json.loads(content), response) + + # m.text message type + path = "/rooms/%s/messages/%s/mid2" % ( + urllib.quote(self.room_id), self.user_id + ) + content = '{"body":"test2","msgtype":"m.text"}' + (code, response) = yield self.mock_server.trigger("PUT", path, content) + self.assertEquals(200, code, msg=str(response)) + + (code, response) = yield self.mock_server.trigger("GET", path, None) + self.assertEquals(200, code, msg=str(response)) + self.assert_dict(json.loads(content), response) + + # trying to send message in different user path + path = "/rooms/%s/messages/%s/mid2" % ( + urllib.quote(self.room_id), "invalid" + self.user_id + ) + content = '{"body":"test2","msgtype":"m.text"}' + (code, response) = yield self.mock_server.trigger("PUT", path, content) + self.assertEquals(403, code, msg=str(response)) diff --git a/tests/rest/utils.py b/tests/rest/utils.py new file mode 100644 index 0000000000..7e4e570eff --- /dev/null +++ b/tests/rest/utils.py @@ -0,0 +1,112 @@ +# -*- coding: utf-8 -*- +# twisted imports +from twisted.internet import defer + +# trial imports +from twisted.trial import unittest + +from synapse.api.constants import Membership + +import time + +class RestTestCase(unittest.TestCase): + """Contains extra helper functions to quickly and clearly perform a given + REST action, which isn't the focus of the test. + + This subclass assumes there are mock_server and auth_user_id attributes. + """ + + def __init__(self, *args, **kwargs): + super(RestTestCase, self).__init__(*args, **kwargs) + self.mock_server = None + self.auth_user_id = None + + def mock_get_user_by_token(self, token=None): + return self.auth_user_id + + @defer.inlineCallbacks + def create_room_as(self, room_id, room_creator, is_public=True, tok=None): + temp_id = self.auth_user_id + self.auth_user_id = room_creator + path = "/rooms/%s" % room_id + content = "{}" + if not is_public: + content = '{"visibility":"private"}' + if tok: + path = path + "?access_token=%s" % tok + (code, response) = yield self.mock_server.trigger("PUT", path, content) + self.assertEquals(200, code, msg=str(response)) + self.auth_user_id = temp_id + + @defer.inlineCallbacks + def invite(self, room=None, src=None, targ=None, expect_code=200, tok=None): + yield self.change_membership(room=room, src=src, targ=targ, tok=tok, + membership=Membership.INVITE, + expect_code=expect_code) + + @defer.inlineCallbacks + def join(self, room=None, user=None, expect_code=200, tok=None): + yield self.change_membership(room=room, src=user, targ=user, tok=tok, + membership=Membership.JOIN, + expect_code=expect_code) + + @defer.inlineCallbacks + def leave(self, room=None, user=None, expect_code=200, tok=None): + yield self.change_membership(room=room, src=user, targ=user, tok=tok, + membership=Membership.LEAVE, + expect_code=expect_code) + + @defer.inlineCallbacks + def change_membership(self, room=None, src=None, targ=None, + membership=None, expect_code=200, tok=None): + temp_id = self.auth_user_id + self.auth_user_id = src + + path = "/rooms/%s/members/%s/state" % (room, targ) + if tok: + path = path + "?access_token=%s" % tok + + if membership == Membership.LEAVE: + (code, response) = yield self.mock_server.trigger("DELETE", path, + None) + self.assertEquals(expect_code, code, msg=str(response)) + else: + (code, response) = yield self.mock_server.trigger("PUT", path, + '{"membership":"%s"}' % membership) + self.assertEquals(expect_code, code, msg=str(response)) + + self.auth_user_id = temp_id + + @defer.inlineCallbacks + def register(self, user_id): + (code, response) = yield self.mock_server.trigger("POST", "/register", + '{"user_id":"%s"}' % user_id) + self.assertEquals(200, code) + defer.returnValue(response) + + @defer.inlineCallbacks + def send(self, room_id, sender_id, body=None, msg_id=None, tok=None, + expect_code=200): + if msg_id is None: + msg_id = "m%s" % (str(time.time())) + if body is None: + body = "body_text_here" + + path = "/rooms/%s/messages/%s/%s" % (room_id, sender_id, msg_id) + content = '{"msgtype":"m.text","body":"%s"}' % body + if tok: + path = path + "?access_token=%s" % tok + + (code, response) = yield self.mock_server.trigger("PUT", path, content) + self.assertEquals(expect_code, code, msg=str(response)) + + def assert_dict(self, required, actual): + """Does a partial assert of a dict. + + Args: + required (dict): The keys and value which MUST be in 'actual'. + actual (dict): The test result. Extra keys will not be checked. + """ + for key in required: + self.assertEquals(required[key], actual[key], + msg="%s mismatch. %s" % (key, actual)) diff --git a/tests/storage/__init__.py b/tests/storage/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/storage/__init__.py diff --git a/tests/storage/test_base.py b/tests/storage/test_base.py new file mode 100644 index 0000000000..72869ef910 --- /dev/null +++ b/tests/storage/test_base.py @@ -0,0 +1,191 @@ +# -*- coding: utf-8 -*- + +from twisted.trial import unittest +from twisted.internet import defer + +from mock import Mock, call + +from collections import OrderedDict + +from synapse.server import HomeServer +from synapse.storage._base import SQLBaseStore + + +class SQLBaseStoreTestCase(unittest.TestCase): + """ Test the "simple" SQL generating methods in SQLBaseStore. """ + + def setUp(self): + self.db_pool = Mock(spec=["runInteraction"]) + self.mock_txn = Mock() + # Our fake runInteraction just runs synchronously inline + + def runInteraction(func, *args, **kwargs): + return defer.succeed(func(self.mock_txn, *args, **kwargs)) + self.db_pool.runInteraction = runInteraction + + hs = HomeServer("test", + db_pool=self.db_pool) + + self.datastore = SQLBaseStore(hs) + + @defer.inlineCallbacks + def test_insert_1col(self): + self.mock_txn.rowcount = 1 + + yield self.datastore._simple_insert( + table="tablename", + values={"columname": "Value"} + ) + + self.mock_txn.execute.assert_called_with( + "INSERT INTO tablename (columname) VALUES(?)", + ["Value"] + ) + + @defer.inlineCallbacks + def test_insert_3cols(self): + self.mock_txn.rowcount = 1 + + yield self.datastore._simple_insert( + table="tablename", + # Use OrderedDict() so we can assert on the SQL generated + values=OrderedDict([("colA", 1), ("colB", 2), ("colC", 3)]) + ) + + self.mock_txn.execute.assert_called_with( + "INSERT INTO tablename (colA, colB, colC) VALUES(?, ?, ?)", + [1, 2, 3] + ) + + @defer.inlineCallbacks + def test_select_one_1col(self): + self.mock_txn.rowcount = 1 + self.mock_txn.fetchone.return_value = ("Value",) + + value = yield self.datastore._simple_select_one_onecol( + table="tablename", + keyvalues={"keycol": "TheKey"}, + retcol="retcol" + ) + + self.assertEquals("Value", value) + self.mock_txn.execute.assert_called_with( + "SELECT retcol FROM tablename WHERE keycol = ?", + ["TheKey"] + ) + + @defer.inlineCallbacks + def test_select_one_3col(self): + self.mock_txn.rowcount = 1 + self.mock_txn.fetchone.return_value = (1, 2, 3) + + ret = yield self.datastore._simple_select_one( + table="tablename", + keyvalues={"keycol": "TheKey"}, + retcols=["colA", "colB", "colC"] + ) + + self.assertEquals({"colA": 1, "colB": 2, "colC": 3}, ret) + self.mock_txn.execute.assert_called_with( + "SELECT colA, colB, colC FROM tablename WHERE keycol = ?", + ["TheKey"] + ) + + @defer.inlineCallbacks + def test_select_one_missing(self): + self.mock_txn.rowcount = 0 + self.mock_txn.fetchone.return_value = None + + ret = yield self.datastore._simple_select_one( + table="tablename", + keyvalues={"keycol": "Not here"}, + retcols=["colA"], + allow_none=True + ) + + self.assertFalse(ret) + + @defer.inlineCallbacks + def test_select_list(self): + self.mock_txn.rowcount = 3; + self.mock_txn.fetchall.return_value = ((1,), (2,), (3,)) + self.mock_txn.description = ( + ("colA", None, None, None, None, None, None), + ) + + ret = yield self.datastore._simple_select_list( + table="tablename", + keyvalues={"keycol": "A set"}, + retcols=["colA"], + ) + + self.assertEquals([{"colA": 1}, {"colA": 2}, {"colA": 3}], ret) + self.mock_txn.execute.assert_called_with( + "SELECT colA FROM tablename WHERE keycol = ?", + ["A set"] + ) + + @defer.inlineCallbacks + def test_update_one_1col(self): + self.mock_txn.rowcount = 1 + + yield self.datastore._simple_update_one( + table="tablename", + keyvalues={"keycol": "TheKey"}, + updatevalues={"columnname": "New Value"} + ) + + self.mock_txn.execute.assert_called_with( + "UPDATE tablename SET columnname = ? WHERE keycol = ?", + ["New Value", "TheKey"] + ) + + @defer.inlineCallbacks + def test_update_one_4cols(self): + self.mock_txn.rowcount = 1 + + yield self.datastore._simple_update_one( + table="tablename", + keyvalues=OrderedDict([("colA", 1), ("colB", 2)]), + updatevalues=OrderedDict([("colC", 3), ("colD", 4)]) + ) + + self.mock_txn.execute.assert_called_with( + "UPDATE tablename SET colC = ?, colD = ? WHERE " + + "colA = ? AND colB = ?", + [3, 4, 1, 2] + ) + + @defer.inlineCallbacks + def test_update_one_with_return(self): + self.mock_txn.rowcount = 1 + self.mock_txn.fetchone.return_value = ("Old Value",) + + ret = yield self.datastore._simple_update_one( + table="tablename", + keyvalues={"keycol": "TheKey"}, + updatevalues={"columname": "New Value"}, + retcols=["columname"] + ) + + self.assertEquals({"columname": "Old Value"}, ret) + self.mock_txn.execute.assert_has_calls([ + call('SELECT columname FROM tablename WHERE keycol = ?', + ['TheKey']), + call("UPDATE tablename SET columname = ? WHERE keycol = ?", + ["New Value", "TheKey"]) + ]) + + @defer.inlineCallbacks + def test_delete_one(self): + self.mock_txn.rowcount = 1 + + yield self.datastore._simple_delete_one( + table="tablename", + keyvalues={"keycol": "Go away"}, + ) + + self.mock_txn.execute.assert_called_with( + "DELETE FROM tablename WHERE keycol = ?", + ["Go away"] + ) diff --git a/tests/test_distributor.py b/tests/test_distributor.py new file mode 100644 index 0000000000..36cbf6c52d --- /dev/null +++ b/tests/test_distributor.py @@ -0,0 +1,74 @@ +import unittest + +from twisted.internet import defer + +from mock import Mock, patch + +from synapse.util.distributor import Distributor + + +class DistributorTestCase(unittest.TestCase): + + def setUp(self): + self.dist = Distributor() + + def test_signal_dispatch(self): + self.dist.declare("alert") + + observer = Mock() + self.dist.observe("alert", observer) + + d = self.dist.fire("alert", 1, 2, 3) + + self.assertTrue(d.called) + observer.assert_called_with(1, 2, 3) + + def test_signal_dispatch_deferred(self): + self.dist.declare("whine") + + d_inner = defer.Deferred() + def observer(): + return d_inner + self.dist.observe("whine", observer) + + d_outer = self.dist.fire("whine") + + self.assertFalse(d_outer.called) + + d_inner.callback(None) + self.assertTrue(d_outer.called) + + def test_signal_catch(self): + self.dist.declare("alarm") + + observers = [Mock() for i in 1, 2] + for o in observers: + self.dist.observe("alarm", o) + + observers[0].side_effect = Exception("Awoogah!") + + with patch("synapse.util.distributor.logger", + spec=["warning"] + ) as mock_logger: + d = self.dist.fire("alarm", "Go") + self.assertTrue(d.called) + + observers[0].assert_called_once("Go") + observers[1].assert_called_once("Go") + + self.assertEquals(mock_logger.warning.call_count, 1) + self.assertIsInstance(mock_logger.warning.call_args[0][0], + str) + + def test_signal_prereg(self): + observer = Mock() + self.dist.observe("flare", observer) + + self.dist.declare("flare") + self.dist.fire("flare", 4, 5) + + observer.assert_called_with(4, 5) + + def test_signal_undeclared(self): + with self.assertRaises(KeyError): + self.dist.fire("notification") diff --git a/tests/test_state.py b/tests/test_state.py new file mode 100644 index 0000000000..8d0251b1e7 --- /dev/null +++ b/tests/test_state.py @@ -0,0 +1,271 @@ +# -*- coding: utf-8 -*- +from twisted.internet import defer +from twisted.trial import unittest + +from synapse.state import StateHandler +from synapse.storage.pdu import PduEntry +from synapse.federation.pdu_codec import encode_event_id + +from collections import namedtuple + +from mock import Mock + + +ReturnType = namedtuple( + "StateReturnType", ["new_branch", "current_branch"] +) + + +class StateTestCase(unittest.TestCase): + def setUp(self): + self.persistence = Mock(spec=[ + "get_unresolved_state_tree", + "update_current_state", + "get_latest_pdus_in_context", + "get_current_state", + ]) + self.replication = Mock(spec=["get_pdu"]) + + hs = Mock(spec=["get_datastore", "get_replication_layer"]) + hs.get_datastore.return_value = self.persistence + hs.get_replication_layer.return_value = self.replication + hs.hostname = "bob.com" + + self.state = StateHandler(hs) + + @defer.inlineCallbacks + def test_new_state_key(self): + # We've never seen anything for this state before + new_pdu = new_fake_pdu_entry("A", "test", "mem", "x", None, 10) + + self.persistence.get_unresolved_state_tree.return_value = ( + ReturnType([new_pdu], []) + ) + + is_new = yield self.state.handle_new_state(new_pdu) + + self.assertTrue(is_new) + + self.persistence.get_unresolved_state_tree.assert_called_once_with( + new_pdu + ) + + self.assertEqual(1, self.persistence.update_current_state.call_count) + + self.assertFalse(self.replication.get_pdu.called) + + @defer.inlineCallbacks + def test_direct_overwrite(self): + # We do a direct overwriting of the old state, i.e., the new state + # points to the old state. + + old_pdu = new_fake_pdu_entry("A", "test", "mem", "x", None, 10) + new_pdu = new_fake_pdu_entry("B", "test", "mem", "x", "A", 5) + + self.persistence.get_unresolved_state_tree.return_value = ( + ReturnType([new_pdu, old_pdu], [old_pdu]) + ) + + is_new = yield self.state.handle_new_state(new_pdu) + + self.assertTrue(is_new) + + self.persistence.get_unresolved_state_tree.assert_called_once_with( + new_pdu + ) + + self.assertEqual(1, self.persistence.update_current_state.call_count) + + self.assertFalse(self.replication.get_pdu.called) + + @defer.inlineCallbacks + def test_power_level_fail(self): + # We try to update the state based on an outdated state, and have a + # too low power level. + + old_pdu_1 = new_fake_pdu_entry("A", "test", "mem", "x", None, 10) + old_pdu_2 = new_fake_pdu_entry("B", "test", "mem", "x", None, 10) + new_pdu = new_fake_pdu_entry("C", "test", "mem", "x", "A", 5) + + self.persistence.get_unresolved_state_tree.return_value = ( + ReturnType([new_pdu, old_pdu_1], [old_pdu_2, old_pdu_1]) + ) + + is_new = yield self.state.handle_new_state(new_pdu) + + self.assertFalse(is_new) + + self.persistence.get_unresolved_state_tree.assert_called_once_with( + new_pdu + ) + + self.assertEqual(0, self.persistence.update_current_state.call_count) + + self.assertFalse(self.replication.get_pdu.called) + + @defer.inlineCallbacks + def test_power_level_succeed(self): + # We try to update the state based on an outdated state, but have + # sufficient power level to force the update. + + old_pdu_1 = new_fake_pdu_entry("A", "test", "mem", "x", None, 10) + old_pdu_2 = new_fake_pdu_entry("B", "test", "mem", "x", None, 10) + new_pdu = new_fake_pdu_entry("C", "test", "mem", "x", "A", 15) + + self.persistence.get_unresolved_state_tree.return_value = ( + ReturnType([new_pdu, old_pdu_1], [old_pdu_2, old_pdu_1]) + ) + + is_new = yield self.state.handle_new_state(new_pdu) + + self.assertTrue(is_new) + + self.persistence.get_unresolved_state_tree.assert_called_once_with( + new_pdu + ) + + self.assertEqual(1, self.persistence.update_current_state.call_count) + + self.assertFalse(self.replication.get_pdu.called) + + @defer.inlineCallbacks + def test_power_level_equal_same_len(self): + # We try to update the state based on an outdated state, the power + # levels are the same and so are the branch lengths + + old_pdu_1 = new_fake_pdu_entry("A", "test", "mem", "x", None, 10) + old_pdu_2 = new_fake_pdu_entry("B", "test", "mem", "x", None, 10) + new_pdu = new_fake_pdu_entry("C", "test", "mem", "x", "A", 10) + + self.persistence.get_unresolved_state_tree.return_value = ( + ReturnType([new_pdu, old_pdu_1], [old_pdu_2, old_pdu_1]) + ) + + is_new = yield self.state.handle_new_state(new_pdu) + + self.assertTrue(is_new) + + self.persistence.get_unresolved_state_tree.assert_called_once_with( + new_pdu + ) + + self.assertEqual(1, self.persistence.update_current_state.call_count) + + self.assertFalse(self.replication.get_pdu.called) + + @defer.inlineCallbacks + def test_power_level_equal_diff_len(self): + # We try to update the state based on an outdated state, the power + # levels are the same but the branch length of the new one is longer. + + old_pdu_1 = new_fake_pdu_entry("A", "test", "mem", "x", None, 10) + old_pdu_2 = new_fake_pdu_entry("B", "test", "mem", "x", None, 10) + old_pdu_3 = new_fake_pdu_entry("C", "test", "mem", "x", "A", 10) + new_pdu = new_fake_pdu_entry("D", "test", "mem", "x", "C", 10) + + self.persistence.get_unresolved_state_tree.return_value = ( + ReturnType([new_pdu, old_pdu_3, old_pdu_1], [old_pdu_2, old_pdu_1]) + ) + + is_new = yield self.state.handle_new_state(new_pdu) + + self.assertTrue(is_new) + + self.persistence.get_unresolved_state_tree.assert_called_once_with( + new_pdu + ) + + self.assertEqual(1, self.persistence.update_current_state.call_count) + + self.assertFalse(self.replication.get_pdu.called) + + @defer.inlineCallbacks + def test_missing_pdu(self): + # We try to update state against a PDU we haven't yet seen, + # triggering a get_pdu request + + # The pdu we haven't seen + old_pdu_1 = new_fake_pdu_entry("A", "test", "mem", "x", None, 10) + + old_pdu_2 = new_fake_pdu_entry("B", "test", "mem", "x", None, 10) + new_pdu = new_fake_pdu_entry("C", "test", "mem", "x", "A", 20) + + # The return_value of `get_unresolved_state_tree`, which changes after + # the call to get_pdu + tree_to_return = [ReturnType([new_pdu], [old_pdu_2])] + + def return_tree(p): + return tree_to_return[0] + + def set_return_tree(*args, **kwargs): + tree_to_return[0] = ReturnType( + [new_pdu, old_pdu_1], [old_pdu_2, old_pdu_1] + ) + + self.persistence.get_unresolved_state_tree.side_effect = return_tree + + self.replication.get_pdu.side_effect = set_return_tree + + is_new = yield self.state.handle_new_state(new_pdu) + + self.assertTrue(is_new) + + self.persistence.get_unresolved_state_tree.assert_called_with( + new_pdu + ) + + self.assertEquals( + 2, self.persistence.get_unresolved_state_tree.call_count + ) + + self.assertEqual(1, self.persistence.update_current_state.call_count) + + @defer.inlineCallbacks + def test_new_event(self): + event = Mock() + + state_pdu = new_fake_pdu_entry("C", "test", "mem", "x", "A", 20) + + tup = ("pdu_id", "origin.com", 5) + pdus = [tup] + + self.persistence.get_latest_pdus_in_context.return_value = pdus + self.persistence.get_current_state.return_value = state_pdu + + yield self.state.handle_new_event(event) + + self.assertLess(tup[2], event.depth) + + self.assertEquals(1, len(event.prev_events)) + + prev_id = event.prev_events[0] + + self.assertEqual(encode_event_id(tup[0], tup[1]), prev_id) + + self.assertEqual( + encode_event_id(state_pdu.pdu_id, state_pdu.origin), + event.prev_state + ) + + +def new_fake_pdu_entry(pdu_id, context, pdu_type, state_key, prev_state_id, + power_level): + new_pdu = PduEntry( + pdu_id=pdu_id, + pdu_type=pdu_type, + state_key=state_key, + power_level=power_level, + prev_state_id=prev_state_id, + origin="example.com", + context="context", + ts=1405353060021, + depth=0, + content_json="{}", + unrecognized_keys="{}", + outlier=True, + is_state=True, + prev_state_origin="example.com", + have_processed=True, + ) + + return new_pdu diff --git a/tests/test_types.py b/tests/test_types.py new file mode 100644 index 0000000000..0f3c9492d0 --- /dev/null +++ b/tests/test_types.py @@ -0,0 +1,49 @@ +import unittest + +from synapse.server import BaseHomeServer +from synapse.types import UserID, RoomAlias + +mock_homeserver = BaseHomeServer(hostname="my.domain") + +class UserIDTestCase(unittest.TestCase): + + def test_parse(self): + user = UserID.from_string("@1234abcd:my.domain", hs=mock_homeserver) + + self.assertEquals("1234abcd", user.localpart) + self.assertEquals("my.domain", user.domain) + self.assertEquals(True, user.is_mine) + + def test_build(self): + user = UserID("5678efgh", "my.domain", True) + + self.assertEquals(user.to_string(), "@5678efgh:my.domain") + + def test_compare(self): + userA = UserID.from_string("@userA:my.domain", hs=mock_homeserver) + userAagain = UserID.from_string("@userA:my.domain", hs=mock_homeserver) + userB = UserID.from_string("@userB:my.domain", hs=mock_homeserver) + + self.assertTrue(userA == userAagain) + self.assertTrue(userA != userB) + + def test_via_homeserver(self): + user = mock_homeserver.parse_userid("@3456ijkl:my.domain") + + self.assertEquals("3456ijkl", user.localpart) + self.assertEquals("my.domain", user.domain) + + +class RoomAliasTestCase(unittest.TestCase): + + def test_parse(self): + room = RoomAlias.from_string("#channel:my.domain", hs=mock_homeserver) + + self.assertEquals("channel", room.localpart) + self.assertEquals("my.domain", room.domain) + self.assertEquals(True, room.is_mine) + + def test_build(self): + room = RoomAlias("channel", "my.domain", True) + + self.assertEquals(room.to_string(), "#channel:my.domain") diff --git a/tests/util/__init__.py b/tests/util/__init__.py new file mode 100644 index 0000000000..40a96afc6f --- /dev/null +++ b/tests/util/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/tests/util/test_lock.py b/tests/util/test_lock.py new file mode 100644 index 0000000000..b7b8779fd3 --- /dev/null +++ b/tests/util/test_lock.py @@ -0,0 +1,94 @@ +# -*- coding: utf-8 -*- + +from twisted.internet import defer +from twisted.trial import unittest + +from synapse.util.lockutils import LockManager + + +class LockManagerTestCase(unittest.TestCase): + + def setUp(self): + self.lock_manager = LockManager() + + @defer.inlineCallbacks + def test_one_lock(self): + key = "test" + deferred_lock1 = self.lock_manager.lock(key) + + self.assertTrue(deferred_lock1.called) + + lock1 = yield deferred_lock1 + + self.assertFalse(lock1.released) + + lock1.release() + + self.assertTrue(lock1.released) + + @defer.inlineCallbacks + def test_concurrent_locks(self): + key = "test" + deferred_lock1 = self.lock_manager.lock(key) + deferred_lock2 = self.lock_manager.lock(key) + + self.assertTrue(deferred_lock1.called) + self.assertFalse(deferred_lock2.called) + + lock1 = yield deferred_lock1 + + self.assertFalse(lock1.released) + self.assertFalse(deferred_lock2.called) + + lock1.release() + + self.assertTrue(lock1.released) + self.assertTrue(deferred_lock2.called) + + lock2 = yield deferred_lock2 + + lock2.release() + + @defer.inlineCallbacks + def test_sequential_locks(self): + key = "test" + deferred_lock1 = self.lock_manager.lock(key) + + self.assertTrue(deferred_lock1.called) + + lock1 = yield deferred_lock1 + + self.assertFalse(lock1.released) + + lock1.release() + + self.assertTrue(lock1.released) + + deferred_lock2 = self.lock_manager.lock(key) + + self.assertTrue(deferred_lock2.called) + + lock2 = yield deferred_lock2 + + self.assertFalse(lock2.released) + + lock2.release() + + self.assertTrue(lock2.released) + + @defer.inlineCallbacks + def test_with_statement(self): + key = "test" + with (yield self.lock_manager.lock(key)) as lock: + self.assertFalse(lock.released) + + self.assertTrue(lock.released) + + @defer.inlineCallbacks + def test_two_with_statement(self): + key = "test" + with (yield self.lock_manager.lock(key)): + pass + + with (yield self.lock_manager.lock(key)): + pass \ No newline at end of file diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000000..13f6b31c9a --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,252 @@ +from synapse.http.server import HttpServer +from synapse.api.errors import cs_error, CodeMessageException, StoreError +from synapse.api.constants import Membership + +from synapse.api.events.room import ( + RoomMemberEvent, MessageEvent +) + +from twisted.internet import defer + +from collections import namedtuple +from mock import patch, Mock +import json +import urlparse + + +class MockHttpServer(HttpServer): + + def __init__(self, prefix=""): + self.callbacks = [] # 3-tuple of method/pattern/function + self.prefix = prefix + + def trigger_get(self, path): + return self.trigger("GET", path, None) + + @patch('twisted.web.http.Request') + @defer.inlineCallbacks + def trigger(self, http_method, path, content, mock_request): + """ Fire an HTTP event. + + Args: + http_method : The HTTP method + path : The HTTP path + content : The HTTP body + mock_request : Mocked request to pass to the event so it can get + content. + Returns: + A tuple of (code, response) + Raises: + KeyError If no event is found which will handle the path. + """ + path = self.prefix + path + + # annoyingly we return a twisted http request which has chained calls + # to get at the http content, hence mock it here. + mock_content = Mock() + config = {'read.return_value': content} + mock_content.configure_mock(**config) + mock_request.content = mock_content + + # return the right path if the event requires it + mock_request.path = path + + # add in query params to the right place + try: + mock_request.args = urlparse.parse_qs(path.split('?')[1]) + mock_request.path = path.split('?')[0] + path = mock_request.path + except: + pass + + for (method, pattern, func) in self.callbacks: + if http_method != method: + continue + + matcher = pattern.match(path) + if matcher: + try: + (code, response) = yield func( + mock_request, + *matcher.groups() + ) + defer.returnValue((code, response)) + except CodeMessageException as e: + defer.returnValue((e.code, cs_error(e.msg))) + + raise KeyError("No event can handle %s" % path) + + def register_path(self, method, path_pattern, callback): + self.callbacks.append((method, path_pattern, callback)) + + +class MemoryDataStore(object): + + class RoomMember(namedtuple( + "RoomMember", + ["room_id", "user_id", "sender", "membership", "content"] + )): + def as_event(self, event_factory): + return event_factory.create_event( + etype=RoomMemberEvent.TYPE, + room_id=self.room_id, + target_user_id=self.user_id, + user_id=self.sender, + content=json.loads(self.content), + ) + + PathData = namedtuple("PathData", + ["room_id", "path", "content"]) + + Message = namedtuple("Message", + ["room_id", "msg_id", "user_id", "content"]) + + Room = namedtuple("Room", + ["room_id", "is_public", "creator"]) + + def __init__(self): + self.tokens_to_users = {} + self.paths_to_content = {} + self.members = {} + self.messages = {} + self.rooms = {} + self.room_members = {} + + def register(self, user_id, token, password_hash): + if user_id in self.tokens_to_users.values(): + raise StoreError(400, "User in use.") + self.tokens_to_users[token] = user_id + + def get_user_by_token(self, token): + try: + return self.tokens_to_users[token] + except: + raise StoreError(400, "User does not exist.") + + def get_room(self, room_id): + try: + return self.rooms[room_id] + except: + return None + + def store_room(self, room_id, room_creator_user_id, is_public): + if room_id in self.rooms: + raise StoreError(409, "Conflicting room!") + + room = MemoryDataStore.Room(room_id=room_id, is_public=is_public, + creator=room_creator_user_id) + self.rooms[room_id] = room + #self.store_room_member(user_id=room_creator_user_id, room_id=room_id, + #membership=Membership.JOIN, + #content={"membership": Membership.JOIN}) + + def get_message(self, user_id=None, room_id=None, msg_id=None): + try: + return self.messages[user_id + room_id + msg_id] + except: + return None + + def store_message(self, user_id=None, room_id=None, msg_id=None, + content=None): + msg = MemoryDataStore.Message(room_id=room_id, msg_id=msg_id, + user_id=user_id, content=content) + self.messages[user_id + room_id + msg_id] = msg + + def get_room_member(self, user_id=None, room_id=None): + try: + return self.members[user_id + room_id] + except: + return None + + def get_room_members(self, room_id=None, membership=None): + try: + return self.room_members[room_id] + except: + return None + + def get_rooms_for_user_where_membership_is(self, user_id, membership_list): + return [r for r in self.room_members + if user_id in self.room_members[r]] + + def store_room_member(self, user_id=None, sender=None, room_id=None, + membership=None, content=None): + member = MemoryDataStore.RoomMember(room_id=room_id, user_id=user_id, + sender=sender, membership=membership, content=json.dumps(content)) + self.members[user_id + room_id] = member + + # TODO should be latest state + if room_id not in self.room_members: + self.room_members[room_id] = [] + self.room_members[room_id].append(member) + + def get_room_data(self, room_id, etype, state_key=""): + path = "%s-%s-%s" % (room_id, etype, state_key) + try: + return self.paths_to_content[path] + except: + return None + + def store_room_data(self, room_id, etype, state_key="", content=None): + path = "%s-%s-%s" % (room_id, etype, state_key) + data = MemoryDataStore.PathData(path=path, room_id=room_id, + content=content) + self.paths_to_content[path] = data + + def get_message_stream(self, user_id=None, from_key=None, to_key=None, + room_id=None, limit=0, with_feedback=False): + return ([], from_key) # TODO + + def get_room_member_stream(self, user_id=None, from_key=None, to_key=None): + return ([], from_key) # TODO + + def get_feedback_stream(self, user_id=None, from_key=None, to_key=None, + room_id=None, limit=0): + return ([], from_key) # TODO + + def get_room_data_stream(self, user_id=None, from_key=None, to_key=None, + room_id=None, limit=0): + return ([], from_key) # TODO + + def to_events(self, data_store_list): + return data_store_list # TODO + + def get_max_message_id(self): + return 0 # TODO + + def get_max_feedback_id(self): + return 0 # TODO + + def get_max_room_member_id(self): + return 0 # TODO + + def get_max_room_data_id(self): + return 0 # TODO + + def get_joined_hosts_for_room(self, room_id): + return defer.succeed([]) + + def persist_event(self, event): + if event.type == MessageEvent.TYPE: + return self.store_message( + user_id=event.user_id, + room_id=event.room_id, + msg_id=event.msg_id, + content=json.dumps(event.content) + ) + elif event.type == RoomMemberEvent.TYPE: + return self.store_room_member( + user_id=event.target_user_id, + room_id=event.room_id, + content=event.content, + membership=event.content["membership"] + ) + else: + raise NotImplementedError( + "Don't know how to persist type=%s" % event.type + ) + + def set_presence_state(self, user_localpart, state): + return defer.succeed({"state": 0}) + + def get_presence_list(self, user_localpart, accepted): + return [] |