Files
synapse/tests/rest/client/test_auth_metadata.py
T
Quentin Gliech e3e847083e [v1.164] Restore the unstable MSC2965 /auth_metadata endpoint (#20460)
This is a partial revert of
https://github.com/element-hq/synapse/pull/20143 to restore the unstable
`/auth_metadata` C-S API endpoint.

The details on why are in #20459 

Since this is affecting live clients (Rust SDK-based clients including
Element X iOS and Android), this PR is against the 1.164 release branch
as we plan to release 1.164.2 with this fix.

The CI failures are unrelated and were already fixed on `develop` in
https://github.com/element-hq/synapse/pull/20386. Please look at
`develop` counterpart of this PR
https://github.com/element-hq/synapse/pull/20461 for the green CI
2026-11-02 16:55:22 +01:00

96 lines
3.1 KiB
Python

#
# This file is licensed under the Affero General Public License (AGPL) version 3.
#
# Copyright 2023 The Matrix.org Foundation C.I.C
# Copyright (C) 2023-2025 New Vector, Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# See the GNU Affero General Public License for more details:
# <https://www.gnu.org/licenses/agpl-3.0.html>.
#
# Originally licensed under the Apache License, Version 2.0:
# <http://www.apache.org/licenses/LICENSE-2.0>.
#
# [This file includes modifications made by New Vector Limited]
#
from http import HTTPStatus
from typing import ClassVar
from unittest.mock import Mock
from parameterized import parameterized_class
from twisted.internet.testing import MemoryReactor
from synapse.api.auth.mas import MasDelegatedAuth
from synapse.rest.client import auth_metadata
from synapse.server import HomeServer
from synapse.types import JsonDict
from synapse.util.clock import Clock
from tests.unittest import HomeserverTestCase
@parameterized_class(
("endpoint",),
[
("/_matrix/client/unstable/org.matrix.msc2965/auth_metadata",),
("/_matrix/client/v1/auth_metadata",),
],
)
class AuthMetadataMasDisabledTestCase(HomeserverTestCase):
endpoint: ClassVar[str]
servlets = [
auth_metadata.register_servlets,
]
def test_returns_404_when_mas_disabled(self) -> None:
# Make an unauthenticated request for the discovery info.
channel = self.make_request("GET", self.endpoint)
self.assertEqual(channel.code, HTTPStatus.NOT_FOUND)
@parameterized_class(
("endpoint",),
[
("/_matrix/client/unstable/org.matrix.msc2965/auth_metadata",),
("/_matrix/client/v1/auth_metadata",),
],
)
class AuthMetadataMasEnabledTestCase(HomeserverTestCase):
endpoint: ClassVar[str]
servlets = [
auth_metadata.register_servlets,
]
def default_config(self) -> JsonDict:
config = super().default_config()
# Registration must be disabled for the MAS integration to be enabled.
config["enable_registration"] = False
config["matrix_authentication_service"] = {
"enabled": True,
"endpoint": "https://auth.example.com/",
"secret": "verysecret",
}
return config
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
# Install a mock `MasDelegatedAuth` implementation.
self.auth = Mock(spec=MasDelegatedAuth)
return self.setup_test_homeserver(auth=self.auth)
def test_returns_metadata_from_mas(self) -> None:
test_auth_metadata = {
"issuer": "https://auth.example.com/",
}
self.auth.auth_metadata.return_value = test_auth_metadata
channel = self.make_request("GET", self.endpoint)
self.assertEqual(channel.code, HTTPStatus.OK, channel.result)
self.assertEqual(channel.json_body, test_auth_metadata)