test_profile.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. """Tests REST events for /profile paths."""
  16. import json
  17. from mock import Mock
  18. from twisted.internet import defer
  19. import synapse.types
  20. from synapse.api.errors import AuthError, SynapseError
  21. from synapse.rest import admin
  22. from synapse.rest.client.v1 import login, profile, room
  23. from tests import unittest
  24. from ....utils import MockHttpResource, setup_test_homeserver
  25. myid = "@1234ABCD:test"
  26. PATH_PREFIX = "/_matrix/client/r0"
  27. class MockHandlerProfileTestCase(unittest.TestCase):
  28. """ Tests rest layer of profile management.
  29. Todo: move these into ProfileTestCase
  30. """
  31. @defer.inlineCallbacks
  32. def setUp(self):
  33. self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
  34. self.mock_handler = Mock(
  35. spec=[
  36. "get_displayname",
  37. "set_displayname",
  38. "get_avatar_url",
  39. "set_avatar_url",
  40. "check_profile_query_allowed",
  41. ]
  42. )
  43. self.mock_handler.get_displayname.return_value = defer.succeed(Mock())
  44. self.mock_handler.set_displayname.return_value = defer.succeed(Mock())
  45. self.mock_handler.get_avatar_url.return_value = defer.succeed(Mock())
  46. self.mock_handler.set_avatar_url.return_value = defer.succeed(Mock())
  47. self.mock_handler.check_profile_query_allowed.return_value = defer.succeed(
  48. Mock()
  49. )
  50. hs = yield setup_test_homeserver(
  51. self.addCleanup,
  52. "test",
  53. http_client=None,
  54. resource_for_client=self.mock_resource,
  55. federation=Mock(),
  56. federation_client=Mock(),
  57. profile_handler=self.mock_handler,
  58. )
  59. def _get_user_by_req(request=None, allow_guest=False):
  60. return defer.succeed(synapse.types.create_requester(myid))
  61. hs.get_auth().get_user_by_req = _get_user_by_req
  62. profile.register_servlets(hs, self.mock_resource)
  63. @defer.inlineCallbacks
  64. def test_get_my_name(self):
  65. mocked_get = self.mock_handler.get_displayname
  66. mocked_get.return_value = defer.succeed("Frank")
  67. (code, response) = yield self.mock_resource.trigger(
  68. "GET", "/profile/%s/displayname" % (myid), None
  69. )
  70. self.assertEquals(200, code)
  71. self.assertEquals({"displayname": "Frank"}, response)
  72. self.assertEquals(mocked_get.call_args[0][0].localpart, "1234ABCD")
  73. @defer.inlineCallbacks
  74. def test_set_my_name(self):
  75. mocked_set = self.mock_handler.set_displayname
  76. mocked_set.return_value = defer.succeed(())
  77. (code, response) = yield self.mock_resource.trigger(
  78. "PUT", "/profile/%s/displayname" % (myid), b'{"displayname": "Frank Jr."}'
  79. )
  80. self.assertEquals(200, code)
  81. self.assertEquals(mocked_set.call_args[0][0].localpart, "1234ABCD")
  82. self.assertEquals(mocked_set.call_args[0][1].user.localpart, "1234ABCD")
  83. self.assertEquals(mocked_set.call_args[0][2], "Frank Jr.")
  84. @defer.inlineCallbacks
  85. def test_set_my_name_noauth(self):
  86. mocked_set = self.mock_handler.set_displayname
  87. mocked_set.side_effect = AuthError(400, "message")
  88. (code, response) = yield self.mock_resource.trigger(
  89. "PUT",
  90. "/profile/%s/displayname" % ("@4567:test"),
  91. b'{"displayname": "Frank Jr."}',
  92. )
  93. self.assertTrue(400 <= code < 499, msg="code %d is in the 4xx range" % (code))
  94. @defer.inlineCallbacks
  95. def test_get_other_name(self):
  96. mocked_get = self.mock_handler.get_displayname
  97. mocked_get.return_value = defer.succeed("Bob")
  98. (code, response) = yield self.mock_resource.trigger(
  99. "GET", "/profile/%s/displayname" % ("@opaque:elsewhere"), None
  100. )
  101. self.assertEquals(200, code)
  102. self.assertEquals({"displayname": "Bob"}, response)
  103. @defer.inlineCallbacks
  104. def test_set_other_name(self):
  105. mocked_set = self.mock_handler.set_displayname
  106. mocked_set.side_effect = SynapseError(400, "message")
  107. (code, response) = yield self.mock_resource.trigger(
  108. "PUT",
  109. "/profile/%s/displayname" % ("@opaque:elsewhere"),
  110. b'{"displayname":"bob"}',
  111. )
  112. self.assertTrue(400 <= code <= 499, msg="code %d is in the 4xx range" % (code))
  113. @defer.inlineCallbacks
  114. def test_get_my_avatar(self):
  115. mocked_get = self.mock_handler.get_avatar_url
  116. mocked_get.return_value = defer.succeed("http://my.server/me.png")
  117. (code, response) = yield self.mock_resource.trigger(
  118. "GET", "/profile/%s/avatar_url" % (myid), None
  119. )
  120. self.assertEquals(200, code)
  121. self.assertEquals({"avatar_url": "http://my.server/me.png"}, response)
  122. self.assertEquals(mocked_get.call_args[0][0].localpart, "1234ABCD")
  123. @defer.inlineCallbacks
  124. def test_set_my_avatar(self):
  125. mocked_set = self.mock_handler.set_avatar_url
  126. mocked_set.return_value = defer.succeed(())
  127. (code, response) = yield self.mock_resource.trigger(
  128. "PUT",
  129. "/profile/%s/avatar_url" % (myid),
  130. b'{"avatar_url": "http://my.server/pic.gif"}',
  131. )
  132. self.assertEquals(200, code)
  133. self.assertEquals(mocked_set.call_args[0][0].localpart, "1234ABCD")
  134. self.assertEquals(mocked_set.call_args[0][1].user.localpart, "1234ABCD")
  135. self.assertEquals(mocked_set.call_args[0][2], "http://my.server/pic.gif")
  136. class ProfileTestCase(unittest.HomeserverTestCase):
  137. servlets = [
  138. admin.register_servlets_for_client_rest_resource,
  139. login.register_servlets,
  140. profile.register_servlets,
  141. ]
  142. def make_homeserver(self, reactor, clock):
  143. self.hs = self.setup_test_homeserver()
  144. return self.hs
  145. def prepare(self, reactor, clock, hs):
  146. self.owner = self.register_user("owner", "pass")
  147. self.owner_tok = self.login("owner", "pass")
  148. def test_set_displayname(self):
  149. request, channel = self.make_request(
  150. "PUT",
  151. "/profile/%s/displayname" % (self.owner,),
  152. content=json.dumps({"displayname": "test"}),
  153. access_token=self.owner_tok,
  154. )
  155. self.render(request)
  156. self.assertEqual(channel.code, 200, channel.result)
  157. res = self.get_displayname()
  158. self.assertEqual(res, "test")
  159. def test_set_displayname_too_long(self):
  160. """Attempts to set a stupid displayname should get a 400"""
  161. request, channel = self.make_request(
  162. "PUT",
  163. "/profile/%s/displayname" % (self.owner,),
  164. content=json.dumps({"displayname": "test" * 100}),
  165. access_token=self.owner_tok,
  166. )
  167. self.render(request)
  168. self.assertEqual(channel.code, 400, channel.result)
  169. res = self.get_displayname()
  170. self.assertEqual(res, "owner")
  171. def get_displayname(self):
  172. request, channel = self.make_request(
  173. "GET", "/profile/%s/displayname" % (self.owner,)
  174. )
  175. self.render(request)
  176. self.assertEqual(channel.code, 200, channel.result)
  177. return channel.json_body["displayname"]
  178. class ProfilesRestrictedTestCase(unittest.HomeserverTestCase):
  179. servlets = [
  180. admin.register_servlets_for_client_rest_resource,
  181. login.register_servlets,
  182. profile.register_servlets,
  183. room.register_servlets,
  184. ]
  185. def make_homeserver(self, reactor, clock):
  186. config = self.default_config()
  187. config["require_auth_for_profile_requests"] = True
  188. config["limit_profile_requests_to_users_who_share_rooms"] = True
  189. self.hs = self.setup_test_homeserver(config=config)
  190. return self.hs
  191. def prepare(self, reactor, clock, hs):
  192. # User owning the requested profile.
  193. self.owner = self.register_user("owner", "pass")
  194. self.owner_tok = self.login("owner", "pass")
  195. self.profile_url = "/profile/%s" % (self.owner)
  196. # User requesting the profile.
  197. self.requester = self.register_user("requester", "pass")
  198. self.requester_tok = self.login("requester", "pass")
  199. self.room_id = self.helper.create_room_as(self.owner, tok=self.owner_tok)
  200. def test_no_auth(self):
  201. self.try_fetch_profile(401)
  202. def test_not_in_shared_room(self):
  203. self.ensure_requester_left_room()
  204. self.try_fetch_profile(403, access_token=self.requester_tok)
  205. def test_in_shared_room(self):
  206. self.ensure_requester_left_room()
  207. self.helper.join(room=self.room_id, user=self.requester, tok=self.requester_tok)
  208. self.try_fetch_profile(200, self.requester_tok)
  209. def try_fetch_profile(self, expected_code, access_token=None):
  210. self.request_profile(expected_code, access_token=access_token)
  211. self.request_profile(
  212. expected_code, url_suffix="/displayname", access_token=access_token
  213. )
  214. self.request_profile(
  215. expected_code, url_suffix="/avatar_url", access_token=access_token
  216. )
  217. def request_profile(self, expected_code, url_suffix="", access_token=None):
  218. request, channel = self.make_request(
  219. "GET", self.profile_url + url_suffix, access_token=access_token
  220. )
  221. self.render(request)
  222. self.assertEqual(channel.code, expected_code, channel.result)
  223. def ensure_requester_left_room(self):
  224. try:
  225. self.helper.leave(
  226. room=self.room_id, user=self.requester, tok=self.requester_tok
  227. )
  228. except AssertionError:
  229. # We don't care whether the leave request didn't return a 200 (e.g.
  230. # if the user isn't already in the room), because we only want to
  231. # make sure the user isn't in the room.
  232. pass
  233. class OwnProfileUnrestrictedTestCase(unittest.HomeserverTestCase):
  234. servlets = [
  235. admin.register_servlets_for_client_rest_resource,
  236. login.register_servlets,
  237. profile.register_servlets,
  238. ]
  239. def make_homeserver(self, reactor, clock):
  240. config = self.default_config()
  241. config["require_auth_for_profile_requests"] = True
  242. config["limit_profile_requests_to_users_who_share_rooms"] = True
  243. self.hs = self.setup_test_homeserver(config=config)
  244. return self.hs
  245. def prepare(self, reactor, clock, hs):
  246. # User requesting the profile.
  247. self.requester = self.register_user("requester", "pass")
  248. self.requester_tok = self.login("requester", "pass")
  249. def test_can_lookup_own_profile(self):
  250. """Tests that a user can lookup their own profile without having to be in a room
  251. if 'require_auth_for_profile_requests' is set to true in the server's config.
  252. """
  253. request, channel = self.make_request(
  254. "GET", "/profile/" + self.requester, access_token=self.requester_tok
  255. )
  256. self.render(request)
  257. self.assertEqual(channel.code, 200, channel.result)
  258. request, channel = self.make_request(
  259. "GET",
  260. "/profile/" + self.requester + "/displayname",
  261. access_token=self.requester_tok,
  262. )
  263. self.render(request)
  264. self.assertEqual(channel.code, 200, channel.result)
  265. request, channel = self.make_request(
  266. "GET",
  267. "/profile/" + self.requester + "/avatar_url",
  268. access_token=self.requester_tok,
  269. )
  270. self.render(request)
  271. self.assertEqual(channel.code, 200, channel.result)