summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorreivilibre <oliverw@matrix.org>2021-12-02 15:30:05 +0000
committerGitHub <noreply@github.com>2021-12-02 15:30:05 +0000
commit435f04480728c5d982e1a63c1b2777784bf9cd26 (patch)
tree1739f511566d586e12cfacb521eab9dd6e0506bd /tests
parent`scripts-dev/sign_json`: support for signing events (#11486) (diff)
downloadsynapse-435f04480728c5d982e1a63c1b2777784bf9cd26.tar.xz
Add type annotations to `tests.storage.test_appservice`. (#11488)
Diffstat (limited to 'tests')
-rw-r--r--tests/storage/test_appservice.py140
1 files changed, 92 insertions, 48 deletions
diff --git a/tests/storage/test_appservice.py b/tests/storage/test_appservice.py
index f26d5acf9c..4b20a28ca2 100644
--- a/tests/storage/test_appservice.py
+++ b/tests/storage/test_appservice.py
@@ -14,19 +14,25 @@
 import json
 import os
 import tempfile
+from typing import Any, Generator, List, Optional, cast
 from unittest.mock import Mock
 
 import yaml
 
 from twisted.internet import defer
+from twisted.internet.defer import Deferred
+from twisted.test.proto_helpers import MemoryReactor
 
 from synapse.appservice import ApplicationService, ApplicationServiceState
 from synapse.config._base import ConfigError
+from synapse.events import EventBase
+from synapse.server import HomeServer
 from synapse.storage.database import DatabasePool, make_conn
 from synapse.storage.databases.main.appservice import (
     ApplicationServiceStore,
     ApplicationServiceTransactionStore,
 )
+from synapse.util import Clock
 
 from tests import unittest
 from tests.test_utils import make_awaitable
@@ -36,7 +42,7 @@ from tests.utils import setup_test_homeserver
 class ApplicationServiceStoreTestCase(unittest.TestCase):
     @defer.inlineCallbacks
     def setUp(self):
-        self.as_yaml_files = []
+        self.as_yaml_files: List[str] = []
         hs = yield setup_test_homeserver(
             self.addCleanup, federation_sender=Mock(), federation_client=Mock()
         )
@@ -58,7 +64,7 @@ class ApplicationServiceStoreTestCase(unittest.TestCase):
             database, make_conn(database._database_config, database.engine, "test"), hs
         )
 
-    def tearDown(self):
+    def tearDown(self) -> None:
         # TODO: suboptimal that we need to create files for tests!
         for f in self.as_yaml_files:
             try:
@@ -66,7 +72,7 @@ class ApplicationServiceStoreTestCase(unittest.TestCase):
             except Exception:
                 pass
 
-    def _add_appservice(self, as_token, id, url, hs_token, sender):
+    def _add_appservice(self, as_token, id, url, hs_token, sender) -> None:
         as_yaml = {
             "url": url,
             "as_token": as_token,
@@ -80,12 +86,13 @@ class ApplicationServiceStoreTestCase(unittest.TestCase):
             outfile.write(yaml.dump(as_yaml))
             self.as_yaml_files.append(as_token)
 
-    def test_retrieve_unknown_service_token(self):
+    def test_retrieve_unknown_service_token(self) -> None:
         service = self.store.get_app_service_by_token("invalid_token")
         self.assertEquals(service, None)
 
-    def test_retrieval_of_service(self):
+    def test_retrieval_of_service(self) -> None:
         stored_service = self.store.get_app_service_by_token(self.as_token)
+        assert stored_service is not None
         self.assertEquals(stored_service.token, self.as_token)
         self.assertEquals(stored_service.id, self.as_id)
         self.assertEquals(stored_service.url, self.as_url)
@@ -93,7 +100,7 @@ class ApplicationServiceStoreTestCase(unittest.TestCase):
         self.assertEquals(stored_service.namespaces[ApplicationService.NS_ROOMS], [])
         self.assertEquals(stored_service.namespaces[ApplicationService.NS_USERS], [])
 
-    def test_retrieval_of_all_services(self):
+    def test_retrieval_of_all_services(self) -> None:
         services = self.store.get_app_services()
         self.assertEquals(len(services), 3)
 
@@ -101,7 +108,7 @@ class ApplicationServiceStoreTestCase(unittest.TestCase):
 class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
     @defer.inlineCallbacks
     def setUp(self):
-        self.as_yaml_files = []
+        self.as_yaml_files: List[str] = []
 
         hs = yield setup_test_homeserver(
             self.addCleanup, federation_sender=Mock(), federation_client=Mock()
@@ -117,7 +124,7 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
             {"token": "gamma_tok", "url": "https://gamma.com", "id": "id_gamma"},
         ]
         for s in self.as_list:
-            yield self._add_service(s["url"], s["token"], s["id"])
+            self._add_service(s["url"], s["token"], s["id"])
 
         self.as_yaml_files = []
 
@@ -131,7 +138,7 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
             database, make_conn(db_config, self.engine, "test"), hs
         )
 
-    def _add_service(self, url, as_token, id):
+    def _add_service(self, url, as_token, id) -> None:
         as_yaml = {
             "url": url,
             "as_token": as_token,
@@ -145,13 +152,15 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
             outfile.write(yaml.dump(as_yaml))
             self.as_yaml_files.append(as_token)
 
-    def _set_state(self, id, state, txn=None):
+    def _set_state(
+        self, id: str, state: ApplicationServiceState, txn: Optional[int] = None
+    ):
         return self.db_pool.runOperation(
             self.engine.convert_param_style(
                 "INSERT INTO application_services_state(as_id, state, last_txn) "
                 "VALUES(?,?,?)"
             ),
-            (id, state, txn),
+            (id, state.value, txn),
         )
 
     def _insert_txn(self, as_id, txn_id, events):
@@ -169,24 +178,30 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
                 "INSERT INTO application_services_state(as_id, last_txn, state) "
                 "VALUES(?,?,?)"
             ),
-            (as_id, txn_id, ApplicationServiceState.UP),
+            (as_id, txn_id, ApplicationServiceState.UP.value),
         )
 
     @defer.inlineCallbacks
