1
0

test_snapshot.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from synapse.events.snapshot import EventContext
  16. from synapse.rest import admin
  17. from synapse.rest.client.v1 import login, room
  18. from tests import unittest
  19. from tests.test_utils.event_injection import create_event
  20. class TestEventContext(unittest.HomeserverTestCase):
  21. servlets = [
  22. admin.register_servlets,
  23. login.register_servlets,
  24. room.register_servlets,
  25. ]
  26. def prepare(self, reactor, clock, hs):
  27. self.store = hs.get_datastore()
  28. self.storage = hs.get_storage()
  29. self.user_id = self.register_user("u1", "pass")
  30. self.user_tok = self.login("u1", "pass")
  31. self.room_id = self.helper.create_room_as(tok=self.user_tok)
  32. def test_serialize_deserialize_msg(self):
  33. """Test that an EventContext for a message event is the same after
  34. serialize/deserialize.
  35. """
  36. event, context = self.get_success(
  37. create_event(
  38. self.hs,
  39. room_id=self.room_id,
  40. type="m.test",
  41. sender=self.user_id,
  42. )
  43. )
  44. self._check_serialize_deserialize(event, context)
  45. def test_serialize_deserialize_state_no_prev(self):
  46. """Test that an EventContext for a state event (with not previous entry)
  47. is the same after serialize/deserialize.
  48. """
  49. event, context = self.get_success(
  50. create_event(
  51. self.hs,
  52. room_id=self.room_id,
  53. type="m.test",
  54. sender=self.user_id,
  55. state_key="",
  56. )
  57. )
  58. self._check_serialize_deserialize(event, context)
  59. def test_serialize_deserialize_state_prev(self):
  60. """Test that an EventContext for a state event (which replaces a
  61. previous entry) is the same after serialize/deserialize.
  62. """
  63. event, context = self.get_success(
  64. create_event(
  65. self.hs,
  66. room_id=self.room_id,
  67. type="m.room.member",
  68. sender=self.user_id,
  69. state_key=self.user_id,
  70. content={"membership": "leave"},
  71. )
  72. )
  73. self._check_serialize_deserialize(event, context)
  74. def _check_serialize_deserialize(self, event, context):
  75. serialized = self.get_success(context.serialize(event, self.store))
  76. d_context = EventContext.deserialize(self.storage, serialized)
  77. self.assertEqual(context.state_group, d_context.state_group)
  78. self.assertEqual(context.rejected, d_context.rejected)
  79. self.assertEqual(
  80. context.state_group_before_event, d_context.state_group_before_event
  81. )
  82. self.assertEqual(context.prev_group, d_context.prev_group)
  83. self.assertEqual(context.delta_ids, d_context.delta_ids)
  84. self.assertEqual(context.app_service, d_context.app_service)
  85. self.assertEqual(
  86. self.get_success(context.get_current_state_ids()),
  87. self.get_success(d_context.get_current_state_ids()),
  88. )
  89. self.assertEqual(
  90. self.get_success(context.get_prev_state_ids()),
  91. self.get_success(d_context.get_prev_state_ids()),
  92. )