|
| 1 | +# |
| 2 | +# This file is licensed under the Affero General Public License (AGPL) version 3. |
| 3 | +# |
| 4 | +# Copyright (C) 2024 New Vector, Ltd |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Affero General Public License as |
| 8 | +# published by the Free Software Foundation, either version 3 of the |
| 9 | +# License, or (at your option) any later version. |
| 10 | +# |
| 11 | +# See the GNU Affero General Public License for more details: |
| 12 | +# <https://www.gnu.org/licenses/agpl-3.0.html>. |
| 13 | + |
| 14 | +from typing import Dict |
| 15 | + |
| 16 | +from twisted.web.resource import Resource |
| 17 | + |
| 18 | +from synapse.rest import admin |
| 19 | +from synapse.rest.client import login |
| 20 | +from synapse.rest.synapse.client import build_synapse_client_resource_tree |
| 21 | + |
| 22 | +from tests import unittest |
| 23 | + |
| 24 | + |
| 25 | +class FederationWhitelistTests(unittest.HomeserverTestCase): |
| 26 | + servlets = [ |
| 27 | + admin.register_servlets_for_client_rest_resource, |
| 28 | + login.register_servlets, |
| 29 | + ] |
| 30 | + |
| 31 | + def create_resource_dict(self) -> Dict[str, Resource]: |
| 32 | + base = super().create_resource_dict() |
| 33 | + base.update(build_synapse_client_resource_tree(self.hs)) |
| 34 | + return base |
| 35 | + |
| 36 | + def test_default(self) -> None: |
| 37 | + "By default the endpoint should 404" |
| 38 | + channel = self.make_request( |
| 39 | + "GET", "/_synapse/client/config/federation_whitelist", shorthand=False |
| 40 | + ) |
| 41 | + |
| 42 | + self.assertEqual(channel.code, 404) |
| 43 | + |
| 44 | + @unittest.override_config({"extension_federation_whitelist_endpoint": True}) |
| 45 | + def test_no_auth(self) -> None: |
| 46 | + "Endpoint requires auth when enabled" |
| 47 | + |
| 48 | + channel = self.make_request( |
| 49 | + "GET", "/_synapse/client/config/federation_whitelist", shorthand=False |
| 50 | + ) |
| 51 | + |
| 52 | + self.assertEqual(channel.code, 401) |
| 53 | + |
| 54 | + @unittest.override_config({"extension_federation_whitelist_endpoint": True}) |
| 55 | + def test_no_whitelist(self) -> None: |
| 56 | + "Test when there is no whitelist configured" |
| 57 | + |
| 58 | + self.register_user("user", "password") |
| 59 | + tok = self.login("user", "password") |
| 60 | + |
| 61 | + channel = self.make_request( |
| 62 | + "GET", |
| 63 | + "/_synapse/client/config/federation_whitelist", |
| 64 | + shorthand=False, |
| 65 | + access_token=tok, |
| 66 | + ) |
| 67 | + |
| 68 | + self.assertEqual(channel.code, 200) |
| 69 | + self.assertEqual( |
| 70 | + channel.json_body, {"whitelist_enabled": False, "whitelist": []} |
| 71 | + ) |
| 72 | + |
| 73 | + @unittest.override_config( |
| 74 | + { |
| 75 | + "extension_federation_whitelist_endpoint": True, |
| 76 | + "federation_domain_whitelist": ["example.com"], |
| 77 | + } |
| 78 | + ) |
| 79 | + def test_whitelist(self) -> None: |
| 80 | + "Test when there is a whitelist configured" |
| 81 | + |
| 82 | + self.register_user("user", "password") |
| 83 | + tok = self.login("user", "password") |
| 84 | + |
| 85 | + channel = self.make_request( |
| 86 | + "GET", |
| 87 | + "/_synapse/client/config/federation_whitelist", |
| 88 | + shorthand=False, |
| 89 | + access_token=tok, |
| 90 | + ) |
| 91 | + |
| 92 | + self.assertEqual(channel.code, 200) |
| 93 | + self.assertEqual( |
| 94 | + channel.json_body, {"whitelist_enabled": True, "whitelist": ["example.com"]} |
| 95 | + ) |
0 commit comments