test_directory.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 tests import unittest
  16. from twisted.internet import defer
  17. from mock import Mock
  18. from synapse.handlers.directory import DirectoryHandler
  19. from synapse.types import RoomAlias
  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(spec=[
  29. "make_query",
  30. ])
  31. self.query_handlers = {}
  32. def register_query_handler(query_type, handler):
  33. self.query_handlers[query_type] = handler
  34. self.mock_federation.register_query_handler = register_query_handler
  35. hs = yield setup_test_homeserver(
  36. http_client=None,
  37. resource_for_federation=Mock(),
  38. replication_layer=self.mock_federation,
  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. )
  74. @defer.inlineCallbacks
  75. def test_incoming_fed_query(self):
  76. yield self.store.create_room_alias_association(
  77. self.your_room, "!8765asdf:test", ["test"]
  78. )
  79. response = yield self.query_handlers["directory"](
  80. {"room_alias": "#your-room:test"}
  81. )
  82. self.assertEquals({
  83. "room_id": "!8765asdf:test",
  84. "servers": ["test"],
  85. }, response)