test_room.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Copyright 2014-2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.api.constants import EventTypes
  15. from synapse.api.room_versions import RoomVersions
  16. from synapse.types import RoomAlias, RoomID, UserID
  17. from tests.unittest import HomeserverTestCase
  18. class RoomStoreTestCase(HomeserverTestCase):
  19. def prepare(self, reactor, clock, hs):
  20. # We can't test RoomStore on its own without the DirectoryStore, for
  21. # management of the 'room_aliases' table
  22. self.store = hs.get_datastores().main
  23. self.room = RoomID.from_string("!abcde:test")
  24. self.alias = RoomAlias.from_string("#a-room-name:test")
  25. self.u_creator = UserID.from_string("@creator:test")
  26. self.get_success(
  27. self.store.store_room(
  28. self.room.to_string(),
  29. room_creator_user_id=self.u_creator.to_string(),
  30. is_public=True,
  31. room_version=RoomVersions.V1,
  32. )
  33. )
  34. def test_get_room(self):
  35. self.assertDictContainsSubset(
  36. {
  37. "room_id": self.room.to_string(),
  38. "creator": self.u_creator.to_string(),
  39. "is_public": True,
  40. },
  41. (self.get_success(self.store.get_room(self.room.to_string()))),
  42. )
  43. def test_get_room_unknown_room(self):
  44. self.assertIsNone(self.get_success(self.store.get_room("!uknown:test")))
  45. def test_get_room_with_stats(self):
  46. self.assertDictContainsSubset(
  47. {
  48. "room_id": self.room.to_string(),
  49. "creator": self.u_creator.to_string(),
  50. "public": True,
  51. },
  52. (self.get_success(self.store.get_room_with_stats(self.room.to_string()))),
  53. )
  54. def test_get_room_with_stats_unknown_room(self):
  55. self.assertIsNone(
  56. (self.get_success(self.store.get_room_with_stats("!uknown:test"))),
  57. )
  58. class RoomEventsStoreTestCase(HomeserverTestCase):
  59. def prepare(self, reactor, clock, hs):
  60. # Room events need the full datastore, for persist_event() and
  61. # get_room_state()
  62. self.store = hs.get_datastores().main
  63. self.storage = hs.get_storage()
  64. self.event_factory = hs.get_event_factory()
  65. self.room = RoomID.from_string("!abcde:test")
  66. self.get_success(
  67. self.store.store_room(
  68. self.room.to_string(),
  69. room_creator_user_id="@creator:text",
  70. is_public=True,
  71. room_version=RoomVersions.V1,
  72. )
  73. )
  74. def inject_room_event(self, **kwargs):
  75. self.get_success(
  76. self.storage.persistence.persist_event(
  77. self.event_factory.create_event(room_id=self.room.to_string(), **kwargs)
  78. )
  79. )
  80. def STALE_test_room_name(self):
  81. name = "A-Room-Name"
  82. self.inject_room_event(
  83. etype=EventTypes.Name, name=name, content={"name": name}, depth=1
  84. )
  85. state = self.get_success(
  86. self.store.get_current_state(room_id=self.room.to_string())
  87. )
  88. self.assertEqual(1, len(state))
  89. self.assertObjectHasAttributes(
  90. {"type": "m.room.name", "room_id": self.room.to_string(), "name": name},
  91. state[0],
  92. )
  93. def STALE_test_room_topic(self):
  94. topic = "A place for things"
  95. self.inject_room_event(
  96. etype=EventTypes.Topic, topic=topic, content={"topic": topic}, depth=1
  97. )
  98. state = self.get_success(
  99. self.store.get_current_state(room_id=self.room.to_string())
  100. )
  101. self.assertEqual(1, len(state))
  102. self.assertObjectHasAttributes(
  103. {"type": "m.room.topic", "room_id": self.room.to_string(), "topic": topic},
  104. state[0],
  105. )
  106. # Not testing the various 'level' methods for now because there's lots
  107. # of them and need coalescing; see JIRA SPEC-11