1
0

test_directory.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. import synapse
  17. import synapse.api.errors
  18. from synapse.api.constants import EventTypes
  19. from synapse.config.room_directory import RoomDirectoryConfig
  20. from synapse.rest.client.v1 import directory, login, room
  21. from synapse.types import RoomAlias, create_requester
  22. from tests import unittest
  23. from tests.test_utils import make_awaitable
  24. class DirectoryTestCase(unittest.HomeserverTestCase):
  25. """ Tests the directory service. """
  26. def make_homeserver(self, reactor, clock):
  27. self.mock_federation = Mock()
  28. self.mock_registry = Mock()
  29. self.query_handlers = {}
  30. def register_query_handler(query_type, handler):
  31. self.query_handlers[query_type] = handler
  32. self.mock_registry.register_query_handler = register_query_handler
  33. hs = self.setup_test_homeserver(
  34. federation_client=self.mock_federation,
  35. federation_registry=self.mock_registry,
  36. )
  37. self.handler = hs.get_directory_handler()
  38. self.store = hs.get_datastore()
  39. self.my_room = RoomAlias.from_string("#my-room:test")
  40. self.your_room = RoomAlias.from_string("#your-room:test")
  41. self.remote_room = RoomAlias.from_string("#another:remote")
  42. return hs
  43. def test_get_local_association(self):
  44. self.get_success(
  45. self.store.create_room_alias_association(
  46. self.my_room, "!8765qwer:test", ["test"]
  47. )
  48. )
  49. result = self.get_success(self.handler.get_association(self.my_room))
  50. self.assertEquals({"room_id": "!8765qwer:test", "servers": ["test"]}, result)
  51. def test_get_remote_association(self):
  52. self.mock_federation.make_query.return_value = make_awaitable(
  53. {"room_id": "!8765qwer:test", "servers": ["test", "remote"]}
  54. )
  55. result = self.get_success(self.handler.get_association(self.remote_room))
  56. self.assertEquals(
  57. {"room_id": "!8765qwer:test", "servers": ["test", "remote"]}, result
  58. )
  59. self.mock_federation.make_query.assert_called_with(
  60. destination="remote",
  61. query_type="directory",
  62. args={"room_alias": "#another:remote"},
  63. retry_on_dns_fail=False,
  64. ignore_backoff=True,
  65. )
  66. def test_incoming_fed_query(self):
  67. self.get_success(
  68. self.store.create_room_alias_association(
  69. self.your_room, "!8765asdf:test", ["test"]
  70. )
  71. )
  72. response = self.get_success(
  73. self.handler.on_directory_query({"room_alias": "#your-room:test"})
  74. )
  75. self.assertEquals({"room_id": "!8765asdf:test", "servers": ["test"]}, response)
  76. class TestCreateAlias(unittest.HomeserverTestCase):
  77. servlets = [
  78. synapse.rest.admin.register_servlets,
  79. login.register_servlets,
  80. room.register_servlets,
  81. directory.register_servlets,
  82. ]
  83. def prepare(self, reactor, clock, hs):
  84. self.handler = hs.get_directory_handler()
  85. # Create user
  86. self.admin_user = self.register_user("admin", "pass", admin=True)
  87. self.admin_user_tok = self.login("admin", "pass")
  88. # Create a test room
  89. self.room_id = self.helper.create_room_as(
  90. self.admin_user, tok=self.admin_user_tok
  91. )
  92. self.test_alias = "#test:test"
  93. self.room_alias = RoomAlias.from_string(self.test_alias)
  94. # Create a test user.
  95. self.test_user = self.register_user("user", "pass", admin=False)
  96. self.test_user_tok = self.login("user", "pass")
  97. self.helper.join(room=self.room_id, user=self.test_user, tok=self.test_user_tok)
  98. def test_create_alias_joined_room(self):
  99. """A user can create an alias for a room they're in."""
  100. self.get_success(
  101. self.handler.create_association(
  102. create_requester(self.test_user), self.room_alias, self.room_id,
  103. )
  104. )
  105. def test_create_alias_other_room(self):
  106. """A user cannot create an alias for a room they're NOT in."""
  107. other_room_id = self.helper.create_room_as(
  108. self.admin_user, tok=self.admin_user_tok
  109. )
  110. self.get_failure(
  111. self.handler.create_association(
  112. create_requester(self.test_user), self.room_alias, other_room_id,
  113. ),
  114. synapse.api.errors.SynapseError,
  115. )
  116. def test_create_alias_admin(self):
  117. """An admin can create an alias for a room they're NOT in."""
  118. other_room_id = self.helper.create_room_as(
  119. self.test_user, tok=self.test_user_tok
  120. )
  121. self.get_success(
  122. self.handler.create_association(
  123. create_requester(self.admin_user), self.room_alias, other_room_id,
  124. )
  125. )
  126. class TestDeleteAlias(unittest.HomeserverTestCase):
  127. servlets = [
  128. synapse.rest.admin.register_servlets,
  129. login.register_servlets,
  130. room.register_servlets,
  131. directory.register_servlets,
  132. ]
  133. def prepare(self, reactor, clock, hs):
  134. self.store = hs.get_datastore()
  135. self.handler = hs.get_directory_handler()
  136. self.state_handler = hs.get_state_handler()
  137. # Create user
  138. self.admin_user = self.register_user("admin", "pass", admin=True)
  139. self.admin_user_tok = self.login("admin", "pass")
  140. # Create a test room
  141. self.room_id = self.helper.create_room_as(
  142. self.admin_user, tok=self.admin_user_tok
  143. )
  144. self.test_alias = "#test:test"
  145. self.room_alias = RoomAlias.from_string(self.test_alias)
  146. # Create a test user.
  147. self.test_user = self.register_user("user", "pass", admin=False)
  148. self.test_user_tok = self.login("user", "pass")
  149. self.helper.join(room=self.room_id, user=self.test_user, tok=self.test_user_tok)
  150. def _create_alias(self, user):
  151. # Create a new alias to this room.
  152. self.get_success(
  153. self.store.create_room_alias_association(
  154. self.room_alias, self.room_id, ["test"], user
  155. )
  156. )
  157. def test_delete_alias_not_allowed(self):
  158. """A user that doesn't meet the expected guidelines cannot delete an alias."""
  159. self._create_alias(self.admin_user)
  160. self.get_failure(
  161. self.handler.delete_association(
  162. create_requester(self.test_user), self.room_alias
  163. ),
  164. synapse.api.errors.AuthError,
  165. )
  166. def test_delete_alias_creator(self):
  167. """An alias creator can delete their own alias."""
  168. # Create an alias from a different user.
  169. self._create_alias(self.test_user)
  170. # Delete the user's alias.
  171. result = self.get_success(
  172. self.handler.delete_association(
  173. create_requester(self.test_user), self.room_alias
  174. )
  175. )
  176. self.assertEquals(self.room_id, result)
  177. # Confirm the alias is gone.
  178. self.get_failure(
  179. self.handler.get_association(self.room_alias),
  180. synapse.api.errors.SynapseError,
  181. )
  182. def test_delete_alias_admin(self):
  183. """A server admin can delete an alias created by another user."""
  184. # Create an alias from a different user.
  185. self._create_alias(self.test_user)
  186. # Delete the user's alias as the admin.
  187. result = self.get_success(
  188. self.handler.delete_association(
  189. create_requester(self.admin_user), self.room_alias
  190. )
  191. )
  192. self.assertEquals(self.room_id, result)
  193. # Confirm the alias is gone.
  194. self.get_failure(
  195. self.handler.get_association(self.room_alias),
  196. synapse.api.errors.SynapseError,
  197. )
  198. def test_delete_alias_sufficient_power(self):
  199. """A user with a sufficient power level should be able to delete an alias."""
  200. self._create_alias(self.admin_user)
  201. # Increase the user's power level.
  202. self.helper.send_state(
  203. self.room_id,
  204. "m.room.power_levels",
  205. {"users": {self.test_user: 100}},
  206. tok=self.admin_user_tok,
  207. )
  208. # They can now delete the alias.
  209. result = self.get_success(
  210. self.handler.delete_association(
  211. create_requester(self.test_user), self.room_alias
  212. )
  213. )
  214. self.assertEquals(self.room_id, result)
  215. # Confirm the alias is gone.
  216. self.get_failure(
  217. self.handler.get_association(self.room_alias),
  218. synapse.api.errors.SynapseError,
  219. )
  220. class CanonicalAliasTestCase(unittest.HomeserverTestCase):
  221. """Test modifications of the canonical alias when delete aliases.
  222. """
  223. servlets = [
  224. synapse.rest.admin.register_servlets,
  225. login.register_servlets,
  226. room.register_servlets,
  227. directory.register_servlets,
  228. ]
  229. def prepare(self, reactor, clock, hs):
  230. self.store = hs.get_datastore()
  231. self.handler = hs.get_directory_handler()
  232. self.state_handler = hs.get_state_handler()
  233. # Create user
  234. self.admin_user = self.register_user("admin", "pass", admin=True)
  235. self.admin_user_tok = self.login("admin", "pass")
  236. # Create a test room
  237. self.room_id = self.helper.create_room_as(
  238. self.admin_user, tok=self.admin_user_tok
  239. )
  240. self.test_alias = "#test:test"
  241. self.room_alias = self._add_alias(self.test_alias)
  242. def _add_alias(self, alias: str) -> RoomAlias:
  243. """Add an alias to the test room."""
  244. room_alias = RoomAlias.from_string(alias)
  245. # Create a new alias to this room.
  246. self.get_success(
  247. self.store.create_room_alias_association(
  248. room_alias, self.room_id, ["test"], self.admin_user
  249. )
  250. )
  251. return room_alias
  252. def _set_canonical_alias(self, content):
  253. """Configure the canonical alias state on the room."""
  254. self.helper.send_state(
  255. self.room_id, "m.room.canonical_alias", content, tok=self.admin_user_tok,
  256. )
  257. def _get_canonical_alias(self):
  258. """Get the canonical alias state of the room."""
  259. return self.get_success(
  260. self.state_handler.get_current_state(
  261. self.room_id, EventTypes.CanonicalAlias, ""
  262. )
  263. )
  264. def test_remove_alias(self):
  265. """Removing an alias that is the canonical alias should remove it there too."""
  266. # Set this new alias as the canonical alias for this room
  267. self._set_canonical_alias(
  268. {"alias": self.test_alias, "alt_aliases": [self.test_alias]}
  269. )
  270. data = self._get_canonical_alias()
  271. self.assertEqual(data["content"]["alias"], self.test_alias)
  272. self.assertEqual(data["content"]["alt_aliases"], [self.test_alias])
  273. # Finally, delete the alias.
  274. self.get_success(
  275. self.handler.delete_association(
  276. create_requester(self.admin_user), self.room_alias
  277. )
  278. )
  279. data = self._get_canonical_alias()
  280. self.assertNotIn("alias", data["content"])
  281. self.assertNotIn("alt_aliases", data["content"])
  282. def test_remove_other_alias(self):
  283. """Removing an alias listed as in alt_aliases should remove it there too."""
  284. # Create a second alias.
  285. other_test_alias = "#test2:test"
  286. other_room_alias = self._add_alias(other_test_alias)
  287. # Set the alias as the canonical alias for this room.
  288. self._set_canonical_alias(
  289. {
  290. "alias": self.test_alias,
  291. "alt_aliases": [self.test_alias, other_test_alias],
  292. }
  293. )
  294. data = self._get_canonical_alias()
  295. self.assertEqual(data["content"]["alias"], self.test_alias)
  296. self.assertEqual(
  297. data["content"]["alt_aliases"], [self.test_alias, other_test_alias]
  298. )
  299. # Delete the second alias.
  300. self.get_success(
  301. self.handler.delete_association(
  302. create_requester(self.admin_user), other_room_alias
  303. )
  304. )
  305. data = self._get_canonical_alias()
  306. self.assertEqual(data["content"]["alias"], self.test_alias)
  307. self.assertEqual(data["content"]["alt_aliases"], [self.test_alias])
  308. class TestCreateAliasACL(unittest.HomeserverTestCase):
  309. user_id = "@test:test"
  310. servlets = [directory.register_servlets, room.register_servlets]
  311. def prepare(self, reactor, clock, hs):
  312. # We cheekily override the config to add custom alias creation rules
  313. config = {}
  314. config["alias_creation_rules"] = [
  315. {"user_id": "*", "alias": "#unofficial_*", "action": "allow"}
  316. ]
  317. config["room_list_publication_rules"] = []
  318. rd_config = RoomDirectoryConfig()
  319. rd_config.read_config(config)
  320. self.hs.config.is_alias_creation_allowed = rd_config.is_alias_creation_allowed
  321. return hs
  322. def test_denied(self):
  323. room_id = self.helper.create_room_as(self.user_id)
  324. channel = self.make_request(
  325. "PUT",
  326. b"directory/room/%23test%3Atest",
  327. ('{"room_id":"%s"}' % (room_id,)).encode("ascii"),
  328. )
  329. self.assertEquals(403, channel.code, channel.result)
  330. def test_allowed(self):
  331. room_id = self.helper.create_room_as(self.user_id)
  332. channel = self.make_request(
  333. "PUT",
  334. b"directory/room/%23unofficial_test%3Atest",
  335. ('{"room_id":"%s"}' % (room_id,)).encode("ascii"),
  336. )
  337. self.assertEquals(200, channel.code, channel.result)
  338. class TestRoomListSearchDisabled(unittest.HomeserverTestCase):
  339. user_id = "@test:test"
  340. servlets = [directory.register_servlets, room.register_servlets]
  341. def prepare(self, reactor, clock, hs):
  342. room_id = self.helper.create_room_as(self.user_id)
  343. channel = self.make_request(
  344. "PUT", b"directory/list/room/%s" % (room_id.encode("ascii"),), b"{}"
  345. )
  346. self.assertEquals(200, channel.code, channel.result)
  347. self.room_list_handler = hs.get_room_list_handler()
  348. self.directory_handler = hs.get_directory_handler()
  349. return hs
  350. def test_disabling_room_list(self):
  351. self.room_list_handler.enable_room_list_search = True
  352. self.directory_handler.enable_room_list_search = True
  353. # Room list is enabled so we should get some results
  354. channel = self.make_request("GET", b"publicRooms")
  355. self.assertEquals(200, channel.code, channel.result)
  356. self.assertTrue(len(channel.json_body["chunk"]) > 0)
  357. self.room_list_handler.enable_room_list_search = False
  358. self.directory_handler.enable_room_list_search = False
  359. # Room list disabled so we should get no results
  360. channel = self.make_request("GET", b"publicRooms")
  361. self.assertEquals(200, channel.code, channel.result)
  362. self.assertTrue(len(channel.json_body["chunk"]) == 0)
  363. # Room list disabled so we shouldn't be allowed to publish rooms
  364. room_id = self.helper.create_room_as(self.user_id)
  365. channel = self.make_request(
  366. "PUT", b"directory/list/room/%s" % (room_id.encode("ascii"),), b"{}"
  367. )
  368. self.assertEquals(403, channel.code, channel.result)