|
| 1 | +# Copyright 2022 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 | +from unittest.mock import Mock |
| 15 | + |
| 16 | +from twisted.test.proto_helpers import MemoryReactor |
| 17 | + |
| 18 | +import synapse.rest.admin |
| 19 | +from synapse.rest.client import login, notifications, receipts, room |
| 20 | +from synapse.server import HomeServer |
| 21 | +from synapse.util import Clock |
| 22 | + |
| 23 | +from tests.test_utils import simple_async_mock |
| 24 | +from tests.unittest import HomeserverTestCase |
| 25 | + |
| 26 | + |
| 27 | +class HTTPPusherTests(HomeserverTestCase): |
| 28 | + servlets = [ |
| 29 | + synapse.rest.admin.register_servlets_for_client_rest_resource, |
| 30 | + room.register_servlets, |
| 31 | + login.register_servlets, |
| 32 | + receipts.register_servlets, |
| 33 | + notifications.register_servlets, |
| 34 | + ] |
| 35 | + |
| 36 | + def prepare( |
| 37 | + self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer |
| 38 | + ) -> None: |
| 39 | + self.store = homeserver.get_datastores().main |
| 40 | + self.module_api = homeserver.get_module_api() |
| 41 | + self.event_creation_handler = homeserver.get_event_creation_handler() |
| 42 | + self.sync_handler = homeserver.get_sync_handler() |
| 43 | + self.auth_handler = homeserver.get_auth_handler() |
| 44 | + |
| 45 | + def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer: |
| 46 | + # Mock out the calls over federation. |
| 47 | + fed_transport_client = Mock(spec=["send_transaction"]) |
| 48 | + fed_transport_client.send_transaction = simple_async_mock({}) |
| 49 | + |
| 50 | + return self.setup_test_homeserver( |
| 51 | + federation_transport_client=fed_transport_client, |
| 52 | + ) |
| 53 | + |
| 54 | + def test_notify_for_local_invites(self) -> None: |
| 55 | + """ |
| 56 | + Local users will get notified for invites |
| 57 | + """ |
| 58 | + |
| 59 | + user_id = self.register_user("user", "pass") |
| 60 | + access_token = self.login("user", "pass") |
| 61 | + other_user_id = self.register_user("otheruser", "pass") |
| 62 | + other_access_token = self.login("otheruser", "pass") |
| 63 | + |
| 64 | + # Create a room |
| 65 | + room = self.helper.create_room_as(user_id, tok=access_token) |
| 66 | + |
| 67 | + # Check we start with no pushes |
| 68 | + channel = self.make_request( |
| 69 | + "GET", |
| 70 | + "/notifications", |
| 71 | + access_token=other_access_token, |
| 72 | + ) |
| 73 | + self.assertEqual(channel.code, 200, channel.result) |
| 74 | + self.assertEqual(len(channel.json_body["notifications"]), 0, channel.json_body) |
| 75 | + |
| 76 | + # Send an invite |
| 77 | + self.helper.invite(room=room, src=user_id, targ=other_user_id, tok=access_token) |
| 78 | + |
| 79 | + # We should have a notification now |
| 80 | + channel = self.make_request( |
| 81 | + "GET", |
| 82 | + "/notifications", |
| 83 | + access_token=other_access_token, |
| 84 | + ) |
| 85 | + self.assertEqual(channel.code, 200) |
| 86 | + self.assertEqual(len(channel.json_body["notifications"]), 1, channel.json_body) |
| 87 | + self.assertEqual( |
| 88 | + channel.json_body["notifications"][0]["event"]["content"]["membership"], |
| 89 | + "invite", |
| 90 | + channel.json_body, |
| 91 | + ) |
0 commit comments