Пример #1
0
    def setUp(self):
        self.hs = yield setup_test_homeserver(
            "red",
            http_client=None,
            replication_layer=Mock(),
            ratelimiter=NonCallableMock(spec_set=[
                "send_message",
            ]),
        )
        self.user_id = "@seeing:red"
        self.user = UserID.from_string(self.user_id)

        self.hs.get_ratelimiter().send_message.return_value = (True, 0)

        self.resource = ReplicationResource(self.hs)
Пример #2
0
    def setUp(self):
        self.hs = yield setup_test_homeserver(
            "blue",
            http_client=None,
            replication_layer=Mock(),
            ratelimiter=NonCallableMock(spec_set=[
                "send_message",
            ]),
        )
        self.hs.get_ratelimiter().send_message.return_value = (True, 0)

        self.replication = ReplicationResource(self.hs)

        self.master_store = self.hs.get_datastore()
        self.slaved_store = self.STORE_TYPE(self.hs.get_db_conn(), self.hs)
        self.event_id = 0
Пример #3
0
class BaseSlavedStoreTestCase(unittest.TestCase):
    @defer.inlineCallbacks
    def setUp(self):
        self.hs = yield setup_test_homeserver(
            "blue",
            http_client=None,
            replication_layer=Mock(),
            ratelimiter=NonCallableMock(spec_set=[
                "send_message",
            ]),
        )
        self.hs.get_ratelimiter().send_message.return_value = (True, 0)

        self.replication = ReplicationResource(self.hs)

        self.master_store = self.hs.get_datastore()
        self.slaved_store = self.STORE_TYPE(self.hs.get_db_conn(), self.hs)
        self.event_id = 0

    @defer.inlineCallbacks
    def replicate(self):
        streams = self.slaved_store.stream_positions()
        writer = yield self.replication.replicate(streams, 100)
        result = writer.finish()
        yield self.slaved_store.process_replication(result)

    @defer.inlineCallbacks
    def check(self, method, args, expected_result=None):
        master_result = yield getattr(self.master_store, method)(*args)
        slaved_result = yield getattr(self.slaved_store, method)(*args)
        if expected_result is not None:
            self.assertEqual(master_result, expected_result)
            self.assertEqual(slaved_result, expected_result)
        self.assertEqual(master_result, slaved_result)
Пример #4
0
class BaseSlavedStoreTestCase(unittest.TestCase):
    @defer.inlineCallbacks
    def setUp(self):
        self.hs = yield setup_test_homeserver(
            "blue",
            http_client=None,
            replication_layer=Mock(),
            ratelimiter=NonCallableMock(spec_set=[
                "send_message",
            ]),
        )
        self.hs.get_ratelimiter().send_message.return_value = (True, 0)

        self.replication = ReplicationResource(self.hs)

        self.master_store = self.hs.get_datastore()
        self.slaved_store = self.STORE_TYPE(self.hs.get_db_conn(), self.hs)
        self.event_id = 0

    @defer.inlineCallbacks
    def replicate(self):
        streams = self.slaved_store.stream_positions()
        result = yield self.replication.replicate(streams, 100)
        yield self.slaved_store.process_replication(result)

    @defer.inlineCallbacks
    def check(self, method, args, expected_result=None):
        master_result = yield getattr(self.master_store, method)(*args)
        slaved_result = yield getattr(self.slaved_store, method)(*args)
        if expected_result is not None:
            self.assertEqual(master_result, expected_result)
            self.assertEqual(slaved_result, expected_result)
        self.assertEqual(master_result, slaved_result)
Пример #5
0
    def setUp(self):
        self.hs = yield setup_test_homeserver(
            "red", http_client=None, replication_layer=Mock(), ratelimiter=NonCallableMock(spec_set=["send_message"])
        )
        self.user_id = "@seeing:red"
        self.user = UserID.from_string(self.user_id)

        self.hs.get_ratelimiter().send_message.return_value = (True, 0)

        self.resource = ReplicationResource(self.hs)
