Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Migrate all tests to use the dict-based config format instead of hanging items off HomeserverConfig #5171

Merged
merged 6 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/5171.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update tests to consistently be configured via the same code that is used when loading from configuration files.
1 change: 1 addition & 0 deletions synapse/rest/media/v1/storage_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class FileStorageProviderBackend(StorageProvider):
"""

def __init__(self, hs, config):
self.hs = hs
self.cache_directory = hs.config.media_store_path
self.base_directory = config

Expand Down
8 changes: 6 additions & 2 deletions tests/handlers/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ def make_homeserver(self, reactor, clock):
hs_config = self.default_config("test")

# some of the tests rely on us having a user consent version
hs_config.user_consent_version = "test_consent_version"
hs_config.max_mau_value = 50
hs_config["user_consent"] = {
"version": "test_consent_version",
"template_dir": ".",
}
hs_config["max_mau_value"] = 50
hs_config["limit_usage_by_mau"] = True

hs = self.setup_test_homeserver(config=hs_config, expire_access_token=True)
return hs
Expand Down
4 changes: 2 additions & 2 deletions tests/handlers/test_user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class UserDirectoryTestCase(unittest.HomeserverTestCase):
def make_homeserver(self, reactor, clock):

config = self.default_config()
config.update_user_directory = True
config["update_user_directory"] = True
return self.setup_test_homeserver(config=config)

def prepare(self, reactor, clock, hs):
Expand Down Expand Up @@ -333,7 +333,7 @@ class TestUserDirSearchDisabled(unittest.HomeserverTestCase):

def make_homeserver(self, reactor, clock):
config = self.default_config()
config.update_user_directory = True
config["update_user_directory"] = True
hs = self.setup_test_homeserver(config=config)

self.config = hs.config
Expand Down
4 changes: 3 additions & 1 deletion tests/http/federation/test_matrix_federation_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ def setUp(self):

self.agent = MatrixFederationAgent(
reactor=self.reactor,
tls_client_options_factory=ClientTLSOptionsFactory(default_config("test")),
tls_client_options_factory=ClientTLSOptionsFactory(
default_config("test", parse=True)
),
_well_known_tls_policy=TrustingTLSPolicyForHTTPS(),
_srv_resolver=self.mock_resolver,
_well_known_cache=self.well_known_cache,
Expand Down
36 changes: 20 additions & 16 deletions tests/push/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,26 @@ def sendmail(*args, **kwargs):
return d

config = self.default_config()
config.email_enable_notifs = True
config.start_pushers = True

config.email_template_dir = os.path.abspath(
pkg_resources.resource_filename('synapse', 'res/templates')
)
config.email_notif_template_html = "notif_mail.html"
config.email_notif_template_text = "notif_mail.txt"
config.email_smtp_host = "127.0.0.1"
config.email_smtp_port = 20
config.require_transport_security = False
config.email_smtp_user = None
config.email_smtp_pass = None
config.email_app_name = "Matrix"
config.email_notif_from = "test@example.com"
config.email_riot_base_url = None
config["email"] = {
"enable_notifs": True,
"template_dir": os.path.abspath(
pkg_resources.resource_filename('synapse', 'res/templates')
),
"expiry_template_html": "notice_expiry.html",
"expiry_template_text": "notice_expiry.txt",
"notif_template_html": "notif_mail.html",
"notif_template_text": "notif_mail.txt",
"smtp_host": "127.0.0.1",
"smtp_port": 20,
"require_transport_security": False,
"smtp_user": None,
"smtp_pass": None,
"app_name": "Matrix",
"notif_from": "test@example.com",
"riot_base_url": None,
}
config["public_baseurl"] = "aaa"
config["start_pushers"] = True

hs = self.setup_test_homeserver(config=config, sendmail=sendmail)

Expand Down
2 changes: 1 addition & 1 deletion tests/push/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def post_json_get_json(url, body):
m.post_json_get_json = post_json_get_json

config = self.default_config()
config.start_pushers = True
config["start_pushers"] = True

hs = self.setup_test_homeserver(config=config, simple_http_client=m)

Expand Down
11 changes: 7 additions & 4 deletions tests/rest/client/test_consent.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ class ConsentResourceTestCase(unittest.HomeserverTestCase):
def make_homeserver(self, reactor, clock):

config = self.default_config()
config.user_consent_version = "1"
config.public_baseurl = ""
config.form_secret = "123abc"
config["public_baseurl"] = "aaaa"
config["form_secret"] = "123abc"

# Make some temporary templates...
temp_consent_path = self.mktemp()
os.mkdir(temp_consent_path)
os.mkdir(os.path.join(temp_consent_path, 'en'))
config.user_consent_template_dir = os.path.abspath(temp_consent_path)

config["user_consent"] = {
"version": "1",
"template_dir": os.path.abspath(temp_consent_path),
}

with open(os.path.join(temp_consent_path, "en/1.html"), 'w') as f:
f.write("{{version}},{{has_consented}}")
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/client/test_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class IdentityTestCase(unittest.HomeserverTestCase):
def make_homeserver(self, reactor, clock):

config = self.default_config()
config.enable_3pid_lookup = False
config["enable_3pid_lookup"] = False
self.hs = self.setup_test_homeserver(config=config)

return self.hs
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/client/v1/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DirectoryTestCase(unittest.HomeserverTestCase):

def make_homeserver(self, reactor, clock):
config = self.default_config()
config.require_membership_for_aliases = True
config["require_membership_for_aliases"] = True

self.hs = self.setup_test_homeserver(config=config)

Expand Down
6 changes: 3 additions & 3 deletions tests/rest/client/v1/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class EventStreamPermissionsTestCase(unittest.HomeserverTestCase):
def make_homeserver(self, reactor, clock):

config = self.default_config()
config.enable_registration_captcha = False
config.enable_registration = True
config.auto_join_rooms = []
config["enable_registration_captcha"] = False
config["enable_registration"] = True
config["auto_join_rooms"] = []

hs = self.setup_test_homeserver(
config=config, ratelimiter=NonCallableMock(spec_set=["can_do_action"])
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/client/v1/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class ProfilesRestrictedTestCase(unittest.HomeserverTestCase):
def make_homeserver(self, reactor, clock):

config = self.default_config()
config.require_auth_for_profile_requests = True
config["require_auth_for_profile_requests"] = True
self.hs = self.setup_test_homeserver(config=config)

return self.hs
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/client/v1/test_rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ def make_homeserver(self, reactor, clock):
self.url = b"/_matrix/client/r0/publicRooms"

config = self.default_config()
config.restrict_public_rooms_to_local_users = True
config["restrict_public_rooms_to_local_users"] = True
self.hs = self.setup_test_homeserver(config=config)

return self.hs
Expand Down
6 changes: 3 additions & 3 deletions tests/rest/client/v2_alpha/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def make_homeserver(self, reactor, clock):

config = self.default_config()

config.enable_registration_captcha = True
config.recaptcha_public_key = "brokencake"
config.registrations_require_3pid = []
config["enable_registration_captcha"] = True
config["recaptcha_public_key"] = "brokencake"
config["registrations_require_3pid"] = []

hs = self.setup_test_homeserver(config=config)
return hs
Expand Down
51 changes: 31 additions & 20 deletions tests/rest/client/v2_alpha/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,11 @@ class AccountValidityTestCase(unittest.HomeserverTestCase):
def make_homeserver(self, reactor, clock):
config = self.default_config()
# Test for account expiring after a week.
config.enable_registration = True
config.account_validity.enabled = True
config.account_validity.period = 604800000 # Time in ms for 1 week
config["enable_registration"] = True
config["account_validity"] = {
"enabled": True,
"period": 604800000, # Time in ms for 1 week
}
self.hs = self.setup_test_homeserver(config=config)

return self.hs
Expand Down Expand Up @@ -299,14 +301,17 @@ class AccountValidityRenewalByEmailTestCase(unittest.HomeserverTestCase):

def make_homeserver(self, reactor, clock):
config = self.default_config()

# Test for account expiring after a week and renewal emails being sent 2
# days before expiry.
config.enable_registration = True
config.account_validity.enabled = True
config.account_validity.renew_by_email_enabled = True
config.account_validity.period = 604800000 # Time in ms for 1 week
config.account_validity.renew_at = 172800000 # Time in ms for 2 days
config.account_validity.renew_email_subject = "Renew your account"
config["enable_registration"] = True
config["account_validity"] = {
"enabled": True,
"period": 604800000, # Time in ms for 1 week
"renew_at": 172800000, # Time in ms for 2 days
"renew_by_email_enabled": True,
"renew_email_subject": "Renew your account",
}

# Email config.
self.email_attempts = []
Expand All @@ -315,17 +320,23 @@ def sendmail(*args, **kwargs):
self.email_attempts.append((args, kwargs))
return

config.email_template_dir = os.path.abspath(
pkg_resources.resource_filename('synapse', 'res/templates')
)
config.email_expiry_template_html = "notice_expiry.html"
config.email_expiry_template_text = "notice_expiry.txt"
config.email_smtp_host = "127.0.0.1"
config.email_smtp_port = 20
config.require_transport_security = False
config.email_smtp_user = None
config.email_smtp_pass = None
config.email_notif_from = "test@example.com"
config["email"] = {
"enable_notifs": True,
"template_dir": os.path.abspath(
pkg_resources.resource_filename('synapse', 'res/templates')
),
"expiry_template_html": "notice_expiry.html",
"expiry_template_text": "notice_expiry.txt",
"notif_template_html": "notif_mail.html",
"notif_template_text": "notif_mail.txt",
"smtp_host": "127.0.0.1",
"smtp_port": 20,
"require_transport_security": False,
"smtp_user": None,
"smtp_pass": None,
"notif_from": "test@example.com",
}
config["public_baseurl"] = "aaa"

self.hs = self.setup_test_homeserver(config=config, sendmail=sendmail)

Expand Down
17 changes: 6 additions & 11 deletions tests/rest/media/v1/test_media_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@
from twisted.internet import defer, reactor
from twisted.internet.defer import Deferred

from synapse.config.repository import MediaStorageProviderConfig
from synapse.rest.media.v1._base import FileInfo
from synapse.rest.media.v1.filepath import MediaFilePaths
from synapse.rest.media.v1.media_storage import MediaStorage
from synapse.rest.media.v1.storage_provider import FileStorageProviderBackend
from synapse.util.logcontext import make_deferred_yieldable
from synapse.util.module_loader import load_module

from tests import unittest

Expand Down Expand Up @@ -120,12 +118,14 @@ def write_to(r):
client.get_file = get_file

self.storage_path = self.mktemp()
self.media_store_path = self.mktemp()
os.mkdir(self.storage_path)
os.mkdir(self.media_store_path)

config = self.default_config()
config.media_store_path = self.storage_path
config.thumbnail_requirements = {}
config.max_image_pixels = 2000000
config["media_store_path"] = self.media_store_path
config["thumbnail_requirements"] = {}
config["max_image_pixels"] = 2000000

provider_config = {
"module": "synapse.rest.media.v1.storage_provider.FileStorageProviderBackend",
Expand All @@ -134,12 +134,7 @@ def write_to(r):
"store_remote": True,
"config": {"directory": self.storage_path},
}

loaded = list(load_module(provider_config)) + [
MediaStorageProviderConfig(False, False, False)
]

config.media_storage_providers = [loaded]
config["media_storage_providers"] = [provider_config]

hs = self.setup_test_homeserver(config=config, http_client=client)

Expand Down
40 changes: 16 additions & 24 deletions tests/rest/media/v1/test_url_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import os

import attr
from netaddr import IPSet

from twisted.internet._resolver import HostResolution
from twisted.internet.address import IPv4Address, IPv6Address
Expand All @@ -25,9 +24,6 @@
from twisted.test.proto_helpers import AccumulatingProtocol
from twisted.web._newclient import ResponseDone

from synapse.config.repository import MediaStorageProviderConfig
from synapse.util.module_loader import load_module

from tests import unittest
from tests.server import FakeTransport

Expand Down Expand Up @@ -67,23 +63,23 @@ class URLPreviewTests(unittest.HomeserverTestCase):

def make_homeserver(self, reactor, clock):

self.storage_path = self.mktemp()
os.mkdir(self.storage_path)

config = self.default_config()
config.url_preview_enabled = True
config.max_spider_size = 9999999
config.url_preview_ip_range_blacklist = IPSet(
(
"192.168.1.1",
"1.0.0.0/8",
"3fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
"2001:800::/21",
)
config["url_preview_enabled"] = True
config["max_spider_size"] = 9999999
config["url_preview_ip_range_blacklist"] = (
"192.168.1.1",
"1.0.0.0/8",
"3fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
"2001:800::/21",
)
config.url_preview_ip_range_whitelist = IPSet(("1.1.1.1",))
config.url_preview_url_blacklist = []
config.media_store_path = self.storage_path
config["url_preview_ip_range_whitelist"] = ("1.1.1.1",)
config["url_preview_url_blacklist"] = []

self.storage_path = self.mktemp()
self.media_store_path = self.mktemp()
os.mkdir(self.storage_path)
os.mkdir(self.media_store_path)
config["media_store_path"] = self.media_store_path

provider_config = {
"module": "synapse.rest.media.v1.storage_provider.FileStorageProviderBackend",
Expand All @@ -93,11 +89,7 @@ def make_homeserver(self, reactor, clock):
"config": {"directory": self.storage_path},
}

loaded = list(load_module(provider_config)) + [
MediaStorageProviderConfig(False, False, False)
]

config.media_storage_providers = [loaded]
config["media_storage_providers"] = [provider_config]

hs = self.setup_test_homeserver(config=config)

Expand Down
Loading