-    def test_get_appservice_state_none(self):
+    def test_get_appservice_state_none(
+        self,
+    ) -> Generator["Deferred[object]", object, None]:
         service = Mock(id="999")
         state = yield defer.ensureDeferred(self.store.get_appservice_state(service))
         self.assertEquals(None, state)
 
     @defer.inlineCallbacks
-    def test_get_appservice_state_up(self):
+    def test_get_appservice_state_up(
+        self,
+    ) -> Generator["Deferred[object]", object, None]:
         yield self._set_state(self.as_list[0]["id"], ApplicationServiceState.UP)
         service = Mock(id=self.as_list[0]["id"])
         state = yield defer.ensureDeferred(self.store.get_appservice_state(service))
         self.assertEquals(ApplicationServiceState.UP, state)
 
     @defer.inlineCallbacks
-    def test_get_appservice_state_down(self):
+    def test_get_appservice_state_down(
+        self,
+    ) -> Generator["Deferred[object]", object, None]:
         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)
@@ -195,14 +210,18 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
         self.assertEquals(ApplicationServiceState.DOWN, state)
 
     @defer.inlineCallbacks
-    def test_get_appservices_by_state_none(self):
+    def test_get_appservices_by_state_none(
+        self,
+    ) -> Generator["Deferred[object]", Any, None]:
         services = yield defer.ensureDeferred(
             self.store.get_appservices_by_state(ApplicationServiceState.DOWN)
         )
         self.assertEquals(0, len(services))
 
     @defer.inlineCallbacks
-    def test_set_appservices_state_down(self):
+    def test_set_appservices_state_down(
+        self,
+    ) -> Generator["Deferred[object]", Any, None]:
         service = Mock(id=self.as_list[1]["id"])
         yield defer.ensureDeferred(
             self.store.set_appservice_state(service, ApplicationServiceState.DOWN)
@@ -211,12 +230,14 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
             self.engine.convert_param_style(
                 "SELECT as_id FROM application_services_state WHERE state=?"
             ),
-            (ApplicationServiceState.DOWN,),
+            (ApplicationServiceState.DOWN.value,),
         )
         self.assertEquals(service.id, rows[0][0])
 
     @defer.inlineCallbacks
-    def test_set_appservices_state_multiple_up(self):
+    def test_set_appservices_state_multiple_up(
+        self,
+    ) -> Generator["Deferred[object]", Any, None]:
         service = Mock(id=self.as_list[1]["id"])
         yield defer.ensureDeferred(
             self.store.set_appservice_state(service, ApplicationServiceState.UP)
@@ -231,14 +252,16 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
             self.engine.convert_param_style(
                 "SELECT as_id FROM application_services_state WHERE state=?"
             ),
-            (ApplicationServiceState.UP,),
+            (ApplicationServiceState.UP.value,),
         )
         self.assertEquals(service.id, rows[0][0])
 
     @defer.inlineCallbacks
-    def test_create_appservice_txn_first(self):
+    def test_create_appservice_txn_first(
+        self,
+    ) -> Generator["Deferred[object]", Any, None]:
         service = Mock(id=self.as_list[0]["id"])
-        events = [Mock(event_id="e1"), Mock(event_id="e2")]
+        events = cast(List[EventBase], [Mock(event_id="e1"), Mock(event_id="e2")])
         txn = yield defer.ensureDeferred(
             self.store.create_appservice_txn(service, events, [])
         )