Пример #6
0
    def setUp(self):
        self.hs = yield setup_test_homeserver(
            "blue",
            http_client=None,
            replication_layer=Mock(),
            ratelimiter=NonCallableMock(spec_set=[
                "send_message",
            ]),
        )
        self.hs.get_ratelimiter().send_message.return_value = (True, 0)

        self.replication = ReplicationResource(self.hs)

        self.master_store = self.hs.get_datastore()
        self.slaved_store = self.STORE_TYPE(self.hs.get_db_conn(), self.hs)
        self.event_id = 0
Пример #7
0
class ReplicationResourceCase(unittest.TestCase):
    @defer.inlineCallbacks
    def setUp(self):
        self.hs = yield setup_test_homeserver(
            "red",
            http_client=None,
            replication_layer=Mock(),
            ratelimiter=NonCallableMock(spec_set=[
                "send_message",
            ]),
        )
        self.user_id = "@seeing:red"
        self.user = UserID.from_string(self.user_id)

        self.hs.get_ratelimiter().send_message.return_value = (True, 0)

        self.resource = ReplicationResource(self.hs)

    @defer.inlineCallbacks
    def test_streams(self):
        # Passing "-1" returns the current stream positions
        code, body = yield self.get(streams="-1")
        self.assertEquals(code, 200)
        self.assertEquals(body["streams"]["field_names"], ["name", "position"])
        position = body["streams"]["position"]
        # Passing the current position returns an empty response after the
        # timeout
        get = self.get(streams=str(position), timeout="0")
        self.hs.clock.advance_time_msec(1)
        code, body = yield get
        self.assertEquals(code, 200)
        self.assertEquals(body, {})

    @defer.inlineCallbacks
    def test_events(self):
        get = self.get(events="-1", timeout="0")
        yield self.hs.get_handlers().room_creation_handler.create_room(
            synapse.types.create_requester(self.user), {})
        code, body = yield get
        self.assertEquals(code, 200)
        self.assertEquals(body["events"]["field_names"], [
            "position",
            "event_id",
            "room_id",
            "type",
            "state_key",
        ])

    @defer.inlineCallbacks
    def test_presence(self):
        get = self.get(presence="-1")
        yield self.hs.get_presence_handler().set_state(self.user,
                                                       {"presence": "online"})
        code, body = yield get
        self.assertEquals(code, 200)
        self.assertEquals(body["presence"]["field_names"], [
            "position",
            "user_id",
            "state",
            "last_active_ts",
            "last_federation_update_ts",
            "last_user_sync_ts",
            "status_msg",
            "currently_active",
        ])

    @defer.inlineCallbacks
    def test_typing(self):
        room_id = yield self.create_room()
        get = self.get(typing="-1")
        yield self.hs.get_typing_handler().started_typing(self.user,
                                                          self.user,
                                                          room_id,
                                                          timeout=2)
        code, body = yield get
        self.assertEquals(code, 200)
        self.assertEquals(body["typing"]["field_names"],
                          ["position", "room_id", "typing"])

    @defer.inlineCallbacks
    def test_receipts(self):
        room_id = yield self.create_room()
        event_id = yield self.send_text_message(room_id, "Hello, World")
        get = self.get(receipts="-1")
        yield self.hs.get_receipts_handler().received_client_receipt(
            room_id, "m.read", self.user_id, event_id)
        code, body = yield get
        self.assertEquals(code, 200)
        self.assertEquals(body["receipts"]["field_names"], [
            "position", "room_id", "receipt_type", "user_id", "event_id",
            "data"
        ])

    def _test_timeout(stream):
        """Check that a request for the given stream timesout"""
        @defer.inlineCallbacks
        def test_timeout(self):
            get = self.get(**{stream: "-1", "timeout": "0"})
            self.hs.clock.advance_time_msec(1)
            code, body = yield get
            self.assertEquals(code, 200)
            self.assertEquals(body.get("rows", []), [])

        test_timeout.__name__ = "test_timeout_%s" % (stream)
        return test_timeout

    test_timeout_events = _test_timeout("events")
    test_timeout_presence = _test_timeout("presence")
    test_timeout_typing = _test_timeout("typing")
    test_timeout_receipts = _test_timeout("receipts")
    test_timeout_user_account_data = _test_timeout("user_account_data")
    test_timeout_room_account_data = _test_timeout("room_account_data")
    test_timeout_tag_account_data = _test_timeout("tag_account_data")
    test_timeout_backfill = _test_timeout("backfill")
    test_timeout_push_rules = _test_timeout("push_rules")
    test_timeout_pushers = _test_timeout("pushers")
    test_timeout_state = _test_timeout("state")

    @defer.inlineCallbacks
    def send_text_message(self, room_id, message):
        handler = self.hs.get_handlers().message_handler
        event = yield handler.create_and_send_nonmember_event(
            synapse.types.create_requester(self.user), {
                "type": "m.room.message",
                "content": {
                    "body": "message",
                    "msgtype": "m.text"
                },
                "room_id": room_id,
                "sender": self.user.to_string(),
            })
        defer.returnValue(event.event_id)

    @defer.inlineCallbacks
    def create_room(self):
        result = yield self.hs.get_handlers(
        ).room_creation_handler.create_room(
            synapse.types.create_requester(self.user), {})
        defer.returnValue(result["room_id"])

    @defer.inlineCallbacks
    def get(self, **params):
        request = NonCallableMock(spec_set=[
            "write", "finish", "setResponseCode", "setHeader", "args",
            "method", "processing"
        ])

        request.method = "GET"
        request.args = {k: [v] for k, v in params.items()}

        @contextlib.contextmanager
        def processing():
            yield

        request.processing = processing

        yield self.resource._async_render_GET(request)
        self.assertTrue(request.finish.called)

        if request.setResponseCode.called:
            response_code = request.setResponseCode.call_args[0][0]
        else:
            response_code = 200

        response_json = "".join(call[0][0]
                                for call in request.write.call_args_list)
        response_body = json.loads(response_json)

        if response_code == 200:
            self.check_response(response_body)

        defer.returnValue((response_code, response_body))

    def check_response(self, response_body):
        for name, stream in response_body.items():
            self.assertIn("field_names", stream)
            field_names = stream["field_names"]
            self.assertIn("rows", stream)
            for row in stream["rows"]:
                self.assertEquals(
                    len(row), len(field_names),
                    "%s: len(row = %r) == len(field_names = %r)" %
                    (name, row, field_names))
