|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2019 The Matrix.org Foundation C.I.C. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the 'License'); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an 'AS IS' BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +import threading |
| 16 | +from typing import Dict |
| 17 | + |
| 18 | +from mock import Mock |
| 19 | + |
| 20 | +from synapse.events import EventBase |
| 21 | +from synapse.module_api import ModuleApi |
| 22 | +from synapse.rest import admin |
| 23 | +from synapse.rest.client.v1 import login, room |
| 24 | +from synapse.types import Requester, StateMap |
| 25 | + |
| 26 | +from tests import unittest |
| 27 | + |
| 28 | +thread_local = threading.local() |
| 29 | + |
| 30 | + |
| 31 | +class ThirdPartyRulesTestModule: |
| 32 | + def __init__(self, config: Dict, module_api: ModuleApi): |
| 33 | + # keep a record of the "current" rules module, so that the test can patch |
| 34 | + # it if desired. |
| 35 | + thread_local.rules_module = self |
| 36 | + self.module_api = module_api |
| 37 | + |
| 38 | + async def on_create_room( |
| 39 | + self, requester: Requester, config: dict, is_requester_admin: bool |
| 40 | + ): |
| 41 | + return True |
| 42 | + |
| 43 | + async def check_event_allowed(self, event: EventBase, state: StateMap[EventBase]): |
| 44 | + return True |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def parse_config(config): |
| 48 | + return config |
| 49 | + |
| 50 | + |
| 51 | +def current_rules_module() -> ThirdPartyRulesTestModule: |
| 52 | + return thread_local.rules_module |
| 53 | + |
| 54 | + |
| 55 | +class ThirdPartyRulesTestCase(unittest.HomeserverTestCase): |
| 56 | + servlets = [ |
| 57 | + admin.register_servlets, |
| 58 | + login.register_servlets, |
| 59 | + room.register_servlets, |
| 60 | + ] |
| 61 | + |
| 62 | + def default_config(self): |
| 63 | + config = super().default_config() |
| 64 | + config["third_party_event_rules"] = { |
| 65 | + "module": __name__ + ".ThirdPartyRulesTestModule", |
| 66 | + "config": {}, |
| 67 | + } |
| 68 | + return config |
| 69 | + |
| 70 | + def prepare(self, reactor, clock, homeserver): |
| 71 | + # Create a user and room to play with during the tests |
| 72 | + self.user_id = self.register_user("kermit", "monkey") |
| 73 | + self.tok = self.login("kermit", "monkey") |
| 74 | + |
| 75 | + self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok) |
| 76 | + |
| 77 | + def test_third_party_rules(self): |
| 78 | + """Tests that a forbidden event is forbidden from being sent, but an allowed one |
| 79 | + can be sent. |
| 80 | + """ |
| 81 | + # patch the rules module with a Mock which will return False for some event |
| 82 | + # types |
| 83 | + async def check(ev, state): |
| 84 | + return ev.type != "foo.bar.forbidden" |
| 85 | + |
| 86 | + callback = Mock(spec=[], side_effect=check) |
| 87 | + current_rules_module().check_event_allowed = callback |
| 88 | + |
| 89 | + request, channel = self.make_request( |
| 90 | + "PUT", |
| 91 | + "/_matrix/client/r0/rooms/%s/send/foo.bar.allowed/1" % self.room_id, |
| 92 | + {}, |
| 93 | + access_token=self.tok, |
| 94 | + ) |
| 95 | + self.render(request) |
| 96 | + self.assertEquals(channel.result["code"], b"200", channel.result) |
| 97 | + |
| 98 | + callback.assert_called_once() |
| 99 | + |
| 100 | + # there should be various state events in the state arg: do some basic checks |
| 101 | + state_arg = callback.call_args[0][1] |
| 102 | + for k in (("m.room.create", ""), ("m.room.member", self.user_id)): |
| 103 | + self.assertIn(k, state_arg) |
| 104 | + ev = state_arg[k] |
| 105 | + self.assertEqual(ev.type, k[0]) |
| 106 | + self.assertEqual(ev.state_key, k[1]) |
| 107 | + |
| 108 | + request, channel = self.make_request( |
| 109 | + "PUT", |
| 110 | + "/_matrix/client/r0/rooms/%s/send/foo.bar.forbidden/1" % self.room_id, |
| 111 | + {}, |
| 112 | + access_token=self.tok, |
| 113 | + ) |
| 114 | + self.render(request) |
| 115 | + self.assertEquals(channel.result["code"], b"403", channel.result) |
| 116 | + |
| 117 | + def test_modify_event(self): |
| 118 | + """Tests that the module can successfully tweak an event before it is persisted. |
| 119 | + """ |
| 120 | + # first patch the event checker so that it will modify the event |
| 121 | + async def check(ev: EventBase, state): |
| 122 | + ev.content = {"x": "y"} |
| 123 | + return True |
| 124 | + |
| 125 | + current_rules_module().check_event_allowed = check |
| 126 | + |
| 127 | + # now send the event |
| 128 | + request, channel = self.make_request( |
| 129 | + "PUT", |
| 130 | + "/_matrix/client/r0/rooms/%s/send/modifyme/1" % self.room_id, |
| 131 | + {"x": "x"}, |
| 132 | + access_token=self.tok, |
| 133 | + ) |
| 134 | + self.render(request) |
| 135 | + self.assertEqual(channel.result["code"], b"200", channel.result) |
| 136 | + event_id = channel.json_body["event_id"] |
| 137 | + |
| 138 | + # ... and check that it got modified |
| 139 | + request, channel = self.make_request( |
| 140 | + "GET", |
| 141 | + "/_matrix/client/r0/rooms/%s/event/%s" % (self.room_id, event_id), |
| 142 | + access_token=self.tok, |
| 143 | + ) |
| 144 | + self.render(request) |
| 145 | + self.assertEqual(channel.result["code"], b"200", channel.result) |
| 146 | + ev = channel.json_body |
| 147 | + self.assertEqual(ev["content"]["x"], "y") |
| 148 | + |
| 149 | + def test_send_event(self): |
| 150 | + """Tests that the module can send an event into a room via the module api""" |
| 151 | + content = { |
| 152 | + "msgtype": "m.text", |
| 153 | + "body": "Hello!", |
| 154 | + } |
| 155 | + event_dict = { |
| 156 | + "room_id": self.room_id, |
| 157 | + "type": "m.room.message", |
| 158 | + "content": content, |
| 159 | + "sender": self.user_id, |
| 160 | + } |
| 161 | + event = self.get_success( |
| 162 | + current_rules_module().module_api.create_and_send_event_into_room( |
| 163 | + event_dict |
| 164 | + ) |
| 165 | + ) # type: EventBase |
| 166 | + |
| 167 | + self.assertEquals(event.sender, self.user_id) |
| 168 | + self.assertEquals(event.room_id, self.room_id) |
| 169 | + self.assertEquals(event.type, "m.room.message") |
| 170 | + self.assertEquals(event.content, content) |
0 commit comments