Skip to content

Commit 6dd6fd1

Browse files
committed
Add tests for MAS config
1 parent 62dcd8c commit 6dd6fd1

1 file changed

Lines changed: 164 additions & 0 deletions

File tree

tests/config/test_oauth_delegation.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#
2121

2222
import os
23+
import tempfile
2324
from unittest.mock import Mock
2425

2526
from synapse.config import ConfigError
@@ -275,3 +276,166 @@ def test_enable_3pid_changes_cannot_be_enabled(self) -> None:
275276
self.config_dict["enable_3pid_changes"] = True
276277
with self.assertRaises(ConfigError):
277278
self.parse_config()
279+
280+
281+
class MasAuthDelegation(TestCase):
282+
"""Test that the Homeserver fails to initialize if the config is invalid."""
283+
284+
def setUp(self) -> None:
285+
self.config_dict: JsonDict = {
286+
**default_config("test"),
287+
"public_baseurl": BASE_URL,
288+
"enable_registration": False,
289+
"matrix_authentication_service": {
290+
"enabled": True,
291+
"endpoint": "http://localhost:1324/",
292+
"secret": "verysecret",
293+
},
294+
}
295+
296+
def parse_config(self) -> HomeServerConfig:
297+
config = HomeServerConfig()
298+
config.parse_config_dict(self.config_dict, "", "")
299+
return config
300+
301+
def test_endpoint_has_to_be_a_url(self) -> None:
302+
self.config_dict["matrix_authentication_service"]["endpoint"] = "not a url"
303+
with self.assertRaises(ConfigError):
304+
self.parse_config()
305+
306+
def test_secret_and_secret_path_are_mutually_exclusive(self) -> None:
307+
with tempfile.NamedTemporaryFile() as f:
308+
self.config_dict["matrix_authentication_service"]["secret"] = "verysecret"
309+
self.config_dict["matrix_authentication_service"]["secret_path"] = f.name
310+
with self.assertRaises(ConfigError):
311+
self.parse_config()
312+
313+
def test_secret_path_loads_secret(self) -> None:
314+
with tempfile.NamedTemporaryFile(buffering=0) as f:
315+
f.write(b"53C237")
316+
del self.config_dict["matrix_authentication_service"]["secret"]
317+
self.config_dict["matrix_authentication_service"]["secret_path"] = f.name
318+
config = self.parse_config()
319+
self.assertEqual(config.mas.secret(), "53C237")
320+
321+
def test_secret_path_must_exist(self) -> None:
322+
del self.config_dict["matrix_authentication_service"]["secret"]
323+
self.config_dict["matrix_authentication_service"]["secret_path"] = (
324+
"/not/a/valid/file"
325+
)
326+
with self.assertRaises(ConfigError):
327+
self.parse_config()
328+
329+
def test_registration_cannot_be_enabled(self) -> None:
330+
self.config_dict["enable_registration"] = True
331+
with self.assertRaises(ConfigError):
332+
self.parse_config()
333+
334+
def test_user_consent_cannot_be_enabled(self) -> None:
335+
tmpdir = self.mktemp()
336+
os.mkdir(tmpdir)
337+
self.config_dict["user_consent"] = {
338+
"require_at_registration": True,
339+
"version": "1",
340+
"template_dir": tmpdir,
341+
"server_notice_content": {
342+
"msgtype": "m.text",
343+
"body": "foo",
344+
},
345+
}
346+
with self.assertRaises(ConfigError):
347+
self.parse_config()
348+
349+
def test_password_config_cannot_be_enabled(self) -> None:
350+
self.config_dict["password_config"] = {"enabled": True}
351+
with self.assertRaises(ConfigError):
352+
self.parse_config()
353+
354+
def test_oidc_sso_cannot_be_enabled(self) -> None:
355+
self.config_dict["oidc_providers"] = [
356+
{
357+
"idp_id": "microsoft",
358+
"idp_name": "Microsoft",
359+
"issuer": "https://login.microsoftonline.com/<tenant id>/v2.0",
360+
"client_id": "<client id>",
361+
"client_secret": "<client secret>",
362+
"scopes": ["openid", "profile"],
363+
"authorization_endpoint": "https://login.microsoftonline.com/<tenant id>/oauth2/v2.0/authorize",
364+
"token_endpoint": "https://login.microsoftonline.com/<tenant id>/oauth2/v2.0/token",
365+
"userinfo_endpoint": "https://graph.microsoft.com/oidc/userinfo",
366+
}
367+
]
368+
369+
with self.assertRaises(ConfigError):
370+
self.parse_config()
371+
372+
def test_cas_sso_cannot_be_enabled(self) -> None:
373+
self.config_dict["cas_config"] = {
374+
"enabled": True,
375+
"server_url": "https://cas-server.com",
376+
"displayname_attribute": "name",
377+
"required_attributes": {"userGroup": "staff", "department": "None"},
378+
}
379+
380+
with self.assertRaises(ConfigError):
381+
self.parse_config()
382+
383+
def test_auth_providers_cannot_be_enabled(self) -> None:
384+
self.config_dict["modules"] = [
385+
{
386+
"module": f"{__name__}.{CustomAuthModule.__qualname__}",
387+
"config": {},
388+
}
389+
]
390+
391+
# This requires actually setting up an HS, as the module will be run on setup,
392+
# which should raise as the module tries to register an auth provider
393+
config = self.parse_config()
394+
reactor, clock = get_clock()
395+
with self.assertRaises(ConfigError):
396+
setup_test_homeserver(
397+
self.addCleanup, reactor=reactor, clock=clock, config=config
398+
)
399+
400+
def test_jwt_auth_cannot_be_enabled(self) -> None:
401+
self.config_dict["jwt_config"] = {
402+
"enabled": True,
403+
"secret": "my-secret-token",
404+
"algorithm": "HS256",
405+
}
406+
407+
with self.assertRaises(ConfigError):
408+
self.parse_config()
409+
410+
def test_login_via_existing_session_cannot_be_enabled(self) -> None:
411+
self.config_dict["login_via_existing_session"] = {"enabled": True}
412+
with self.assertRaises(ConfigError):
413+
self.parse_config()
414+
415+
def test_captcha_cannot_be_enabled(self) -> None:
416+
self.config_dict.update(
417+
enable_registration_captcha=True,
418+
recaptcha_public_key="test",
419+
recaptcha_private_key="test",
420+
)
421+
with self.assertRaises(ConfigError):
422+
self.parse_config()
423+
424+
def test_refreshable_tokens_cannot_be_enabled(self) -> None:
425+
self.config_dict.update(
426+
refresh_token_lifetime="24h",
427+
refreshable_access_token_lifetime="10m",
428+
nonrefreshable_access_token_lifetime="24h",
429+
)
430+
with self.assertRaises(ConfigError):
431+
self.parse_config()
432+
433+
def test_session_lifetime_cannot_be_set(self) -> None:
434+
self.config_dict["session_lifetime"] = "24h"
435+
with self.assertRaises(ConfigError):
436+
self.parse_config()
437+
438+
def test_enable_3pid_changes_cannot_be_enabled(self) -> None:
439+
self.config_dict["enable_3pid_changes"] = True
440+
with self.assertRaises(ConfigError):
441+
self.parse_config()

0 commit comments

Comments
 (0)