test_directory.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "register_edu_handler",
  31. ])
  32. self.query_handlers = {}
  33. def register_query_handler(query_type, handler):
  34. self.query_handlers[query_type] = handler
  35. self.mock_federation.register_query_handler = register_query_handler
  36. hs = yield setup_test_homeserver(
  37. http_client=None,
  38. resource_for_federation=Mock(),
  39. replication_layer=self.mock_federation,
  40. )
  41. hs.handlers = DirectoryHandlers(hs)
  42. self.handler = hs.get_handlers().directory_handler
  43. self.store = hs.get_datastore()
  44. self.my_room = RoomAlias.from_string("#my-room:test")
  45. self.your_room = RoomAlias.from_string("#your-room:test")
  46. self.remote_room = RoomAlias.from_string("#another:remote")
  47. @defer.inlineCallbacks
  48. def test_get_local_association(self):
  49. yield self.store.create_room_alias_association(
  50. self.my_room, "!8765qwer:test", ["test"]
  51. )
  52. result = yield self.handler.get_association(self.my_room)
  53. self.assertEquals({
  54. "room_id": "!8765qwer:test",
  55. "servers": ["test"],
  56. }, result)
  57. @defer.inlineCallbacks
  58. def test_get_remote_association(self):
  59. self.mock_federation.make_query.return_value = defer.succeed(
  60. {"room_id": "!8765qwer:test", "servers": ["test", "remote"]}
  61. )
  62. result = yield self.handler.get_association(self.remote_room)
  63. self.assertEquals({
  64. "room_id": "!8765qwer:test",
  65. "servers": ["test", "remote"],
  66. }, result)
  67. self.mock_federation.make_query.assert_called_with(
  68. destination="remote",
  69. query_type="directory",
  70. args={
  71. "room_alias": "#another:remote",
  72. },
  73. retry_on_dns_fail=False,
  74. ignore_backoff=True,
  75. )
  76. @defer.inlineCallbacks
  77. def test_incoming_fed_query(self):
  78. yield self.store.create_room_alias_association(
  79. self.your_room, "!8765asdf:test", ["test"]
  80. )
  81. response = yield self.query_handlers["directory"](
  82. {"room_alias": "#your-room:test"}
  83. )
  84. self.assertEquals({
  85. "room_id": "!8765asdf:test",
  86. "servers": ["test"],
  87. }, response)