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