test_directory.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright 2019 New Vector Ltd
  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. import json
  15. from synapse.rest import admin
  16. from synapse.rest.client import directory, login, room
  17. from synapse.types import RoomAlias
  18. from synapse.util.stringutils import random_string
  19. from tests import unittest
  20. from tests.unittest import override_config
  21. class DirectoryTestCase(unittest.HomeserverTestCase):
  22. servlets = [
  23. admin.register_servlets_for_client_rest_resource,
  24. directory.register_servlets,
  25. login.register_servlets,
  26. room.register_servlets,
  27. ]
  28. def make_homeserver(self, reactor, clock):
  29. config = self.default_config()
  30. config["require_membership_for_aliases"] = True
  31. self.hs = self.setup_test_homeserver(config=config)
  32. return self.hs
  33. def prepare(self, reactor, clock, homeserver):
  34. self.room_owner = self.register_user("room_owner", "test")
  35. self.room_owner_tok = self.login("room_owner", "test")
  36. self.room_id = self.helper.create_room_as(
  37. self.room_owner, tok=self.room_owner_tok
  38. )
  39. self.user = self.register_user("user", "test")
  40. self.user_tok = self.login("user", "test")
  41. def test_state_event_not_in_room(self):
  42. self.ensure_user_left_room()
  43. self.set_alias_via_state_event(403)
  44. def test_directory_endpoint_not_in_room(self):
  45. self.ensure_user_left_room()
  46. self.set_alias_via_directory(403)
  47. def test_state_event_in_room_too_long(self):
  48. self.ensure_user_joined_room()
  49. self.set_alias_via_state_event(400, alias_length=256)
  50. def test_directory_in_room_too_long(self):
  51. self.ensure_user_joined_room()
  52. self.set_alias_via_directory(400, alias_length=256)
  53. @override_config({"default_room_version": 5})
  54. def test_state_event_user_in_v5_room(self):
  55. """Test that a regular user can add alias events before room v6"""
  56. self.ensure_user_joined_room()
  57. self.set_alias_via_state_event(200)
  58. @override_config({"default_room_version": 6})
  59. def test_state_event_v6_room(self):
  60. """Test that a regular user can *not* add alias events from room v6"""
  61. self.ensure_user_joined_room()
  62. self.set_alias_via_state_event(403)
  63. def test_directory_in_room(self):
  64. self.ensure_user_joined_room()
  65. self.set_alias_via_directory(200)
  66. def test_room_creation_too_long(self):
  67. url = "/_matrix/client/r0/createRoom"
  68. # We use deliberately a localpart under the length threshold so
  69. # that we can make sure that the check is done on the whole alias.
  70. data = {"room_alias_name": random_string(256 - len(self.hs.hostname))}
  71. request_data = json.dumps(data)
  72. channel = self.make_request(
  73. "POST", url, request_data, access_token=self.user_tok
  74. )
  75. self.assertEqual(channel.code, 400, channel.result)
  76. def test_room_creation(self):
  77. url = "/_matrix/client/r0/createRoom"
  78. # Check with an alias of allowed length. There should already be
  79. # a test that ensures it works in test_register.py, but let's be
  80. # as cautious as possible here.
  81. data = {"room_alias_name": random_string(5)}
  82. request_data = json.dumps(data)
  83. channel = self.make_request(
  84. "POST", url, request_data, access_token=self.user_tok
  85. )
  86. self.assertEqual(channel.code, 200, channel.result)
  87. def set_alias_via_state_event(self, expected_code, alias_length=5):
  88. url = "/_matrix/client/r0/rooms/%s/state/m.room.aliases/%s" % (
  89. self.room_id,
  90. self.hs.hostname,
  91. )
  92. data = {"aliases": [self.random_alias(alias_length)]}
  93. request_data = json.dumps(data)
  94. channel = self.make_request(
  95. "PUT", url, request_data, access_token=self.user_tok
  96. )
  97. self.assertEqual(channel.code, expected_code, channel.result)
  98. def set_alias_via_directory(self, expected_code, alias_length=5):
  99. url = "/_matrix/client/r0/directory/room/%s" % self.random_alias(alias_length)
  100. data = {"room_id": self.room_id}
  101. request_data = json.dumps(data)
  102. channel = self.make_request(
  103. "PUT", url, request_data, access_token=self.user_tok
  104. )
  105. self.assertEqual(channel.code, expected_code, channel.result)
  106. def random_alias(self, length):
  107. return RoomAlias(random_string(length), self.hs.hostname).to_string()
  108. def ensure_user_left_room(self):
  109. self.ensure_membership("leave")
  110. def ensure_user_joined_room(self):
  111. self.ensure_membership("join")
  112. def ensure_membership(self, membership):
  113. try:
  114. if membership == "leave":
  115. self.helper.leave(room=self.room_id, user=self.user, tok=self.user_tok)
  116. if membership == "join":
  117. self.helper.join(room=self.room_id, user=self.user, tok=self.user_tok)
  118. except AssertionError:
  119. # We don't care whether the leave request didn't return a 200 (e.g.
  120. # if the user isn't already in the room), because we only want to
  121. # make sure the user isn't in the room.
  122. pass