Exemple #1
0
class ApplicationServiceStoreTestCase(unittest.TestCase):
    @defer.inlineCallbacks
    def setUp(self):
        self.as_yaml_files = []
        config = Mock(app_service_config_files=self.as_yaml_files)
        hs = yield setup_test_homeserver(config=config)

        self.as_token = "token1"
        self.as_url = "some_url"
        self._add_appservice(self.as_token, self.as_url, "some_hs_token",
                             "bob")
        self._add_appservice("token2", "some_url", "some_hs_token", "bob")
        self._add_appservice("token3", "some_url", "some_hs_token", "bob")
        # must be done after inserts
        self.store = ApplicationServiceStore(hs)

    def tearDown(self):
        # TODO: suboptimal that we need to create files for tests!
        for f in self.as_yaml_files:
            try:
                os.remove(f)
            except:
                pass

    def _add_appservice(self, as_token, url, hs_token, sender):
        as_yaml = dict(url=url,
                       as_token=as_token,
                       hs_token=hs_token,
                       sender_localpart=sender,
                       namespaces={})
        # use the token as the filename
        with open(as_token, 'w') as outfile:
            outfile.write(yaml.dump(as_yaml))
            self.as_yaml_files.append(as_token)

    @defer.inlineCallbacks
    def test_retrieve_unknown_service_token(self):
        service = yield self.store.get_app_service_by_token("invalid_token")
        self.assertEquals(service, None)

    @defer.inlineCallbacks
    def test_retrieval_of_service(self):
        stored_service = yield self.store.get_app_service_by_token(
            self.as_token)
        self.assertEquals(stored_service.token, self.as_token)
        self.assertEquals(stored_service.url, self.as_url)
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ALIASES], [])
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ROOMS], [])
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_USERS], [])

    @defer.inlineCallbacks
    def test_retrieval_of_all_services(self):
        services = yield self.store.get_app_services()
        self.assertEquals(len(services), 3)
Exemple #2
0
class ApplicationServiceStoreTestCase(unittest.TestCase):
    @defer.inlineCallbacks
    def setUp(self):
        self.as_yaml_files = []
        hs = yield setup_test_homeserver(self.addCleanup,
                                         federation_sender=Mock(),
                                         federation_client=Mock())

        hs.config.app_service_config_files = self.as_yaml_files
        hs.config.event_cache_size = 1
        hs.config.password_providers = []

        self.as_token = "token1"
        self.as_url = "some_url"
        self.as_id = "as1"
        self._add_appservice(self.as_token, self.as_id, self.as_url,
                             "some_hs_token", "bob")
        self._add_appservice("token2", "as2", "some_url", "some_hs_token",
                             "bob")
        self._add_appservice("token3", "as3", "some_url", "some_hs_token",
                             "bob")
        # must be done after inserts
        self.store = ApplicationServiceStore(hs.get_db_conn(), hs)

    def tearDown(self):
        # TODO: suboptimal that we need to create files for tests!
        for f in self.as_yaml_files:
            try:
                os.remove(f)
            except Exception:
                pass

    def _add_appservice(self, as_token, id, url, hs_token, sender):
        as_yaml = dict(
            url=url,
            as_token=as_token,
            hs_token=hs_token,
            id=id,
            sender_localpart=sender,
            namespaces={},
        )
        # use the token as the filename
        with open(as_token, 'w') as outfile:
            outfile.write(yaml.dump(as_yaml))
            self.as_yaml_files.append(as_token)

    def test_retrieve_unknown_service_token(self):
        service = self.store.get_app_service_by_token("invalid_token")
        self.assertEquals(service, None)

    def test_retrieval_of_service(self):
        stored_service = self.store.get_app_service_by_token(self.as_token)
        self.assertEquals(stored_service.token, self.as_token)
        self.assertEquals(stored_service.id, self.as_id)
        self.assertEquals(stored_service.url, self.as_url)
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ALIASES], [])
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ROOMS], [])
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_USERS], [])

    def test_retrieval_of_all_services(self):
        services = self.store.get_app_services()
        self.assertEquals(len(services), 3)
