test_directory.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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, hs, reactor, clock):
  85. # We cheekily override the config to add custom alias creation rules
  86. config = {}
  87. config["alias_creation_rules"] = [
  88. {
  89. "user_id": "*",
  90. "alias": "#unofficial_*",
  91. "action": "allow",
  92. }
  93. ]
  94. rd_config = RoomDirectoryConfig()
  95. rd_config.read_config(config)
  96. self.hs.config.is_alias_creation_allowed = rd_config.is_alias_creation_allowed
  97. return hs
  98. def test_denied(self):
  99. room_id = self.helper.create_room_as(self.user_id)
  100. request, channel = self.make_request(
  101. "PUT",
  102. b"directory/room/%23test%3Atest",
  103. ('{"room_id":"%s"}' % (room_id,)).encode('ascii'),
  104. )
  105. self.render(request)
  106. self.assertEquals(403, channel.code, channel.result)
  107. def test_allowed(self):
  108. room_id = self.helper.create_room_as(self.user_id)
  109. request, channel = self.make_request(
  110. "PUT",
  111. b"directory/room/%23unofficial_test%3Atest",
  112. ('{"room_id":"%s"}' % (room_id,)).encode('ascii'),
  113. )
  114. self.render(request)
  115. self.assertEquals(200, channel.code, channel.result)