@@ -247,9 +270,11 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
         self.assertEquals(txn.service, service)
 
     @defer.inlineCallbacks
-    def test_create_appservice_txn_older_last_txn(self):
+    def test_create_appservice_txn_older_last_txn(
+        self,
+    ) -> Generator["Deferred[object]", Any, None]:
         service = Mock(id=self.as_list[0]["id"])
-        events = [Mock(event_id="e1"), Mock(event_id="e2")]
+        events = cast(List[EventBase], [Mock(event_id="e1"), Mock(event_id="e2")])
         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)
@@ -261,9 +286,11 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
         self.assertEquals(txn.service, service)
 
     @defer.inlineCallbacks
-    def test_create_appservice_txn_up_to_date_last_txn(self):
+    def test_create_appservice_txn_up_to_date_last_txn(
+        self,
+    ) -> Generator["Deferred[object]", Any, None]:
         service = Mock(id=self.as_list[0]["id"])
-        events = [Mock(event_id="e1"), Mock(event_id="e2")]
+        events = cast(List[EventBase], [Mock(event_id="e1"), Mock(event_id="e2")])
         yield self._set_last_txn(service.id, 9643)
         txn = yield defer.ensureDeferred(
             self.store.create_appservice_txn(service, events, [])
@@ -273,9 +300,11 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
         self.assertEquals(txn.service, service)
 
     @defer.inlineCallbacks
-    def test_create_appservice_txn_up_fuzzing(self):
+    def test_create_appservice_txn_up_fuzzing(
+        self,
+    ) -> Generator["Deferred[object]", Any, None]:
         service = Mock(id=self.as_list[0]["id"])
-        events = [Mock(event_id="e1"), Mock(event_id="e2")]
+        events = cast(List[EventBase], [Mock(event_id="e1"), Mock(event_id="e2")])
         yield self._set_last_txn(service.id, 9643)
 
         # dump in rows with higher IDs to make sure the queries aren't wrong.
@@ -296,7 +325,9 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
         self.assertEquals(txn.service, service)
 
     @defer.inlineCallbacks
-    def test_complete_appservice_txn_first_txn(self):
+    def test_complete_appservice_txn_first_txn(
+        self,
+    ) -> Generator["Deferred[object]", Any, None]:
         service = Mock(id=self.as_list[0]["id"])
         events = [Mock(event_id="e1"), Mock(event_id="e2")]
         txn_id = 1
@@ -324,7 +355,9 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
         self.assertEquals(0, len(res))
 
     @defer.inlineCallbacks
-    def test_complete_appservice_txn_existing_in_state_table(self):
+    def test_complete_appservice_txn_existing_in_state_table(
+        self,
+    ) -> Generator["Deferred[object]", Any, None]:
         service = Mock(id=self.as_list[0]["id"])
         events = [Mock(event_id="e1"), Mock(event_id="e2")]
         txn_id = 5
@@ -342,7 +375,7 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
         )
         self.assertEquals(1, len(res))
         self.assertEquals(txn_id, res[0][0])
-        self.assertEquals(ApplicationServiceState.UP, res[0][1])
+        self.assertEquals(ApplicationServiceState.UP.value, res[0][1])
 
         res = yield self.db_pool.runQuery(
             self.engine.convert_param_style(
@@ -353,20 +386,23 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
         self.assertEquals(0, len(res))
 
     @defer.inlineCallbacks
-    def test_get_oldest_unsent_txn_none(self):
+    def test_get_oldest_unsent_txn_none(
+        self,
+    ) -> Generator["Deferred[object]", Any, None]:
         service = Mock(id=self.as_list[0]["id"])
 
         txn = yield defer.ensureDeferred(self.store.get_oldest_unsent_txn(service))
         self.assertEquals(None, txn)
 
     @defer.inlineCallbacks
-    def test_get_oldest_unsent_txn(self):
+    def test_get_oldest_unsent_txn(self) -> Generator["Deferred[object]", Any, None]:
         service = Mock(id=self.as_list[0]["id"])
         events = [Mock(event_id="e1"), Mock(event_id="e2")]
         other_events = [Mock(event_id="e5"), Mock(event_id="e6")]
 
         # we aren't testing store._base stuff here, so mock this out
-        self.store.get_events_as_list = Mock(return_value=make_awaitable(events))
+        # (ignore needed because Mypy won't allow us to assign to a method otherwise)
+        self.store.get_events_as_list = Mock(return_value=make_awaitable(events))  # type: ignore[assignment]
 
         yield self._insert_txn(self.as_list[1]["id"], 9, other_events)
         yield self._insert_txn(service.id, 10, events)
@@ -379,7 +415,9 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
         self.assertEquals(events, txn.events)
 
     @defer.inlineCallbacks
-    def test_get_appservices_by_state_single(self):
+    def test_get_appservices_by_state_single(
+        self,
+    ) -> Generator["Deferred[object]", Any, None]:
         yield self._set_state(self.as_list[0]["id"], ApplicationServiceState.DOWN)
         yield self._set_state(self.as_list[1]["id"], ApplicationServiceState.UP)
 
@@ -390,7 +428,9 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
         self.assertEquals(self.as_list[0]["id"], services[0].id)
 
     @defer.inlineCallbacks
-    def test_get_appservices_by_state_multiple(self):
+    def test_get_appservices_by_state_multiple(
+        self,
+    ) -> Generator["Deferred[object]", Any, None]:
         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)
@@ -407,16 +447,20 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
 
 
 class ApplicationServiceStoreTypeStreamIds(unittest.HomeserverTestCase):
-    def make_homeserver(self, reactor, clock):
+    def make_homeserver(self, reactor, clock) -> "HomeServer":
         hs = self.setup_test_homeserver()
         return hs
 
-    def prepare(self, hs, reactor, clock):
+    def prepare(
+        self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
+    ) -> None:
         self.service = Mock(id="foo")
         self.store = self.hs.get_datastore()
-        self.get_success(self.store.set_appservice_state(self.service, "up"))
+        self.get_success(
+            self.store.set_appservice_state(self.service, ApplicationServiceState.UP)
+        )
 
-    def test_get_type_stream_id_for_appservice_no_value(self):
+    def test_get_type_stream_id_for_appservice_no_value(self) -> None:
         value = self.get_success(
             self.store.get_type_stream_id_for_appservice(self.service, "read_receipt")
         )
@@ -427,13 +471,13 @@ class ApplicationServiceStoreTypeStreamIds(unittest.HomeserverTestCase):
         )
         self.assertEquals(value, 0)
 
