test_registration_tokens.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. # Copyright 2021 Callum Brown
  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. import random
  15. import string
  16. import synapse.rest.admin
  17. from synapse.api.errors import Codes
  18. from synapse.rest.client import login
  19. from tests import unittest
  20. class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
  21. servlets = [
  22. synapse.rest.admin.register_servlets,
  23. login.register_servlets,
  24. ]
  25. def prepare(self, reactor, clock, hs):
  26. self.store = hs.get_datastore()
  27. self.admin_user = self.register_user("admin", "pass", admin=True)
  28. self.admin_user_tok = self.login("admin", "pass")
  29. self.other_user = self.register_user("user", "pass")
  30. self.other_user_tok = self.login("user", "pass")
  31. self.url = "/_synapse/admin/v1/registration_tokens"
  32. def _new_token(self, **kwargs):
  33. """Helper function to create a token."""
  34. token = kwargs.get(
  35. "token",
  36. "".join(random.choices(string.ascii_letters, k=8)),
  37. )
  38. self.get_success(
  39. self.store.db_pool.simple_insert(
  40. "registration_tokens",
  41. {
  42. "token": token,
  43. "uses_allowed": kwargs.get("uses_allowed", None),
  44. "pending": kwargs.get("pending", 0),
  45. "completed": kwargs.get("completed", 0),
  46. "expiry_time": kwargs.get("expiry_time", None),
  47. },
  48. )
  49. )
  50. return token
  51. # CREATION
  52. def test_create_no_auth(self):
  53. """Try to create a token without authentication."""
  54. channel = self.make_request("POST", self.url + "/new", {})
  55. self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
  56. self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
  57. def test_create_requester_not_admin(self):
  58. """Try to create a token while not an admin."""
  59. channel = self.make_request(
  60. "POST",
  61. self.url + "/new",
  62. {},
  63. access_token=self.other_user_tok,
  64. )
  65. self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
  66. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  67. def test_create_using_defaults(self):
  68. """Create a token using all the defaults."""
  69. channel = self.make_request(
  70. "POST",
  71. self.url + "/new",
  72. {},
  73. access_token=self.admin_user_tok,
  74. )
  75. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  76. self.assertEqual(len(channel.json_body["token"]), 16)
  77. self.assertIsNone(channel.json_body["uses_allowed"])
  78. self.assertIsNone(channel.json_body["expiry_time"])
  79. self.assertEqual(channel.json_body["pending"], 0)
  80. self.assertEqual(channel.json_body["completed"], 0)
  81. def test_create_specifying_fields(self):
  82. """Create a token specifying the value of all fields."""
  83. # As many of the allowed characters as possible with length <= 64
  84. token = "adefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._~-"
  85. data = {
  86. "token": token,
  87. "uses_allowed": 1,
  88. "expiry_time": self.clock.time_msec() + 1000000,
  89. }
  90. channel = self.make_request(
  91. "POST",
  92. self.url + "/new",
  93. data,
  94. access_token=self.admin_user_tok,
  95. )
  96. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  97. self.assertEqual(channel.json_body["token"], token)
  98. self.assertEqual(channel.json_body["uses_allowed"], 1)
  99. self.assertEqual(channel.json_body["expiry_time"], data["expiry_time"])
  100. self.assertEqual(channel.json_body["pending"], 0)
  101. self.assertEqual(channel.json_body["completed"], 0)
  102. def test_create_with_null_value(self):
  103. """Create a token specifying unlimited uses and no expiry."""
  104. data = {
  105. "uses_allowed": None,
  106. "expiry_time": None,
  107. }
  108. channel = self.make_request(
  109. "POST",
  110. self.url + "/new",
  111. data,
  112. access_token=self.admin_user_tok,
  113. )
  114. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  115. self.assertEqual(len(channel.json_body["token"]), 16)
  116. self.assertIsNone(channel.json_body["uses_allowed"])
  117. self.assertIsNone(channel.json_body["expiry_time"])
  118. self.assertEqual(channel.json_body["pending"], 0)
  119. self.assertEqual(channel.json_body["completed"], 0)
  120. def test_create_token_too_long(self):
  121. """Check token longer than 64 chars is invalid."""
  122. data = {"token": "a" * 65}
  123. channel = self.make_request(
  124. "POST",
  125. self.url + "/new",
  126. data,
  127. access_token=self.admin_user_tok,
  128. )
  129. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  130. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  131. def test_create_token_invalid_chars(self):
  132. """Check you can't create token with invalid characters."""
  133. data = {
  134. "token": "abc/def",
  135. }
  136. channel = self.make_request(
  137. "POST",
  138. self.url + "/new",
  139. data,
  140. access_token=self.admin_user_tok,
  141. )
  142. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  143. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  144. def test_create_token_already_exists(self):
  145. """Check you can't create token that already exists."""
  146. data = {
  147. "token": "abcd",
  148. }
  149. channel1 = self.make_request(
  150. "POST",
  151. self.url + "/new",
  152. data,
  153. access_token=self.admin_user_tok,
  154. )
  155. self.assertEqual(200, int(channel1.result["code"]), msg=channel1.result["body"])
  156. channel2 = self.make_request(
  157. "POST",
  158. self.url + "/new",
  159. data,
  160. access_token=self.admin_user_tok,
  161. )
  162. self.assertEqual(400, int(channel2.result["code"]), msg=channel2.result["body"])
  163. self.assertEqual(channel2.json_body["errcode"], Codes.INVALID_PARAM)
  164. def test_create_unable_to_generate_token(self):
  165. """Check right error is raised when server can't generate unique token."""
  166. # Create all possible single character tokens
  167. tokens = []
  168. for c in string.ascii_letters + string.digits + "._~-":
  169. tokens.append(
  170. {
  171. "token": c,
  172. "uses_allowed": None,
  173. "pending": 0,
  174. "completed": 0,
  175. "expiry_time": None,
  176. }
  177. )
  178. self.get_success(
  179. self.store.db_pool.simple_insert_many(
  180. "registration_tokens",
  181. tokens,
  182. "create_all_registration_tokens",
  183. )
  184. )
  185. # Check creating a single character token fails with a 500 status code
  186. channel = self.make_request(
  187. "POST",
  188. self.url + "/new",
  189. {"length": 1},
  190. access_token=self.admin_user_tok,
  191. )
  192. self.assertEqual(500, int(channel.result["code"]), msg=channel.result["body"])
  193. def test_create_uses_allowed(self):
  194. """Check you can only create a token with good values for uses_allowed."""
  195. # Should work with 0 (token is invalid from the start)
  196. channel = self.make_request(
  197. "POST",
  198. self.url + "/new",
  199. {"uses_allowed": 0},
  200. access_token=self.admin_user_tok,
  201. )
  202. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  203. self.assertEqual(channel.json_body["uses_allowed"], 0)
  204. # Should fail with negative integer
  205. channel = self.make_request(
  206. "POST",
  207. self.url + "/new",
  208. {"uses_allowed": -5},
  209. access_token=self.admin_user_tok,
  210. )
  211. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  212. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  213. # Should fail with float
  214. channel = self.make_request(
  215. "POST",
  216. self.url + "/new",
  217. {"uses_allowed": 1.5},
  218. access_token=self.admin_user_tok,
  219. )
  220. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  221. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  222. def test_create_expiry_time(self):
  223. """Check you can't create a token with an invalid expiry_time."""
  224. # Should fail with a time in the past
  225. channel = self.make_request(
  226. "POST",
  227. self.url + "/new",
  228. {"expiry_time": self.clock.time_msec() - 10000},
  229. access_token=self.admin_user_tok,
  230. )
  231. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  232. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  233. # Should fail with float
  234. channel = self.make_request(
  235. "POST",
  236. self.url + "/new",
  237. {"expiry_time": self.clock.time_msec() + 1000000.5},
  238. access_token=self.admin_user_tok,
  239. )
  240. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  241. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  242. def test_create_length(self):
  243. """Check you can only generate a token with a valid length."""
  244. # Should work with 64
  245. channel = self.make_request(
  246. "POST",
  247. self.url + "/new",
  248. {"length": 64},
  249. access_token=self.admin_user_tok,
  250. )
  251. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  252. self.assertEqual(len(channel.json_body["token"]), 64)
  253. # Should fail with 0
  254. channel = self.make_request(
  255. "POST",
  256. self.url + "/new",
  257. {"length": 0},
  258. access_token=self.admin_user_tok,
  259. )
  260. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  261. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  262. # Should fail with a negative integer
  263. channel = self.make_request(
  264. "POST",
  265. self.url + "/new",
  266. {"length": -5},
  267. access_token=self.admin_user_tok,
  268. )
  269. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  270. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  271. # Should fail with a float
  272. channel = self.make_request(
  273. "POST",
  274. self.url + "/new",
  275. {"length": 8.5},
  276. access_token=self.admin_user_tok,
  277. )
  278. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  279. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  280. # Should fail with 65
  281. channel = self.make_request(
  282. "POST",
  283. self.url + "/new",
  284. {"length": 65},
  285. access_token=self.admin_user_tok,
  286. )
  287. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  288. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  289. # UPDATING
  290. def test_update_no_auth(self):
  291. """Try to update a token without authentication."""
  292. channel = self.make_request(
  293. "PUT",
  294. self.url + "/1234", # Token doesn't exist but that doesn't matter
  295. {},
  296. )
  297. self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
  298. self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
  299. def test_update_requester_not_admin(self):
  300. """Try to update a token while not an admin."""
  301. channel = self.make_request(
  302. "PUT",
  303. self.url + "/1234", # Token doesn't exist but that doesn't matter
  304. {},
  305. access_token=self.other_user_tok,
  306. )
  307. self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
  308. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  309. def test_update_non_existent(self):
  310. """Try to update a token that doesn't exist."""
  311. channel = self.make_request(
  312. "PUT",
  313. self.url + "/1234",
  314. {"uses_allowed": 1},
  315. access_token=self.admin_user_tok,
  316. )
  317. self.assertEqual(404, int(channel.result["code"]), msg=channel.result["body"])
  318. self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
  319. def test_update_uses_allowed(self):
  320. """Test updating just uses_allowed."""
  321. # Create new token using default values
  322. token = self._new_token()
  323. # Should succeed with 1
  324. channel = self.make_request(
  325. "PUT",
  326. self.url + "/" + token,
  327. {"uses_allowed": 1},
  328. access_token=self.admin_user_tok,
  329. )
  330. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  331. self.assertEqual(channel.json_body["uses_allowed"], 1)
  332. self.assertIsNone(channel.json_body["expiry_time"])
  333. # Should succeed with 0 (makes token invalid)
  334. channel = self.make_request(
  335. "PUT",
  336. self.url + "/" + token,
  337. {"uses_allowed": 0},
  338. access_token=self.admin_user_tok,
  339. )
  340. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  341. self.assertEqual(channel.json_body["uses_allowed"], 0)
  342. self.assertIsNone(channel.json_body["expiry_time"])
  343. # Should succeed with null
  344. channel = self.make_request(
  345. "PUT",
  346. self.url + "/" + token,
  347. {"uses_allowed": None},
  348. access_token=self.admin_user_tok,
  349. )
  350. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  351. self.assertIsNone(channel.json_body["uses_allowed"])
  352. self.assertIsNone(channel.json_body["expiry_time"])
  353. # Should fail with a float
  354. channel = self.make_request(
  355. "PUT",
  356. self.url + "/" + token,
  357. {"uses_allowed": 1.5},
  358. access_token=self.admin_user_tok,
  359. )
  360. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  361. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  362. # Should fail with a negative integer
  363. channel = self.make_request(
  364. "PUT",
  365. self.url + "/" + token,
  366. {"uses_allowed": -5},
  367. access_token=self.admin_user_tok,
  368. )
  369. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  370. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  371. def test_update_expiry_time(self):
  372. """Test updating just expiry_time."""
  373. # Create new token using default values
  374. token = self._new_token()
  375. new_expiry_time = self.clock.time_msec() + 1000000
  376. # Should succeed with a time in the future
  377. channel = self.make_request(
  378. "PUT",
  379. self.url + "/" + token,
  380. {"expiry_time": new_expiry_time},
  381. access_token=self.admin_user_tok,
  382. )
  383. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  384. self.assertEqual(channel.json_body["expiry_time"], new_expiry_time)
  385. self.assertIsNone(channel.json_body["uses_allowed"])
  386. # Should succeed with null
  387. channel = self.make_request(
  388. "PUT",
  389. self.url + "/" + token,
  390. {"expiry_time": None},
  391. access_token=self.admin_user_tok,
  392. )
  393. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  394. self.assertIsNone(channel.json_body["expiry_time"])
  395. self.assertIsNone(channel.json_body["uses_allowed"])
  396. # Should fail with a time in the past
  397. past_time = self.clock.time_msec() - 10000
  398. channel = self.make_request(
  399. "PUT",
  400. self.url + "/" + token,
  401. {"expiry_time": past_time},
  402. access_token=self.admin_user_tok,
  403. )
  404. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  405. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  406. # Should fail a float
  407. channel = self.make_request(
  408. "PUT",
  409. self.url + "/" + token,
  410. {"expiry_time": new_expiry_time + 0.5},
  411. access_token=self.admin_user_tok,
  412. )
  413. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  414. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  415. def test_update_both(self):
  416. """Test updating both uses_allowed and expiry_time."""
  417. # Create new token using default values
  418. token = self._new_token()
  419. new_expiry_time = self.clock.time_msec() + 1000000
  420. data = {
  421. "uses_allowed": 1,
  422. "expiry_time": new_expiry_time,
  423. }
  424. channel = self.make_request(
  425. "PUT",
  426. self.url + "/" + token,
  427. data,
  428. access_token=self.admin_user_tok,
  429. )
  430. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  431. self.assertEqual(channel.json_body["uses_allowed"], 1)
  432. self.assertEqual(channel.json_body["expiry_time"], new_expiry_time)
  433. def test_update_invalid_type(self):
  434. """Test using invalid types doesn't work."""
  435. # Create new token using default values
  436. token = self._new_token()
  437. data = {
  438. "uses_allowed": False,
  439. "expiry_time": "1626430124000",
  440. }
  441. channel = self.make_request(
  442. "PUT",
  443. self.url + "/" + token,
  444. data,
  445. access_token=self.admin_user_tok,
  446. )
  447. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  448. self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
  449. # DELETING
  450. def test_delete_no_auth(self):
  451. """Try to delete a token without authentication."""
  452. channel = self.make_request(
  453. "DELETE",
  454. self.url + "/1234", # Token doesn't exist but that doesn't matter
  455. {},
  456. )
  457. self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
  458. self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
  459. def test_delete_requester_not_admin(self):
  460. """Try to delete a token while not an admin."""
  461. channel = self.make_request(
  462. "DELETE",
  463. self.url + "/1234", # Token doesn't exist but that doesn't matter
  464. {},
  465. access_token=self.other_user_tok,
  466. )
  467. self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
  468. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  469. def test_delete_non_existent(self):
  470. """Try to delete a token that doesn't exist."""
  471. channel = self.make_request(
  472. "DELETE",
  473. self.url + "/1234",
  474. {},
  475. access_token=self.admin_user_tok,
  476. )
  477. self.assertEqual(404, int(channel.result["code"]), msg=channel.result["body"])
  478. self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
  479. def test_delete(self):
  480. """Test deleting a token."""
  481. # Create new token using default values
  482. token = self._new_token()
  483. channel = self.make_request(
  484. "DELETE",
  485. self.url + "/" + token,
  486. {},
  487. access_token=self.admin_user_tok,
  488. )
  489. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  490. # GETTING ONE
  491. def test_get_no_auth(self):
  492. """Try to get a token without authentication."""
  493. channel = self.make_request(
  494. "GET",
  495. self.url + "/1234", # Token doesn't exist but that doesn't matter
  496. {},
  497. )
  498. self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
  499. self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
  500. def test_get_requester_not_admin(self):
  501. """Try to get a token while not an admin."""
  502. channel = self.make_request(
  503. "GET",
  504. self.url + "/1234", # Token doesn't exist but that doesn't matter
  505. {},
  506. access_token=self.other_user_tok,
  507. )
  508. self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
  509. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  510. def test_get_non_existent(self):
  511. """Try to get a token that doesn't exist."""
  512. channel = self.make_request(
  513. "GET",
  514. self.url + "/1234",
  515. {},
  516. access_token=self.admin_user_tok,
  517. )
  518. self.assertEqual(404, int(channel.result["code"]), msg=channel.result["body"])
  519. self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
  520. def test_get(self):
  521. """Test getting a token."""
  522. # Create new token using default values
  523. token = self._new_token()
  524. channel = self.make_request(
  525. "GET",
  526. self.url + "/" + token,
  527. {},
  528. access_token=self.admin_user_tok,
  529. )
  530. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  531. self.assertEqual(channel.json_body["token"], token)
  532. self.assertIsNone(channel.json_body["uses_allowed"])
  533. self.assertIsNone(channel.json_body["expiry_time"])
  534. self.assertEqual(channel.json_body["pending"], 0)
  535. self.assertEqual(channel.json_body["completed"], 0)
  536. # LISTING
  537. def test_list_no_auth(self):
  538. """Try to list tokens without authentication."""
  539. channel = self.make_request("GET", self.url, {})
  540. self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
  541. self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
  542. def test_list_requester_not_admin(self):
  543. """Try to list tokens while not an admin."""
  544. channel = self.make_request(
  545. "GET",
  546. self.url,
  547. {},
  548. access_token=self.other_user_tok,
  549. )
  550. self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
  551. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  552. def test_list_all(self):
  553. """Test listing all tokens."""
  554. # Create new token using default values
  555. token = self._new_token()
  556. channel = self.make_request(
  557. "GET",
  558. self.url,
  559. {},
  560. access_token=self.admin_user_tok,
  561. )
  562. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  563. self.assertEqual(len(channel.json_body["registration_tokens"]), 1)
  564. token_info = channel.json_body["registration_tokens"][0]
  565. self.assertEqual(token_info["token"], token)
  566. self.assertIsNone(token_info["uses_allowed"])
  567. self.assertIsNone(token_info["expiry_time"])
  568. self.assertEqual(token_info["pending"], 0)
  569. self.assertEqual(token_info["completed"], 0)
  570. def test_list_invalid_query_parameter(self):
  571. """Test with `valid` query parameter not `true` or `false`."""
  572. channel = self.make_request(
  573. "GET",
  574. self.url + "?valid=x",
  575. {},
  576. access_token=self.admin_user_tok,
  577. )
  578. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  579. def _test_list_query_parameter(self, valid: str):
  580. """Helper used to test both valid=true and valid=false."""
  581. # Create 2 valid and 2 invalid tokens.
  582. now = self.hs.get_clock().time_msec()
  583. # Create always valid token
  584. valid1 = self._new_token()
  585. # Create token that hasn't been used up
  586. valid2 = self._new_token(uses_allowed=1)
  587. # Create token that has expired
  588. invalid1 = self._new_token(expiry_time=now - 10000)
  589. # Create token that has been used up but hasn't expired
  590. invalid2 = self._new_token(
  591. uses_allowed=2,
  592. pending=1,
  593. completed=1,
  594. expiry_time=now + 1000000,
  595. )
  596. if valid == "true":
  597. tokens = [valid1, valid2]
  598. else:
  599. tokens = [invalid1, invalid2]
  600. channel = self.make_request(
  601. "GET",
  602. self.url + "?valid=" + valid,
  603. {},
  604. access_token=self.admin_user_tok,
  605. )
  606. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  607. self.assertEqual(len(channel.json_body["registration_tokens"]), 2)
  608. token_info_1 = channel.json_body["registration_tokens"][0]
  609. token_info_2 = channel.json_body["registration_tokens"][1]
  610. self.assertIn(token_info_1["token"], tokens)
  611. self.assertIn(token_info_2["token"], tokens)
  612. def test_list_valid(self):
  613. """Test listing just valid tokens."""
  614. self._test_list_query_parameter(valid="true")
  615. def test_list_invalid(self):
  616. """Test listing just invalid tokens."""
  617. self._test_list_query_parameter(valid="false")