Пример #8
0
    def _listener_http(self, config, listener_config):
        port = listener_config["port"]
        bind_address = listener_config.get("bind_address", "")
        tls = listener_config.get("tls", False)
        site_tag = listener_config.get("tag", port)

        if tls and config.no_tls:
            return

        resources = {}
        for res in listener_config["resources"]:
            for name in res["names"]:
                if name == "client":
                    client_resource = ClientRestResource(self)
                    if res["compress"]:
                        client_resource = gz_wrap(client_resource)

                    resources.update({
                        "/_matrix/client/api/v1":
                        client_resource,
                        "/_matrix/client/r0":
                        client_resource,
                        "/_matrix/client/unstable":
                        client_resource,
                        "/_matrix/client/v2_alpha":
                        client_resource,
                        "/_matrix/client/versions":
                        client_resource,
                    })

                if name == "federation":
                    resources.update({
                        FEDERATION_PREFIX:
                        TransportLayerServer(self),
                    })

                if name in ["static", "client"]:
                    resources.update({
                        STATIC_PREFIX:
                        File(
                            os.path.join(os.path.dirname(synapse.__file__),
                                         "static")),
                    })

                if name in ["media", "federation", "client"]:
                    media_repo = MediaRepositoryResource(self)
                    resources.update({
                        MEDIA_PREFIX:
                        media_repo,
                        LEGACY_MEDIA_PREFIX:
                        media_repo,
                        CONTENT_REPO_PREFIX:
                        ContentRepoResource(self, self.config.uploads_path,
                                            self.auth, self.content_addr),
                    })

                if name in ["keys", "federation"]:
                    resources.update({
                        SERVER_KEY_PREFIX:
                        LocalKey(self),
                        SERVER_KEY_V2_PREFIX:
                        KeyApiV2Resource(self),
                    })

                if name == "webclient":
                    resources[
                        WEB_CLIENT_PREFIX] = build_resource_for_web_client(
                            self)

                if name == "metrics" and self.get_config().enable_metrics:
                    resources[METRICS_PREFIX] = MetricsResource(self)

                if name == "replication":
                    resources[REPLICATION_PREFIX] = ReplicationResource(self)

        if WEB_CLIENT_PREFIX in resources:
            root_resource = RootRedirect(WEB_CLIENT_PREFIX)
        else:
            root_resource = Resource()

        root_resource = create_resource_tree(resources, root_resource)
        if tls:
            reactor.listenSSL(port,
                              SynapseSite(
                                  "synapse.access.https.%s" % (site_tag, ),
                                  site_tag,
                                  listener_config,
                                  root_resource,
                              ),
                              self.tls_server_context_factory,
                              interface=bind_address)
        else:
            reactor.listenTCP(port,
                              SynapseSite(
                                  "synapse.access.http.%s" % (site_tag, ),
                                  site_tag,
                                  listener_config,
                                  root_resource,
                              ),
                              interface=bind_address)
        logger.info("Synapse now listening on port %d", port)
