test_event_reports.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. # Copyright 2020 Dirk Klimpel
  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
  15. from twisted.test.proto_helpers import MemoryReactor
  16. import synapse.rest.admin
  17. from synapse.api.errors import Codes
  18. from synapse.rest.client import login, report_event, room
  19. from synapse.server import HomeServer
  20. from synapse.types import JsonDict
  21. from synapse.util import Clock
  22. from tests import unittest
  23. class EventReportsTestCase(unittest.HomeserverTestCase):
  24. servlets = [
  25. synapse.rest.admin.register_servlets,
  26. login.register_servlets,
  27. room.register_servlets,
  28. report_event.register_servlets,
  29. ]
  30. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  31. self.admin_user = self.register_user("admin", "pass", admin=True)
  32. self.admin_user_tok = self.login("admin", "pass")
  33. self.other_user = self.register_user("user", "pass")
  34. self.other_user_tok = self.login("user", "pass")
  35. self.room_id1 = self.helper.create_room_as(
  36. self.other_user, tok=self.other_user_tok, is_public=True
  37. )
  38. self.helper.join(self.room_id1, user=self.admin_user, tok=self.admin_user_tok)
  39. self.room_id2 = self.helper.create_room_as(
  40. self.other_user, tok=self.other_user_tok, is_public=True
  41. )
  42. self.helper.join(self.room_id2, user=self.admin_user, tok=self.admin_user_tok)
  43. # Two rooms and two users. Every user sends and reports every room event
  44. for _ in range(5):
  45. self._create_event_and_report(
  46. room_id=self.room_id1,
  47. user_tok=self.other_user_tok,
  48. )
  49. for _ in range(5):
  50. self._create_event_and_report(
  51. room_id=self.room_id2,
  52. user_tok=self.other_user_tok,
  53. )
  54. for _ in range(5):
  55. self._create_event_and_report(
  56. room_id=self.room_id1,
  57. user_tok=self.admin_user_tok,
  58. )
  59. for _ in range(5):
  60. self._create_event_and_report_without_parameters(
  61. room_id=self.room_id2,
  62. user_tok=self.admin_user_tok,
  63. )
  64. self.url = "/_synapse/admin/v1/event_reports"
  65. def test_no_auth(self) -> None:
  66. """
  67. Try to get an event report without authentication.
  68. """
  69. channel = self.make_request("GET", self.url, {})
  70. self.assertEqual(401, channel.code, msg=channel.json_body)
  71. self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
  72. def test_requester_is_no_admin(self) -> None:
  73. """
  74. If the user is not a server admin, an error 403 is returned.
  75. """
  76. channel = self.make_request(
  77. "GET",
  78. self.url,
  79. access_token=self.other_user_tok,
  80. )
  81. self.assertEqual(403, channel.code, msg=channel.json_body)
  82. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  83. def test_default_success(self) -> None:
  84. """
  85. Testing list of reported events
  86. """
  87. channel = self.make_request(
  88. "GET",
  89. self.url,
  90. access_token=self.admin_user_tok,
  91. )
  92. self.assertEqual(200, channel.code, msg=channel.json_body)
  93. self.assertEqual(channel.json_body["total"], 20)
  94. self.assertEqual(len(channel.json_body["event_reports"]), 20)
  95. self.assertNotIn("next_token", channel.json_body)
  96. self._check_fields(channel.json_body["event_reports"])
  97. def test_limit(self) -> None:
  98. """
  99. Testing list of reported events with limit
  100. """
  101. channel = self.make_request(
  102. "GET",
  103. self.url + "?limit=5",
  104. access_token=self.admin_user_tok,
  105. )
  106. self.assertEqual(200, channel.code, msg=channel.json_body)
  107. self.assertEqual(channel.json_body["total"], 20)
  108. self.assertEqual(len(channel.json_body["event_reports"]), 5)
  109. self.assertEqual(channel.json_body["next_token"], 5)
  110. self._check_fields(channel.json_body["event_reports"])
  111. def test_from(self) -> None:
  112. """
  113. Testing list of reported events with a defined starting point (from)
  114. """
  115. channel = self.make_request(
  116. "GET",
  117. self.url + "?from=5",
  118. access_token=self.admin_user_tok,
  119. )
  120. self.assertEqual(200, channel.code, msg=channel.json_body)
  121. self.assertEqual(channel.json_body["total"], 20)
  122. self.assertEqual(len(channel.json_body["event_reports"]), 15)
  123. self.assertNotIn("next_token", channel.json_body)
  124. self._check_fields(channel.json_body["event_reports"])
  125. def test_limit_and_from(self) -> None:
  126. """
  127. Testing list of reported events with a defined starting point and limit
  128. """
  129. channel = self.make_request(
  130. "GET",
  131. self.url + "?from=5&limit=10",
  132. access_token=self.admin_user_tok,
  133. )
  134. self.assertEqual(200, channel.code, msg=channel.json_body)
  135. self.assertEqual(channel.json_body["total"], 20)
  136. self.assertEqual(channel.json_body["next_token"], 15)
  137. self.assertEqual(len(channel.json_body["event_reports"]), 10)
  138. self._check_fields(channel.json_body["event_reports"])
  139. def test_filter_room(self) -> None:
  140. """
  141. Testing list of reported events with a filter of room
  142. """
  143. channel = self.make_request(
  144. "GET",
  145. self.url + "?room_id=%s" % self.room_id1,
  146. access_token=self.admin_user_tok,
  147. )
  148. self.assertEqual(200, channel.code, msg=channel.json_body)
  149. self.assertEqual(channel.json_body["total"], 10)
  150. self.assertEqual(len(channel.json_body["event_reports"]), 10)
  151. self.assertNotIn("next_token", channel.json_body)
  152. self._check_fields(channel.json_body["event_reports"])
  153. for report in channel.json_body["event_reports"]:
  154. self.assertEqual(report["room_id"], self.room_id1)
  155. def test_filter_user(self) -> None:
  156. """
  157. Testing list of reported events with a filter of user
  158. """
  159. channel = self.make_request(
  160. "GET",
  161. self.url + "?user_id=%s" % self.other_user,
  162. access_token=self.admin_user_tok,
  163. )
  164. self.assertEqual(200, channel.code, msg=channel.json_body)
  165. self.assertEqual(channel.json_body["total"], 10)
  166. self.assertEqual(len(channel.json_body["event_reports"]), 10)
  167. self.assertNotIn("next_token", channel.json_body)
  168. self._check_fields(channel.json_body["event_reports"])
  169. for report in channel.json_body["event_reports"]:
  170. self.assertEqual(report["user_id"], self.other_user)
  171. def test_filter_user_and_room(self) -> None:
  172. """
  173. Testing list of reported events with a filter of user and room
  174. """
  175. channel = self.make_request(
  176. "GET",
  177. self.url + "?user_id=%s&room_id=%s" % (self.other_user, self.room_id1),
  178. access_token=self.admin_user_tok,
  179. )
  180. self.assertEqual(200, channel.code, msg=channel.json_body)
  181. self.assertEqual(channel.json_body["total"], 5)
  182. self.assertEqual(len(channel.json_body["event_reports"]), 5)
  183. self.assertNotIn("next_token", channel.json_body)
  184. self._check_fields(channel.json_body["event_reports"])
  185. for report in channel.json_body["event_reports"]:
  186. self.assertEqual(report["user_id"], self.other_user)
  187. self.assertEqual(report["room_id"], self.room_id1)
  188. def test_valid_search_order(self) -> None:
  189. """
  190. Testing search order. Order by timestamps.
  191. """
  192. # fetch the most recent first, largest timestamp
  193. channel = self.make_request(
  194. "GET",
  195. self.url + "?dir=b",
  196. access_token=self.admin_user_tok,
  197. )
  198. self.assertEqual(200, channel.code, msg=channel.json_body)
  199. self.assertEqual(channel.json_body["total"], 20)
  200. self.assertEqual(len(channel.json_body["event_reports"]), 20)
  201. report = 1
  202. while report < len(channel.json_body["event_reports"]):
  203. self.assertGreaterEqual(
  204. channel.json_body["event_reports"][report - 1]["received_ts"],
  205. channel.json_body["event_reports"][report]["received_ts"],
  206. )
  207. report += 1
  208. # fetch the oldest first, smallest timestamp
  209. channel = self.make_request(
  210. "GET",
  211. self.url + "?dir=f",
  212. access_token=self.admin_user_tok,
  213. )
  214. self.assertEqual(200, channel.code, msg=channel.json_body)
  215. self.assertEqual(channel.json_body["total"], 20)
  216. self.assertEqual(len(channel.json_body["event_reports"]), 20)
  217. report = 1
  218. while report < len(channel.json_body["event_reports"]):
  219. self.assertLessEqual(
  220. channel.json_body["event_reports"][report - 1]["received_ts"],
  221. channel.json_body["event_reports"][report]["received_ts"],
  222. )
  223. report += 1
  224. def test_invalid_search_order(self) -> None:
  225. """
  226. Testing that a invalid search order returns a 400
  227. """
  228. channel = self.make_request(
  229. "GET",
  230. self.url + "?dir=bar",
  231. access_token=self.admin_user_tok,
  232. )
  233. self.assertEqual(400, channel.code, msg=channel.json_body)
  234. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  235. self.assertEqual(
  236. "Query parameter 'dir' must be one of ['b', 'f']",
  237. channel.json_body["error"],
  238. )
  239. def test_limit_is_negative(self) -> None:
  240. """
  241. Testing that a negative limit parameter returns a 400
  242. """
  243. channel = self.make_request(
  244. "GET",
  245. self.url + "?limit=-5",
  246. access_token=self.admin_user_tok,
  247. )
  248. self.assertEqual(400, channel.code, msg=channel.json_body)
  249. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  250. def test_from_is_negative(self) -> None:
  251. """
  252. Testing that a negative from parameter returns a 400
  253. """
  254. channel = self.make_request(
  255. "GET",
  256. self.url + "?from=-5",
  257. access_token=self.admin_user_tok,
  258. )
  259. self.assertEqual(400, channel.code, msg=channel.json_body)
  260. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  261. def test_next_token(self) -> None:
  262. """
  263. Testing that `next_token` appears at the right place
  264. """
  265. # `next_token` does not appear
  266. # Number of results is the number of entries
  267. channel = self.make_request(
  268. "GET",
  269. self.url + "?limit=20",
  270. access_token=self.admin_user_tok,
  271. )
  272. self.assertEqual(200, channel.code, msg=channel.json_body)
  273. self.assertEqual(channel.json_body["total"], 20)
  274. self.assertEqual(len(channel.json_body["event_reports"]), 20)
  275. self.assertNotIn("next_token", channel.json_body)
  276. # `next_token` does not appear
  277. # Number of max results is larger than the number of entries
  278. channel = self.make_request(
  279. "GET",
  280. self.url + "?limit=21",
  281. access_token=self.admin_user_tok,
  282. )
  283. self.assertEqual(200, channel.code, msg=channel.json_body)
  284. self.assertEqual(channel.json_body["total"], 20)
  285. self.assertEqual(len(channel.json_body["event_reports"]), 20)
  286. self.assertNotIn("next_token", channel.json_body)
  287. # `next_token` does appear
  288. # Number of max results is smaller than the number of entries
  289. channel = self.make_request(
  290. "GET",
  291. self.url + "?limit=19",
  292. access_token=self.admin_user_tok,
  293. )
  294. self.assertEqual(200, channel.code, msg=channel.json_body)
  295. self.assertEqual(channel.json_body["total"], 20)
  296. self.assertEqual(len(channel.json_body["event_reports"]), 19)
  297. self.assertEqual(channel.json_body["next_token"], 19)
  298. # Check
  299. # Set `from` to value of `next_token` for request remaining entries
  300. # `next_token` does not appear
  301. channel = self.make_request(
  302. "GET",
  303. self.url + "?from=19",
  304. access_token=self.admin_user_tok,
  305. )
  306. self.assertEqual(200, channel.code, msg=channel.json_body)
  307. self.assertEqual(channel.json_body["total"], 20)
  308. self.assertEqual(len(channel.json_body["event_reports"]), 1)
  309. self.assertNotIn("next_token", channel.json_body)
  310. def _create_event_and_report(self, room_id: str, user_tok: str) -> None:
  311. """Create and report events"""
  312. resp = self.helper.send(room_id, tok=user_tok)
  313. event_id = resp["event_id"]
  314. channel = self.make_request(
  315. "POST",
  316. "rooms/%s/report/%s" % (room_id, event_id),
  317. {"score": -100, "reason": "this makes me sad"},
  318. access_token=user_tok,
  319. )
  320. self.assertEqual(200, channel.code, msg=channel.json_body)
  321. def _create_event_and_report_without_parameters(
  322. self, room_id: str, user_tok: str
  323. ) -> None:
  324. """Create and report an event, but omit reason and score"""
  325. resp = self.helper.send(room_id, tok=user_tok)
  326. event_id = resp["event_id"]
  327. channel = self.make_request(
  328. "POST",
  329. "rooms/%s/report/%s" % (room_id, event_id),
  330. {},
  331. access_token=user_tok,
  332. )
  333. self.assertEqual(200, channel.code, msg=channel.json_body)
  334. def _check_fields(self, content: List[JsonDict]) -> None:
  335. """Checks that all attributes are present in an event report"""
  336. for c in content:
  337. self.assertIn("id", c)
  338. self.assertIn("received_ts", c)
  339. self.assertIn("room_id", c)
  340. self.assertIn("event_id", c)
  341. self.assertIn("user_id", c)
  342. self.assertIn("sender", c)
  343. self.assertIn("canonical_alias", c)
  344. self.assertIn("name", c)
  345. self.assertIn("score", c)
  346. self.assertIn("reason", c)
  347. def test_count_correct_despite_table_deletions(self) -> None:
  348. """
  349. Tests that the count matches the number of rows, even if rows in joined tables
  350. are missing.
  351. """
  352. # Delete rows from room_stats_state for one of our rooms.
  353. self.get_success(
  354. self.hs.get_datastores().main.db_pool.simple_delete(
  355. "room_stats_state", {"room_id": self.room_id1}, desc="_"
  356. )
  357. )
  358. channel = self.make_request(
  359. "GET",
  360. self.url,
  361. access_token=self.admin_user_tok,
  362. )
  363. self.assertEqual(200, channel.code, msg=channel.json_body)
  364. # The 'total' field is 10 because only 10 reports will actually
  365. # be retrievable since we deleted the rows in the room_stats_state
  366. # table.
  367. self.assertEqual(channel.json_body["total"], 10)
  368. # This is consistent with the number of rows actually returned.
  369. self.assertEqual(len(channel.json_body["event_reports"]), 10)
  370. class EventReportDetailTestCase(unittest.HomeserverTestCase):
  371. servlets = [
  372. synapse.rest.admin.register_servlets,
  373. login.register_servlets,
  374. room.register_servlets,
  375. report_event.register_servlets,
  376. ]
  377. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  378. self.admin_user = self.register_user("admin", "pass", admin=True)
  379. self.admin_user_tok = self.login("admin", "pass")
  380. self.other_user = self.register_user("user", "pass")
  381. self.other_user_tok = self.login("user", "pass")
  382. self.room_id1 = self.helper.create_room_as(
  383. self.other_user, tok=self.other_user_tok, is_public=True
  384. )
  385. self.helper.join(self.room_id1, user=self.admin_user, tok=self.admin_user_tok)
  386. self._create_event_and_report(
  387. room_id=self.room_id1,
  388. user_tok=self.other_user_tok,
  389. )
  390. # first created event report gets `id`=2
  391. self.url = "/_synapse/admin/v1/event_reports/2"
  392. def test_no_auth(self) -> None:
  393. """
  394. Try to get event report without authentication.
  395. """
  396. channel = self.make_request("GET", self.url, {})
  397. self.assertEqual(401, channel.code, msg=channel.json_body)
  398. self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
  399. def test_requester_is_no_admin(self) -> None:
  400. """
  401. If the user is not a server admin, an error 403 is returned.
  402. """
  403. channel = self.make_request(
  404. "GET",
  405. self.url,
  406. access_token=self.other_user_tok,
  407. )
  408. self.assertEqual(403, channel.code, msg=channel.json_body)
  409. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  410. def test_default_success(self) -> None:
  411. """
  412. Testing get a reported event
  413. """
  414. channel = self.make_request(
  415. "GET",
  416. self.url,
  417. access_token=self.admin_user_tok,
  418. )
  419. self.assertEqual(200, channel.code, msg=channel.json_body)
  420. self._check_fields(channel.json_body)
  421. def test_invalid_report_id(self) -> None:
  422. """
  423. Testing that an invalid `report_id` returns a 400.
  424. """
  425. # `report_id` is negative
  426. channel = self.make_request(
  427. "GET",
  428. "/_synapse/admin/v1/event_reports/-123",
  429. access_token=self.admin_user_tok,
  430. )
  431. self.assertEqual(400, channel.code, msg=channel.json_body)
  432. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  433. self.assertEqual(
  434. "The report_id parameter must be a string representing a positive integer.",
  435. channel.json_body["error"],
  436. )
  437. # `report_id` is a non-numerical string
  438. channel = self.make_request(
  439. "GET",
  440. "/_synapse/admin/v1/event_reports/abcdef",
  441. access_token=self.admin_user_tok,
  442. )
  443. self.assertEqual(400, channel.code, msg=channel.json_body)
  444. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  445. self.assertEqual(
  446. "The report_id parameter must be a string representing a positive integer.",
  447. channel.json_body["error"],
  448. )
  449. # `report_id` is undefined
  450. channel = self.make_request(
  451. "GET",
  452. "/_synapse/admin/v1/event_reports/",
  453. access_token=self.admin_user_tok,
  454. )
  455. self.assertEqual(400, channel.code, msg=channel.json_body)
  456. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  457. self.assertEqual(
  458. "The report_id parameter must be a string representing a positive integer.",
  459. channel.json_body["error"],
  460. )
  461. def test_report_id_not_found(self) -> None:
  462. """
  463. Testing that a not existing `report_id` returns a 404.
  464. """
  465. channel = self.make_request(
  466. "GET",
  467. "/_synapse/admin/v1/event_reports/123",
  468. access_token=self.admin_user_tok,
  469. )
  470. self.assertEqual(404, channel.code, msg=channel.json_body)
  471. self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
  472. self.assertEqual("Event report not found", channel.json_body["error"])
  473. def _create_event_and_report(self, room_id: str, user_tok: str) -> None:
  474. """Create and report events"""
  475. resp = self.helper.send(room_id, tok=user_tok)
  476. event_id = resp["event_id"]
  477. channel = self.make_request(
  478. "POST",
  479. "rooms/%s/report/%s" % (room_id, event_id),
  480. {"score": -100, "reason": "this makes me sad"},
  481. access_token=user_tok,
  482. )
  483. self.assertEqual(200, channel.code, msg=channel.json_body)
  484. def _check_fields(self, content: JsonDict) -> None:
  485. """Checks that all attributes are present in a event report"""
  486. self.assertIn("id", content)
  487. self.assertIn("received_ts", content)
  488. self.assertIn("room_id", content)
  489. self.assertIn("event_id", content)
  490. self.assertIn("user_id", content)
  491. self.assertIn("sender", content)
  492. self.assertIn("canonical_alias", content)
  493. self.assertIn("name", content)
  494. self.assertIn("event_json", content)
  495. self.assertIn("score", content)
  496. self.assertIn("reason", content)
  497. self.assertIn("auth_events", content["event_json"])
  498. self.assertIn("type", content["event_json"])
  499. self.assertIn("room_id", content["event_json"])
  500. self.assertIn("sender", content["event_json"])
  501. self.assertIn("content", content["event_json"])
  502. class DeleteEventReportTestCase(unittest.HomeserverTestCase):
  503. servlets = [
  504. synapse.rest.admin.register_servlets,
  505. login.register_servlets,
  506. ]
  507. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  508. self._store = hs.get_datastores().main
  509. self.admin_user = self.register_user("admin", "pass", admin=True)
  510. self.admin_user_tok = self.login("admin", "pass")
  511. self.other_user = self.register_user("user", "pass")
  512. self.other_user_tok = self.login("user", "pass")
  513. # create report
  514. event_id = self.get_success(
  515. self._store.add_event_report(
  516. "room_id",
  517. "event_id",
  518. self.other_user,
  519. "this makes me sad",
  520. {},
  521. self.clock.time_msec(),
  522. )
  523. )
  524. self.url = f"/_synapse/admin/v1/event_reports/{event_id}"
  525. def test_no_auth(self) -> None:
  526. """
  527. Try to delete event report without authentication.
  528. """
  529. channel = self.make_request("DELETE", self.url)
  530. self.assertEqual(401, channel.code, msg=channel.json_body)
  531. self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
  532. def test_requester_is_no_admin(self) -> None:
  533. """
  534. If the user is not a server admin, an error 403 is returned.
  535. """
  536. channel = self.make_request(
  537. "DELETE",
  538. self.url,
  539. access_token=self.other_user_tok,
  540. )
  541. self.assertEqual(403, channel.code, msg=channel.json_body)
  542. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  543. def test_delete_success(self) -> None:
  544. """
  545. Testing delete a report.
  546. """
  547. channel = self.make_request(
  548. "DELETE",
  549. self.url,
  550. access_token=self.admin_user_tok,
  551. )
  552. self.assertEqual(200, channel.code, msg=channel.json_body)
  553. self.assertEqual({}, channel.json_body)
  554. channel = self.make_request(
  555. "GET",
  556. self.url,
  557. access_token=self.admin_user_tok,
  558. )
  559. # check that report was deleted
  560. self.assertEqual(404, channel.code, msg=channel.json_body)
  561. self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
  562. def test_invalid_report_id(self) -> None:
  563. """
  564. Testing that an invalid `report_id` returns a 400.
  565. """
  566. # `report_id` is negative
  567. channel = self.make_request(
  568. "DELETE",
  569. "/_synapse/admin/v1/event_reports/-123",
  570. access_token=self.admin_user_tok,
  571. )
  572. self.assertEqual(400, channel.code, msg=channel.json_body)
  573. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  574. self.assertEqual(
  575. "The report_id parameter must be a string representing a positive integer.",
  576. channel.json_body["error"],
  577. )
  578. # `report_id` is a non-numerical string
  579. channel = self.make_request(
  580. "DELETE",
  581. "/_synapse/admin/v1/event_reports/abcdef",
  582. access_token=self.admin_user_tok,
  583. )
  584. self.assertEqual(400, channel.code, msg=channel.json_body)
  585. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  586. self.assertEqual(
  587. "The report_id parameter must be a string representing a positive integer.",
  588. channel.json_body["error"],
  589. )
  590. # `report_id` is undefined
  591. channel = self.make_request(
  592. "DELETE",
  593. "/_synapse/admin/v1/event_reports/",
  594. access_token=self.admin_user_tok,
  595. )
  596. self.assertEqual(400, channel.code, msg=channel.json_body)
  597. self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
  598. self.assertEqual(
  599. "The report_id parameter must be a string representing a positive integer.",
  600. channel.json_body["error"],
  601. )
  602. def test_report_id_not_found(self) -> None:
  603. """
  604. Testing that a not existing `report_id` returns a 404.
  605. """
  606. channel = self.make_request(
  607. "DELETE",
  608. "/_synapse/admin/v1/event_reports/123",
  609. access_token=self.admin_user_tok,
  610. )
  611. self.assertEqual(404, channel.code, msg=channel.json_body)
  612. self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
  613. self.assertEqual("Event report not found", channel.json_body["error"])