You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

115 lines
4.0 KiB

  1. # Copyright 2020 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.events.snapshot import EventContext
  17. from synapse.rest import admin
  18. from synapse.rest.client import login, room
  19. from synapse.server import HomeServer
  20. from synapse.util import Clock
  21. from tests import unittest
  22. from tests.test_utils.event_injection import create_event
  23. class TestEventContext(unittest.HomeserverTestCase):
  24. servlets = [
  25. admin.register_servlets,
  26. login.register_servlets,
  27. room.register_servlets,
  28. ]
  29. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  30. self.store = hs.get_datastores().main
  31. self._storage_controllers = hs.get_storage_controllers()
  32. self.user_id = self.register_user("u1", "pass")
  33. self.user_tok = self.login("u1", "pass")
  34. self.room_id = self.helper.create_room_as(tok=self.user_tok)
  35. def test_serialize_deserialize_msg(self) -> None:
  36. """Test that an EventContext for a message event is the same after
  37. serialize/deserialize.
  38. """
  39. event, context = self.get_success(
  40. create_event(
  41. self.hs,
  42. room_id=self.room_id,
  43. type="m.test",
  44. sender=self.user_id,
  45. )
  46. )
  47. self._check_serialize_deserialize(event, context)
  48. def test_serialize_deserialize_state_no_prev(self) -> None:
  49. """Test that an EventContext for a state event (with not previous entry)
  50. is the same after serialize/deserialize.
  51. """
  52. event, context = self.get_success(
  53. create_event(
  54. self.hs,
  55. room_id=self.room_id,
  56. type="m.test",
  57. sender=self.user_id,
  58. state_key="",
  59. )
  60. )
  61. self._check_serialize_deserialize(event, context)
  62. def test_serialize_deserialize_state_prev(self) -> None:
  63. """Test that an EventContext for a state event (which replaces a
  64. previous entry) is the same after serialize/deserialize.
  65. """
  66. event, context = self.get_success(
  67. create_event(
  68. self.hs,
  69. room_id=self.room_id,
  70. type="m.room.member",
  71. sender=self.user_id,
  72. state_key=self.user_id,
  73. content={"membership": "leave"},
  74. )
  75. )
  76. self._check_serialize_deserialize(event, context)
  77. def _check_serialize_deserialize(
  78. self, event: EventBase, context: EventContext
  79. ) -> None:
  80. serialized = self.get_success(context.serialize(event, self.store))
  81. d_context = EventContext.deserialize(self._storage_controllers, serialized)
  82. self.assertEqual(context.state_group, d_context.state_group)
  83. self.assertEqual(context.rejected, d_context.rejected)
  84. self.assertEqual(
  85. context.state_group_before_event, d_context.state_group_before_event
  86. )
  87. self.assertEqual(context.state_group_deltas, d_context.state_group_deltas)
  88. self.assertEqual(context.app_service, d_context.app_service)
  89. self.assertEqual(
  90. self.get_success(context.get_current_state_ids()),
  91. self.get_success(d_context.get_current_state_ids()),
  92. )
  93. self.assertEqual(
  94. self.get_success(context.get_prev_state_ids()),
  95. self.get_success(d_context.get_prev_state_ids()),
  96. )