Пример #9
0
class ReplicationResourceCase(unittest.TestCase):
    @defer.inlineCallbacks
    def setUp(self):
        self.hs = yield setup_test_homeserver(
            "red",
            http_client=None,
            replication_layer=Mock(),
            ratelimiter=NonCallableMock(spec_set=[
                "send_message",
            ]),
        )
        self.user_id = "@seeing:red"
        self.user = UserID.from_string(self.user_id)

        self.hs.get_ratelimiter().send_message.return_value = (True, 0)

        self.resource = ReplicationResource(self.hs)

    @defer.inlineCallbacks
    def test_streams(self):
        # Passing "-1" returns the current stream positions
        code, body = yield self.get(streams="-1")
        self.assertEquals(code, 200)
        self.assertEquals(body["streams"]["field_names"], ["name", "position"])
        position = body["streams"]["position"]
        # Passing the current position returns an empty response after the
        # timeout
        get = self.get(streams=str(position), timeout="0")
        self.hs.clock.advance_time_msec(1)
        code, body = yield get
        self.assertEquals(code, 200)
        self.assertEquals(body, {})

    @defer.inlineCallbacks
    def test_events_and_state(self):
        get = self.get(events="-1", state="-1", timeout="0")
        yield self.hs.get_handlers().room_creation_handler.create_room(
            synapse.types.create_requester(self.user), {}
        )
        code, body = yield get
        self.assertEquals(code, 200)
        self.assertEquals(body["events"]["field_names"], [
            "position", "internal", "json", "state_group"
        ])
        self.assertEquals(body["state_groups"]["field_names"], [
            "position", "room_id", "event_id"
        ])
        self.assertEquals(body["state_group_state"]["field_names"], [
            "position", "type", "state_key", "event_id"
        ])

    @defer.inlineCallbacks
    def test_presence(self):
        get = self.get(presence="-1")
        yield self.hs.get_presence_handler().set_state(
            self.user, {"presence": "online"}
        )
        code, body = yield get
        self.assertEquals(code, 200)
        self.assertEquals(body["presence"]["field_names"], [
            "position", "user_id", "state", "last_active_ts",
            "last_federation_update_ts", "last_user_sync_ts",
            "status_msg", "currently_active",
        ])

    @defer.inlineCallbacks
    def test_typing(self):
        room_id = yield self.create_room()
        get = self.get(typing="-1")
        yield self.hs.get_typing_handler().started_typing(
            self.user, self.user, room_id, timeout=2
        )
        code, body = yield get
        self.assertEquals(code, 200)
        self.assertEquals(body["typing"]["field_names"], [
            "position", "room_id", "typing"
        ])

    @defer.inlineCallbacks
    def test_receipts(self):
        room_id = yield self.create_room()
        event_id = yield self.send_text_message(room_id, "Hello, World")
        get = self.get(receipts="-1")
        yield self.hs.get_handlers().receipts_handler.received_client_receipt(
            room_id, "m.read", self.user_id, event_id
        )
        code, body = yield get
        self.assertEquals(code, 200)
        self.assertEquals(body["receipts"]["field_names"], [
            "position", "room_id", "receipt_type", "user_id", "event_id", "data"
        ])

    def _test_timeout(stream):
        """Check that a request for the given stream timesout"""
        @defer.inlineCallbacks
        def test_timeout(self):
            get = self.get(**{stream: "-1", "timeout": "0"})
            self.hs.clock.advance_time_msec(1)
            code, body = yield get
            self.assertEquals(code, 200)
            self.assertEquals(body, {})
        test_timeout.__name__ = "test_timeout_%s" % (stream)
        return test_timeout

    test_timeout_events = _test_timeout("events")
    test_timeout_presence = _test_timeout("presence")
    test_timeout_typing = _test_timeout("typing")
    test_timeout_receipts = _test_timeout("receipts")
    test_timeout_user_account_data = _test_timeout("user_account_data")
    test_timeout_room_account_data = _test_timeout("room_account_data")
    test_timeout_tag_account_data = _test_timeout("tag_account_data")
    test_timeout_backfill = _test_timeout("backfill")
    test_timeout_push_rules = _test_timeout("push_rules")
    test_timeout_pushers = _test_timeout("pushers")
    test_timeout_state = _test_timeout("state")

    @defer.inlineCallbacks
    def send_text_message(self, room_id, message):
        handler = self.hs.get_handlers().message_handler
        event = yield handler.create_and_send_nonmember_event(
            synapse.types.create_requester(self.user),
            {
                "type": "m.room.message",
                "content": {"body": "message", "msgtype": "m.text"},
                "room_id": room_id,
                "sender": self.user.to_string(),
            }
        )
        defer.returnValue(event.event_id)

    @defer.inlineCallbacks
    def create_room(self):
        result = yield self.hs.get_handlers().room_creation_handler.create_room(
            synapse.types.create_requester(self.user), {}
        )
        defer.returnValue(result["room_id"])

    @defer.inlineCallbacks
    def get(self, **params):
        request = NonCallableMock(spec_set=[
            "write", "finish", "setResponseCode", "setHeader", "args",
            "method", "processing"
        ])

        request.method = "GET"
        request.args = {k: [v] for k, v in params.items()}

        @contextlib.contextmanager
        def processing():
            yield
        request.processing = processing

        yield self.resource._async_render_GET(request)
        self.assertTrue(request.finish.called)

        if request.setResponseCode.called:
            response_code = request.setResponseCode.call_args[0][0]
        else:
            response_code = 200

        response_json = "".join(
            call[0][0] for call in request.write.call_args_list
        )
        response_body = json.loads(response_json)

        if response_code == 200:
            self.check_response(response_body)

        defer.returnValue((response_code, response_body))

    def check_response(self, response_body):
        for name, stream in response_body.items():
            self.assertIn("field_names", stream)
            field_names = stream["field_names"]
            self.assertIn("rows", stream)
            self.assertTrue(stream["rows"])
            for row in stream["rows"]:
                self.assertEquals(
                    len(row), len(field_names),
                    "%s: len(row = %r) == len(field_names = %r)" % (
                        name, row, field_names
                    )
                )