1
0

test_profile.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. import urllib.parse
  16. from http import HTTPStatus
  17. from typing import Any, Dict, Optional
  18. from twisted.test.proto_helpers import MemoryReactor
  19. from synapse.api.errors import Codes
  20. from synapse.rest import admin
  21. from synapse.rest.client import login, profile, room
  22. from synapse.server import HomeServer
  23. from synapse.types import UserID
  24. from synapse.util import Clock
  25. from tests import unittest
  26. class ProfileTestCase(unittest.HomeserverTestCase):
  27. servlets = [
  28. admin.register_servlets_for_client_rest_resource,
  29. login.register_servlets,
  30. profile.register_servlets,
  31. room.register_servlets,
  32. ]
  33. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  34. self.hs = self.setup_test_homeserver()
  35. return self.hs
  36. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  37. self.owner = self.register_user("owner", "pass")
  38. self.owner_tok = self.login("owner", "pass")
  39. self.other = self.register_user("other", "pass", displayname="Bob")
  40. def test_get_displayname(self) -> None:
  41. res = self._get_displayname()
  42. self.assertEqual(res, "owner")
  43. def test_get_displayname_rejects_bad_username(self) -> None:
  44. channel = self.make_request(
  45. "GET", f"/profile/{urllib.parse.quote('@alice:')}/displayname"
  46. )
  47. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
  48. def test_set_displayname(self) -> None:
  49. channel = self.make_request(
  50. "PUT",
  51. "/profile/%s/displayname" % (self.owner,),
  52. content={"displayname": "test"},
  53. access_token=self.owner_tok,
  54. )
  55. self.assertEqual(channel.code, 200, channel.result)
  56. res = self._get_displayname()
  57. self.assertEqual(res, "test")
  58. def test_set_displayname_with_extra_spaces(self) -> None:
  59. channel = self.make_request(
  60. "PUT",
  61. "/profile/%s/displayname" % (self.owner,),
  62. content={"displayname": " test "},
  63. access_token=self.owner_tok,
  64. )
  65. self.assertEqual(channel.code, 200, channel.result)
  66. res = self._get_displayname()
  67. self.assertEqual(res, "test")
  68. def test_set_displayname_noauth(self) -> None:
  69. channel = self.make_request(
  70. "PUT",
  71. "/profile/%s/displayname" % (self.owner,),
  72. content={"displayname": "test"},
  73. )
  74. self.assertEqual(channel.code, 401, channel.result)
  75. def test_set_displayname_too_long(self) -> None:
  76. """Attempts to set a stupid displayname should get a 400"""
  77. channel = self.make_request(
  78. "PUT",
  79. "/profile/%s/displayname" % (self.owner,),
  80. content={"displayname": "test" * 100},
  81. access_token=self.owner_tok,
  82. )
  83. self.assertEqual(channel.code, 400, channel.result)
  84. res = self._get_displayname()
  85. self.assertEqual(res, "owner")
  86. def test_get_displayname_other(self) -> None:
  87. res = self._get_displayname(self.other)
  88. self.assertEqual(res, "Bob")
  89. def test_set_displayname_other(self) -> None:
  90. channel = self.make_request(
  91. "PUT",
  92. "/profile/%s/displayname" % (self.other,),
  93. content={"displayname": "test"},
  94. access_token=self.owner_tok,
  95. )
  96. self.assertEqual(channel.code, 400, channel.result)
  97. def test_get_avatar_url(self) -> None:
  98. res = self._get_avatar_url()
  99. self.assertIsNone(res)
  100. def test_set_avatar_url(self) -> None:
  101. channel = self.make_request(
  102. "PUT",
  103. "/profile/%s/avatar_url" % (self.owner,),
  104. content={"avatar_url": "http://my.server/pic.gif"},
  105. access_token=self.owner_tok,
  106. )
  107. self.assertEqual(channel.code, 200, channel.result)
  108. res = self._get_avatar_url()
  109. self.assertEqual(res, "http://my.server/pic.gif")
  110. def test_set_avatar_url_noauth(self) -> None:
  111. channel = self.make_request(
  112. "PUT",
  113. "/profile/%s/avatar_url" % (self.owner,),
  114. content={"avatar_url": "http://my.server/pic.gif"},
  115. )
  116. self.assertEqual(channel.code, 401, channel.result)
  117. def test_set_avatar_url_too_long(self) -> None:
  118. """Attempts to set a stupid avatar_url should get a 400"""
  119. channel = self.make_request(
  120. "PUT",
  121. "/profile/%s/avatar_url" % (self.owner,),
  122. content={"avatar_url": "http://my.server/pic.gif" * 100},
  123. access_token=self.owner_tok,
  124. )
  125. self.assertEqual(channel.code, 400, channel.result)
  126. res = self._get_avatar_url()
  127. self.assertIsNone(res)
  128. def test_get_avatar_url_other(self) -> None:
  129. res = self._get_avatar_url(self.other)
  130. self.assertIsNone(res)
  131. def test_set_avatar_url_other(self) -> None:
  132. channel = self.make_request(
  133. "PUT",
  134. "/profile/%s/avatar_url" % (self.other,),
  135. content={"avatar_url": "http://my.server/pic.gif"},
  136. access_token=self.owner_tok,
  137. )
  138. self.assertEqual(channel.code, 400, channel.result)
  139. def _get_displayname(self, name: Optional[str] = None) -> Optional[str]:
  140. channel = self.make_request(
  141. "GET", "/profile/%s/displayname" % (name or self.owner,)
  142. )
  143. self.assertEqual(channel.code, 200, channel.result)
  144. # FIXME: If a user has no displayname set, Synapse returns 200 and omits a
  145. # displayname from the response. This contradicts the spec, see #13137.
  146. return channel.json_body.get("displayname")
  147. def _get_avatar_url(self, name: Optional[str] = None) -> Optional[str]:
  148. channel = self.make_request(
  149. "GET", "/profile/%s/avatar_url" % (name or self.owner,)
  150. )
  151. self.assertEqual(channel.code, 200, channel.result)
  152. # FIXME: If a user has no avatar set, Synapse returns 200 and omits an
  153. # avatar_url from the response. This contradicts the spec, see #13137.
  154. return channel.json_body.get("avatar_url")
  155. @unittest.override_config({"max_avatar_size": 50})
  156. def test_avatar_size_limit_global(self) -> None:
  157. """Tests that the maximum size limit for avatars is enforced when updating a
  158. global profile.
  159. """
  160. self._setup_local_files(
  161. {
  162. "small": {"size": 40},
  163. "big": {"size": 60},
  164. }
  165. )
  166. channel = self.make_request(
  167. "PUT",
  168. f"/profile/{self.owner}/avatar_url",
  169. content={"avatar_url": "mxc://test/big"},
  170. access_token=self.owner_tok,
  171. )
  172. self.assertEqual(channel.code, 403, channel.result)
  173. self.assertEqual(
  174. channel.json_body["errcode"], Codes.FORBIDDEN, channel.json_body
  175. )
  176. channel = self.make_request(
  177. "PUT",
  178. f"/profile/{self.owner}/avatar_url",
  179. content={"avatar_url": "mxc://test/small"},
  180. access_token=self.owner_tok,
  181. )
  182. self.assertEqual(channel.code, 200, channel.result)
  183. @unittest.override_config({"max_avatar_size": 50})
  184. def test_avatar_size_limit_per_room(self) -> None:
  185. """Tests that the maximum size limit for avatars is enforced when updating a
  186. per-room profile.
  187. """
  188. self._setup_local_files(
  189. {
  190. "small": {"size": 40},
  191. "big": {"size": 60},
  192. }
  193. )
  194. room_id = self.helper.create_room_as(tok=self.owner_tok)
  195. channel = self.make_request(
  196. "PUT",
  197. f"/rooms/{room_id}/state/m.room.member/{self.owner}",
  198. content={"membership": "join", "avatar_url": "mxc://test/big"},
  199. access_token=self.owner_tok,
  200. )
  201. self.assertEqual(channel.code, 403, channel.result)
  202. self.assertEqual(
  203. channel.json_body["errcode"], Codes.FORBIDDEN, channel.json_body
  204. )
  205. channel = self.make_request(
  206. "PUT",
  207. f"/rooms/{room_id}/state/m.room.member/{self.owner}",
  208. content={"membership": "join", "avatar_url": "mxc://test/small"},
  209. access_token=self.owner_tok,
  210. )
  211. self.assertEqual(channel.code, 200, channel.result)
  212. @unittest.override_config({"allowed_avatar_mimetypes": ["image/png"]})
  213. def test_avatar_allowed_mime_type_global(self) -> None:
  214. """Tests that the MIME type whitelist for avatars is enforced when updating a
  215. global profile.
  216. """
  217. self._setup_local_files(
  218. {
  219. "good": {"mimetype": "image/png"},
  220. "bad": {"mimetype": "application/octet-stream"},
  221. }
  222. )
  223. channel = self.make_request(
  224. "PUT",
  225. f"/profile/{self.owner}/avatar_url",
  226. content={"avatar_url": "mxc://test/bad"},
  227. access_token=self.owner_tok,
  228. )
  229. self.assertEqual(channel.code, 403, channel.result)
  230. self.assertEqual(
  231. channel.json_body["errcode"], Codes.FORBIDDEN, channel.json_body
  232. )
  233. channel = self.make_request(
  234. "PUT",
  235. f"/profile/{self.owner}/avatar_url",
  236. content={"avatar_url": "mxc://test/good"},
  237. access_token=self.owner_tok,
  238. )
  239. self.assertEqual(channel.code, 200, channel.result)
  240. @unittest.override_config({"allowed_avatar_mimetypes": ["image/png"]})
  241. def test_avatar_allowed_mime_type_per_room(self) -> None:
  242. """Tests that the MIME type whitelist for avatars is enforced when updating a
  243. per-room profile.
  244. """
  245. self._setup_local_files(
  246. {
  247. "good": {"mimetype": "image/png"},
  248. "bad": {"mimetype": "application/octet-stream"},
  249. }
  250. )
  251. room_id = self.helper.create_room_as(tok=self.owner_tok)
  252. channel = self.make_request(
  253. "PUT",
  254. f"/rooms/{room_id}/state/m.room.member/{self.owner}",
  255. content={"membership": "join", "avatar_url": "mxc://test/bad"},
  256. access_token=self.owner_tok,
  257. )
  258. self.assertEqual(channel.code, 403, channel.result)
  259. self.assertEqual(
  260. channel.json_body["errcode"], Codes.FORBIDDEN, channel.json_body
  261. )
  262. channel = self.make_request(
  263. "PUT",
  264. f"/rooms/{room_id}/state/m.room.member/{self.owner}",
  265. content={"membership": "join", "avatar_url": "mxc://test/good"},
  266. access_token=self.owner_tok,
  267. )
  268. self.assertEqual(channel.code, 200, channel.result)
  269. def _setup_local_files(self, names_and_props: Dict[str, Dict[str, Any]]) -> None:
  270. """Stores metadata about files in the database.
  271. Args:
  272. names_and_props: A dictionary with one entry per file, with the key being the
  273. file's name, and the value being a dictionary of properties. Supported
  274. properties are "mimetype" (for the file's type) and "size" (for the
  275. file's size).
  276. """
  277. store = self.hs.get_datastores().main
  278. for name, props in names_and_props.items():
  279. self.get_success(
  280. store.store_local_media(
  281. media_id=name,
  282. media_type=props.get("mimetype", "image/png"),
  283. time_now_ms=self.clock.time_msec(),
  284. upload_name=None,
  285. media_length=props.get("size", 50),
  286. user_id=UserID.from_string("@rin:test"),
  287. )
  288. )
  289. class ProfilesRestrictedTestCase(unittest.HomeserverTestCase):
  290. servlets = [
  291. admin.register_servlets_for_client_rest_resource,
  292. login.register_servlets,
  293. profile.register_servlets,
  294. room.register_servlets,
  295. ]
  296. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  297. config = self.default_config()
  298. config["require_auth_for_profile_requests"] = True
  299. config["limit_profile_requests_to_users_who_share_rooms"] = True
  300. self.hs = self.setup_test_homeserver(config=config)
  301. return self.hs
  302. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  303. # User owning the requested profile.
  304. self.owner = self.register_user("owner", "pass")
  305. self.owner_tok = self.login("owner", "pass")
  306. self.profile_url = "/profile/%s" % (self.owner)
  307. # User requesting the profile.
  308. self.requester = self.register_user("requester", "pass")
  309. self.requester_tok = self.login("requester", "pass")
  310. self.room_id = self.helper.create_room_as(self.owner, tok=self.owner_tok)
  311. def test_no_auth(self) -> None:
  312. self.try_fetch_profile(401)
  313. def test_not_in_shared_room(self) -> None:
  314. self.ensure_requester_left_room()
  315. self.try_fetch_profile(403, access_token=self.requester_tok)
  316. def test_in_shared_room(self) -> None:
  317. self.ensure_requester_left_room()
  318. self.helper.join(room=self.room_id, user=self.requester, tok=self.requester_tok)
  319. self.try_fetch_profile(200, self.requester_tok)
  320. def try_fetch_profile(
  321. self, expected_code: int, access_token: Optional[str] = None
  322. ) -> None:
  323. self.request_profile(expected_code, access_token=access_token)
  324. self.request_profile(
  325. expected_code, url_suffix="/displayname", access_token=access_token
  326. )
  327. self.request_profile(
  328. expected_code, url_suffix="/avatar_url", access_token=access_token
  329. )
  330. def request_profile(
  331. self,
  332. expected_code: int,
  333. url_suffix: str = "",
  334. access_token: Optional[str] = None,
  335. ) -> None:
  336. channel = self.make_request(
  337. "GET", self.profile_url + url_suffix, access_token=access_token
  338. )
  339. self.assertEqual(channel.code, expected_code, channel.result)
  340. def ensure_requester_left_room(self) -> None:
  341. try:
  342. self.helper.leave(
  343. room=self.room_id, user=self.requester, tok=self.requester_tok
  344. )
  345. except AssertionError:
  346. # We don't care whether the leave request didn't return a 200 (e.g.
  347. # if the user isn't already in the room), because we only want to
  348. # make sure the user isn't in the room.
  349. pass
  350. class OwnProfileUnrestrictedTestCase(unittest.HomeserverTestCase):
  351. servlets = [
  352. admin.register_servlets_for_client_rest_resource,
  353. login.register_servlets,
  354. profile.register_servlets,
  355. ]
  356. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  357. config = self.default_config()
  358. config["require_auth_for_profile_requests"] = True
  359. config["limit_profile_requests_to_users_who_share_rooms"] = True
  360. self.hs = self.setup_test_homeserver(config=config)
  361. return self.hs
  362. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  363. # User requesting the profile.
  364. self.requester = self.register_user("requester", "pass")
  365. self.requester_tok = self.login("requester", "pass")
  366. def test_can_lookup_own_profile(self) -> None:
  367. """Tests that a user can lookup their own profile without having to be in a room
  368. if 'require_auth_for_profile_requests' is set to true in the server's config.
  369. """
  370. channel = self.make_request(
  371. "GET", "/profile/" + self.requester, access_token=self.requester_tok
  372. )
  373. self.assertEqual(channel.code, 200, channel.result)
  374. channel = self.make_request(
  375. "GET",
  376. "/profile/" + self.requester + "/displayname",
  377. access_token=self.requester_tok,
  378. )
  379. self.assertEqual(channel.code, 200, channel.result)
  380. channel = self.make_request(
  381. "GET",
  382. "/profile/" + self.requester + "/avatar_url",
  383. access_token=self.requester_tok,
  384. )
  385. self.assertEqual(channel.code, 200, channel.result)