test_profile.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. # Copyright 2014-2016 OpenMarket 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. """Tests REST events for /profile paths."""
  15. from synapse.rest import admin
  16. from synapse.rest.client import login, profile, room
  17. from tests import unittest
  18. class ProfileTestCase(unittest.HomeserverTestCase):
  19. servlets = [
  20. admin.register_servlets_for_client_rest_resource,
  21. login.register_servlets,
  22. profile.register_servlets,
  23. ]
  24. def make_homeserver(self, reactor, clock):
  25. self.hs = self.setup_test_homeserver()
  26. return self.hs
  27. def prepare(self, reactor, clock, hs):
  28. self.owner = self.register_user("owner", "pass")
  29. self.owner_tok = self.login("owner", "pass")
  30. self.other = self.register_user("other", "pass", displayname="Bob")
  31. def test_get_displayname(self):
  32. res = self._get_displayname()
  33. self.assertEqual(res, "owner")
  34. def test_set_displayname(self):
  35. channel = self.make_request(
  36. "PUT",
  37. "/profile/%s/displayname" % (self.owner,),
  38. content={"displayname": "test"},
  39. access_token=self.owner_tok,
  40. )
  41. self.assertEqual(channel.code, 200, channel.result)
  42. res = self._get_displayname()
  43. self.assertEqual(res, "test")
  44. def test_set_displayname_noauth(self):
  45. channel = self.make_request(
  46. "PUT",
  47. "/profile/%s/displayname" % (self.owner,),
  48. content={"displayname": "test"},
  49. )
  50. self.assertEqual(channel.code, 401, channel.result)
  51. def test_set_displayname_too_long(self):
  52. """Attempts to set a stupid displayname should get a 400"""
  53. channel = self.make_request(
  54. "PUT",
  55. "/profile/%s/displayname" % (self.owner,),
  56. content={"displayname": "test" * 100},
  57. access_token=self.owner_tok,
  58. )
  59. self.assertEqual(channel.code, 400, channel.result)
  60. res = self._get_displayname()
  61. self.assertEqual(res, "owner")
  62. def test_get_displayname_other(self):
  63. res = self._get_displayname(self.other)
  64. self.assertEquals(res, "Bob")
  65. def test_set_displayname_other(self):
  66. channel = self.make_request(
  67. "PUT",
  68. "/profile/%s/displayname" % (self.other,),
  69. content={"displayname": "test"},
  70. access_token=self.owner_tok,
  71. )
  72. self.assertEqual(channel.code, 400, channel.result)
  73. def test_get_avatar_url(self):
  74. res = self._get_avatar_url()
  75. self.assertIsNone(res)
  76. def test_set_avatar_url(self):
  77. channel = self.make_request(
  78. "PUT",
  79. "/profile/%s/avatar_url" % (self.owner,),
  80. content={"avatar_url": "http://my.server/pic.gif"},
  81. access_token=self.owner_tok,
  82. )
  83. self.assertEqual(channel.code, 200, channel.result)
  84. res = self._get_avatar_url()
  85. self.assertEqual(res, "http://my.server/pic.gif")
  86. def test_set_avatar_url_noauth(self):
  87. channel = self.make_request(
  88. "PUT",
  89. "/profile/%s/avatar_url" % (self.owner,),
  90. content={"avatar_url": "http://my.server/pic.gif"},
  91. )
  92. self.assertEqual(channel.code, 401, channel.result)
  93. def test_set_avatar_url_too_long(self):
  94. """Attempts to set a stupid avatar_url should get a 400"""
  95. channel = self.make_request(
  96. "PUT",
  97. "/profile/%s/avatar_url" % (self.owner,),
  98. content={"avatar_url": "http://my.server/pic.gif" * 100},
  99. access_token=self.owner_tok,
  100. )
  101. self.assertEqual(channel.code, 400, channel.result)
  102. res = self._get_avatar_url()
  103. self.assertIsNone(res)
  104. def test_get_avatar_url_other(self):
  105. res = self._get_avatar_url(self.other)
  106. self.assertIsNone(res)
  107. def test_set_avatar_url_other(self):
  108. channel = self.make_request(
  109. "PUT",
  110. "/profile/%s/avatar_url" % (self.other,),
  111. content={"avatar_url": "http://my.server/pic.gif"},
  112. access_token=self.owner_tok,
  113. )
  114. self.assertEqual(channel.code, 400, channel.result)
  115. def _get_displayname(self, name=None):
  116. channel = self.make_request(
  117. "GET", "/profile/%s/displayname" % (name or self.owner,)
  118. )
  119. self.assertEqual(channel.code, 200, channel.result)
  120. return channel.json_body["displayname"]
  121. def _get_avatar_url(self, name=None):
  122. channel = self.make_request(
  123. "GET", "/profile/%s/avatar_url" % (name or self.owner,)
  124. )
  125. self.assertEqual(channel.code, 200, channel.result)
  126. return channel.json_body.get("avatar_url")
  127. class ProfilesRestrictedTestCase(unittest.HomeserverTestCase):
  128. servlets = [
  129. admin.register_servlets_for_client_rest_resource,
  130. login.register_servlets,
  131. profile.register_servlets,
  132. room.register_servlets,
  133. ]
  134. def make_homeserver(self, reactor, clock):
  135. config = self.default_config()
  136. config["require_auth_for_profile_requests"] = True
  137. config["limit_profile_requests_to_users_who_share_rooms"] = True
  138. self.hs = self.setup_test_homeserver(config=config)
  139. return self.hs
  140. def prepare(self, reactor, clock, hs):
  141. # User owning the requested profile.
  142. self.owner = self.register_user("owner", "pass")
  143. self.owner_tok = self.login("owner", "pass")
  144. self.profile_url = "/profile/%s" % (self.owner)
  145. # User requesting the profile.
  146. self.requester = self.register_user("requester", "pass")
  147. self.requester_tok = self.login("requester", "pass")
  148. self.room_id = self.helper.create_room_as(self.owner, tok=self.owner_tok)
  149. def test_no_auth(self):
  150. self.try_fetch_profile(401)
  151. def test_not_in_shared_room(self):
  152. self.ensure_requester_left_room()
  153. self.try_fetch_profile(403, access_token=self.requester_tok)
  154. def test_in_shared_room(self):
  155. self.ensure_requester_left_room()
  156. self.helper.join(room=self.room_id, user=self.requester, tok=self.requester_tok)
  157. self.try_fetch_profile(200, self.requester_tok)
  158. def try_fetch_profile(self, expected_code, access_token=None):
  159. self.request_profile(expected_code, access_token=access_token)
  160. self.request_profile(
  161. expected_code, url_suffix="/displayname", access_token=access_token
  162. )
  163. self.request_profile(
  164. expected_code, url_suffix="/avatar_url", access_token=access_token
  165. )
  166. def request_profile(self, expected_code, url_suffix="", access_token=None):
  167. channel = self.make_request(
  168. "GET", self.profile_url + url_suffix, access_token=access_token
  169. )
  170. self.assertEqual(channel.code, expected_code, channel.result)
  171. def ensure_requester_left_room(self):
  172. try:
  173. self.helper.leave(
  174. room=self.room_id, user=self.requester, tok=self.requester_tok
  175. )
  176. except AssertionError:
  177. # We don't care whether the leave request didn't return a 200 (e.g.
  178. # if the user isn't already in the room), because we only want to
  179. # make sure the user isn't in the room.
  180. pass
  181. class OwnProfileUnrestrictedTestCase(unittest.HomeserverTestCase):
  182. servlets = [
  183. admin.register_servlets_for_client_rest_resource,
  184. login.register_servlets,
  185. profile.register_servlets,
  186. ]
  187. def make_homeserver(self, reactor, clock):
  188. config = self.default_config()
  189. config["require_auth_for_profile_requests"] = True
  190. config["limit_profile_requests_to_users_who_share_rooms"] = True
  191. self.hs = self.setup_test_homeserver(config=config)
  192. return self.hs
  193. def prepare(self, reactor, clock, hs):
  194. # User requesting the profile.
  195. self.requester = self.register_user("requester", "pass")
  196. self.requester_tok = self.login("requester", "pass")
  197. def test_can_lookup_own_profile(self):
  198. """Tests that a user can lookup their own profile without having to be in a room
  199. if 'require_auth_for_profile_requests' is set to true in the server's config.
  200. """
  201. channel = self.make_request(
  202. "GET", "/profile/" + self.requester, access_token=self.requester_tok
  203. )
  204. self.assertEqual(channel.code, 200, channel.result)
  205. channel = self.make_request(
  206. "GET",
  207. "/profile/" + self.requester + "/displayname",
  208. access_token=self.requester_tok,
  209. )
  210. self.assertEqual(channel.code, 200, channel.result)
  211. channel = self.make_request(
  212. "GET",
  213. "/profile/" + self.requester + "/avatar_url",
  214. access_token=self.requester_tok,
  215. )
  216. self.assertEqual(channel.code, 200, channel.result)