From b587f1f2915f3ef52c18e552ca36d6fb2d78c8bd Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 6 Apr 2022 11:51:34 -0400 Subject: [PATCH 1/3] Avoid printing errors in tests. --- tests/test_casefold_migration.py | 12 +----------- tests/test_email.py | 9 +-------- tests/test_jinja_templates.py | 9 +-------- tests/test_replication.py | 7 +------ tests/test_store_invite.py | 5 ----- tests/utils.py | 28 +++++++++++++++++++++------- 6 files changed, 25 insertions(+), 45 deletions(-) diff --git a/tests/test_casefold_migration.py b/tests/test_casefold_migration.py index 59e28cd7..6ee6b3a2 100644 --- a/tests/test_casefold_migration.py +++ b/tests/test_casefold_migration.py @@ -27,17 +27,7 @@ def create_signedassoc(self, medium, address, mxid, ts, not_before, not_after): 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 = [] diff --git a/tests/test_email.py b/tests/test_email.py index b7f48c1f..b3a7f7ad 100644 --- a/tests/test_email.py +++ b/tests/test_email.py @@ -22,14 +22,7 @@ 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. diff --git a/tests/test_jinja_templates.py b/tests/test_jinja_templates.py index 030e6e90..6e0b37cb 100644 --- a/tests/test_jinja_templates.py +++ b/tests/test_jinja_templates.py @@ -25,14 +25,7 @@ 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 = { diff --git a/tests/test_replication.py b/tests/test_replication.py index 46db25d0..e119d224 100644 --- a/tests/test_replication.py +++ b/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" diff --git a/tests/test_store_invite.py b/tests/test_store_invite.py index 5f19fdb0..94b6847d 100644 --- a/tests/test_store_invite.py +++ b/tests/test_store_invite.py @@ -27,11 +27,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 ", }, diff --git a/tests/utils.py b/tests/utils.py index 902b3bef..80be5c16 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -2,7 +2,7 @@ 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 @@ """ -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() From 1ef131b56432eb76cb869dcd4223b211444ea9dd Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 6 Apr 2022 11:52:40 -0400 Subject: [PATCH 2/3] Newsfragment --- changelog.d/516.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/516.misc diff --git a/changelog.d/516.misc b/changelog.d/516.misc new file mode 100644 index 00000000..6d2aaf21 --- /dev/null +++ b/changelog.d/516.misc @@ -0,0 +1 @@ +Avoid spurious warnings in tests. From ebf8c5388e628b99b76910394cd4d23142558ec5 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 6 Apr 2022 11:55:24 -0400 Subject: [PATCH 3/3] Lint --- tests/test_casefold_migration.py | 14 +++++++++++++- tests/test_email.py | 1 - tests/test_store_invite.py | 1 - 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/test_casefold_migration.py b/tests/test_casefold_migration.py index 6ee6b3a2..e24b0b20 100644 --- a/tests/test_casefold_migration.py +++ b/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 diff --git a/tests/test_email.py b/tests/test_email.py index b3a7f7ad..f8d1c908 100644 --- a/tests/test_email.py +++ b/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 diff --git a/tests/test_store_invite.py b/tests/test_store_invite.py index 94b6847d..9f5cb6ff 100644 --- a/tests/test_store_invite.py +++ b/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