2015-02-04 16:21:03 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-07 05:26:29 +01:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-02-04 16:21:03 +01:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2018-07-09 08:09:20 +02:00
|
|
|
import json
|
|
|
|
import os
|
2016-01-14 15:34:01 +01:00
|
|
|
import tempfile
|
2018-07-09 08:09:20 +02:00
|
|
|
|
|
|
|
from mock import Mock
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
2015-02-04 16:21:03 +01:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2015-03-09 14:10:31 +01:00
|
|
|
from synapse.appservice import ApplicationService, ApplicationServiceState
|
2018-07-09 08:09:20 +02:00
|
|
|
from synapse.config._base import ConfigError
|
2020-08-05 22:38:57 +02:00
|
|
|
from synapse.storage.database import DatabasePool, make_conn
|
|
|
|
from synapse.storage.databases.main.appservice import (
|
2018-07-09 08:09:20 +02:00
|
|
|
ApplicationServiceStore,
|
|
|
|
ApplicationServiceTransactionStore,
|
2015-03-09 14:10:31 +01:00
|
|
|
)
|
2015-02-04 16:21:03 +01:00
|
|
|
|
2018-07-09 08:09:20 +02:00
|
|
|
from tests import unittest
|
2020-08-18 22:20:49 +02:00
|
|
|
from tests.test_utils import make_awaitable
|
2018-07-09 08:09:20 +02:00
|
|
|
from tests.utils import setup_test_homeserver
|
2015-02-04 16:21:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ApplicationServiceStoreTestCase(unittest.TestCase):
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def setUp(self):
|
2015-03-31 12:35:45 +02:00
|
|
|
self.as_yaml_files = []
|
2017-01-26 17:06:54 +01:00
|
|
|
hs = yield setup_test_homeserver(
|
2018-09-03 18:21:48 +02:00
|
|
|
self.addCleanup, federation_sender=Mock(), federation_client=Mock()
|
2017-01-26 17:06:54 +01:00
|
|
|
)
|
2015-04-01 15:12:33 +02:00
|
|
|
|
2018-09-03 18:21:48 +02:00
|
|
|
hs.config.app_service_config_files = self.as_yaml_files
|
2020-05-11 19:45:23 +02:00
|
|
|
hs.config.caches.event_cache_size = 1
|
2018-09-03 18:21:48 +02:00
|
|
|
hs.config.password_providers = []
|
|
|
|
|
2015-02-04 16:21:03 +01:00
|
|
|
self.as_token = "token1"
|
2015-03-31 12:35:45 +02:00
|
|
|
self.as_url = "some_url"
|
2016-01-14 15:34:01 +01:00
|
|
|
self.as_id = "as1"
|
|
|
|
self._add_appservice(
|
2018-08-10 15:54:09 +02:00
|
|
|
self.as_token, self.as_id, self.as_url, "some_hs_token", "bob"
|
2016-01-14 15:34:01 +01:00
|
|
|
)
|
|
|
|
self._add_appservice("token2", "as2", "some_url", "some_hs_token", "bob")
|
|
|
|
self._add_appservice("token3", "as3", "some_url", "some_hs_token", "bob")
|
2015-02-04 16:21:03 +01:00
|
|
|
# must be done after inserts
|
2019-12-18 11:45:12 +01:00
|
|
|
database = hs.get_datastores().databases[0]
|
|
|
|
self.store = ApplicationServiceStore(
|
|
|
|
database, make_conn(database._database_config, database.engine), hs
|
|
|
|
)
|
2015-02-04 16:21:03 +01:00
|
|
|
|
2015-03-31 12:35:45 +02:00
|
|
|
def tearDown(self):
|
|
|
|
# TODO: suboptimal that we need to create files for tests!
|
|
|
|
for f in self.as_yaml_files:
|
|
|
|
try:
|
|
|
|
os.remove(f)
|
2017-10-23 16:52:32 +02:00
|
|
|
except Exception:
|
2015-03-31 12:35:45 +02:00
|
|
|
pass
|
2015-02-27 12:10:48 +01:00
|
|
|
|
2016-01-14 15:34:01 +01:00
|
|
|
def _add_appservice(self, as_token, id, url, hs_token, sender):
|
2020-02-21 13:15:07 +01:00
|
|
|
as_yaml = {
|
|
|
|
"url": url,
|
|
|
|
"as_token": as_token,
|
|
|
|
"hs_token": hs_token,
|
|
|
|
"id": id,
|
|
|
|
"sender_localpart": sender,
|
|
|
|
"namespaces": {},
|
|
|
|
}
|
2015-03-31 12:35:45 +02:00
|
|
|
# use the token as the filename
|
2019-06-20 11:32:02 +02:00
|
|
|
with open(as_token, "w") as outfile:
|
2015-03-31 12:35:45 +02:00
|
|
|
outfile.write(yaml.dump(as_yaml))
|
|
|
|
self.as_yaml_files.append(as_token)
|
2015-02-04 16:21:03 +01:00
|
|
|
|
|
|
|
def test_retrieve_unknown_service_token(self):
|
2016-10-06 10:43:32 +02:00
|
|
|
service = self.store.get_app_service_by_token("invalid_token")
|
2015-02-04 16:21:03 +01:00
|
|
|
self.assertEquals(service, None)
|
|
|
|
|
|
|
|
def test_retrieval_of_service(self):
|
2018-08-10 15:54:09 +02:00
|
|
|
stored_service = self.store.get_app_service_by_token(self.as_token)
|
2015-02-04 16:21:03 +01:00
|
|
|
self.assertEquals(stored_service.token, self.as_token)
|
2016-01-14 15:34:01 +01:00
|
|
|
self.assertEquals(stored_service.id, self.as_id)
|
2015-03-31 12:35:45 +02:00
|
|
|
self.assertEquals(stored_service.url, self.as_url)
|
2018-08-10 15:54:09 +02:00
|
|
|
self.assertEquals(stored_service.namespaces[ApplicationService.NS_ALIASES], [])
|
|
|
|
self.assertEquals(stored_service.namespaces[ApplicationService.NS_ROOMS], [])
|
|
|
|
self.assertEquals(stored_service.namespaces[ApplicationService.NS_USERS], [])
|
2015-02-04 16:21:03 +01:00
|
|
|
|
|
|
|
def test_retrieval_of_all_services(self):
|
2016-10-06 10:43:32 +02:00
|
|
|
services = self.store.get_app_services()
|
2015-02-04 16:21:03 +01:00
|
|
|
self.assertEquals(len(services), 3)
|
2015-03-09 14:10:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def setUp(self):
|
2015-03-31 13:07:56 +02:00
|
|
|
self.as_yaml_files = []
|
2015-04-07 19:05:39 +02:00
|
|
|
|
2017-01-26 17:06:54 +01:00
|
|
|
hs = yield setup_test_homeserver(
|
2018-09-03 18:21:48 +02:00
|
|
|
self.addCleanup, federation_sender=Mock(), federation_client=Mock()
|
2017-01-26 17:06:54 +01:00
|
|
|
)
|
2018-09-03 18:21:48 +02:00
|
|
|
|
|
|
|
hs.config.app_service_config_files = self.as_yaml_files
|
2020-05-11 19:45:23 +02:00
|
|
|
hs.config.caches.event_cache_size = 1
|
2018-09-03 18:21:48 +02:00
|
|
|
hs.config.password_providers = []
|
|
|
|
|
2015-03-09 14:10:31 +01:00
|
|
|
self.as_list = [
|
2018-08-10 15:54:09 +02:00
|
|
|
{"token": "token1", "url": "https://matrix-as.org", "id": "id_1"},
|
|
|
|
{"token": "alpha_tok", "url": "https://alpha.com", "id": "id_alpha"},
|
|
|
|
{"token": "beta_tok", "url": "https://beta.com", "id": "id_beta"},
|
|
|
|
{"token": "gamma_tok", "url": "https://gamma.com", "id": "id_gamma"},
|
2015-03-09 14:10:31 +01:00
|
|
|
]
|
|
|
|
for s in self.as_list:
|
2016-01-14 15:34:01 +01:00
|
|
|
yield self._add_service(s["url"], s["token"], s["id"])
|
2015-03-09 14:10:31 +01:00
|
|
|
|
2015-04-07 19:05:39 +02:00
|
|
|
self.as_yaml_files = []
|
|
|
|
|
2019-12-18 11:45:12 +01:00
|
|
|
# We assume there is only one database in these tests
|
|
|
|
database = hs.get_datastores().databases[0]
|
|
|
|
self.db_pool = database._db_pool
|
|
|
|
self.engine = database.engine
|
|
|
|
|
|
|
|
db_config = hs.config.get_single_database()
|
|
|
|
self.store = TestTransactionStore(
|
|
|
|
database, make_conn(db_config, self.engine), hs
|
|
|
|
)
|
2015-03-31 13:07:56 +02:00
|
|
|
|
2016-01-14 15:34:01 +01:00
|
|
|
def _add_service(self, url, as_token, id):
|
2020-02-21 13:15:07 +01:00
|
|
|
as_yaml = {
|
|
|
|
"url": url,
|
|
|
|
"as_token": as_token,
|
|
|
|
"hs_token": "something",
|
|
|
|
"id": id,
|
|
|
|
"sender_localpart": "a_sender",
|
|
|
|
"namespaces": {},
|
|
|
|
}
|
2015-03-31 13:07:56 +02:00
|
|
|
# use the token as the filename
|
2019-06-20 11:32:02 +02:00
|
|
|
with open(as_token, "w") as outfile:
|
2015-03-31 13:07:56 +02:00
|
|
|
outfile.write(yaml.dump(as_yaml))
|
|
|
|
self.as_yaml_files.append(as_token)
|
2015-03-09 14:10:31 +01:00
|
|
|
|
|
|
|
def _set_state(self, id, state, txn=None):
|
2018-09-03 18:21:48 +02:00
|
|
|
return self.db_pool.runOperation(
|
|
|
|
self.engine.convert_param_style(
|
|
|
|
"INSERT INTO application_services_state(as_id, state, last_txn) "
|
|
|
|
"VALUES(?,?,?)"
|
|
|
|
),
|
2018-08-10 15:54:09 +02:00
|
|
|
(id, state, txn),
|
2015-03-09 14:10:31 +01:00
|
|
|
)
|
|
|
|
|
2015-03-09 18:25:20 +01:00
|
|
|
def _insert_txn(self, as_id, txn_id, events):
|
2018-09-03 18:21:48 +02:00
|
|
|
return self.db_pool.runOperation(
|
|
|
|
self.engine.convert_param_style(
|
|
|
|
"INSERT INTO application_services_txns(as_id, txn_id, event_ids) "
|
|
|
|
"VALUES(?,?,?)"
|
|
|
|
),
|
2018-08-10 15:54:09 +02:00
|
|
|
(as_id, txn_id, json.dumps([e.event_id for e in events])),
|
2015-03-09 14:54:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def _set_last_txn(self, as_id, txn_id):
|
2018-09-03 18:21:48 +02:00
|
|
|
return self.db_pool.runOperation(
|
|
|
|
self.engine.convert_param_style(
|
|
|
|
"INSERT INTO application_services_state(as_id, last_txn, state) "
|
|
|
|
"VALUES(?,?,?)"
|
|
|
|
),
|
2018-08-10 15:54:09 +02:00
|
|
|
(as_id, txn_id, ApplicationServiceState.UP),
|
2015-03-09 14:54:20 +01:00
|
|
|
)
|
|
|
|
|
2015-03-09 14:10:31 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_appservice_state_none(self):
|
2018-09-03 18:21:48 +02:00
|
|
|
service = Mock(id="999")
|
2020-08-12 15:28:48 +02:00
|
|
|
state = yield defer.ensureDeferred(self.store.get_appservice_state(service))
|
2015-03-09 14:10:31 +01:00
|
|
|
self.assertEquals(None, state)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_appservice_state_up(self):
|
2018-08-10 15:54:09 +02:00
|
|
|
yield self._set_state(self.as_list[0]["id"], ApplicationServiceState.UP)
|
2015-03-09 14:10:31 +01:00
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
2020-08-12 15:28:48 +02:00
|
|
|
state = yield defer.ensureDeferred(self.store.get_appservice_state(service))
|
2015-03-09 14:10:31 +01:00
|
|
|
self.assertEquals(ApplicationServiceState.UP, state)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_appservice_state_down(self):
|
2018-08-10 15:54:09 +02:00
|
|
|
yield self._set_state(self.as_list[0]["id"], ApplicationServiceState.UP)
|
|
|
|
yield self._set_state(self.as_list[1]["id"], ApplicationServiceState.DOWN)
|
|
|
|
yield self._set_state(self.as_list[2]["id"], ApplicationServiceState.DOWN)
|
2015-03-09 14:10:31 +01:00
|
|
|
service = Mock(id=self.as_list[1]["id"])
|
2020-08-12 15:28:48 +02:00
|
|
|
state = yield defer.ensureDeferred(self.store.get_appservice_state(service))
|
2015-03-09 14:10:31 +01:00
|
|
|
self.assertEquals(ApplicationServiceState.DOWN, state)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_appservices_by_state_none(self):
|
2020-08-12 15:28:48 +02:00
|
|
|
services = yield defer.ensureDeferred(
|
|
|
|
self.store.get_appservices_by_state(ApplicationServiceState.DOWN)
|
2015-03-09 14:10:31 +01:00
|
|
|
)
|
|
|
|
self.assertEquals(0, len(services))
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_set_appservices_state_down(self):
|
|
|
|
service = Mock(id=self.as_list[1]["id"])
|
2020-08-17 18:18:01 +02:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.store.set_appservice_state(service, ApplicationServiceState.DOWN)
|
|
|
|
)
|
2015-03-09 14:10:31 +01:00
|
|
|
rows = yield self.db_pool.runQuery(
|
2018-09-03 18:21:48 +02:00
|
|
|
self.engine.convert_param_style(
|
|
|
|
"SELECT as_id FROM application_services_state WHERE state=?"
|
|
|
|
),
|
2018-08-10 15:54:09 +02:00
|
|
|
(ApplicationServiceState.DOWN,),
|
2015-03-09 14:10:31 +01:00
|
|
|
)
|
|
|
|
self.assertEquals(service.id, rows[0][0])
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_set_appservices_state_multiple_up(self):
|
|
|
|
service = Mock(id=self.as_list[1]["id"])
|
2020-08-17 18:18:01 +02:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.store.set_appservice_state(service, ApplicationServiceState.UP)
|
|
|
|
)
|
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.store.set_appservice_state(service, ApplicationServiceState.DOWN)
|
|
|
|
)
|
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.store.set_appservice_state(service, ApplicationServiceState.UP)
|
|
|
|
)
|
2015-03-09 14:10:31 +01:00
|
|
|
rows = yield self.db_pool.runQuery(
|
2018-09-03 18:21:48 +02:00
|
|
|
self.engine.convert_param_style(
|
|
|
|
"SELECT as_id FROM application_services_state WHERE state=?"
|
|
|
|
),
|
2018-08-10 15:54:09 +02:00
|
|
|
(ApplicationServiceState.UP,),
|
2015-03-09 14:10:31 +01:00
|
|
|
)
|
|
|
|
self.assertEquals(service.id, rows[0][0])
|
|
|
|
|
2015-03-09 14:54:20 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_create_appservice_txn_first(self):
|
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
2015-03-09 18:25:20 +01:00
|
|
|
events = [Mock(event_id="e1"), Mock(event_id="e2")]
|
2020-08-27 23:24:46 +02:00
|
|
|
txn = yield defer.ensureDeferred(
|
|
|
|
self.store.create_appservice_txn(service, events)
|
|
|
|
)
|
2015-03-09 14:54:20 +01:00
|
|
|
self.assertEquals(txn.id, 1)
|
|
|
|
self.assertEquals(txn.events, events)
|
|
|
|
self.assertEquals(txn.service, service)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_create_appservice_txn_older_last_txn(self):
|
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
2015-03-09 18:25:20 +01:00
|
|
|
events = [Mock(event_id="e1"), Mock(event_id="e2")]
|
2015-03-09 14:54:20 +01:00
|
|
|
yield self._set_last_txn(service.id, 9643) # AS is falling behind
|
|
|
|
yield self._insert_txn(service.id, 9644, events)
|
|
|
|
yield self._insert_txn(service.id, 9645, events)
|
2020-08-27 23:24:46 +02:00
|
|
|
txn = yield defer.ensureDeferred(
|
|
|
|
self.store.create_appservice_txn(service, events)
|
|
|
|
)
|
2015-03-09 14:54:20 +01:00
|
|
|
self.assertEquals(txn.id, 9646)
|
|
|
|
self.assertEquals(txn.events, events)
|
|
|
|
self.assertEquals(txn.service, service)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_create_appservice_txn_up_to_date_last_txn(self):
|
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
2015-03-09 18:25:20 +01:00
|
|
|
events = [Mock(event_id="e1"), Mock(event_id="e2")]
|
2015-03-09 14:54:20 +01:00
|
|
|
yield self._set_last_txn(service.id, 9643)
|
2020-08-27 23:24:46 +02:00
|
|
|
txn = yield defer.ensureDeferred(
|
|
|
|
self.store.create_appservice_txn(service, events)
|
|
|
|
)
|
2015-03-09 14:54:20 +01:00
|
|
|
self.assertEquals(txn.id, 9644)
|
|
|
|
self.assertEquals(txn.events, events)
|
|
|
|
self.assertEquals(txn.service, service)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_create_appservice_txn_up_fuzzing(self):
|
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
2015-03-09 18:25:20 +01:00
|
|
|
events = [Mock(event_id="e1"), Mock(event_id="e2")]
|
2015-03-09 14:54:20 +01:00
|
|
|
yield self._set_last_txn(service.id, 9643)
|
|
|
|
|
|
|
|
# dump in rows with higher IDs to make sure the queries aren't wrong.
|
|
|
|
yield self._set_last_txn(self.as_list[1]["id"], 119643)
|
|
|
|
yield self._set_last_txn(self.as_list[2]["id"], 9)
|
|
|
|
yield self._set_last_txn(self.as_list[3]["id"], 9643)
|
|
|
|
yield self._insert_txn(self.as_list[1]["id"], 119644, events)
|
|
|
|
yield self._insert_txn(self.as_list[1]["id"], 119645, events)
|
|
|
|
yield self._insert_txn(self.as_list[1]["id"], 119646, events)
|
|
|
|
yield self._insert_txn(self.as_list[2]["id"], 10, events)
|
|
|
|
yield self._insert_txn(self.as_list[3]["id"], 9643, events)
|
|
|
|
|
2020-08-27 23:24:46 +02:00
|
|
|
txn = yield defer.ensureDeferred(
|
|
|
|
self.store.create_appservice_txn(service, events)
|
|
|
|
)
|
2015-03-09 14:54:20 +01:00
|
|
|
self.assertEquals(txn.id, 9644)
|
|
|
|
self.assertEquals(txn.events, events)
|
|
|
|
self.assertEquals(txn.service, service)
|
|
|
|
|
2015-03-09 16:53:03 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_complete_appservice_txn_first_txn(self):
|
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
2015-03-09 18:25:20 +01:00
|
|
|
events = [Mock(event_id="e1"), Mock(event_id="e2")]
|
2015-03-09 16:53:03 +01:00
|
|
|
txn_id = 1
|
|
|
|
|
|
|
|
yield self._insert_txn(service.id, txn_id, events)
|
2020-08-27 23:24:46 +02:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.store.complete_appservice_txn(txn_id=txn_id, service=service)
|
|
|
|
)
|
2015-03-09 16:53:03 +01:00
|
|
|
|
|
|
|
res = yield self.db_pool.runQuery(
|
2018-09-03 18:21:48 +02:00
|
|
|
self.engine.convert_param_style(
|
|
|
|
"SELECT last_txn FROM application_services_state WHERE as_id=?"
|
|
|
|
),
|
2018-08-10 15:54:09 +02:00
|
|
|
(service.id,),
|
2015-03-09 16:53:03 +01:00
|
|
|
)
|
|
|
|
self.assertEquals(1, len(res))
|
2015-04-07 19:05:39 +02:00
|
|
|
self.assertEquals(txn_id, res[0][0])
|
2015-03-09 16:53:03 +01:00
|
|
|
|
|
|
|
res = yield self.db_pool.runQuery(
|
2018-09-03 18:21:48 +02:00
|
|
|
self.engine.convert_param_style(
|
|
|
|
"SELECT * FROM application_services_txns WHERE txn_id=?"
|
|
|
|
),
|
|
|
|
(txn_id,),
|
2015-03-09 16:53:03 +01:00
|
|
|
)
|
|
|
|
self.assertEquals(0, len(res))
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_complete_appservice_txn_existing_in_state_table(self):
|
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
2015-03-09 18:25:20 +01:00
|
|
|
events = [Mock(event_id="e1"), Mock(event_id="e2")]
|
2015-03-09 16:53:03 +01:00
|
|
|
txn_id = 5
|
|
|
|
yield self._set_last_txn(service.id, 4)
|
|
|
|
yield self._insert_txn(service.id, txn_id, events)
|
2020-08-27 23:24:46 +02:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.store.complete_appservice_txn(txn_id=txn_id, service=service)
|
|
|
|
)
|
2015-03-09 16:53:03 +01:00
|
|
|
|
|
|
|
res = yield self.db_pool.runQuery(
|
2018-09-03 18:21:48 +02:00
|
|
|
self.engine.convert_param_style(
|
|
|
|
"SELECT last_txn, state FROM application_services_state WHERE as_id=?"
|
|
|
|
),
|
2018-08-10 15:54:09 +02:00
|
|
|
(service.id,),
|
2015-03-09 16:53:03 +01:00
|
|
|
)
|
|
|
|
self.assertEquals(1, len(res))
|
2015-04-07 19:05:39 +02:00
|
|
|
self.assertEquals(txn_id, res[0][0])
|
2015-03-09 16:53:03 +01:00
|
|
|
self.assertEquals(ApplicationServiceState.UP, res[0][1])
|
|
|
|
|
|
|
|
res = yield self.db_pool.runQuery(
|
2018-09-03 18:21:48 +02:00
|
|
|
self.engine.convert_param_style(
|
|
|
|
"SELECT * FROM application_services_txns WHERE txn_id=?"
|
|
|
|
),
|
|
|
|
(txn_id,),
|
2015-03-09 16:53:03 +01:00
|
|
|
)
|
|
|
|
self.assertEquals(0, len(res))
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_oldest_unsent_txn_none(self):
|
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
|
|
|
|
2020-08-12 15:28:48 +02:00
|
|
|
txn = yield defer.ensureDeferred(self.store.get_oldest_unsent_txn(service))
|
2015-03-09 16:53:03 +01:00
|
|
|
self.assertEquals(None, txn)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_oldest_unsent_txn(self):
|
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
2016-06-07 11:24:50 +02:00
|
|
|
events = [Mock(event_id="e1"), Mock(event_id="e2")]
|
2015-03-09 18:25:20 +01:00
|
|
|
other_events = [Mock(event_id="e5"), Mock(event_id="e6")]
|
2015-03-09 16:53:03 +01:00
|
|
|
|
2015-03-09 18:25:20 +01:00
|
|
|
# we aren't testing store._base stuff here, so mock this out
|
2020-08-18 22:20:49 +02:00
|
|
|
self.store.get_events_as_list = Mock(return_value=make_awaitable(events))
|
2015-03-09 18:25:20 +01:00
|
|
|
|
|
|
|
yield self._insert_txn(self.as_list[1]["id"], 9, other_events)
|
2016-06-07 11:24:50 +02:00
|
|
|
yield self._insert_txn(service.id, 10, events)
|
2015-03-09 18:25:20 +01:00
|
|
|
yield self._insert_txn(service.id, 11, other_events)
|
|
|
|
yield self._insert_txn(service.id, 12, other_events)
|
2015-03-09 16:53:03 +01:00
|
|
|
|
2020-08-12 15:28:48 +02:00
|
|
|
txn = yield defer.ensureDeferred(self.store.get_oldest_unsent_txn(service))
|
2015-03-09 16:53:03 +01:00
|
|
|
self.assertEquals(service, txn.service)
|
|
|
|
self.assertEquals(10, txn.id)
|
2016-06-07 11:24:50 +02:00
|
|
|
self.assertEquals(events, txn.events)
|
2015-03-09 16:53:03 +01:00
|
|
|
|
2015-03-09 14:10:31 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_appservices_by_state_single(self):
|
2018-08-10 15:54:09 +02:00
|
|
|
yield self._set_state(self.as_list[0]["id"], ApplicationServiceState.DOWN)
|
|
|
|
yield self._set_state(self.as_list[1]["id"], ApplicationServiceState.UP)
|
2015-03-09 14:10:31 +01:00
|
|
|
|
2020-08-12 15:28:48 +02:00
|
|
|
services = yield defer.ensureDeferred(
|
|
|
|
self.store.get_appservices_by_state(ApplicationServiceState.DOWN)
|
2015-03-09 14:10:31 +01:00
|
|
|
)
|
|
|
|
self.assertEquals(1, len(services))
|
|
|
|
self.assertEquals(self.as_list[0]["id"], services[0].id)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_appservices_by_state_multiple(self):
|
2018-08-10 15:54:09 +02:00
|
|
|
yield self._set_state(self.as_list[0]["id"], ApplicationServiceState.DOWN)
|
|
|
|
yield self._set_state(self.as_list[1]["id"], ApplicationServiceState.UP)
|
|
|
|
yield self._set_state(self.as_list[2]["id"], ApplicationServiceState.DOWN)
|
|
|
|
yield self._set_state(self.as_list[3]["id"], ApplicationServiceState.UP)
|
2015-03-09 14:10:31 +01:00
|
|
|
|
2020-08-12 15:28:48 +02:00
|
|
|
services = yield defer.ensureDeferred(
|
|
|
|
self.store.get_appservices_by_state(ApplicationServiceState.DOWN)
|
2015-03-09 14:10:31 +01:00
|
|
|
)
|
|
|
|
self.assertEquals(2, len(services))
|
2015-03-31 13:07:56 +02:00
|
|
|
self.assertEquals(
|
2020-02-21 13:15:07 +01:00
|
|
|
{self.as_list[2]["id"], self.as_list[0]["id"]},
|
|
|
|
{services[0].id, services[1].id},
|
2015-03-31 13:07:56 +02:00
|
|
|
)
|
2015-03-09 14:10:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
# required for ApplicationServiceTransactionStoreTestCase tests
|
2018-08-10 15:54:09 +02:00
|
|
|
class TestTransactionStore(ApplicationServiceTransactionStore, ApplicationServiceStore):
|
2020-08-05 22:38:57 +02:00
|
|
|
def __init__(self, database: DatabasePool, db_conn, hs):
|
2020-09-18 15:56:44 +02:00
|
|
|
super().__init__(database, db_conn, hs)
|
2016-01-14 15:34:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ApplicationServiceStoreConfigTestCase(unittest.TestCase):
|
|
|
|
def _write_config(self, suffix, **kwargs):
|
|
|
|
vals = {
|
|
|
|
"id": "id" + suffix,
|
|
|
|
"url": "url" + suffix,
|
|
|
|
"as_token": "as_token" + suffix,
|
|
|
|
"hs_token": "hs_token" + suffix,
|
|
|
|
"sender_localpart": "sender_localpart" + suffix,
|
|
|
|
"namespaces": {},
|
|
|
|
}
|
|
|
|
vals.update(kwargs)
|
|
|
|
|
|
|
|
_, path = tempfile.mkstemp(prefix="as_config")
|
|
|
|
with open(path, "w") as f:
|
|
|
|
f.write(yaml.dump(vals))
|
|
|
|
return path
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_unique_works(self):
|
|
|
|
f1 = self._write_config(suffix="1")
|
|
|
|
f2 = self._write_config(suffix="2")
|
|
|
|
|
2016-11-21 12:53:02 +01:00
|
|
|
hs = yield setup_test_homeserver(
|
2018-09-03 18:21:48 +02:00
|
|
|
self.addCleanup, federation_sender=Mock(), federation_client=Mock()
|
2016-11-21 12:53:02 +01:00
|
|
|
)
|
2016-01-14 15:34:01 +01:00
|
|
|
|
2018-09-03 18:21:48 +02:00
|
|
|
hs.config.app_service_config_files = [f1, f2]
|
2020-05-11 19:45:23 +02:00
|
|
|
hs.config.caches.event_cache_size = 1
|
2018-09-03 18:21:48 +02:00
|
|
|
hs.config.password_providers = []
|
|
|
|
|
2019-12-18 11:45:12 +01:00
|
|
|
database = hs.get_datastores().databases[0]
|
|
|
|
ApplicationServiceStore(
|
|
|
|
database, make_conn(database._database_config, database.engine), hs
|
|
|
|
)
|
2016-01-14 15:34:01 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_duplicate_ids(self):
|
|
|
|
f1 = self._write_config(id="id", suffix="1")
|
|
|
|
f2 = self._write_config(id="id", suffix="2")
|
|
|
|
|
2016-11-21 12:53:02 +01:00
|
|
|
hs = yield setup_test_homeserver(
|
2018-09-03 18:21:48 +02:00
|
|
|
self.addCleanup, federation_sender=Mock(), federation_client=Mock()
|
2016-11-21 12:53:02 +01:00
|
|
|
)
|
2016-01-14 15:34:01 +01:00
|
|
|
|
2018-09-03 18:21:48 +02:00
|
|
|
hs.config.app_service_config_files = [f1, f2]
|
2020-05-11 19:45:23 +02:00
|
|
|
hs.config.caches.event_cache_size = 1
|
2018-09-03 18:21:48 +02:00
|
|
|
hs.config.password_providers = []
|
|
|
|
|
2016-01-14 15:34:01 +01:00
|
|
|
with self.assertRaises(ConfigError) as cm:
|
2019-12-18 11:45:12 +01:00
|
|
|
database = hs.get_datastores().databases[0]
|
|
|
|
ApplicationServiceStore(
|
|
|
|
database, make_conn(database._database_config, database.engine), hs
|
|
|
|
)
|
2016-01-14 15:34:01 +01:00
|
|
|
|
|
|
|
e = cm.exception
|
2018-04-15 17:20:37 +02:00
|
|
|
self.assertIn(f1, str(e))
|
|
|
|
self.assertIn(f2, str(e))
|
|
|
|
self.assertIn("id", str(e))
|
2016-01-14 15:34:01 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_duplicate_as_tokens(self):
|
|
|
|
f1 = self._write_config(as_token="as_token", suffix="1")
|
|
|
|
f2 = self._write_config(as_token="as_token", suffix="2")
|
|
|
|
|
2016-11-21 12:53:02 +01:00
|
|
|
hs = yield setup_test_homeserver(
|
2018-09-03 18:21:48 +02:00
|
|
|
self.addCleanup, federation_sender=Mock(), federation_client=Mock()
|
2016-11-21 12:53:02 +01:00
|
|
|
)
|
2016-01-14 15:34:01 +01:00
|
|
|
|
2018-09-03 18:21:48 +02:00
|
|
|
hs.config.app_service_config_files = [f1, f2]
|
2020-05-11 19:45:23 +02:00
|
|
|
hs.config.caches.event_cache_size = 1
|
2018-09-03 18:21:48 +02:00
|
|
|
hs.config.password_providers = []
|
|
|
|
|
2016-01-14 15:34:01 +01:00
|
|
|
with self.assertRaises(ConfigError) as cm:
|
2019-12-18 11:45:12 +01:00
|
|
|
database = hs.get_datastores().databases[0]
|
|
|
|
ApplicationServiceStore(
|
|
|
|
database, make_conn(database._database_config, database.engine), hs
|
|
|
|
)
|
2016-01-14 15:34:01 +01:00
|
|
|
|
|
|
|
e = cm.exception
|
2018-04-15 17:20:37 +02:00
|
|
|
self.assertIn(f1, str(e))
|
|
|
|
self.assertIn(f2, str(e))
|
|
|
|
self.assertIn("as_token", str(e))
|