test_snapshot.py 3.7 KB

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