1
0

test_room.py 4.5 KB

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