2016-01-07 07:26:29 +03:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-02-04 18:21:03 +03: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 09:09:20 +03:00
|
|
|
import json
|
|
|
|
import os
|
2016-01-14 17:34:01 +03:00
|
|
|
import tempfile
|
2022-05-17 13:07:18 +03:00
|
|
|
from typing import List, cast
|
2023-08-25 02:38:46 +03:00
|
|
|
from unittest.mock import AsyncMock, Mock
|
2018-07-09 09:09:20 +03:00
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
2015-02-04 18:21:03 +03:00
|
|
|
from twisted.internet import defer
|
2021-12-02 18:30:05 +03:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
2015-02-04 18:21:03 +03:00
|
|
|
|
2015-03-09 16:10:31 +03:00
|
|
|
from synapse.appservice import ApplicationService, ApplicationServiceState
|
2018-07-09 09:09:20 +03:00
|
|
|
from synapse.config._base import ConfigError
|
2021-12-02 18:30:05 +03:00
|
|
|
from synapse.events import EventBase
|
|
|
|
from synapse.server import HomeServer
|
2022-12-09 20:36:32 +03:00
|
|
|
from synapse.storage.database import DatabasePool, LoggingDatabaseConnection, make_conn
|
2020-08-05 23:38:57 +03:00
|
|
|
from synapse.storage.databases.main.appservice import (
|
2018-07-09 09:09:20 +03:00
|
|
|
ApplicationServiceStore,
|
|
|
|
ApplicationServiceTransactionStore,
|
2015-03-09 16:10:31 +03:00
|
|
|
)
|
2022-03-30 16:39:27 +03:00
|
|
|
from synapse.types import DeviceListUpdates
|
2021-12-02 18:30:05 +03:00
|
|
|
from synapse.util import Clock
|
2015-02-04 18:21:03 +03:00
|
|
|
|
2018-07-09 09:09:20 +03:00
|
|
|
from tests import unittest
|
2015-02-04 18:21:03 +03:00
|
|
|
|
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
class ApplicationServiceStoreTestCase(unittest.HomeserverTestCase):
|
2022-12-09 20:36:32 +03:00
|
|
|
def setUp(self) -> None:
|
2023-08-15 15:11:20 +03:00
|
|
|
super().setUp()
|
2021-12-02 21:43:33 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
self.as_yaml_files: List[str] = []
|
2015-04-01 16:12:33 +03:00
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
self.hs.config.appservice.app_service_config_files = self.as_yaml_files
|
|
|
|
self.hs.config.caches.event_cache_size = 1
|
2018-09-03 19:21:48 +03:00
|
|
|
|
2015-02-04 18:21:03 +03:00
|
|
|
self.as_token = "token1"
|
2015-03-31 13:35:45 +03:00
|
|
|
self.as_url = "some_url"
|
2016-01-14 17:34:01 +03:00
|
|
|
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")
|
2015-02-04 18:21:03 +03:00
|
|
|
# must be done after inserts
|
2021-12-02 21:43:33 +03:00
|
|
|
database = self.hs.get_datastores().databases[0]
|
2019-12-18 13:45:12 +03:00
|
|
|
self.store = ApplicationServiceStore(
|
2021-12-02 21:43:33 +03:00
|
|
|
database,
|
|
|
|
make_conn(database._database_config, database.engine, "test"),
|
|
|
|
self.hs,
|
2019-12-18 13:45:12 +03:00
|
|
|
)
|
2015-02-04 18:21:03 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def tearDown(self) -> None:
|
2015-03-31 13:35:45 +03:00
|
|
|
# TODO: suboptimal that we need to create files for tests!
|
|
|
|
for f in self.as_yaml_files:
|
|
|
|
try:
|
|
|
|
os.remove(f)
|
2017-10-23 17:52:32 +03:00
|
|
|
except Exception:
|
2015-03-31 13:35:45 +03:00
|
|
|
pass
|
2015-02-27 14:10:48 +03:00
|
|
|
|
2023-08-15 15:11:20 +03:00
|
|
|
super().tearDown()
|
2021-12-02 21:43:33 +03:00
|
|
|
|
2022-12-09 20:36:32 +03:00
|
|
|
def _add_appservice(
|
|
|
|
self, as_token: str, id: str, url: str, hs_token: str, sender: str
|
|
|
|
) -> None:
|
2020-02-21 15:15:07 +03:00
|
|
|
as_yaml = {
|
|
|
|
"url": url,
|
|
|
|
"as_token": as_token,
|
|
|
|
"hs_token": hs_token,
|
|
|
|
"id": id,
|
|
|
|
"sender_localpart": sender,
|
|
|
|
"namespaces": {},
|
|
|
|
}
|
2015-03-31 13:35:45 +03:00
|
|
|
# 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)
|
2015-02-04 18:21:03 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_retrieve_unknown_service_token(self) -> None:
|
2016-10-06 11:43:32 +03:00
|
|
|
service = self.store.get_app_service_by_token("invalid_token")
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(service, None)
|
2015-02-04 18:21:03 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_retrieval_of_service(self) -> None:
|
2016-10-06 11:43:32 +03:00
|
|
|
stored_service = self.store.get_app_service_by_token(self.as_token)
|
2021-12-02 18:30:05 +03:00
|
|
|
assert stored_service is not None
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(stored_service.token, self.as_token)
|
|
|
|
self.assertEqual(stored_service.id, self.as_id)
|
|
|
|
self.assertEqual(stored_service.url, self.as_url)
|
|
|
|
self.assertEqual(stored_service.namespaces[ApplicationService.NS_ALIASES], [])
|
|
|
|
self.assertEqual(stored_service.namespaces[ApplicationService.NS_ROOMS], [])
|
|
|
|
self.assertEqual(stored_service.namespaces[ApplicationService.NS_USERS], [])
|
2015-02-04 18:21:03 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_retrieval_of_all_services(self) -> None:
|
2016-10-06 11:43:32 +03:00
|
|
|
services = self.store.get_app_services()
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(len(services), 3)
|
2015-03-09 16:10:31 +03:00
|
|
|
|
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
class ApplicationServiceTransactionStoreTestCase(unittest.HomeserverTestCase):
|
|
|
|
def setUp(self) -> None:
|
2023-08-15 15:11:20 +03:00
|
|
|
super().setUp()
|
2021-12-02 18:30:05 +03:00
|
|
|
self.as_yaml_files: List[str] = []
|
2015-04-07 20:05:39 +03:00
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
self.hs.config.appservice.app_service_config_files = self.as_yaml_files
|
|
|
|
self.hs.config.caches.event_cache_size = 1
|
2018-09-03 19:21:48 +03:00
|
|
|
|
2015-03-09 16:10:31 +03:00
|
|
|
self.as_list = [
|
|
|
|
{"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"},
|
2016-01-14 17:34:01 +03:00
|
|
|
{"token": "gamma_tok", "url": "https://gamma.com", "id": "id_gamma"},
|
2015-03-09 16:10:31 +03:00
|
|
|
]
|
|
|
|
for s in self.as_list:
|
2021-12-02 18:30:05 +03:00
|
|
|
self._add_service(s["url"], s["token"], s["id"])
|
2015-03-09 16:10:31 +03:00
|
|
|
|
2015-04-07 20:05:39 +03:00
|
|
|
self.as_yaml_files = []
|
|
|
|
|
2019-12-18 13:45:12 +03:00
|
|
|
# We assume there is only one database in these tests
|
2021-12-02 21:43:33 +03:00
|
|
|
database = self.hs.get_datastores().databases[0]
|
2019-12-18 13:45:12 +03:00
|
|
|
self.db_pool = database._db_pool
|
|
|
|
self.engine = database.engine
|
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
db_config = self.hs.config.database.get_single_database()
|
2019-12-18 13:45:12 +03:00
|
|
|
self.store = TestTransactionStore(
|
2021-12-02 21:43:33 +03:00
|
|
|
database, make_conn(db_config, self.engine, "test"), self.hs
|
2019-12-18 13:45:12 +03:00
|
|
|
)
|
2015-03-31 14:07:56 +03:00
|
|
|
|
2022-12-09 20:36:32 +03:00
|
|
|
def _add_service(self, url: str, as_token: str, id: str) -> None:
|
2020-02-21 15:15:07 +03:00
|
|
|
as_yaml = {
|
|
|
|
"url": url,
|
|
|
|
"as_token": as_token,
|
|
|
|
"hs_token": "something",
|
|
|
|
"id": id,
|
|
|
|
"sender_localpart": "a_sender",
|
|
|
|
"namespaces": {},
|
|
|
|
}
|
2015-03-31 14:07:56 +03:00
|
|
|
# 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)
|
2015-03-09 16:10:31 +03:00
|
|
|
|
2022-12-09 20:36:32 +03:00
|
|
|
def _set_state(self, id: str, state: ApplicationServiceState) -> defer.Deferred:
|
2018-09-03 19:21:48 +03:00
|
|
|
return self.db_pool.runOperation(
|
|
|
|
self.engine.convert_param_style(
|
2022-05-17 13:07:18 +03:00
|
|
|
"INSERT INTO application_services_state(as_id, state) VALUES(?,?)"
|
2018-09-03 19:21:48 +03:00
|
|
|
),
|
2022-05-17 13:07:18 +03:00
|
|
|
(id, state.value),
|
2015-03-09 16:10:31 +03:00
|
|
|
)
|
|
|
|
|
2022-12-09 20:36:32 +03:00
|
|
|
def _insert_txn(
|
|
|
|
self, as_id: str, txn_id: int, events: List[Mock]
|
|
|
|
) -> "defer.Deferred[None]":
|
2018-09-03 19:21:48 +03:00
|
|
|
return self.db_pool.runOperation(
|
|
|
|
self.engine.convert_param_style(
|
|
|
|
"INSERT INTO application_services_txns(as_id, txn_id, event_ids) "
|
|
|
|
"VALUES(?,?,?)"
|
|
|
|
),
|
2015-03-09 20:25:20 +03:00
|
|
|
(as_id, txn_id, json.dumps([e.event_id for e in events])),
|
2015-03-09 16:54:20 +03:00
|
|
|
)
|
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_get_appservice_state_none(
|
|
|
|
self,
|
2021-12-02 21:43:33 +03:00
|
|
|
) -> None:
|
2018-09-03 19:21:48 +03:00
|
|
|
service = Mock(id="999")
|
2021-12-02 21:43:33 +03:00
|
|
|
state = self.get_success(self.store.get_appservice_state(service))
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(None, state)
|
2015-03-09 16:10:31 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_get_appservice_state_up(
|
|
|
|
self,
|
2021-12-02 21:43:33 +03:00
|
|
|
) -> None:
|
|
|
|
self.get_success(
|
|
|
|
self._set_state(self.as_list[0]["id"], ApplicationServiceState.UP)
|
|
|
|
)
|
2015-03-09 16:10:31 +03:00
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
2021-12-02 21:43:33 +03:00
|
|
|
state = self.get_success(
|
|
|
|
defer.ensureDeferred(self.store.get_appservice_state(service))
|
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(ApplicationServiceState.UP, state)
|
2015-03-09 16:10:31 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_get_appservice_state_down(
|
|
|
|
self,
|
2021-12-02 21:43:33 +03:00
|
|
|
) -> None:
|
|
|
|
self.get_success(
|
|
|
|
self._set_state(self.as_list[0]["id"], ApplicationServiceState.UP)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self._set_state(self.as_list[1]["id"], ApplicationServiceState.DOWN)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self._set_state(self.as_list[2]["id"], ApplicationServiceState.DOWN)
|
|
|
|
)
|
2015-03-09 16:10:31 +03:00
|
|
|
service = Mock(id=self.as_list[1]["id"])
|
2021-12-02 21:43:33 +03:00
|
|
|
state = self.get_success(self.store.get_appservice_state(service))
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(ApplicationServiceState.DOWN, state)
|
2015-03-09 16:10:31 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_get_appservices_by_state_none(
|
|
|
|
self,
|
2021-12-02 21:43:33 +03:00
|
|
|
) -> None:
|
|
|
|
services = self.get_success(
|
2020-08-12 16:28:48 +03:00
|
|
|
self.store.get_appservices_by_state(ApplicationServiceState.DOWN)
|
2015-03-09 16:10:31 +03:00
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(0, len(services))
|
2015-03-09 16:10:31 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_set_appservices_state_down(
|
|
|
|
self,
|
2021-12-02 21:43:33 +03:00
|
|
|
) -> None:
|
2015-03-09 16:10:31 +03:00
|
|
|
service = Mock(id=self.as_list[1]["id"])
|
2021-12-02 21:43:33 +03:00
|
|
|
self.get_success(
|
2020-08-17 19:18:01 +03:00
|
|
|
self.store.set_appservice_state(service, ApplicationServiceState.DOWN)
|
|
|
|
)
|
2021-12-02 21:43:33 +03:00
|
|
|
rows = self.get_success(
|
|
|
|
self.db_pool.runQuery(
|
|
|
|
self.engine.convert_param_style(
|
|
|
|
"SELECT as_id FROM application_services_state WHERE state=?"
|
|
|
|
),
|
|
|
|
(ApplicationServiceState.DOWN.value,),
|
|
|
|
)
|
2015-03-09 16:10:31 +03:00
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(service.id, rows[0][0])
|
2015-03-09 16:10:31 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_set_appservices_state_multiple_up(
|
|
|
|
self,
|
2021-12-02 21:43:33 +03:00
|
|
|
) -> None:
|
2015-03-09 16:10:31 +03:00
|
|
|
service = Mock(id=self.as_list[1]["id"])
|
2021-12-02 21:43:33 +03:00
|
|
|
self.get_success(
|
2020-08-17 19:18:01 +03:00
|
|
|
self.store.set_appservice_state(service, ApplicationServiceState.UP)
|
|
|
|
)
|
2021-12-02 21:43:33 +03:00
|
|
|
self.get_success(
|
2020-08-17 19:18:01 +03:00
|
|
|
self.store.set_appservice_state(service, ApplicationServiceState.DOWN)
|
|
|
|
)
|
2021-12-02 21:43:33 +03:00
|
|
|
self.get_success(
|
2020-08-17 19:18:01 +03:00
|
|
|
self.store.set_appservice_state(service, ApplicationServiceState.UP)
|
|
|
|
)
|
2021-12-02 21:43:33 +03:00
|
|
|
rows = self.get_success(
|
|
|
|
self.db_pool.runQuery(
|
|
|
|
self.engine.convert_param_style(
|
|
|
|
"SELECT as_id FROM application_services_state WHERE state=?"
|
|
|
|
),
|
|
|
|
(ApplicationServiceState.UP.value,),
|
|
|
|
)
|
2015-03-09 16:10:31 +03:00
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(service.id, rows[0][0])
|
2015-03-09 16:10:31 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_create_appservice_txn_first(
|
|
|
|
self,
|
2021-12-02 21:43:33 +03:00
|
|
|
) -> None:
|
2015-03-09 16:54:20 +03:00
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
2021-12-02 18:30:05 +03:00
|
|
|
events = cast(List[EventBase], [Mock(event_id="e1"), Mock(event_id="e2")])
|
2021-12-02 21:43:33 +03:00
|
|
|
txn = self.get_success(
|
2022-02-01 17:13:38 +03:00
|
|
|
defer.ensureDeferred(
|
2022-03-30 16:39:27 +03:00
|
|
|
self.store.create_appservice_txn(
|
|
|
|
service, events, [], [], {}, {}, DeviceListUpdates()
|
|
|
|
)
|
2022-02-01 17:13:38 +03:00
|
|
|
)
|
2020-08-28 00:24:46 +03:00
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(txn.id, 1)
|
|
|
|
self.assertEqual(txn.events, events)
|
|
|
|
self.assertEqual(txn.service, service)
|
2015-03-09 16:54:20 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_complete_appservice_txn_first_txn(
|
|
|
|
self,
|
2021-12-02 21:43:33 +03:00
|
|
|
) -> None:
|
2015-03-09 18:53:03 +03:00
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
2015-03-09 20:25:20 +03:00
|
|
|
events = [Mock(event_id="e1"), Mock(event_id="e2")]
|
2015-03-09 18:53:03 +03:00
|
|
|
txn_id = 1
|
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
self.get_success(self._insert_txn(service.id, txn_id, events))
|
|
|
|
self.get_success(
|
2020-08-28 00:24:46 +03:00
|
|
|
self.store.complete_appservice_txn(txn_id=txn_id, service=service)
|
|
|
|
)
|
2015-03-09 18:53:03 +03:00
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
res = self.get_success(
|
|
|
|
self.db_pool.runQuery(
|
|
|
|
self.engine.convert_param_style(
|
|
|
|
"SELECT * FROM application_services_txns WHERE txn_id=?"
|
|
|
|
),
|
|
|
|
(txn_id,),
|
|
|
|
)
|
2015-03-09 18:53:03 +03:00
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(0, len(res))
|
2015-03-09 18:53:03 +03:00
|
|
|
|
2022-04-01 16:33:25 +03:00
|
|
|
def test_complete_appservice_txn_updates_last_txn_state(
|
2021-12-02 18:30:05 +03:00
|
|
|
self,
|
2021-12-02 21:43:33 +03:00
|
|
|
) -> None:
|
2015-03-09 18:53:03 +03:00
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
2015-03-09 20:25:20 +03:00
|
|
|
events = [Mock(event_id="e1"), Mock(event_id="e2")]
|
2015-03-09 18:53:03 +03:00
|
|
|
txn_id = 5
|
2022-04-01 16:33:25 +03:00
|
|
|
self._set_state(self.as_list[0]["id"], ApplicationServiceState.UP)
|
2021-12-02 21:43:33 +03:00
|
|
|
self.get_success(self._insert_txn(service.id, txn_id, events))
|
|
|
|
self.get_success(
|
2020-08-28 00:24:46 +03:00
|
|
|
self.store.complete_appservice_txn(txn_id=txn_id, service=service)
|
|
|
|
)
|
2015-03-09 18:53:03 +03:00
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
res = self.get_success(
|
|
|
|
self.db_pool.runQuery(
|
|
|
|
self.engine.convert_param_style(
|
2022-05-17 13:07:18 +03:00
|
|
|
"SELECT state FROM application_services_state WHERE as_id=?"
|
2021-12-02 21:43:33 +03:00
|
|
|
),
|
|
|
|
(service.id,),
|
|
|
|
)
|
2015-03-09 18:53:03 +03:00
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(1, len(res))
|
2022-05-17 13:07:18 +03:00
|
|
|
self.assertEqual(ApplicationServiceState.UP.value, res[0][0])
|
2015-03-09 18:53:03 +03:00
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
res = self.get_success(
|
|
|
|
self.db_pool.runQuery(
|
|
|
|
self.engine.convert_param_style(
|
|
|
|
"SELECT * FROM application_services_txns WHERE txn_id=?"
|
|
|
|
),
|
|
|
|
(txn_id,),
|
|
|
|
)
|
2015-03-09 18:53:03 +03:00
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(0, len(res))
|
2015-03-09 18:53:03 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_get_oldest_unsent_txn_none(
|
|
|
|
self,
|
2021-12-02 21:43:33 +03:00
|
|
|
) -> None:
|
2015-03-09 18:53:03 +03:00
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
txn = self.get_success(self.store.get_oldest_unsent_txn(service))
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(None, txn)
|
2015-03-09 18:53:03 +03:00
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
def test_get_oldest_unsent_txn(self) -> None:
|
2015-03-09 18:53:03 +03:00
|
|
|
service = Mock(id=self.as_list[0]["id"])
|
2016-06-07 12:24:50 +03:00
|
|
|
events = [Mock(event_id="e1"), Mock(event_id="e2")]
|
2015-03-09 20:25:20 +03:00
|
|
|
other_events = [Mock(event_id="e5"), Mock(event_id="e6")]
|
2015-03-09 18:53:03 +03:00
|
|
|
|
2015-03-09 20:25:20 +03:00
|
|
|
# we aren't testing store._base stuff here, so mock this out
|
2021-12-02 18:30:05 +03:00
|
|
|
# (ignore needed because Mypy won't allow us to assign to a method otherwise)
|
2023-08-25 02:38:46 +03:00
|
|
|
self.store.get_events_as_list = AsyncMock(return_value=events) # type: ignore[assignment]
|
2015-03-09 20:25:20 +03:00
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
self.get_success(self._insert_txn(self.as_list[1]["id"], 9, other_events))
|
|
|
|
self.get_success(self._insert_txn(service.id, 10, events))
|
|
|
|
self.get_success(self._insert_txn(service.id, 11, other_events))
|
|
|
|
self.get_success(self._insert_txn(service.id, 12, other_events))
|
2015-03-09 18:53:03 +03:00
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
txn = self.get_success(self.store.get_oldest_unsent_txn(service))
|
2022-04-01 19:04:16 +03:00
|
|
|
assert txn is not None
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(service, txn.service)
|
|
|
|
self.assertEqual(10, txn.id)
|
|
|
|
self.assertEqual(events, txn.events)
|
2015-03-09 18:53:03 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_get_appservices_by_state_single(
|
|
|
|
self,
|
2021-12-02 21:43:33 +03:00
|
|
|
) -> None:
|
|
|
|
self.get_success(
|
|
|
|
self._set_state(self.as_list[0]["id"], ApplicationServiceState.DOWN)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self._set_state(self.as_list[1]["id"], ApplicationServiceState.UP)
|
|
|
|
)
|
2015-03-09 16:10:31 +03:00
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
services = self.get_success(
|
2020-08-12 16:28:48 +03:00
|
|
|
self.store.get_appservices_by_state(ApplicationServiceState.DOWN)
|
2015-03-09 16:10:31 +03:00
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(1, len(services))
|
|
|
|
self.assertEqual(self.as_list[0]["id"], services[0].id)
|
2015-03-09 16:10:31 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_get_appservices_by_state_multiple(
|
|
|
|
self,
|
2021-12-02 21:43:33 +03:00
|
|
|
) -> None:
|
|
|
|
self.get_success(
|
|
|
|
self._set_state(self.as_list[0]["id"], ApplicationServiceState.DOWN)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self._set_state(self.as_list[1]["id"], ApplicationServiceState.UP)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self._set_state(self.as_list[2]["id"], ApplicationServiceState.DOWN)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self._set_state(self.as_list[3]["id"], ApplicationServiceState.UP)
|
|
|
|
)
|
2015-03-09 16:10:31 +03:00
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
services = self.get_success(
|
2020-08-12 16:28:48 +03:00
|
|
|
self.store.get_appservices_by_state(ApplicationServiceState.DOWN)
|
2015-03-09 16:10:31 +03:00
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(2, len(services))
|
|
|
|
self.assertEqual(
|
2020-02-21 15:15:07 +03:00
|
|
|
{self.as_list[2]["id"], self.as_list[0]["id"]},
|
|
|
|
{services[0].id, services[1].id},
|
2015-03-31 14:07:56 +03:00
|
|
|
)
|
2015-03-09 16:10:31 +03:00
|
|
|
|
|
|
|
|
2020-10-26 17:51:33 +03:00
|
|
|
class ApplicationServiceStoreTypeStreamIds(unittest.HomeserverTestCase):
|
2021-12-02 18:30:05 +03:00
|
|
|
def prepare(
|
|
|
|
self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
|
|
|
|
) -> None:
|
2020-10-26 17:51:33 +03:00
|
|
|
self.service = Mock(id="foo")
|
2022-02-23 14:04:02 +03:00
|
|
|
self.store = self.hs.get_datastores().main
|
2021-12-02 18:30:05 +03:00
|
|
|
self.get_success(
|
|
|
|
self.store.set_appservice_state(self.service, ApplicationServiceState.UP)
|
|
|
|
)
|
2020-10-26 17:51:33 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_get_type_stream_id_for_appservice_no_value(self) -> None:
|
2020-10-26 17:51:33 +03:00
|
|
|
value = self.get_success(
|
|
|
|
self.store.get_type_stream_id_for_appservice(self.service, "read_receipt")
|
|
|
|
)
|
2022-03-30 14:10:12 +03:00
|
|
|
self.assertEqual(value, 1)
|
2020-10-26 17:51:33 +03:00
|
|
|
|
|
|
|
value = self.get_success(
|
|
|
|
self.store.get_type_stream_id_for_appservice(self.service, "presence")
|
|
|
|
)
|
2022-03-30 14:10:12 +03:00
|
|
|
self.assertEqual(value, 1)
|
2020-10-26 17:51:33 +03:00
|
|
|
|
2021-12-02 18:30:05 +03:00
|
|
|
def test_get_type_stream_id_for_appservice_invalid_type(self) -> None:
|
2020-10-26 17:51:33 +03:00
|
|
|
self.get_failure(
|
|
|
|
self.store.get_type_stream_id_for_appservice(self.service, "foobar"),
|
|
|
|
ValueError,
|
|
|
|
)
|
|
|
|
|
2022-02-01 17:13:38 +03:00
|
|
|
def test_set_appservice_stream_type_pos(self) -> None:
|
2020-10-26 17:51:33 +03:00
|
|
|
read_receipt_value = 1024
|
|
|
|
self.get_success(
|
2022-02-01 17:13:38 +03:00
|
|
|
self.store.set_appservice_stream_type_pos(
|
2020-10-26 17:51:33 +03:00
|
|
|
self.service, "read_receipt", read_receipt_value
|
|
|
|
)
|
|
|
|
)
|
|
|
|
result = self.get_success(
|
|
|
|
self.store.get_type_stream_id_for_appservice(self.service, "read_receipt")
|
|
|
|
)
|
|
|
|
self.assertEqual(result, read_receipt_value)
|
|
|
|
|
|
|
|
self.get_success(
|
2022-02-01 17:13:38 +03:00
|
|
|
self.store.set_appservice_stream_type_pos(
|
2020-10-26 17:51:33 +03:00
|
|
|
self.service, "presence", read_receipt_value
|
|
|
|
)
|
|
|
|
)
|
|
|
|
result = self.get_success(
|
|
|
|
self.store.get_type_stream_id_for_appservice(self.service, "presence")
|
|
|
|
)
|
|
|
|
self.assertEqual(result, read_receipt_value)
|
|
|
|
|
2022-02-01 17:13:38 +03:00
|
|
|
def test_set_appservice_stream_type_pos_invalid_type(self) -> None:
|
2020-10-26 17:51:33 +03:00
|
|
|
self.get_failure(
|
2022-02-01 17:13:38 +03:00
|
|
|
self.store.set_appservice_stream_type_pos(self.service, "foobar", 1024),
|
2020-10-26 17:51:33 +03:00
|
|
|
ValueError,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-03-09 16:10:31 +03:00
|
|
|
# required for ApplicationServiceTransactionStoreTestCase tests
|
|
|
|
class TestTransactionStore(ApplicationServiceTransactionStore, ApplicationServiceStore):
|
2022-12-09 20:36:32 +03:00
|
|
|
def __init__(
|
|
|
|
self, database: DatabasePool, db_conn: LoggingDatabaseConnection, hs: HomeServer
|
|
|
|
) -> None:
|
2020-09-18 16:56:44 +03:00
|
|
|
super().__init__(database, db_conn, hs)
|
2016-01-14 17:34:01 +03:00
|
|
|
|
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
class ApplicationServiceStoreConfigTestCase(unittest.HomeserverTestCase):
|
2022-12-09 20:36:32 +03:00
|
|
|
def _write_config(self, suffix: str, **kwargs: str) -> str:
|
2016-01-14 17:34:01 +03:00
|
|
|
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
|
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
def test_unique_works(self) -> None:
|
2016-01-14 17:34:01 +03:00
|
|
|
f1 = self._write_config(suffix="1")
|
|
|
|
f2 = self._write_config(suffix="2")
|
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
self.hs.config.appservice.app_service_config_files = [f1, f2]
|
|
|
|
self.hs.config.caches.event_cache_size = 1
|
2018-09-03 19:21:48 +03:00
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
database = self.hs.get_datastores().databases[0]
|
2019-12-18 13:45:12 +03:00
|
|
|
ApplicationServiceStore(
|
2021-12-02 21:43:33 +03:00
|
|
|
database,
|
|
|
|
make_conn(database._database_config, database.engine, "test"),
|
|
|
|
self.hs,
|
2019-12-18 13:45:12 +03:00
|
|
|
)
|
2016-01-14 17:34:01 +03:00
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
def test_duplicate_ids(self) -> None:
|
2016-01-14 17:34:01 +03:00
|
|
|
f1 = self._write_config(id="id", suffix="1")
|
|
|
|
f2 = self._write_config(id="id", suffix="2")
|
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
self.hs.config.appservice.app_service_config_files = [f1, f2]
|
|
|
|
self.hs.config.caches.event_cache_size = 1
|
2018-09-03 19:21:48 +03:00
|
|
|
|
2016-01-14 17:34:01 +03:00
|
|
|
with self.assertRaises(ConfigError) as cm:
|
2021-12-02 21:43:33 +03:00
|
|
|
database = self.hs.get_datastores().databases[0]
|
2019-12-18 13:45:12 +03:00
|
|
|
ApplicationServiceStore(
|
2020-10-02 17:20:45 +03:00
|
|
|
database,
|
|
|
|
make_conn(database._database_config, database.engine, "test"),
|
2021-12-02 21:43:33 +03:00
|
|
|
self.hs,
|
2019-12-18 13:45:12 +03:00
|
|
|
)
|
2016-01-14 17:34:01 +03:00
|
|
|
|
|
|
|
e = cm.exception
|
2018-04-15 18:20:37 +03:00
|
|
|
self.assertIn(f1, str(e))
|
|
|
|
self.assertIn(f2, str(e))
|
|
|
|
self.assertIn("id", str(e))
|
2016-01-14 17:34:01 +03:00
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
def test_duplicate_as_tokens(self) -> None:
|
2016-01-14 17:34:01 +03:00
|
|
|
f1 = self._write_config(as_token="as_token", suffix="1")
|
|
|
|
f2 = self._write_config(as_token="as_token", suffix="2")
|
|
|
|
|
2021-12-02 21:43:33 +03:00
|
|
|
self.hs.config.appservice.app_service_config_files = [f1, f2]
|
|
|
|
self.hs.config.caches.event_cache_size = 1
|
2018-09-03 19:21:48 +03:00
|
|
|
|
2016-01-14 17:34:01 +03:00
|
|
|
with self.assertRaises(ConfigError) as cm:
|
2021-12-02 21:43:33 +03:00
|
|
|
database = self.hs.get_datastores().databases[0]
|
2019-12-18 13:45:12 +03:00
|
|
|
ApplicationServiceStore(
|
2020-10-02 17:20:45 +03:00
|
|
|
database,
|
|
|
|
make_conn(database._database_config, database.engine, "test"),
|
2021-12-02 21:43:33 +03:00
|
|
|
self.hs,
|
2019-12-18 13:45:12 +03:00
|
|
|
)
|
2016-01-14 17:34:01 +03:00
|
|
|
|
|
|
|
e = cm.exception
|
2018-04-15 18:20:37 +03:00
|
|
|
self.assertIn(f1, str(e))
|
|
|
|
self.assertIn(f2, str(e))
|
|
|
|
self.assertIn("as_token", str(e))
|