summary refs log tree commit diff
path: root/tests/rulecheck/test_domainrulecheck.py
blob: a887249b59753f187a3ee7f4fd6a467b57beede3 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# -*- coding: utf-8 -*-
# Copyright 2018 New Vector Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import json

from synapse.config._base import ConfigError
from synapse.rest.client.v1 import admin, login, room
from synapse.rulecheck.domain_rule_checker import DomainRuleChecker

from tests import unittest
from tests.server import make_request, render


class DomainRuleCheckerTestCase(unittest.TestCase):
    def test_allowed(self):
        config = {
            "default": False,
            "domain_mapping": {
                "source_one": ["target_one", "target_two"],
                "source_two": ["target_two"],
            },
        }
        check = DomainRuleChecker(config)
        self.assertTrue(
            check.user_may_invite("test:source_one", "test:target_one", "room", False)
        )
        self.assertTrue(
            check.user_may_invite("test:source_one", "test:target_two", "room", False)
        )
        self.assertTrue(
            check.user_may_invite("test:source_two", "test:target_two", "room", False)
        )

    def test_disallowed(self):
        config = {
            "default": True,
            "domain_mapping": {
                "source_one": ["target_one", "target_two"],
                "source_two": ["target_two"],
                "source_four": [],
            },
        }
        check = DomainRuleChecker(config)
        self.assertFalse(
            check.user_may_invite("test:source_one", "test:target_three", "room", False)
        )
        self.assertFalse(
            check.user_may_invite("test:source_two", "test:target_three", "room", False)
        )
        self.assertFalse(
            check.user_may_invite("test:source_two", "test:target_one", "room", False)
        )
        self.assertFalse(
            check.user_may_invite("test:source_four", "test:target_one", "room", False)
        )

    def test_default_allow(self):
        config = {
            "default": True,
            "domain_mapping": {
                "source_one": ["target_one", "target_two"],
                "source_two": ["target_two"],
            },
        }
        check = DomainRuleChecker(config)
        self.assertTrue(
            check.user_may_invite("test:source_three", "test:target_one", "room", False)
        )

    def test_default_deny(self):
        config = {
            "default": False,
            "domain_mapping": {
                "source_one": ["target_one", "target_two"],
                "source_two": ["target_two"],
            },
        }
        check = DomainRuleChecker(config)
        self.assertFalse(
            check.user_may_invite("test:source_three", "test:target_one", "room", False)
        )

    def test_config_parse(self):
        config = {
            "default": False,
            "domain_mapping": {
                "source_one": ["target_one", "target_two"],
                "source_two": ["target_two"],
            },
        }
        self.assertEquals(config, DomainRuleChecker.parse_config(config))

    def test_config_parse_failure(self):
        config = {
            "domain_mapping": {
                "source_one": ["target_one", "target_two"],
                "source_two": ["target_two"],
            }
        }
        self.assertRaises(ConfigError, DomainRuleChecker.parse_config, config)


class DomainRuleCheckerRoomTestCase(unittest.HomeserverTestCase):
    servlets = [
        admin.register_servlets,
        room.register_servlets,
        login.register_servlets,
    ]

    hijack_auth = False

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

        config.spam_checker = (DomainRuleChecker, {
            "default": True,
            "domain_mapping": {},
            "can_only_join_rooms_with_invite": True,
            "can_only_create_one_to_one_rooms": True,
            "can_only_invite_during_room_creation": True,
            "can_invite_by_third_party_id": False,
        })

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

    def prepare(self, reactor, clock, hs):
        self.admin_user_id = self.register_user("admin_user", "pass", admin=True)
        self.admin_access_token = self.login("admin_user", "pass")

        self.normal_user_id = self.register_user("normal_user", "pass", admin=False)
        self.normal_access_token = self.login("normal_user", "pass")

        self.other_user_id = self.register_user("other_user", "pass", admin=False)

    def test_admin_can_create_room(self):
        channel = self._create_room(self.admin_access_token)
        assert channel.result["code"] == b"200", channel.result

    def test_normal_user_cannot_create_empty_room(self):
        channel = self._create_room(self.normal_access_token)
        assert channel.result["code"] == b"403", channel.result

    def test_normal_user_cannot_create_room_with_multiple_invites(self):
        channel = self._create_room(self.normal_access_token, content={
            "invite": [self.other_user_id, self.admin_user_id],
        })
        assert channel.result["code"] == b"403", channel.result

        # Test that it correctly counts both normal and third party invites
        channel = self._create_room(self.normal_access_token, content={
            "invite": [self.other_user_id],
            "invite_3pid": [{"medium": "email", "address": "foo@example.com"}],
        })
        assert channel.result["code"] == b"403", channel.result

        # Test that it correctly rejects third party invites
        channel = self._create_room(self.normal_access_token, content={
            "invite": [],
            "invite_3pid": [{"medium": "email", "address": "foo@example.com"}],
        })
        assert channel.result["code"] == b"403", channel.result

    def test_normal_user_can_room_with_single_invites(self):
        channel = self._create_room(self.normal_access_token, content={
            "invite": [self.other_user_id],
        })
        assert channel.result["code"] == b"200", channel.result

    def test_cannot_join_public_room(self):
        channel = self._create_room(self.admin_access_token)
        assert channel.result["code"] == b"200", channel.result

        room_id = channel.json_body["room_id"]

        self.helper.join(
            room_id, self.normal_user_id,
            tok=self.normal_access_token,
            expect_code=403,
        )

    def test_can_join_invited_room(self):
        channel = self._create_room(self.admin_access_token)
        assert channel.result["code"] == b"200", channel.result

        room_id = channel.json_body["room_id"]

        self.helper.invite(
            room_id,
            src=self.admin_user_id,
            targ=self.normal_user_id,
            tok=self.admin_access_token,
        )

        self.helper.join(
            room_id, self.normal_user_id,
            tok=self.normal_access_token,
            expect_code=200,
        )

    def test_cannot_invite(self):
        channel = self._create_room(self.admin_access_token)
        assert channel.result["code"] == b"200", channel.result

        room_id = channel.json_body["room_id"]

        self.helper.invite(
            room_id,
            src=self.admin_user_id,
            targ=self.normal_user_id,
            tok=self.admin_access_token,
        )

        self.helper.join(
            room_id, self.normal_user_id,
            tok=self.normal_access_token,
            expect_code=200,
        )

        self.helper.invite(
            room_id,
            src=self.normal_user_id,
            targ=self.other_user_id,
            tok=self.normal_access_token,
            expect_code=403,
        )

    def _create_room(self, token, content={}):
        path = "/_matrix/client/r0/createRoom?access_token=%s" % (
            token,
        )

        request, channel = make_request(
            self.hs.get_reactor(), "POST", path,
            content=json.dumps(content).encode("utf8"),
        )
        render(request, self.resource, self.hs.get_reactor())

        return channel