1
0

test_federation.py 29 KB

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