class ApplicationServiceStoreTestCase(unittest.TestCase):
    @defer.inlineCallbacks
    def setUp(self):
        self.as_yaml_files = []
        hs = yield setup_test_homeserver(
            self.addCleanup, federation_sender=Mock(), federation_client=Mock()
        )

        hs.config.app_service_config_files = self.as_yaml_files
        hs.config.event_cache_size = 1
        hs.config.password_providers = []

        self.as_token = "token1"
        self.as_url = "some_url"
        self.as_id = "as1"
        self._add_appservice(
            self.as_token, self.as_id, self.as_url, "some_hs_token", "bob"
        )
        self._add_appservice("token2", "as2", "some_url", "some_hs_token", "bob")
        self._add_appservice("token3", "as3", "some_url", "some_hs_token", "bob")
        # must be done after inserts
        self.store = ApplicationServiceStore(hs.get_db_conn(), hs)

    def tearDown(self):
        # TODO: suboptimal that we need to create files for tests!
        for f in self.as_yaml_files:
            try:
                os.remove(f)
            except Exception:
                pass

    def _add_appservice(self, as_token, id, url, hs_token, sender):
        as_yaml = dict(
            url=url,
            as_token=as_token,
            hs_token=hs_token,
            id=id,
            sender_localpart=sender,
            namespaces={},
        )
        # use the token as the filename
        with open(as_token, 'w') as outfile:
            outfile.write(yaml.dump(as_yaml))
            self.as_yaml_files.append(as_token)

    def test_retrieve_unknown_service_token(self):
        service = self.store.get_app_service_by_token("invalid_token")
        self.assertEquals(service, None)

    def test_retrieval_of_service(self):
        stored_service = self.store.get_app_service_by_token(self.as_token)
        self.assertEquals(stored_service.token, self.as_token)
        self.assertEquals(stored_service.id, self.as_id)
        self.assertEquals(stored_service.url, self.as_url)
        self.assertEquals(stored_service.namespaces[ApplicationService.NS_ALIASES], [])
        self.assertEquals(stored_service.namespaces[ApplicationService.NS_ROOMS], [])
        self.assertEquals(stored_service.namespaces[ApplicationService.NS_USERS], [])

    def test_retrieval_of_all_services(self):
        services = self.store.get_app_services()
        self.assertEquals(len(services), 3)
Exemple #4
0
class ApplicationServiceStoreTestCase(unittest.TestCase):
    @defer.inlineCallbacks
    def setUp(self):
        db_pool = SQLiteMemoryDbPool()
        yield db_pool.prepare()
        hs = HomeServer("test",
                        db_pool=db_pool,
                        clock=MockClock(),
                        config=Mock())
        self.as_token = "token1"
        db_pool.runQuery("INSERT INTO application_services(token) VALUES(?)",
                         (self.as_token, ))
        db_pool.runQuery("INSERT INTO application_services(token) VALUES(?)",
                         ("token2", ))
        db_pool.runQuery("INSERT INTO application_services(token) VALUES(?)",
                         ("token3", ))
        # must be done after inserts
        self.store = ApplicationServiceStore(hs)

    @defer.inlineCallbacks
    def test_update_and_retrieval_of_service(self):
        url = "https://matrix.org/appservices/foobar"
        hs_token = "hstok"
        user_regex = ["@foobar_.*:matrix.org"]
        alias_regex = ["#foobar_.*:matrix.org"]
        room_regex = []
        service = ApplicationService(url=url,
                                     hs_token=hs_token,
                                     token=self.as_token,
                                     namespaces={
                                         ApplicationService.NS_USERS:
                                         user_regex,
                                         ApplicationService.NS_ALIASES:
                                         alias_regex,
                                         ApplicationService.NS_ROOMS:
                                         room_regex
                                     })
        yield self.store.update_app_service(service)

        stored_service = yield self.store.get_app_service_by_token(
            self.as_token)
        self.assertEquals(stored_service.token, self.as_token)
        self.assertEquals(stored_service.url, url)
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ALIASES],
            alias_regex)
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ROOMS], room_regex)
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_USERS], user_regex)

    @defer.inlineCallbacks
    def test_retrieve_unknown_service_token(self):
        service = yield self.store.get_app_service_by_token("invalid_token")
        self.assertEquals(service, None)

    @defer.inlineCallbacks
    def test_retrieval_of_service(self):
        stored_service = yield self.store.get_app_service_by_token(
            self.as_token)
        self.assertEquals(stored_service.token, self.as_token)
        self.assertEquals(stored_service.url, None)
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ALIASES], [])
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ROOMS], [])
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_USERS], [])

    @defer.inlineCallbacks
    def test_retrieval_of_all_services(self):
        services = yield self.store.get_app_services()
        self.assertEquals(len(services), 3)
