test_user.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 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 hashlib
  16. import hmac
  17. import json
  18. import urllib.parse
  19. from mock import Mock
  20. import synapse.rest.admin
  21. from synapse.api.constants import UserTypes
  22. from synapse.rest.client.v1 import login
  23. from tests import unittest
  24. class UserRegisterTestCase(unittest.HomeserverTestCase):
  25. servlets = [synapse.rest.admin.register_servlets_for_client_rest_resource]
  26. def make_homeserver(self, reactor, clock):
  27. self.url = "/_matrix/client/r0/admin/register"
  28. self.registration_handler = Mock()
  29. self.identity_handler = Mock()
  30. self.login_handler = Mock()
  31. self.device_handler = Mock()
  32. self.device_handler.check_device_registered = Mock(return_value="FAKE")
  33. self.datastore = Mock(return_value=Mock())
  34. self.datastore.get_current_state_deltas = Mock(return_value=(0, []))
  35. self.secrets = Mock()
  36. self.hs = self.setup_test_homeserver()
  37. self.hs.config.registration_shared_secret = "shared"
  38. self.hs.get_media_repository = Mock()
  39. self.hs.get_deactivate_account_handler = Mock()
  40. return self.hs
  41. def test_disabled(self):
  42. """
  43. If there is no shared secret, registration through this method will be
  44. prevented.
  45. """
  46. self.hs.config.registration_shared_secret = None
  47. request, channel = self.make_request("POST", self.url, b"{}")
  48. self.render(request)
  49. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  50. self.assertEqual(
  51. "Shared secret registration is not enabled", channel.json_body["error"]
  52. )
  53. def test_get_nonce(self):
  54. """
  55. Calling GET on the endpoint will return a randomised nonce, using the
  56. homeserver's secrets provider.
  57. """
  58. secrets = Mock()
  59. secrets.token_hex = Mock(return_value="abcd")
  60. self.hs.get_secrets = Mock(return_value=secrets)
  61. request, channel = self.make_request("GET", self.url)
  62. self.render(request)
  63. self.assertEqual(channel.json_body, {"nonce": "abcd"})
  64. def test_expired_nonce(self):
  65. """
  66. Calling GET on the endpoint will return a randomised nonce, which will
  67. only last for SALT_TIMEOUT (60s).
  68. """
  69. request, channel = self.make_request("GET", self.url)
  70. self.render(request)
  71. nonce = channel.json_body["nonce"]
  72. # 59 seconds
  73. self.reactor.advance(59)
  74. body = json.dumps({"nonce": nonce})
  75. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  76. self.render(request)
  77. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  78. self.assertEqual("username must be specified", channel.json_body["error"])
  79. # 61 seconds
  80. self.reactor.advance(2)
  81. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  82. self.render(request)
  83. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  84. self.assertEqual("unrecognised nonce", channel.json_body["error"])
  85. def test_register_incorrect_nonce(self):
  86. """
  87. Only the provided nonce can be used, as it's checked in the MAC.
  88. """
  89. request, channel = self.make_request("GET", self.url)
  90. self.render(request)
  91. nonce = channel.json_body["nonce"]
  92. want_mac = hmac.new(key=b"shared", digestmod=hashlib.sha1)
  93. want_mac.update(b"notthenonce\x00bob\x00abc123\x00admin")
  94. want_mac = want_mac.hexdigest()
  95. body = json.dumps(
  96. {
  97. "nonce": nonce,
  98. "username": "bob",
  99. "password": "abc123",
  100. "admin": True,
  101. "mac": want_mac,
  102. }
  103. )
  104. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  105. self.render(request)
  106. self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
  107. self.assertEqual("HMAC incorrect", channel.json_body["error"])
  108. def test_register_correct_nonce(self):
  109. """
  110. When the correct nonce is provided, and the right key is provided, the
  111. user is registered.
  112. """
  113. request, channel = self.make_request("GET", self.url)
  114. self.render(request)
  115. nonce = channel.json_body["nonce"]
  116. want_mac = hmac.new(key=b"shared", digestmod=hashlib.sha1)
  117. want_mac.update(
  118. nonce.encode("ascii") + b"\x00bob\x00abc123\x00admin\x00support"
  119. )
  120. want_mac = want_mac.hexdigest()
  121. body = json.dumps(
  122. {
  123. "nonce": nonce,
  124. "username": "bob",
  125. "password": "abc123",
  126. "admin": True,
  127. "user_type": UserTypes.SUPPORT,
  128. "mac": want_mac,
  129. }
  130. )
  131. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  132. self.render(request)
  133. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  134. self.assertEqual("@bob:test", channel.json_body["user_id"])
  135. def test_nonce_reuse(self):
  136. """
  137. A valid unrecognised nonce.
  138. """
  139. request, channel = self.make_request("GET", self.url)
  140. self.render(request)
  141. nonce = channel.json_body["nonce"]
  142. want_mac = hmac.new(key=b"shared", digestmod=hashlib.sha1)
  143. want_mac.update(nonce.encode("ascii") + b"\x00bob\x00abc123\x00admin")
  144. want_mac = want_mac.hexdigest()
  145. body = json.dumps(
  146. {
  147. "nonce": nonce,
  148. "username": "bob",
  149. "password": "abc123",
  150. "admin": True,
  151. "mac": want_mac,
  152. }
  153. )
  154. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  155. self.render(request)
  156. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  157. self.assertEqual("@bob:test", channel.json_body["user_id"])
  158. # Now, try and reuse it
  159. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  160. self.render(request)
  161. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  162. self.assertEqual("unrecognised nonce", channel.json_body["error"])
  163. def test_missing_parts(self):
  164. """
  165. Synapse will complain if you don't give nonce, username, password, and
  166. mac. Admin and user_types are optional. Additional checks are done for length
  167. and type.
  168. """
  169. def nonce():
  170. request, channel = self.make_request("GET", self.url)
  171. self.render(request)
  172. return channel.json_body["nonce"]
  173. #
  174. # Nonce check
  175. #
  176. # Must be present
  177. body = json.dumps({})
  178. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  179. self.render(request)
  180. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  181. self.assertEqual("nonce must be specified", channel.json_body["error"])
  182. #
  183. # Username checks
  184. #
  185. # Must be present
  186. body = json.dumps({"nonce": nonce()})
  187. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  188. self.render(request)
  189. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  190. self.assertEqual("username must be specified", channel.json_body["error"])
  191. # Must be a string
  192. body = json.dumps({"nonce": nonce(), "username": 1234})
  193. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  194. self.render(request)
  195. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  196. self.assertEqual("Invalid username", channel.json_body["error"])
  197. # Must not have null bytes
  198. body = json.dumps({"nonce": nonce(), "username": "abcd\u0000"})
  199. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  200. self.render(request)
  201. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  202. self.assertEqual("Invalid username", channel.json_body["error"])
  203. # Must not have null bytes
  204. body = json.dumps({"nonce": nonce(), "username": "a" * 1000})
  205. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  206. self.render(request)
  207. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  208. self.assertEqual("Invalid username", channel.json_body["error"])
  209. #
  210. # Password checks
  211. #
  212. # Must be present
  213. body = json.dumps({"nonce": nonce(), "username": "a"})
  214. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  215. self.render(request)
  216. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  217. self.assertEqual("password must be specified", channel.json_body["error"])
  218. # Must be a string
  219. body = json.dumps({"nonce": nonce(), "username": "a", "password": 1234})
  220. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  221. self.render(request)
  222. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  223. self.assertEqual("Invalid password", channel.json_body["error"])
  224. # Must not have null bytes
  225. body = json.dumps({"nonce": nonce(), "username": "a", "password": "abcd\u0000"})
  226. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  227. self.render(request)
  228. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  229. self.assertEqual("Invalid password", channel.json_body["error"])
  230. # Super long
  231. body = json.dumps({"nonce": nonce(), "username": "a", "password": "A" * 1000})
  232. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  233. self.render(request)
  234. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  235. self.assertEqual("Invalid password", channel.json_body["error"])
  236. #
  237. # user_type check
  238. #
  239. # Invalid user_type
  240. body = json.dumps(
  241. {
  242. "nonce": nonce(),
  243. "username": "a",
  244. "password": "1234",
  245. "user_type": "invalid",
  246. }
  247. )
  248. request, channel = self.make_request("POST", self.url, body.encode("utf8"))
  249. self.render(request)
  250. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  251. self.assertEqual("Invalid user type", channel.json_body["error"])
  252. class UsersListTestCase(unittest.HomeserverTestCase):
  253. servlets = [
  254. synapse.rest.admin.register_servlets,
  255. login.register_servlets,
  256. ]
  257. url = "/_synapse/admin/v2/users"
  258. def prepare(self, reactor, clock, hs):
  259. self.admin_user = self.register_user("admin", "pass", admin=True)
  260. self.admin_user_tok = self.login("admin", "pass")
  261. self.register_user("user1", "pass1", admin=False)
  262. self.register_user("user2", "pass2", admin=False)
  263. def test_no_auth(self):
  264. """
  265. Try to list users without authentication.
  266. """
  267. request, channel = self.make_request("GET", self.url, b"{}")
  268. self.render(request)
  269. self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
  270. self.assertEqual("M_MISSING_TOKEN", channel.json_body["errcode"])
  271. def test_all_users(self):
  272. """
  273. List all users, including deactivated users.
  274. """
  275. request, channel = self.make_request(
  276. "GET",
  277. self.url + "?deactivated=true",
  278. b"{}",
  279. access_token=self.admin_user_tok,
  280. )
  281. self.render(request)
  282. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  283. self.assertEqual(3, len(channel.json_body["users"]))
  284. class UserRestTestCase(unittest.HomeserverTestCase):
  285. servlets = [
  286. synapse.rest.admin.register_servlets,
  287. login.register_servlets,
  288. ]
  289. def prepare(self, reactor, clock, hs):
  290. self.store = hs.get_datastore()
  291. self.admin_user = self.register_user("admin", "pass", admin=True)
  292. self.admin_user_tok = self.login("admin", "pass")
  293. self.other_user = self.register_user("user", "pass")
  294. self.other_user_token = self.login("user", "pass")
  295. self.url_other_user = "/_synapse/admin/v2/users/%s" % urllib.parse.quote(
  296. self.other_user
  297. )
  298. def test_requester_is_no_admin(self):
  299. """
  300. If the user is not a server admin, an error is returned.
  301. """
  302. self.hs.config.registration_shared_secret = None
  303. url = "/_synapse/admin/v2/users/@bob:test"
  304. request, channel = self.make_request(
  305. "GET", url, access_token=self.other_user_token,
  306. )
  307. self.render(request)
  308. self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
  309. self.assertEqual("You are not a server admin", channel.json_body["error"])
  310. request, channel = self.make_request(
  311. "PUT", url, access_token=self.other_user_token, content=b"{}",
  312. )
  313. self.render(request)
  314. self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
  315. self.assertEqual("You are not a server admin", channel.json_body["error"])
  316. def test_user_does_not_exist(self):
  317. """
  318. Tests that a lookup for a user that does not exist returns a 404
  319. """
  320. self.hs.config.registration_shared_secret = None
  321. request, channel = self.make_request(
  322. "GET",
  323. "/_synapse/admin/v2/users/@unknown_person:test",
  324. access_token=self.admin_user_tok,
  325. )
  326. self.render(request)
  327. self.assertEqual(404, channel.code, msg=channel.json_body)
  328. self.assertEqual("M_NOT_FOUND", channel.json_body["errcode"])
  329. def test_create_server_admin(self):
  330. """
  331. Check that a new admin user is created successfully.
  332. """
  333. self.hs.config.registration_shared_secret = None
  334. url = "/_synapse/admin/v2/users/@bob:test"
  335. # Create user (server admin)
  336. body = json.dumps(
  337. {
  338. "password": "abc123",
  339. "admin": True,
  340. "displayname": "Bob's name",
  341. "threepids": [{"medium": "email", "address": "bob@bob.bob"}],
  342. }
  343. )
  344. request, channel = self.make_request(
  345. "PUT",
  346. url,
  347. access_token=self.admin_user_tok,
  348. content=body.encode(encoding="utf_8"),
  349. )
  350. self.render(request)
  351. self.assertEqual(201, int(channel.result["code"]), msg=channel.result["body"])
  352. self.assertEqual("@bob:test", channel.json_body["name"])
  353. self.assertEqual("Bob's name", channel.json_body["displayname"])
  354. self.assertEqual("email", channel.json_body["threepids"][0]["medium"])
  355. self.assertEqual("bob@bob.bob", channel.json_body["threepids"][0]["address"])
  356. self.assertEqual(True, channel.json_body["admin"])
  357. # Get user
  358. request, channel = self.make_request(
  359. "GET", url, access_token=self.admin_user_tok,
  360. )
  361. self.render(request)
  362. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  363. self.assertEqual("@bob:test", channel.json_body["name"])
  364. self.assertEqual("Bob's name", channel.json_body["displayname"])
  365. self.assertEqual("email", channel.json_body["threepids"][0]["medium"])
  366. self.assertEqual("bob@bob.bob", channel.json_body["threepids"][0]["address"])
  367. self.assertEqual(True, channel.json_body["admin"])
  368. self.assertEqual(False, channel.json_body["is_guest"])
  369. self.assertEqual(False, channel.json_body["deactivated"])
  370. def test_create_user(self):
  371. """
  372. Check that a new regular user is created successfully.
  373. """
  374. self.hs.config.registration_shared_secret = None
  375. url = "/_synapse/admin/v2/users/@bob:test"
  376. # Create user
  377. body = json.dumps(
  378. {
  379. "password": "abc123",
  380. "admin": False,
  381. "displayname": "Bob's name",
  382. "threepids": [{"medium": "email", "address": "bob@bob.bob"}],
  383. }
  384. )
  385. request, channel = self.make_request(
  386. "PUT",
  387. url,
  388. access_token=self.admin_user_tok,
  389. content=body.encode(encoding="utf_8"),
  390. )
  391. self.render(request)
  392. self.assertEqual(201, int(channel.result["code"]), msg=channel.result["body"])
  393. self.assertEqual("@bob:test", channel.json_body["name"])
  394. self.assertEqual("Bob's name", channel.json_body["displayname"])
  395. self.assertEqual("email", channel.json_body["threepids"][0]["medium"])
  396. self.assertEqual("bob@bob.bob", channel.json_body["threepids"][0]["address"])
  397. self.assertEqual(False, channel.json_body["admin"])
  398. # Get user
  399. request, channel = self.make_request(
  400. "GET", url, access_token=self.admin_user_tok,
  401. )
  402. self.render(request)
  403. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  404. self.assertEqual("@bob:test", channel.json_body["name"])
  405. self.assertEqual("Bob's name", channel.json_body["displayname"])
  406. self.assertEqual("email", channel.json_body["threepids"][0]["medium"])
  407. self.assertEqual("bob@bob.bob", channel.json_body["threepids"][0]["address"])
  408. self.assertEqual(False, channel.json_body["admin"])
  409. self.assertEqual(False, channel.json_body["is_guest"])
  410. self.assertEqual(False, channel.json_body["deactivated"])
  411. def test_set_password(self):
  412. """
  413. Test setting a new password for another user.
  414. """
  415. self.hs.config.registration_shared_secret = None
  416. # Change password
  417. body = json.dumps({"password": "hahaha"})
  418. request, channel = self.make_request(
  419. "PUT",
  420. self.url_other_user,
  421. access_token=self.admin_user_tok,
  422. content=body.encode(encoding="utf_8"),
  423. )
  424. self.render(request)
  425. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  426. def test_set_displayname(self):
  427. """
  428. Test setting the displayname of another user.
  429. """
  430. self.hs.config.registration_shared_secret = None
  431. # Modify user
  432. body = json.dumps({"displayname": "foobar"})
  433. request, channel = self.make_request(
  434. "PUT",
  435. self.url_other_user,
  436. access_token=self.admin_user_tok,
  437. content=body.encode(encoding="utf_8"),
  438. )
  439. self.render(request)
  440. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  441. self.assertEqual("@user:test", channel.json_body["name"])
  442. self.assertEqual("foobar", channel.json_body["displayname"])
  443. # Get user
  444. request, channel = self.make_request(
  445. "GET", self.url_other_user, access_token=self.admin_user_tok,
  446. )
  447. self.render(request)
  448. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  449. self.assertEqual("@user:test", channel.json_body["name"])
  450. self.assertEqual("foobar", channel.json_body["displayname"])
  451. def test_set_threepid(self):
  452. """
  453. Test setting threepid for an other user.
  454. """
  455. self.hs.config.registration_shared_secret = None
  456. # Delete old and add new threepid to user
  457. body = json.dumps(
  458. {"threepids": [{"medium": "email", "address": "bob3@bob.bob"}]}
  459. )
  460. request, channel = self.make_request(
  461. "PUT",
  462. self.url_other_user,
  463. access_token=self.admin_user_tok,
  464. content=body.encode(encoding="utf_8"),
  465. )
  466. self.render(request)
  467. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  468. self.assertEqual("@user:test", channel.json_body["name"])
  469. self.assertEqual("email", channel.json_body["threepids"][0]["medium"])
  470. self.assertEqual("bob3@bob.bob", channel.json_body["threepids"][0]["address"])
  471. # Get user
  472. request, channel = self.make_request(
  473. "GET", self.url_other_user, access_token=self.admin_user_tok,
  474. )
  475. self.render(request)
  476. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  477. self.assertEqual("@user:test", channel.json_body["name"])
  478. self.assertEqual("email", channel.json_body["threepids"][0]["medium"])
  479. self.assertEqual("bob3@bob.bob", channel.json_body["threepids"][0]["address"])
  480. def test_deactivate_user(self):
  481. """
  482. Test deactivating another user.
  483. """
  484. # Deactivate user
  485. body = json.dumps({"deactivated": True})
  486. request, channel = self.make_request(
  487. "PUT",
  488. self.url_other_user,
  489. access_token=self.admin_user_tok,
  490. content=body.encode(encoding="utf_8"),
  491. )
  492. self.render(request)
  493. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  494. self.assertEqual("@user:test", channel.json_body["name"])
  495. self.assertEqual(True, channel.json_body["deactivated"])
  496. # the user is deactivated, the threepid will be deleted
  497. # Get user
  498. request, channel = self.make_request(
  499. "GET", self.url_other_user, access_token=self.admin_user_tok,
  500. )
  501. self.render(request)
  502. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  503. self.assertEqual("@user:test", channel.json_body["name"])
  504. self.assertEqual(True, channel.json_body["deactivated"])
  505. def test_set_user_as_admin(self):
  506. """
  507. Test setting the admin flag on a user.
  508. """
  509. self.hs.config.registration_shared_secret = None
  510. # Set a user as an admin
  511. body = json.dumps({"admin": True})
  512. request, channel = self.make_request(
  513. "PUT",
  514. self.url_other_user,
  515. access_token=self.admin_user_tok,
  516. content=body.encode(encoding="utf_8"),
  517. )
  518. self.render(request)
  519. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  520. self.assertEqual("@user:test", channel.json_body["name"])
  521. self.assertEqual(True, channel.json_body["admin"])
  522. # Get user
  523. request, channel = self.make_request(
  524. "GET", self.url_other_user, access_token=self.admin_user_tok,
  525. )
  526. self.render(request)
  527. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  528. self.assertEqual("@user:test", channel.json_body["name"])
  529. self.assertEqual(True, channel.json_body["admin"])
  530. def test_accidental_deactivation_prevention(self):
  531. """
  532. Ensure an account can't accidentally be deactivated by using a str value
  533. for the deactivated body parameter
  534. """
  535. self.hs.config.registration_shared_secret = None
  536. url = "/_synapse/admin/v2/users/@bob:test"
  537. # Create user
  538. body = json.dumps({"password": "abc123"})
  539. request, channel = self.make_request(
  540. "PUT",
  541. url,
  542. access_token=self.admin_user_tok,
  543. content=body.encode(encoding="utf_8"),
  544. )
  545. self.render(request)
  546. self.assertEqual(201, int(channel.result["code"]), msg=channel.result["body"])
  547. self.assertEqual("@bob:test", channel.json_body["name"])
  548. self.assertEqual("bob", channel.json_body["displayname"])
  549. # Get user
  550. request, channel = self.make_request(
  551. "GET", url, access_token=self.admin_user_tok,
  552. )
  553. self.render(request)
  554. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  555. self.assertEqual("@bob:test", channel.json_body["name"])
  556. self.assertEqual("bob", channel.json_body["displayname"])
  557. self.assertEqual(0, channel.json_body["deactivated"])
  558. # Change password (and use a str for deactivate instead of a bool)
  559. body = json.dumps({"password": "abc123", "deactivated": "false"}) # oops!
  560. request, channel = self.make_request(
  561. "PUT",
  562. url,
  563. access_token=self.admin_user_tok,
  564. content=body.encode(encoding="utf_8"),
  565. )
  566. self.render(request)
  567. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  568. # Check user is not deactivated
  569. request, channel = self.make_request(
  570. "GET", url, access_token=self.admin_user_tok,
  571. )
  572. self.render(request)
  573. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  574. self.assertEqual("@bob:test", channel.json_body["name"])
  575. self.assertEqual("bob", channel.json_body["displayname"])
  576. # Ensure they're still alive
  577. self.assertEqual(0, channel.json_body["deactivated"])