1
0

test_directory.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 mock import Mock
  16. from twisted.internet import defer
  17. from synapse.config.room_directory import RoomDirectoryConfig
  18. from synapse.handlers.directory import DirectoryHandler
  19. from synapse.rest.client.v1 import directory, room
  20. from synapse.types import RoomAlias
  21. from tests import unittest
  22. from tests.utils import setup_test_homeserver
  23. class DirectoryHandlers(object):
  24. def __init__(self, hs):
  25. self.directory_handler = DirectoryHandler(hs)
  26. class DirectoryTestCase(unittest.TestCase):
  27. """ Tests the directory service. """
  28. @defer.inlineCallbacks
  29. def setUp(self):
  30. self.mock_federation = Mock()
  31. self.mock_registry = Mock()
  32. self.query_handlers = {}
  33. def register_query_handler(query_type, handler):
  34. self.query_handlers[query_type] = handler
  35. self.mock_registry.register_query_handler = register_query_handler
  36. hs = yield setup_test_homeserver(
  37. self.addCleanup,
  38. http_client=None,
  39. resource_for_federation=Mock(),
  40. federation_client=self.mock_federation,
  41. federation_registry=self.mock_registry,
  42. )
  43. hs.handlers = DirectoryHandlers(hs)
  44. self.handler = hs.get_handlers().directory_handler
  45. self.store = hs.get_datastore()
  46. self.my_room = RoomAlias.from_string("#my-room:test")
  47. self.your_room = RoomAlias.from_string("#your-room:test")
  48. self.remote_room = RoomAlias.from_string("#another:remote")
  49. @defer.inlineCallbacks
  50. def test_get_local_association(self):
  51. yield self.store.create_room_alias_association(
  52. self.my_room, "!8765qwer:test", ["test"]
  53. )
  54. result = yield self.handler.get_association(self.my_room)
  55. self.assertEquals({"room_id": "!8765qwer:test", "servers": ["test"]}, result)
  56. @defer.inlineCallbacks
  57. def test_get_remote_association(self):
  58. self.mock_federation.make_query.return_value = defer.succeed(
  59. {"room_id": "!8765qwer:test", "servers": ["test", "remote"]}
  60. )
  61. result = yield self.handler.get_association(self.remote_room)
  62. self.assertEquals(
  63. {"room_id": "!8765qwer:test", "servers": ["test", "remote"]}, result
  64. )
  65. self.mock_federation.make_query.assert_called_with(
  66. destination="remote",
  67. query_type="directory",
  68. args={"room_alias": "#another:remote"},
  69. retry_on_dns_fail=False,
  70. ignore_backoff=True,
  71. )
  72. @defer.inlineCallbacks
  73. def test_incoming_fed_query(self):
  74. yield self.store.create_room_alias_association(
  75. self.your_room, "!8765asdf:test", ["test"]
  76. )
  77. response = yield self.query_handlers["directory"](
  78. {"room_alias": "#your-room:test"}
  79. )
  80. self.assertEquals({"room_id": "!8765asdf:test", "servers": ["test"]}, response)
  81. class TestCreateAliasACL(unittest.HomeserverTestCase):
  82. user_id = "@test:test"
  83. servlets = [directory.register_servlets, room.register_servlets]
  84. def prepare(self, reactor, clock, hs):
  85. # We cheekily override the config to add custom alias creation rules
  86. config = {}
  87. config["alias_creation_rules"] = [
  88. {"user_id": "*", "alias": "#unofficial_*", "action": "allow"}
  89. ]
  90. config["room_list_publication_rules"] = []
  91. rd_config = RoomDirectoryConfig()
  92. rd_config.read_config(config)
  93. self.hs.config.is_alias_creation_allowed = rd_config.is_alias_creation_allowed
  94. return hs
  95. def test_denied(self):
  96. room_id = self.helper.create_room_as(self.user_id)
  97. request, channel = self.make_request(
  98. "PUT",
  99. b"directory/room/%23test%3Atest",
  100. ('{"room_id":"%s"}' % (room_id,)).encode("ascii"),
  101. )
  102. self.render(request)
  103. self.assertEquals(403, channel.code, channel.result)
  104. def test_allowed(self):
  105. room_id = self.helper.create_room_as(self.user_id)
  106. request, channel = self.make_request(
  107. "PUT",
  108. b"directory/room/%23unofficial_test%3Atest",
  109. ('{"room_id":"%s"}' % (room_id,)).encode("ascii"),
  110. )
  111. self.render(request)
  112. self.assertEquals(200, channel.code, channel.result)
  113. class TestRoomListSearchDisabled(unittest.HomeserverTestCase):
  114. user_id = "@test:test"
  115. servlets = [directory.register_servlets, room.register_servlets]
  116. def prepare(self, reactor, clock, hs):
  117. room_id = self.helper.create_room_as(self.user_id)
  118. request, channel = self.make_request(
  119. "PUT", b"directory/list/room/%s" % (room_id.encode("ascii"),), b"{}"
  120. )
  121. self.render(request)
  122. self.assertEquals(200, channel.code, channel.result)
  123. self.room_list_handler = hs.get_room_list_handler()
  124. self.directory_handler = hs.get_handlers().directory_handler
  125. return hs
  126. def test_disabling_room_list(self):
  127. self.room_list_handler.enable_room_list_search = True
  128. self.directory_handler.enable_room_list_search = True
  129. # Room list is enabled so we should get some results
  130. request, channel = self.make_request("GET", b"publicRooms")
  131. self.render(request)
  132. self.assertEquals(200, channel.code, channel.result)
  133. self.assertTrue(len(channel.json_body["chunk"]) > 0)
  134. self.room_list_handler.enable_room_list_search = False
  135. self.directory_handler.enable_room_list_search = False
  136. # Room list disabled so we should get no results
  137. request, channel = self.make_request("GET", b"publicRooms")
  138. self.render(request)
  139. self.assertEquals(200, channel.code, channel.result)
  140. self.assertTrue(len(channel.json_body["chunk"]) == 0)
  141. # Room list disabled so we shouldn't be allowed to publish rooms
  142. room_id = self.helper.create_room_as(self.user_id)
  143. request, channel = self.make_request(
  144. "PUT", b"directory/list/room/%s" % (room_id.encode("ascii"),), b"{}"
  145. )
  146. self.render(request)
  147. self.assertEquals(403, channel.code, channel.result)