test_snapshot.py 3.5 KB

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