test_directory.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright 2014-2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.types import RoomAlias, RoomID
  15. from tests.unittest import HomeserverTestCase
  16. class DirectoryStoreTestCase(HomeserverTestCase):
  17. def prepare(self, reactor, clock, hs):
  18. self.store = hs.get_datastores().main
  19. self.room = RoomID.from_string("!abcde:test")
  20. self.alias = RoomAlias.from_string("#my-room:test")
  21. def test_room_to_alias(self):
  22. self.get_success(
  23. self.store.create_room_alias_association(
  24. room_alias=self.alias, room_id=self.room.to_string(), servers=["test"]
  25. )
  26. )
  27. self.assertEqual(
  28. ["#my-room:test"],
  29. (self.get_success(self.store.get_aliases_for_room(self.room.to_string()))),
  30. )
  31. def test_alias_to_room(self):
  32. self.get_success(
  33. self.store.create_room_alias_association(
  34. room_alias=self.alias, room_id=self.room.to_string(), servers=["test"]
  35. )
  36. )
  37. self.assertObjectHasAttributes(
  38. {"room_id": self.room.to_string(), "servers": ["test"]},
  39. (self.get_success(self.store.get_association_from_room_alias(self.alias))),
  40. )
  41. def test_delete_alias(self):
  42. self.get_success(
  43. self.store.create_room_alias_association(
  44. room_alias=self.alias, room_id=self.room.to_string(), servers=["test"]
  45. )
  46. )
  47. room_id = self.get_success(self.store.delete_room_alias(self.alias))
  48. self.assertEqual(self.room.to_string(), room_id)
  49. self.assertIsNone(
  50. self.get_success(self.store.get_association_from_room_alias(self.alias))
  51. )