test_event_unsigned_addition.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright 2023 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 twisted.test.proto_helpers import MemoryReactor
  15. from synapse.events import EventBase
  16. from synapse.rest import admin, login, room
  17. from synapse.server import HomeServer
  18. from synapse.types import JsonDict
  19. from synapse.util import Clock
  20. from tests.unittest import HomeserverTestCase
  21. class EventUnsignedAdditionTestCase(HomeserverTestCase):
  22. servlets = [
  23. room.register_servlets,
  24. admin.register_servlets,
  25. login.register_servlets,
  26. ]
  27. def prepare(
  28. self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
  29. ) -> None:
  30. self._store = homeserver.get_datastores().main
  31. self._module_api = homeserver.get_module_api()
  32. self._account_data_mgr = self._module_api.account_data_manager
  33. def test_annotate_event(self) -> None:
  34. """Test that we can annotate an event when we request it from the
  35. server.
  36. """
  37. async def add_unsigned_event(event: EventBase) -> JsonDict:
  38. return {"test_key": event.event_id}
  39. self._module_api.register_add_extra_fields_to_unsigned_client_event_callbacks(
  40. add_field_to_unsigned_callback=add_unsigned_event
  41. )
  42. user_id = self.register_user("user", "password")
  43. token = self.login("user", "password")
  44. room_id = self.helper.create_room_as(user_id, tok=token)
  45. result = self.helper.send(room_id, "Hello!", tok=token)
  46. event_id = result["event_id"]
  47. event_json = self.helper.get_event(room_id, event_id, tok=token)
  48. self.assertEqual(event_json["unsigned"].get("test_key"), event_id)