test_federation.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  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. from typing import List, Optional
  15. from parameterized import parameterized
  16. from twisted.test.proto_helpers import MemoryReactor
  17. import synapse.rest.admin
  18. from synapse.api.errors import Codes
  19. from synapse.rest.client import login, room
  20. from synapse.server import HomeServer
  21. from synapse.types import JsonDict
  22. from synapse.util import Clock
  23. from tests import unittest
  24. class FederationTestCase(unittest.HomeserverTestCase):
  25. servlets = [
  26. synapse.rest.admin.register_servlets,
  27. login.register_servlets,
  28. ]
  29. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  30. self.store = hs.get_datastores().main
  31. self.register_user("admin", "pass", admin=True)
  32. self.admin_user_tok = self.login("admin", "pass")
  33. self.url = "/_synapse/admin/v1/federation/destinations"
  34. @parameterized.expand(
  35. [
  36. ("GET", "/_synapse/admin/v1/federation/destinations"),
  37. ("GET", "/_synapse/admin/v1/federation/destinations/dummy"),
  38. (
  39. "POST",
  40. "/_synapse/admin/v1/federation/destinations/dummy/reset_connection",
  41. ),
  42. ]
  43. )
  44. def test_requester_is_no_admin(self, method: str, url: str) -> None:
  45. """If the user is not a server admin, an error 403 is returned."""
  46. self.register_user("user", "pass", admin=False)
  47. other_user_tok = self.login("user", "pass")
  48. channel = self.make_request(
  49. method,
  50. url,
  51. content={},
  52. access_token=other_user_tok,
  53. )
  54. self.assertEqual(403, channel.code, msg=channel.json_body)
  55. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  56. def test_invalid_parameter(self) -> None:
  57. """If parameters are invalid, an error is returned."""
  58. # negative limit
  59. channel = self.make_request(
  60. "GET",
  61. self.url + "?limit=-5",
  62. access_token=self.admin_user_tok,
  63. )
  64. self.assertEqual(400, channel.code, msg=channel.json_body)
  65. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  66. # negative from
  67. channel = self.make_request(
  68. "GET",
  69. self.url + "?from=-5",
  70. access_token=self.admin_user_tok,
  71. )
  72. self.assertEqual(400, channel.code, msg=channel.json_body)
  73. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  74. # unkown order_by
  75. channel = self.make_request(
  76. "GET",
  77. self.url + "?order_by=bar",
  78. access_token=self.admin_user_tok,
  79. )
  80. self.assertEqual(400, channel.code, msg=channel.json_body)
  81. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  82. # invalid search order
  83. channel = self.make_request(
  84. "GET",
  85. self.url + "?dir=bar",
  86. access_token=self.admin_user_tok,
  87. )
  88. self.assertEqual(400, channel.code, msg=channel.json_body)
  89. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  90. # invalid destination
  91. channel = self.make_request(
  92. "GET",
  93. self.url + "/dummy",
  94. access_token=self.admin_user_tok,
  95. )
  96. self.assertEqual(404, channel.code, msg=channel.json_body)
  97. self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
  98. # invalid destination
  99. channel = self.make_request(
  100. "POST",
  101. self.url + "/dummy/reset_connection",
  102. access_token=self.admin_user_tok,
  103. )
  104. self.assertEqual(404, channel.code, msg=channel.json_body)
  105. self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
  106. def test_limit(self) -> None:
  107. """Testing list of destinations with limit"""
  108. number_destinations = 20
  109. self._create_destinations(number_destinations)
  110. channel = self.make_request(
  111. "GET",
  112. self.url + "?limit=5",
  113. access_token=self.admin_user_tok,
  114. )
  115. self.assertEqual(200, channel.code, msg=channel.json_body)
  116. self.assertEqual(channel.json_body["total"], number_destinations)
  117. self.assertEqual(len(channel.json_body["destinations"]), 5)
  118. self.assertEqual(channel.json_body["next_token"], "5")
  119. self._check_fields(channel.json_body["destinations"])
  120. def test_from(self) -> None:
  121. """Testing list of destinations with a defined starting point (from)"""
  122. number_destinations = 20
  123. self._create_destinations(number_destinations)
  124. channel = self.make_request(
  125. "GET",
  126. self.url + "?from=5",
  127. access_token=self.admin_user_tok,
  128. )
  129. self.assertEqual(200, channel.code, msg=channel.json_body)
  130. self.assertEqual(channel.json_body["total"], number_destinations)
  131. self.assertEqual(len(channel.json_body["destinations"]), 15)
  132. self.assertNotIn("next_token", channel.json_body)
  133. self._check_fields(channel.json_body["destinations"])
  134. def test_limit_and_from(self) -> None:
  135. """Testing list of destinations with a defined starting point and limit"""
  136. number_destinations = 20
  137. self._create_destinations(number_destinations)
  138. channel = self.make_request(
  139. "GET",
  140. self.url + "?from=5&limit=10",
  141. access_token=self.admin_user_tok,
  142. )
  143. self.assertEqual(200, channel.code, msg=channel.json_body)
  144. self.assertEqual(channel.json_body["total"], number_destinations)
  145. self.assertEqual(channel.json_body["next_token"], "15")
  146. self.assertEqual(len(channel.json_body["destinations"]), 10)
  147. self._check_fields(channel.json_body["destinations"])
  148. def test_next_token(self) -> None:
  149. """Testing that `next_token` appears at the right place"""
  150. number_destinations = 20
  151. self._create_destinations(number_destinations)
  152. # `next_token` does not appear
  153. # Number of results is the number of entries
  154. channel = self.make_request(
  155. "GET",
  156. self.url + "?limit=20",
  157. access_token=self.admin_user_tok,
  158. )
  159. self.assertEqual(200, channel.code, msg=channel.json_body)
  160. self.assertEqual(channel.json_body["total"], number_destinations)
  161. self.assertEqual(len(channel.json_body["destinations"]), number_destinations)
  162. self.assertNotIn("next_token", channel.json_body)
  163. # `next_token` does not appear
  164. # Number of max results is larger than the number of entries
  165. channel = self.make_request(
  166. "GET",
  167. self.url + "?limit=21",
  168. access_token=self.admin_user_tok,
  169. )
  170. self.assertEqual(200, channel.code, msg=channel.json_body)
  171. self.assertEqual(channel.json_body["total"], number_destinations)
  172. self.assertEqual(len(channel.json_body["destinations"]), number_destinations)
  173. self.assertNotIn("next_token", channel.json_body)
  174. # `next_token` does appear
  175. # Number of max results is smaller than the number of entries
  176. channel = self.make_request(
  177. "GET",
  178. self.url + "?limit=19",
  179. access_token=self.admin_user_tok,
  180. )
  181. self.assertEqual(200, channel.code, msg=channel.json_body)
  182. self.assertEqual(channel.json_body["total"], number_destinations)
  183. self.assertEqual(len(channel.json_body["destinations"]), 19)
  184. self.assertEqual(channel.json_body["next_token"], "19")
  185. # Check
  186. # Set `from` to value of `next_token` for request remaining entries
  187. # `next_token` does not appear
  188. channel = self.make_request(
  189. "GET",
  190. self.url + "?from=19",
  191. access_token=self.admin_user_tok,
  192. )
  193. self.assertEqual(200, channel.code, msg=channel.json_body)
  194. self.assertEqual(channel.json_body["total"], number_destinations)
  195. self.assertEqual(len(channel.json_body["destinations"]), 1)
  196. self.assertNotIn("next_token", channel.json_body)
  197. def test_list_all_destinations(self) -> None:
  198. """List all destinations."""
  199. number_destinations = 5
  200. self._create_destinations(number_destinations)
  201. channel = self.make_request(
  202. "GET",
  203. self.url,
  204. {},
  205. access_token=self.admin_user_tok,
  206. )
  207. self.assertEqual(200, channel.code, msg=channel.json_body)
  208. self.assertEqual(number_destinations, len(channel.json_body["destinations"]))
  209. self.assertEqual(number_destinations, channel.json_body["total"])
  210. # Check that all fields are available
  211. self._check_fields(channel.json_body["destinations"])
  212. def test_order_by(self) -> None:
  213. """Testing order list with parameter `order_by`"""
  214. def _order_test(
  215. expected_destination_list: List[str],
  216. order_by: Optional[str],
  217. dir: Optional[str] = None,
  218. ) -> None:
  219. """Request the list of destinations in a certain order.
  220. Assert that order is what we expect
  221. Args:
  222. expected_destination_list: The list of user_id in the order
  223. we expect to get back from the server
  224. order_by: The type of ordering to give the server
  225. dir: The direction of ordering to give the server
  226. """
  227. url = f"{self.url}?"
  228. if order_by is not None:
  229. url += f"order_by={order_by}&"
  230. if dir is not None and dir in ("b", "f"):
  231. url += f"dir={dir}"
  232. channel = self.make_request(
  233. "GET",
  234. url,
  235. access_token=self.admin_user_tok,
  236. )
  237. self.assertEqual(200, channel.code, msg=channel.json_body)
  238. self.assertEqual(channel.json_body["total"], len(expected_destination_list))
  239. returned_order = [
  240. row["destination"] for row in channel.json_body["destinations"]
  241. ]
  242. self.assertEqual(expected_destination_list, returned_order)
  243. self._check_fields(channel.json_body["destinations"])
  244. # create destinations
  245. dest = [
  246. ("sub-a.example.com", 100, 300, 200, 300),
  247. ("sub-b.example.com", 200, 200, 100, 100),
  248. ("sub-c.example.com", 300, 100, 300, 200),
  249. ]
  250. for (
  251. destination,
  252. failure_ts,
  253. retry_last_ts,
  254. retry_interval,
  255. last_successful_stream_ordering,
  256. ) in dest:
  257. self._create_destination(
  258. destination,
  259. failure_ts,
  260. retry_last_ts,
  261. retry_interval,
  262. last_successful_stream_ordering,
  263. )
  264. # order by default (destination)
  265. _order_test([dest[0][0], dest[1][0], dest[2][0]], None)
  266. _order_test([dest[0][0], dest[1][0], dest[2][0]], None, "f")
  267. _order_test([dest[2][0], dest[1][0], dest[0][0]], None, "b")
  268. # order by destination
  269. _order_test([dest[0][0], dest[1][0], dest[2][0]], "destination")
  270. _order_test([dest[0][0], dest[1][0], dest[2][0]], "destination", "f")
  271. _order_test([dest[2][0], dest[1][0], dest[0][0]], "destination", "b")
  272. # order by failure_ts
  273. _order_test([dest[0][0], dest[1][0], dest[2][0]], "failure_ts")
  274. _order_test([dest[0][0], dest[1][0], dest[2][0]], "failure_ts", "f")
  275. _order_test([dest[2][0], dest[1][0], dest[0][0]], "failure_ts", "b")
  276. # order by retry_last_ts
  277. _order_test([dest[2][0], dest[1][0], dest[0][0]], "retry_last_ts")
  278. _order_test([dest[2][0], dest[1][0], dest[0][0]], "retry_last_ts", "f")
  279. _order_test([dest[0][0], dest[1][0], dest[2][0]], "retry_last_ts", "b")
  280. # order by retry_interval
  281. _order_test([dest[1][0], dest[0][0], dest[2][0]], "retry_interval")
  282. _order_test([dest[1][0], dest[0][0], dest[2][0]], "retry_interval", "f")
  283. _order_test([dest[2][0], dest[0][0], dest[1][0]], "retry_interval", "b")
  284. # order by last_successful_stream_ordering
  285. _order_test(
  286. [dest[1][0], dest[2][0], dest[0][0]], "last_successful_stream_ordering"
  287. )
  288. _order_test(
  289. [dest[1][0], dest[2][0], dest[0][0]], "last_successful_stream_ordering", "f"
  290. )
  291. _order_test(
  292. [dest[0][0], dest[2][0], dest[1][0]], "last_successful_stream_ordering", "b"
  293. )
  294. def test_search_term(self) -> None:
  295. """Test that searching for a destination works correctly"""
  296. def _search_test(
  297. expected_destination: Optional[str],
  298. search_term: str,
  299. ) -> None:
  300. """Search for a destination and check that the returned destinationis a match
  301. Args:
  302. expected_destination: The room_id expected to be returned by the API.
  303. Set to None to expect zero results for the search
  304. search_term: The term to search for room names with
  305. """
  306. url = f"{self.url}?destination={search_term}"
  307. channel = self.make_request(
  308. "GET",
  309. url.encode("ascii"),
  310. access_token=self.admin_user_tok,
  311. )
  312. self.assertEqual(200, channel.code, msg=channel.json_body)
  313. # Check that destinations were returned
  314. self.assertTrue("destinations" in channel.json_body)
  315. self._check_fields(channel.json_body["destinations"])
  316. destinations = channel.json_body["destinations"]
  317. # Check that the expected number of destinations were returned
  318. expected_destination_count = 1 if expected_destination else 0
  319. self.assertEqual(len(destinations), expected_destination_count)
  320. self.assertEqual(channel.json_body["total"], expected_destination_count)
  321. if expected_destination:
  322. # Check that the first returned destination is correct
  323. self.assertEqual(expected_destination, destinations[0]["destination"])
  324. number_destinations = 3
  325. self._create_destinations(number_destinations)
  326. # Test searching
  327. _search_test("sub0.example.com", "0")
  328. _search_test("sub0.example.com", "sub0")
  329. _search_test("sub1.example.com", "1")
  330. _search_test("sub1.example.com", "1.")
  331. # Test case insensitive
  332. _search_test("sub0.example.com", "SUB0")
  333. _search_test(None, "foo")
  334. _search_test(None, "bar")
  335. def test_get_single_destination_with_retry_timings(self) -> None:
  336. """Get one specific destination which has retry timings."""
  337. self._create_destinations(1)
  338. channel = self.make_request(
  339. "GET",
  340. self.url + "/sub0.example.com",
  341. access_token=self.admin_user_tok,
  342. )
  343. self.assertEqual(200, channel.code, msg=channel.json_body)
  344. self.assertEqual("sub0.example.com", channel.json_body["destination"])
  345. # Check that all fields are available
  346. # convert channel.json_body into a List
  347. self._check_fields([channel.json_body])
  348. def test_get_single_destination_no_retry_timings(self) -> None:
  349. """Get one specific destination which has no retry timings."""
  350. self._create_destination("sub0.example.com")
  351. channel = self.make_request(
  352. "GET",
  353. self.url + "/sub0.example.com",
  354. access_token=self.admin_user_tok,
  355. )
  356. self.assertEqual(200, channel.code, msg=channel.json_body)
  357. self.assertEqual("sub0.example.com", channel.json_body["destination"])
  358. self.assertEqual(0, channel.json_body["retry_last_ts"])
  359. self.assertEqual(0, channel.json_body["retry_interval"])
  360. self.assertIsNone(channel.json_body["failure_ts"])
  361. self.assertIsNone(channel.json_body["last_successful_stream_ordering"])
  362. def test_destination_reset_connection(self) -> None:
  363. """Reset timeouts and wake up destination."""
  364. self._create_destination("sub0.example.com", 100, 100, 100)
  365. channel = self.make_request(
  366. "POST",
  367. self.url + "/sub0.example.com/reset_connection",
  368. access_token=self.admin_user_tok,
  369. )
  370. self.assertEqual(200, channel.code, msg=channel.json_body)
  371. retry_timings = self.get_success(
  372. self.store.get_destination_retry_timings("sub0.example.com")
  373. )
  374. self.assertIsNone(retry_timings)
  375. def test_destination_reset_connection_not_required(self) -> None:
  376. """Try to reset timeouts of a destination with no timeouts and get an error."""
  377. self._create_destination("sub0.example.com", None, 0, 0)
  378. channel = self.make_request(
  379. "POST",
  380. self.url + "/sub0.example.com/reset_connection",
  381. access_token=self.admin_user_tok,
  382. )
  383. self.assertEqual(400, channel.code, msg=channel.json_body)
  384. self.assertEqual(
  385. "The retry timing does not need to be reset for this destination.",
  386. channel.json_body["error"],
  387. )
  388. def _create_destination(
  389. self,
  390. destination: str,
  391. failure_ts: Optional[int] = None,
  392. retry_last_ts: int = 0,
  393. retry_interval: int = 0,
  394. last_successful_stream_ordering: Optional[int] = None,
  395. ) -> None:
  396. """Create one specific destination
  397. Args:
  398. destination: the destination we have successfully sent to
  399. failure_ts: when the server started failing (ms since epoch)
  400. retry_last_ts: time of last retry attempt in unix epoch ms
  401. retry_interval: how long until next retry in ms
  402. last_successful_stream_ordering: the stream_ordering of the most
  403. recent successfully-sent PDU
  404. """
  405. self.get_success(
  406. self.store.set_destination_retry_timings(
  407. destination, failure_ts, retry_last_ts, retry_interval
  408. )
  409. )
  410. if last_successful_stream_ordering is not None:
  411. self.get_success(
  412. self.store.set_destination_last_successful_stream_ordering(
  413. destination, last_successful_stream_ordering
  414. )
  415. )
  416. def _create_destinations(self, number_destinations: int) -> None:
  417. """Create a number of destinations
  418. Args:
  419. number_destinations: Number of destinations to be created
  420. """
  421. for i in range(0, number_destinations):
  422. dest = f"sub{i}.example.com"
  423. self._create_destination(dest, 50, 50, 50, 100)
  424. def _check_fields(self, content: List[JsonDict]) -> None:
  425. """Checks that the expected destination attributes are present in content
  426. Args:
  427. content: List that is checked for content
  428. """
  429. for c in content:
  430. self.assertIn("destination", c)
  431. self.assertIn("retry_last_ts", c)
  432. self.assertIn("retry_interval", c)
  433. self.assertIn("failure_ts", c)
  434. self.assertIn("last_successful_stream_ordering", c)
  435. class DestinationMembershipTestCase(unittest.HomeserverTestCase):
  436. servlets = [
  437. synapse.rest.admin.register_servlets,
  438. login.register_servlets,
  439. room.register_servlets,
  440. ]
  441. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  442. self.store = hs.get_datastores().main
  443. self.admin_user = self.register_user("admin", "pass", admin=True)
  444. self.admin_user_tok = self.login("admin", "pass")
  445. self.dest = "sub0.example.com"
  446. self.url = f"/_synapse/admin/v1/federation/destinations/{self.dest}/rooms"
  447. # Record that we successfully contacted a destination in the DB.
  448. self.get_success(
  449. self.store.set_destination_retry_timings(self.dest, None, 0, 0)
  450. )
  451. def test_requester_is_no_admin(self) -> None:
  452. """If the user is not a server admin, an error 403 is returned."""
  453. self.register_user("user", "pass", admin=False)
  454. other_user_tok = self.login("user", "pass")
  455. channel = self.make_request(
  456. "GET",
  457. self.url,
  458. access_token=other_user_tok,
  459. )
  460. self.assertEqual(403, channel.code, msg=channel.json_body)
  461. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  462. def test_invalid_parameter(self) -> None:
  463. """If parameters are invalid, an error is returned."""
  464. # negative limit
  465. channel = self.make_request(
  466. "GET",
  467. self.url + "?limit=-5",
  468. access_token=self.admin_user_tok,
  469. )
  470. self.assertEqual(400, channel.code, msg=channel.json_body)
  471. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  472. # negative from
  473. channel = self.make_request(
  474. "GET",
  475. self.url + "?from=-5",
  476. access_token=self.admin_user_tok,
  477. )
  478. self.assertEqual(400, channel.code, msg=channel.json_body)
  479. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  480. # invalid search order
  481. channel = self.make_request(
  482. "GET",
  483. self.url + "?dir=bar",
  484. access_token=self.admin_user_tok,
  485. )
  486. self.assertEqual(400, channel.code, msg=channel.json_body)
  487. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  488. # invalid destination
  489. channel = self.make_request(
  490. "GET",
  491. "/_synapse/admin/v1/federation/destinations/%s/rooms" % ("invalid",),
  492. access_token=self.admin_user_tok,
  493. )
  494. self.assertEqual(404, channel.code, msg=channel.json_body)
  495. self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
  496. def test_limit(self) -> None:
  497. """Testing list of destinations with limit"""
  498. number_rooms = 5
  499. self._create_destination_rooms(number_rooms)
  500. channel = self.make_request(
  501. "GET",
  502. self.url + "?limit=3",
  503. access_token=self.admin_user_tok,
  504. )
  505. self.assertEqual(200, channel.code, msg=channel.json_body)
  506. self.assertEqual(channel.json_body["total"], number_rooms)
  507. self.assertEqual(len(channel.json_body["rooms"]), 3)
  508. self.assertEqual(channel.json_body["next_token"], "3")
  509. self._check_fields(channel.json_body["rooms"])
  510. def test_from(self) -> None:
  511. """Testing list of rooms with a defined starting point (from)"""
  512. number_rooms = 10
  513. self._create_destination_rooms(number_rooms)
  514. channel = self.make_request(
  515. "GET",
  516. self.url + "?from=5",
  517. access_token=self.admin_user_tok,
  518. )
  519. self.assertEqual(200, channel.code, msg=channel.json_body)
  520. self.assertEqual(channel.json_body["total"], number_rooms)
  521. self.assertEqual(len(channel.json_body["rooms"]), 5)
  522. self.assertNotIn("next_token", channel.json_body)
  523. self._check_fields(channel.json_body["rooms"])
  524. def test_limit_and_from(self) -> None:
  525. """Testing list of rooms with a defined starting point and limit"""
  526. number_rooms = 10
  527. self._create_destination_rooms(number_rooms)
  528. channel = self.make_request(
  529. "GET",
  530. self.url + "?from=3&limit=5",
  531. access_token=self.admin_user_tok,
  532. )
  533. self.assertEqual(200, channel.code, msg=channel.json_body)
  534. self.assertEqual(channel.json_body["total"], number_rooms)
  535. self.assertEqual(channel.json_body["next_token"], "8")
  536. self.assertEqual(len(channel.json_body["rooms"]), 5)
  537. self._check_fields(channel.json_body["rooms"])
  538. def test_order_direction(self) -> None:
  539. """Testing order list with parameter `dir`"""
  540. number_rooms = 4
  541. self._create_destination_rooms(number_rooms)
  542. # get list in forward direction
  543. channel_asc = self.make_request(
  544. "GET",
  545. self.url + "?dir=f",
  546. access_token=self.admin_user_tok,
  547. )
  548. self.assertEqual(200, channel_asc.code, msg=channel_asc.json_body)
  549. self.assertEqual(channel_asc.json_body["total"], number_rooms)
  550. self.assertEqual(number_rooms, len(channel_asc.json_body["rooms"]))
  551. self._check_fields(channel_asc.json_body["rooms"])
  552. # get list in backward direction
  553. channel_desc = self.make_request(
  554. "GET",
  555. self.url + "?dir=b",
  556. access_token=self.admin_user_tok,
  557. )
  558. self.assertEqual(200, channel_desc.code, msg=channel_desc.json_body)
  559. self.assertEqual(channel_desc.json_body["total"], number_rooms)
  560. self.assertEqual(number_rooms, len(channel_desc.json_body["rooms"]))
  561. self._check_fields(channel_desc.json_body["rooms"])
  562. # test that both lists have different directions
  563. for i in range(0, number_rooms):
  564. self.assertEqual(
  565. channel_asc.json_body["rooms"][i]["room_id"],
  566. channel_desc.json_body["rooms"][number_rooms - 1 - i]["room_id"],
  567. )
  568. def test_next_token(self) -> None:
  569. """Testing that `next_token` appears at the right place"""
  570. number_rooms = 5
  571. self._create_destination_rooms(number_rooms)
  572. # `next_token` does not appear
  573. # Number of results is the number of entries
  574. channel = self.make_request(
  575. "GET",
  576. self.url + "?limit=5",
  577. access_token=self.admin_user_tok,
  578. )
  579. self.assertEqual(200, channel.code, msg=channel.json_body)
  580. self.assertEqual(channel.json_body["total"], number_rooms)
  581. self.assertEqual(len(channel.json_body["rooms"]), number_rooms)
  582. self.assertNotIn("next_token", channel.json_body)
  583. # `next_token` does not appear
  584. # Number of max results is larger than the number of entries
  585. channel = self.make_request(
  586. "GET",
  587. self.url + "?limit=6",
  588. access_token=self.admin_user_tok,
  589. )
  590. self.assertEqual(200, channel.code, msg=channel.json_body)
  591. self.assertEqual(channel.json_body["total"], number_rooms)
  592. self.assertEqual(len(channel.json_body["rooms"]), number_rooms)
  593. self.assertNotIn("next_token", channel.json_body)
  594. # `next_token` does appear
  595. # Number of max results is smaller than the number of entries
  596. channel = self.make_request(
  597. "GET",
  598. self.url + "?limit=4",
  599. access_token=self.admin_user_tok,
  600. )
  601. self.assertEqual(200, channel.code, msg=channel.json_body)
  602. self.assertEqual(channel.json_body["total"], number_rooms)
  603. self.assertEqual(len(channel.json_body["rooms"]), 4)
  604. self.assertEqual(channel.json_body["next_token"], "4")
  605. # Check
  606. # Set `from` to value of `next_token` for request remaining entries
  607. # `next_token` does not appear
  608. channel = self.make_request(
  609. "GET",
  610. self.url + "?from=4",
  611. access_token=self.admin_user_tok,
  612. )
  613. self.assertEqual(200, channel.code, msg=channel.json_body)
  614. self.assertEqual(channel.json_body["total"], number_rooms)
  615. self.assertEqual(len(channel.json_body["rooms"]), 1)
  616. self.assertNotIn("next_token", channel.json_body)
  617. def test_destination_rooms(self) -> None:
  618. """Testing that request the list of rooms is successfully."""
  619. number_rooms = 3
  620. self._create_destination_rooms(number_rooms)
  621. channel = self.make_request(
  622. "GET",
  623. self.url,
  624. access_token=self.admin_user_tok,
  625. )
  626. self.assertEqual(200, channel.code, msg=channel.json_body)
  627. self.assertEqual(channel.json_body["total"], number_rooms)
  628. self.assertEqual(number_rooms, len(channel.json_body["rooms"]))
  629. self._check_fields(channel.json_body["rooms"])
  630. def _create_destination_rooms(self, number_rooms: int) -> None:
  631. """Create a number rooms for destination
  632. Args:
  633. number_rooms: Number of rooms to be created
  634. """
  635. for _ in range(0, number_rooms):
  636. room_id = self.helper.create_room_as(
  637. self.admin_user, tok=self.admin_user_tok
  638. )
  639. self.get_success(
  640. self.store.store_destination_rooms_entries((self.dest,), room_id, 1234)
  641. )
  642. def _check_fields(self, content: List[JsonDict]) -> None:
  643. """Checks that the expected room attributes are present in content
  644. Args:
  645. content: List that is checked for content
  646. """
  647. for c in content:
  648. self.assertIn("room_id", c)
  649. self.assertIn("stream_ordering", c)