Browse Source

Use a more realistic configuration in tests. (#516)

To clean-up warnings when running the tests.
Patrick Cloke 2 years ago
parent
commit
a0aeb8470d

+ 1 - 0
changelog.d/516.misc

@@ -0,0 +1 @@
+Avoid spurious warnings in tests.

+ 14 - 12
tests/test_casefold_migration.py

@@ -1,5 +1,17 @@
+# Copyright 2021 Matrix.org Foundation C.I.C.
+#
+# 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.
 import json
-import os.path
 from unittest.mock import patch
 
 from twisted.trial import unittest
@@ -27,17 +39,7 @@ class MigrationTestCase(unittest.TestCase):
 
     def setUp(self):
         # Create a new sydent
-        config = {
-            "general": {
-                "templates.path": os.path.join(
-                    os.path.dirname(os.path.dirname(__file__)), "res"
-                ),
-            },
-            "crypto": {
-                "ed25519.signingkey": "ed25519 0 FJi1Rnpj3/otydngacrwddFvwz/dTDsBv62uZDN2fZM"
-            },
-        }
-        self.sydent = make_sydent(test_config=config)
+        self.sydent = make_sydent()
 
         # create some local associations
         associations = []

+ 1 - 9
tests/test_email.py

@@ -11,7 +11,6 @@
 #  See the License for the specific language governing permissions and
 #  limitations under the License.
 
-import os.path
 from unittest.mock import patch
 
 from twisted.trial import unittest
@@ -22,14 +21,7 @@ from tests.utils import make_request, make_sydent
 class TestRequestCode(unittest.TestCase):
     def setUp(self):
         # Create a new sydent
-        config = {
-            "general": {
-                "templates.path": os.path.join(
-                    os.path.dirname(os.path.dirname(__file__)), "res"
-                ),
-            },
-        }
-        self.sydent = make_sydent(test_config=config)
+        self.sydent = make_sydent()
 
     def _render_request(self, request):
         # Patch out the email sending so we can investigate the resulting email.

+ 1 - 8
tests/test_jinja_templates.py

@@ -25,14 +25,7 @@ from tests.utils import make_sydent
 class TestTemplate(unittest.TestCase):
     def setUp(self):
         # Create a new sydent
-        config = {
-            "general": {
-                "templates.path": os.path.join(
-                    os.path.dirname(os.path.dirname(__file__)), "res"
-                ),
-            },
-        }
-        self.sydent = make_sydent(test_config=config)
+        self.sydent = make_sydent()
 
     def test_jinja_vector_invite(self):
         substitutions = {

+ 1 - 6
tests/test_replication.py

@@ -15,12 +15,7 @@ class ReplicationTestCase(unittest.TestCase):
 
     def setUp(self):
         # Create a new sydent
-        config = {
-            "crypto": {
-                "ed25519.signingkey": "ed25519 0 FJi1Rnpj3/otydngacrwddFvwz/dTDsBv62uZDN2fZM"
-            }
-        }
-        self.sydent = make_sydent(test_config=config)
+        self.sydent = make_sydent()
 
         # Create a fake peer to replicate to.
         peer_public_key_base64 = "+vB8mTaooD/MA8YYZM8t9+vnGhP1937q2icrqPV9JTs"

+ 0 - 6
tests/test_store_invite.py

@@ -11,7 +11,6 @@
 # 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.
-import os.path
 from unittest.mock import patch
 
 from parameterized import parameterized
@@ -27,11 +26,6 @@ class StoreInviteTestCase(unittest.TestCase):
     def setUp(self) -> None:
         # Create a new sydent
         config = {
-            "general": {
-                "templates.path": os.path.join(
-                    os.path.dirname(os.path.dirname(__file__)), "res"
-                ),
-            },
             "email": {
                 "email.from": "Sydent Validation <noreply@hostname>",
             },

+ 21 - 7
tests/utils.py

@@ -2,7 +2,7 @@ import json
 import logging
 import os
 from io import BytesIO
-from typing import Dict
+from typing import Dict, Optional
 from unittest.mock import MagicMock
 
 import attr
@@ -53,20 +53,34 @@ tWVEpHfT+G7AjA8=
 """
 
 
-def make_sydent(test_config={}):
+def make_sydent(test_config: Optional[dict] = None) -> Sydent:
     """Create a new sydent
 
     Args:
-        test_config (dict): any configuration variables for overriding the default sydent
+        test_config: Configuration variables for overriding the default sydent
             config
     """
+    if test_config is None:
+        test_config = {}
+
     # Use an in-memory SQLite database. Note that the database isn't cleaned up between
     # tests, so by default the same database will be used for each test if changed to be
     # a file on disk.
-    if "db" not in test_config:
-        test_config["db"] = {"db.file": ":memory:"}
-    else:
-        test_config["db"].setdefault("db.file", ":memory:")
+    test_config.setdefault("db", {}).setdefault("db.file", ":memory:")
+
+    # Specify a server name to avoid warnings.
+    general_config = test_config.setdefault("general", {})
+    general_config.setdefault("server.name", ":test:")
+    # Specify the default templates.
+    general_config.setdefault(
+        "templates.path",
+        os.path.join(os.path.dirname(os.path.dirname(__file__)), "res"),
+    )
+
+    # Specify a signing key.
+    test_config.setdefault("crypto", {}).setdefault(
+        "ed25519.signingkey", "ed25519 0 FJi1Rnpj3/otydngacrwddFvwz/dTDsBv62uZDN2fZM"
+    )
 
     reactor = ResolvingMemoryReactorClock()