|
| 1 | +# Copyright 2019 The Matrix.org Foundation C.I.C. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Dict, Iterable, Optional, Tuple, Union |
| 16 | +from unittest.mock import Mock |
| 17 | + |
| 18 | +import synapse.rest.admin |
| 19 | +import synapse.storage |
| 20 | +from synapse.appservice import ApplicationService |
| 21 | +from synapse.rest.client import login, receipts, room |
| 22 | +from synapse.util.stringutils import random_string |
| 23 | + |
| 24 | +from tests import unittest |
| 25 | + |
| 26 | + |
| 27 | +class ApplicationServiceEphemeralEventsTestCase(unittest.HomeserverTestCase): |
| 28 | + servlets = [ |
| 29 | + synapse.rest.admin.register_servlets_for_client_rest_resource, |
| 30 | + login.register_servlets, |
| 31 | + room.register_servlets, |
| 32 | + receipts.register_servlets, |
| 33 | + ] |
| 34 | + |
| 35 | + def prepare(self, reactor, clock, hs): |
| 36 | + # Mock the application service scheduler so that we can track any outgoing transactions |
| 37 | + self.mock_scheduler = Mock() |
| 38 | + |
| 39 | + hs.get_application_service_handler().scheduler = self.mock_scheduler |
| 40 | + |
| 41 | + self.user1 = self.register_user("user1", "password") |
| 42 | + self.token1 = self.login("user1", "password") |
| 43 | + |
| 44 | + self.user2 = self.register_user("user2", "password") |
| 45 | + self.token2 = self.login("user2", "password") |
| 46 | + |
| 47 | + def test_application_services_receive_read_receipts(self): |
| 48 | + """ |
| 49 | + Test that when an application service sends a read receipt in a room with |
| 50 | + another user, and that is in an application service's user namespace, that |
| 51 | + application service will receive that read receipt. |
| 52 | + """ |
| 53 | + ( |
| 54 | + interested_service, |
| 55 | + _, |
| 56 | + ) = self._register_interested_and_uninterested_application_services() |
| 57 | + |
| 58 | + # Create a room with both user1 and user2 |
| 59 | + room_id = self.helper.create_room_as( |
| 60 | + self.user1, tok=self.token1, is_public=True |
| 61 | + ) |
| 62 | + self.helper.join(room_id, self.user2, tok=self.token2) |
| 63 | + |
| 64 | + # Have user2 send a message into the room |
| 65 | + response_dict = self.helper.send(room_id, body="read me", tok=self.token2) |
| 66 | + |
| 67 | + # Have user1 send a read receipt for the message with an empty body |
| 68 | + channel = self.make_request( |
| 69 | + "POST", |
| 70 | + "/rooms/%s/receipt/m.read/%s" % (room_id, response_dict["event_id"]), |
| 71 | + access_token=self.token1, |
| 72 | + ) |
| 73 | + self.assertEqual(channel.code, 200) |
| 74 | + |
| 75 | + # user2 should have been the recipient of that read receipt. |
| 76 | + # Check if our application service - that is interested in user2 - received |
| 77 | + # the read receipt as part of an AS transaction. |
| 78 | + # |
| 79 | + # The uninterested application service should not have been notified. |
| 80 | + service, events = self.mock_scheduler.submit_ephemeral_events_for_as.call_args[ |
| 81 | + 0 |
| 82 | + ] |
| 83 | + self.assertEqual(service, interested_service) |
| 84 | + self.assertEqual(events[0]["type"], "m.receipt") |
| 85 | + self.assertEqual(events[0]["room_id"], room_id) |
| 86 | + |
| 87 | + # Assert that this was a read receipt from user1 |
| 88 | + read_receipts = list(events[0]["content"].values()) |
| 89 | + self.assertIn(self.user1, read_receipts[0]["m.read"]) |
| 90 | + |
| 91 | + def _register_interested_and_uninterested_application_services( |
| 92 | + self, |
| 93 | + ) -> Tuple[ApplicationService, ApplicationService]: |
| 94 | + # Create an application service with exclusive interest in user2 |
| 95 | + interested_service = self._make_application_service( |
| 96 | + namespaces={ |
| 97 | + ApplicationService.NS_USERS: [ |
| 98 | + { |
| 99 | + "regex": "@user2:.+", |
| 100 | + "exclusive": True, |
| 101 | + } |
| 102 | + ], |
| 103 | + }, |
| 104 | + ) |
| 105 | + uninterested_service = self._make_application_service() |
| 106 | + |
| 107 | + # Register this application service, along with another, uninterested one |
| 108 | + services = [ |
| 109 | + uninterested_service, |
| 110 | + interested_service, |
| 111 | + ] |
| 112 | + self.hs.get_datastore().get_app_services = Mock(return_value=services) |
| 113 | + |
| 114 | + return interested_service, uninterested_service |
| 115 | + |
| 116 | + def _make_application_service( |
| 117 | + self, |
| 118 | + namespaces: Optional[ |
| 119 | + Dict[ |
| 120 | + Union[ |
| 121 | + ApplicationService.NS_USERS, |
| 122 | + ApplicationService.NS_ALIASES, |
| 123 | + ApplicationService.NS_ROOMS, |
| 124 | + ], |
| 125 | + Iterable[Dict], |
| 126 | + ] |
| 127 | + ] = None, |
| 128 | + supports_ephemeral: Optional[bool] = True, |
| 129 | + ) -> ApplicationService: |
| 130 | + return ApplicationService( |
| 131 | + token=None, |
| 132 | + hostname="example.com", |
| 133 | + id=random_string(10), |
| 134 | + sender="@as:example.com", |
| 135 | + rate_limited=False, |
| 136 | + namespaces=namespaces, |
| 137 | + supports_ephemeral=supports_ephemeral, |
| 138 | + ) |
| 139 | + |
| 140 | + # TODO: Test that ephemeral messages aren't sent to application services that have |
| 141 | + # ephemeral: false |
0 commit comments