test_directory.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 New Vector 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. import json
  16. from synapse.rest import admin
  17. from synapse.rest.client.v1 import directory, login, room
  18. from synapse.types import RoomAlias
  19. from synapse.util.stringutils import random_string
  20. from tests import unittest
  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. def test_state_event_in_room(self):
  54. self.ensure_user_joined_room()
  55. self.set_alias_via_state_event(200)
  56. def test_directory_in_room(self):
  57. self.ensure_user_joined_room()
  58. self.set_alias_via_directory(200)
  59. def test_room_creation_too_long(self):
  60. url = "/_matrix/client/r0/createRoom"
  61. # We use deliberately a localpart under the length threshold so
  62. # that we can make sure that the check is done on the whole alias.
  63. data = {"room_alias_name": random_string(256 - len(self.hs.hostname))}
  64. request_data = json.dumps(data)
  65. request, channel = self.make_request(
  66. "POST", url, request_data, access_token=self.user_tok
  67. )
  68. self.render(request)
  69. self.assertEqual(channel.code, 400, channel.result)
  70. def test_room_creation(self):
  71. url = "/_matrix/client/r0/createRoom"
  72. # Check with an alias of allowed length. There should already be
  73. # a test that ensures it works in test_register.py, but let's be
  74. # as cautious as possible here.
  75. data = {"room_alias_name": random_string(5)}
  76. request_data = json.dumps(data)
  77. request, channel = self.make_request(
  78. "POST", url, request_data, access_token=self.user_tok
  79. )
  80. self.render(request)
  81. self.assertEqual(channel.code, 200, channel.result)
  82. def set_alias_via_state_event(self, expected_code, alias_length=5):
  83. url = "/_matrix/client/r0/rooms/%s/state/m.room.aliases/%s" % (
  84. self.room_id,
  85. self.hs.hostname,
  86. )
  87. data = {"aliases": [self.random_alias(alias_length)]}
  88. request_data = json.dumps(data)
  89. request, channel = self.make_request(
  90. "PUT", url, request_data, access_token=self.user_tok
  91. )
  92. self.render(request)
  93. self.assertEqual(channel.code, expected_code, channel.result)
  94. def set_alias_via_directory(self, expected_code, alias_length=5):
  95. url = "/_matrix/client/r0/directory/room/%s" % self.random_alias(alias_length)
  96. data = {"room_id": self.room_id}
  97. request_data = json.dumps(data)
  98. request, channel = self.make_request(
  99. "PUT", url, request_data, access_token=self.user_tok
  100. )
  101. self.render(request)
  102. self.assertEqual(channel.code, expected_code, channel.result)
  103. def random_alias(self, length):
  104. return RoomAlias(random_string(length), self.hs.hostname).to_string()
  105. def ensure_user_left_room(self):
  106. self.ensure_membership("leave")
  107. def ensure_user_joined_room(self):
  108. self.ensure_membership("join")
  109. def ensure_membership(self, membership):
  110. try:
  111. if membership == "leave":
  112. self.helper.leave(room=self.room_id, user=self.user, tok=self.user_tok)
  113. if membership == "join":
  114. self.helper.join(room=self.room_id, user=self.user, tok=self.user_tok)
  115. except AssertionError:
  116. # We don't care whether the leave request didn't return a 200 (e.g.
  117. # if the user isn't already in the room), because we only want to
  118. # make sure the user isn't in the room.
  119. pass