class ApplicationServiceStoreTestCase(unittest.TestCase):

    @defer.inlineCallbacks
    def setUp(self):
        db_pool = SQLiteMemoryDbPool()
        yield db_pool.prepare()
        hs = HomeServer(
            "test", db_pool=db_pool, clock=MockClock(), config=Mock()
        )
        self.as_token = "token1"
        db_pool.runQuery(
            "INSERT INTO application_services(token) VALUES(?)",
            (self.as_token,)
        )
        db_pool.runQuery(
            "INSERT INTO application_services(token) VALUES(?)", ("token2",)
        )
        db_pool.runQuery(
            "INSERT INTO application_services(token) VALUES(?)", ("token3",)
        )
        # must be done after inserts
        self.store = ApplicationServiceStore(hs)

    @defer.inlineCallbacks
    def test_update_and_retrieval_of_service(self):
        url = "https://matrix.org/appservices/foobar"
        hs_token = "hstok"
        user_regex = [
            {"regex": "@foobar_.*:matrix.org", "exclusive": True}
        ]
        alias_regex = [
            {"regex": "#foobar_.*:matrix.org", "exclusive": False}
        ]
        room_regex = [

        ]
        service = ApplicationService(
            url=url, hs_token=hs_token, token=self.as_token, namespaces={
                ApplicationService.NS_USERS: user_regex,
                ApplicationService.NS_ALIASES: alias_regex,
                ApplicationService.NS_ROOMS: room_regex
        })
        yield self.store.update_app_service(service)

        stored_service = yield self.store.get_app_service_by_token(
            self.as_token
        )
        self.assertEquals(stored_service.token, self.as_token)
        self.assertEquals(stored_service.url, url)
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ALIASES],
            alias_regex
        )
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ROOMS],
            room_regex
        )
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_USERS],
            user_regex
        )

    @defer.inlineCallbacks
    def test_retrieve_unknown_service_token(self):
        service = yield self.store.get_app_service_by_token("invalid_token")
        self.assertEquals(service, None)

    @defer.inlineCallbacks
    def test_retrieval_of_service(self):
        stored_service = yield self.store.get_app_service_by_token(
            self.as_token
        )
        self.assertEquals(stored_service.token, self.as_token)
        self.assertEquals(stored_service.url, None)
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ALIASES],
            []
        )
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ROOMS],
            []
        )
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_USERS],
            []
        )

    @defer.inlineCallbacks
    def test_retrieval_of_all_services(self):
        services = yield self.store.get_app_services()
        self.assertEquals(len(services), 3)
class ApplicationServiceStoreTestCase(unittest.TestCase):

    @defer.inlineCallbacks
    def setUp(self):
        self.as_yaml_files = []
        config = Mock(
            app_service_config_files=self.as_yaml_files
        )
        hs = yield setup_test_homeserver(config=config)

        self.as_token = "token1"
        self.as_url = "some_url"
        self._add_appservice(self.as_token, self.as_url, "some_hs_token", "bob")
        self._add_appservice("token2", "some_url", "some_hs_token", "bob")
        self._add_appservice("token3", "some_url", "some_hs_token", "bob")
        # must be done after inserts
        self.store = ApplicationServiceStore(hs)

    def tearDown(self):
        # TODO: suboptimal that we need to create files for tests!
        for f in self.as_yaml_files:
            try:
                os.remove(f)
            except:
                pass

    def _add_appservice(self, as_token, url, hs_token, sender):
        as_yaml = dict(url=url, as_token=as_token, hs_token=hs_token,
                       sender_localpart=sender, namespaces={})
        # use the token as the filename
        with open(as_token, 'w') as outfile:
            outfile.write(yaml.dump(as_yaml))
            self.as_yaml_files.append(as_token)

    @defer.inlineCallbacks
    def test_retrieve_unknown_service_token(self):
        service = yield self.store.get_app_service_by_token("invalid_token")
        self.assertEquals(service, None)

    @defer.inlineCallbacks
    def test_retrieval_of_service(self):
        stored_service = yield self.store.get_app_service_by_token(
            self.as_token
        )
        self.assertEquals(stored_service.token, self.as_token)
        self.assertEquals(stored_service.url, self.as_url)
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ALIASES],
            []
        )
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ROOMS],
            []
        )
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_USERS],
            []
        )

    @defer.inlineCallbacks
    def test_retrieval_of_all_services(self):
        services = yield self.store.get_app_services()
        self.assertEquals(len(services), 3)