1
0

test_snapshot.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, room_id=self.room_id, type="m.test", sender=self.user_id,
  39. )
  40. )
  41. self._check_serialize_deserialize(event, context)
  42. def test_serialize_deserialize_state_no_prev(self):
  43. """Test that an EventContext for a state event (with not previous entry)
  44. is the same after serialize/deserialize.
  45. """
  46. event, context = self.get_success(
  47. create_event(
  48. self.hs,
  49. room_id=self.room_id,
  50. type="m.test",
  51. sender=self.user_id,
  52. state_key="",
  53. )
  54. )
  55. self._check_serialize_deserialize(event, context)
  56. def test_serialize_deserialize_state_prev(self):
  57. """Test that an EventContext for a state event (which replaces a
  58. previous entry) is the same after serialize/deserialize.
  59. """
  60. event, context = self.get_success(
  61. create_event(
  62. self.hs,
  63. room_id=self.room_id,
  64. type="m.room.member",
  65. sender=self.user_id,
  66. state_key=self.user_id,
  67. content={"membership": "leave"},
  68. )
  69. )
  70. self._check_serialize_deserialize(event, context)
  71. def _check_serialize_deserialize(self, event, context):
  72. serialized = self.get_success(context.serialize(event, self.store))
  73. d_context = EventContext.deserialize(self.storage, serialized)
  74. self.assertEqual(context.state_group, d_context.state_group)
  75. self.assertEqual(context.rejected, d_context.rejected)
  76. self.assertEqual(
  77. context.state_group_before_event, d_context.state_group_before_event
  78. )
  79. self.assertEqual(context.prev_group, d_context.prev_group)
  80. self.assertEqual(context.delta_ids, d_context.delta_ids)
  81. self.assertEqual(context.app_service, d_context.app_service)
  82. self.assertEqual(
  83. self.get_success(context.get_current_state_ids()),
  84. self.get_success(d_context.get_current_state_ids()),
  85. )
  86. self.assertEqual(
  87. self.get_success(context.get_prev_state_ids()),
  88. self.get_success(d_context.get_prev_state_ids()),
  89. )