1
0

test_room.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 self.store.get_room(self.room.to_string())),
  48. )
  49. @defer.inlineCallbacks
  50. def test_get_room_unknown_room(self):
  51. self.assertIsNone((yield self.store.get_room("!uknown:test")),)
  52. @defer.inlineCallbacks
  53. def test_get_room_with_stats(self):
  54. self.assertDictContainsSubset(
  55. {
  56. "room_id": self.room.to_string(),
  57. "creator": self.u_creator.to_string(),
  58. "public": True,
  59. },
  60. (yield self.store.get_room_with_stats(self.room.to_string())),
  61. )
  62. @defer.inlineCallbacks
  63. def test_get_room_with_stats_unknown_room(self):
  64. self.assertIsNone((yield self.store.get_room_with_stats("!uknown:test")),)
  65. class RoomEventsStoreTestCase(unittest.TestCase):
  66. @defer.inlineCallbacks
  67. def setUp(self):
  68. hs = setup_test_homeserver(self.addCleanup)
  69. # Room events need the full datastore, for persist_event() and
  70. # get_room_state()
  71. self.store = hs.get_datastore()
  72. self.storage = hs.get_storage()
  73. self.event_factory = hs.get_event_factory()
  74. self.room = RoomID.from_string("!abcde:test")
  75. yield defer.ensureDeferred(
  76. self.store.store_room(
  77. self.room.to_string(),
  78. room_creator_user_id="@creator:text",
  79. is_public=True,
  80. room_version=RoomVersions.V1,
  81. )
  82. )
  83. @defer.inlineCallbacks
  84. def inject_room_event(self, **kwargs):
  85. yield defer.ensureDeferred(
  86. self.storage.persistence.persist_event(
  87. self.event_factory.create_event(room_id=self.room.to_string(), **kwargs)
  88. )
  89. )
  90. @defer.inlineCallbacks
  91. def STALE_test_room_name(self):
  92. name = "A-Room-Name"
  93. yield self.inject_room_event(
  94. etype=EventTypes.Name, name=name, content={"name": name}, depth=1
  95. )
  96. state = yield defer.ensureDeferred(
  97. self.store.get_current_state(room_id=self.room.to_string())
  98. )
  99. self.assertEquals(1, len(state))
  100. self.assertObjectHasAttributes(
  101. {"type": "m.room.name", "room_id": self.room.to_string(), "name": name},
  102. state[0],
  103. )
  104. @defer.inlineCallbacks
  105. def STALE_test_room_topic(self):
  106. topic = "A place for things"
  107. yield self.inject_room_event(
  108. etype=EventTypes.Topic, topic=topic, content={"topic": topic}, depth=1
  109. )
  110. state = yield defer.ensureDeferred(
  111. self.store.get_current_state(room_id=self.room.to_string())
  112. )
  113. self.assertEquals(1, len(state))
  114. self.assertObjectHasAttributes(
  115. {"type": "m.room.topic", "room_id": self.room.to_string(), "topic": topic},
  116. state[0],
  117. )
  118. # Not testing the various 'level' methods for now because there's lots
  119. # of them and need coalescing; see JIRA SPEC-11