test_notifications.py 3.3 KB

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