test_room.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  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 twisted.internet import defer
  16. from synapse.api.constants import EventTypes
  17. from synapse.api.room_versions import RoomVersions
  18. from synapse.types import RoomAlias, RoomID, UserID
  19. from tests import unittest
  20. from tests.utils import setup_test_homeserver
  21. class RoomStoreTestCase(unittest.TestCase):
  22. @defer.inlineCallbacks
  23. def setUp(self):
  24. hs = yield setup_test_homeserver(self.addCleanup)
  25. # We can't test RoomStore on its own without the DirectoryStore, for
  26. # management of the 'room_aliases' table
  27. self.store = hs.get_datastore()
  28. self.room = RoomID.from_string("!abcde:test")
  29. self.alias = RoomAlias.from_string("#a-room-name:test")
  30. self.u_creator = UserID.from_string("@creator:test")
  31. yield defer.ensureDeferred(
  32. self.store.store_room(
  33. self.room.to_string(),
  34. room_creator_user_id=self.u_creator.to_string(),
  35. is_public=True,
  36. room_version=RoomVersions.V1,
  37. )
  38. )
  39. @defer.inlineCallbacks
  40. def test_get_room(self):
  41. self.assertDictContainsSubset(
  42. {
  43. "room_id": self.room.to_string(),
  44. "creator": self.u_creator.to_string(),
  45. "is_public": True,
  46. },
  47. (yield defer.ensureDeferred(self.store.get_room(self.room.to_string()))),
  48. )
  49. @defer.inlineCallbacks
  50. def test_get_room_unknown_room(self):
  51. self.assertIsNone(
  52. (yield defer.ensureDeferred(self.store.get_room("!uknown:test")))
  53. )
  54. @defer.inlineCallbacks
  55. def test_get_room_with_stats(self):
  56. self.assertDictContainsSubset(
  57. {
  58. "room_id": self.room.to_string(),
  59. "creator": self.u_creator.to_string(),
  60. "public": True,
  61. },
  62. (
  63. yield defer.ensureDeferred(
  64. self.store.get_room_with_stats(self.room.to_string())
  65. )
  66. ),
  67. )
  68. @defer.inlineCallbacks
  69. def test_get_room_with_stats_unknown_room(self):
  70. self.assertIsNone(
  71. (
  72. yield defer.ensureDeferred(
  73. self.store.get_room_with_stats("!uknown:test")
  74. )
  75. ),
  76. )
  77. class RoomEventsStoreTestCase(unittest.TestCase):
  78. @defer.inlineCallbacks
  79. def setUp(self):
  80. hs = setup_test_homeserver(self.addCleanup)
  81. # Room events need the full datastore, for persist_event() and
  82. # get_room_state()
  83. self.store = hs.get_datastore()
  84. self.storage = hs.get_storage()
  85. self.event_factory = hs.get_event_factory()
  86. self.room = RoomID.from_string("!abcde:test")
  87. yield defer.ensureDeferred(
  88. self.store.store_room(
  89. self.room.to_string(),
  90. room_creator_user_id="@creator:text",
  91. is_public=True,
  92. room_version=RoomVersions.V1,
  93. )
  94. )
  95. @defer.inlineCallbacks
  96. def inject_room_event(self, **kwargs):
  97. yield defer.ensureDeferred(
  98. self.storage.persistence.persist_event(
  99. self.event_factory.create_event(room_id=self.room.to_string(), **kwargs)
  100. )
  101. )
  102. @defer.inlineCallbacks
  103. def STALE_test_room_name(self):
  104. name = "A-Room-Name"
  105. yield self.inject_room_event(
  106. etype=EventTypes.Name, name=name, content={"name": name}, depth=1
  107. )
  108. state = yield defer.ensureDeferred(
  109. self.store.get_current_state(room_id=self.room.to_string())
  110. )
  111. self.assertEquals(1, len(state))
  112. self.assertObjectHasAttributes(
  113. {"type": "m.room.name", "room_id": self.room.to_string(), "name": name},
  114. state[0],
  115. )
  116. @defer.inlineCallbacks
  117. def STALE_test_room_topic(self):
  118. topic = "A place for things"
  119. yield self.inject_room_event(
  120. etype=EventTypes.Topic, topic=topic, content={"topic": topic}, depth=1
  121. )
  122. state = yield defer.ensureDeferred(
  123. self.store.get_current_state(room_id=self.room.to_string())
  124. )
  125. self.assertEquals(1, len(state))
  126. self.assertObjectHasAttributes(
  127. {"type": "m.room.topic", "room_id": self.room.to_string(), "topic": topic},
  128. state[0],
  129. )
  130. # Not testing the various 'level' methods for now because there's lots
  131. # of them and need coalescing; see JIRA SPEC-11