summary refs log tree commit diff
path: root/tests/config/test_api.py
blob: 6773c9a2773a787d9d341aa67f7d51f8ea39169e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from unittest import TestCase as StdlibTestCase

import yaml

from synapse.config import ConfigError
from synapse.config.api import ApiConfig
from synapse.types.state import StateFilter

DEFAULT_PREJOIN_STATE_PAIRS = {
    ("m.room.join_rules", ""),
    ("m.room.canonical_alias", ""),
    ("m.room.avatar", ""),
    ("m.room.encryption", ""),
    ("m.room.name", ""),
    ("m.room.create", ""),
    ("m.room.topic", ""),
}


class TestRoomPrejoinState(StdlibTestCase):
    def read_config(self, source: str) -> ApiConfig:
        config = ApiConfig()
        config.read_config(yaml.safe_load(source))
        return config

    def test_no_prejoin_state(self) -> None:
        config = self.read_config("foo: bar")
        self.assertFalse(config.room_prejoin_state.has_wildcards())
        self.assertEqual(
            set(config.room_prejoin_state.concrete_types()), DEFAULT_PREJOIN_STATE_PAIRS
        )

    def test_disable_default_event_types(self) -> None:
        config = self.read_config(
            """
room_prejoin_state:
    disable_default_event_types: true
        """
        )
        self.assertEqual(config.room_prejoin_state, StateFilter.none())

    def test_event_without_state_key(self) -> None:
        config = self.read_config(
            """
room_prejoin_state:
    disable_default_event_types: true
    additional_event_types:
        - foo
        """
        )
        self.assertEqual(config.room_prejoin_state.wildcard_types(), ["foo"])
        self.assertEqual(config.room_prejoin_state.concrete_types(), [])

    def test_event_with_specific_state_key(self) -> None:
        config = self.read_config(
            """
room_prejoin_state:
    disable_default_event_types: true
    additional_event_types:
        - [foo, bar]
        """
        )
        self.assertFalse(config.room_prejoin_state.has_wildcards())
        self.assertEqual(
            set(config.room_prejoin_state.concrete_types()),
            {("foo", "bar")},
        )

    def test_repeated_event_with_specific_state_key(self) -> None:
        config = self.read_config(
            """
room_prejoin_state:
    disable_default_event_types: true
    additional_event_types:
        - [foo, bar]
        - [foo, baz]
        """
        )
        self.assertFalse(config.room_prejoin_state.has_wildcards())
        self.assertEqual(
            set(config.room_prejoin_state.concrete_types()),
            {("foo", "bar"), ("foo", "baz")},
        )

    def test_no_specific_state_key_overrides_specific_state_key(self) -> None:
        config = self.read_config(
            """
room_prejoin_state:
    disable_default_event_types: true
    additional_event_types:
        - [foo, bar]
        - foo
        """
        )
        self.assertEqual(config.room_prejoin_state.wildcard_types(), ["foo"])
        self.assertEqual(config.room_prejoin_state.concrete_types(), [])

        config = self.read_config(
            """
room_prejoin_state:
    disable_default_event_types: true
    additional_event_types:
        - foo
        - [foo, bar]
        """
        )
        self.assertEqual(config.room_prejoin_state.wildcard_types(), ["foo"])
        self.assertEqual(config.room_prejoin_state.concrete_types(), [])

    def test_bad_event_type_entry_raises(self) -> None:
        with self.assertRaises(ConfigError):
            self.read_config(
                """
room_prejoin_state:
    additional_event_types:
        - []
            """
            )

        with self.assertRaises(ConfigError):
            self.read_config(
                """
room_prejoin_state:
    additional_event_types:
        - [a]
            """
            )

        with self.assertRaises(ConfigError):
            self.read_config(
                """
room_prejoin_state:
    additional_event_types:
        - [a, b, c]
            """
            )

        with self.assertRaises(ConfigError):
            self.read_config(
                """
room_prejoin_state:
    additional_event_types:
        - [true, 1.23]
            """
            )