test_directory.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.handlers.directory import DirectoryHandler
  18. from synapse.types import RoomAlias
  19. from tests import unittest
  20. from tests.utils import setup_test_homeserver
  21. class DirectoryHandlers(object):
  22. def __init__(self, hs):
  23. self.directory_handler = DirectoryHandler(hs)
  24. class DirectoryTestCase(unittest.TestCase):
  25. """ Tests the directory service. """
  26. @defer.inlineCallbacks
  27. def setUp(self):
  28. self.mock_federation = Mock()
  29. self.mock_registry = Mock()
  30. self.query_handlers = {}
  31. def register_query_handler(query_type, handler):
  32. self.query_handlers[query_type] = handler
  33. self.mock_registry.register_query_handler = register_query_handler
  34. hs = yield setup_test_homeserver(
  35. http_client=None,
  36. resource_for_federation=Mock(),
  37. federation_client=self.mock_federation,
  38. federation_registry=self.mock_registry,
  39. )
  40. hs.handlers = DirectoryHandlers(hs)
  41. self.handler = hs.get_handlers().directory_handler
  42. self.store = hs.get_datastore()
  43. self.my_room = RoomAlias.from_string("#my-room:test")
  44. self.your_room = RoomAlias.from_string("#your-room:test")
  45. self.remote_room = RoomAlias.from_string("#another:remote")
  46. @defer.inlineCallbacks
  47. def test_get_local_association(self):
  48. yield self.store.create_room_alias_association(
  49. self.my_room, "!8765qwer:test", ["test"]
  50. )
  51. result = yield self.handler.get_association(self.my_room)
  52. self.assertEquals({
  53. "room_id": "!8765qwer:test",
  54. "servers": ["test"],
  55. }, 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",
  64. "servers": ["test", "remote"],
  65. }, result)
  66. self.mock_federation.make_query.assert_called_with(
  67. destination="remote",
  68. query_type="directory",
  69. args={
  70. "room_alias": "#another:remote",
  71. },
  72. retry_on_dns_fail=False,
  73. ignore_backoff=True,
  74. )
  75. @defer.inlineCallbacks
  76. def test_incoming_fed_query(self):
  77. yield self.store.create_room_alias_association(
  78. self.your_room, "!8765asdf:test", ["test"]
  79. )
  80. response = yield self.query_handlers["directory"](
  81. {"room_alias": "#your-room:test"}
  82. )
  83. self.assertEquals({
  84. "room_id": "!8765asdf:test",
  85. "servers": ["test"],
  86. }, response)