1
0

event_injection.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright 2018 New Vector Ltd
  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 typing import Any, List, Optional, Tuple
  16. import synapse.server
  17. from synapse.api.constants import EventTypes
  18. from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
  19. from synapse.events import EventBase
  20. from synapse.events.snapshot import EventContext
  21. """
  22. Utility functions for poking events into the storage of the server under test.
  23. """
  24. async def inject_member_event(
  25. hs: synapse.server.HomeServer,
  26. room_id: str,
  27. sender: str,
  28. membership: str,
  29. target: Optional[str] = None,
  30. extra_content: Optional[dict] = None,
  31. **kwargs: Any,
  32. ) -> EventBase:
  33. """Inject a membership event into a room."""
  34. if target is None:
  35. target = sender
  36. content = {"membership": membership}
  37. if extra_content:
  38. content.update(extra_content)
  39. return await inject_event(
  40. hs,
  41. room_id=room_id,
  42. type=EventTypes.Member,
  43. sender=sender,
  44. state_key=target,
  45. content=content,
  46. **kwargs,
  47. )
  48. async def inject_event(
  49. hs: synapse.server.HomeServer,
  50. room_version: Optional[str] = None,
  51. prev_event_ids: Optional[List[str]] = None,
  52. **kwargs: Any,
  53. ) -> EventBase:
  54. """Inject a generic event into a room
  55. Args:
  56. hs: the homeserver under test
  57. room_version: the version of the room we're inserting into.
  58. if not specified, will be looked up
  59. prev_event_ids: prev_events for the event. If not specified, will be looked up
  60. kwargs: fields for the event to be created
  61. """
  62. event, context = await create_event(hs, room_version, prev_event_ids, **kwargs)
  63. persistence = hs.get_storage_controllers().persistence
  64. assert persistence is not None
  65. await persistence.persist_event(event, context)
  66. return event
  67. async def create_event(
  68. hs: synapse.server.HomeServer,
  69. room_version: Optional[str] = None,
  70. prev_event_ids: Optional[List[str]] = None,
  71. **kwargs: Any,
  72. ) -> Tuple[EventBase, EventContext]:
  73. if room_version is None:
  74. room_version = await hs.get_datastores().main.get_room_version_id(
  75. kwargs["room_id"]
  76. )
  77. builder = hs.get_event_builder_factory().for_room_version(
  78. KNOWN_ROOM_VERSIONS[room_version], kwargs
  79. )
  80. (
  81. event,
  82. unpersisted_context,
  83. ) = await hs.get_event_creation_handler().create_new_client_event(
  84. builder, prev_event_ids=prev_event_ids
  85. )
  86. context = await unpersisted_context.persist(event)
  87. return event, context
  88. async def mark_event_as_partial_state(
  89. hs: synapse.server.HomeServer,
  90. event_id: str,
  91. room_id: str,
  92. ) -> None:
  93. """
  94. (Falsely) mark an event as having partial state.
  95. Naughty, but occasionally useful when checking that partial state doesn't
  96. block something from happening.
  97. If the event already has partial state, this insert will fail (event_id is unique
  98. in this table).
  99. """
  100. store = hs.get_datastores().main
  101. await store.db_pool.simple_upsert(
  102. table="partial_state_rooms",
  103. keyvalues={"room_id": room_id},
  104. values={},
  105. insertion_values={"room_id": room_id},
  106. )
  107. await store.db_pool.simple_insert(
  108. table="partial_state_events",
  109. values={
  110. "room_id": room_id,
  111. "event_id": event_id,
  112. },
  113. )