-    def test_get_type_stream_id_for_appservice_invalid_type(self):
+    def test_get_type_stream_id_for_appservice_invalid_type(self) -> None:
         self.get_failure(
             self.store.get_type_stream_id_for_appservice(self.service, "foobar"),
             ValueError,
         )
 
-    def test_set_type_stream_id_for_appservice(self):
+    def test_set_type_stream_id_for_appservice(self) -> None:
         read_receipt_value = 1024
         self.get_success(
             self.store.set_type_stream_id_for_appservice(
@@ -455,7 +499,7 @@ class ApplicationServiceStoreTypeStreamIds(unittest.HomeserverTestCase):
         )
         self.assertEqual(result, read_receipt_value)
 
-    def test_set_type_stream_id_for_appservice_invalid_type(self):
+    def test_set_type_stream_id_for_appservice_invalid_type(self) -> None:
         self.get_failure(
             self.store.set_type_stream_id_for_appservice(self.service, "foobar", 1024),
             ValueError,
@@ -464,12 +508,12 @@ class ApplicationServiceStoreTypeStreamIds(unittest.HomeserverTestCase):
 
 # required for ApplicationServiceTransactionStoreTestCase tests
 class TestTransactionStore(ApplicationServiceTransactionStore, ApplicationServiceStore):
-    def __init__(self, database: DatabasePool, db_conn, hs):
+    def __init__(self, database: DatabasePool, db_conn, hs) -> None:
         super().__init__(database, db_conn, hs)
 
 
 class ApplicationServiceStoreConfigTestCase(unittest.TestCase):
-    def _write_config(self, suffix, **kwargs):
+    def _write_config(self, suffix, **kwargs) -> str:
         vals = {
             "id": "id" + suffix,
             "url": "url" + suffix,
@@ -486,7 +530,7 @@ class ApplicationServiceStoreConfigTestCase(unittest.TestCase):
         return path
 
     @defer.inlineCallbacks
-    def test_unique_works(self):
+    def test_unique_works(self) -> Generator["Deferred[object]", Any, None]:
         f1 = self._write_config(suffix="1")
         f2 = self._write_config(suffix="2")
 
@@ -503,7 +547,7 @@ class ApplicationServiceStoreConfigTestCase(unittest.TestCase):
         )
 
     @defer.inlineCallbacks
-    def test_duplicate_ids(self):
+    def test_duplicate_ids(self) -> Generator["Deferred[object]", Any, None]:
         f1 = self._write_config(id="id", suffix="1")
         f2 = self._write_config(id="id", suffix="2")
 
@@ -528,7 +572,7 @@ class ApplicationServiceStoreConfigTestCase(unittest.TestCase):
         self.assertIn("id", str(e))
 
     @defer.inlineCallbacks
-    def test_duplicate_as_tokens(self):
+    def test_duplicate_as_tokens(self) -> Generator["Deferred[object]", Any, None]:
         f1 = self._write_config(as_token="as_token", suffix="1")
         f2 = self._write_config(as_token="as_token", suffix="2")