test_report_event.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. from twisted.test.proto_helpers import MemoryReactor
  15. import synapse.rest.admin
  16. from synapse.rest.client import login, report_event, room
  17. from synapse.server import HomeServer
  18. from synapse.types import JsonDict
  19. from synapse.util import Clock
  20. from tests import unittest
  21. class ReportEventTestCase(unittest.HomeserverTestCase):
  22. servlets = [
  23. synapse.rest.admin.register_servlets,
  24. login.register_servlets,
  25. room.register_servlets,
  26. report_event.register_servlets,
  27. ]
  28. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  29. self.admin_user = self.register_user("admin", "pass", admin=True)
  30. self.admin_user_tok = self.login("admin", "pass")
  31. self.other_user = self.register_user("user", "pass")
  32. self.other_user_tok = self.login("user", "pass")
  33. self.room_id = self.helper.create_room_as(
  34. self.other_user, tok=self.other_user_tok, is_public=True
  35. )
  36. self.helper.join(self.room_id, user=self.admin_user, tok=self.admin_user_tok)
  37. resp = self.helper.send(self.room_id, tok=self.admin_user_tok)
  38. self.event_id = resp["event_id"]
  39. self.report_path = f"rooms/{self.room_id}/report/{self.event_id}"
  40. def test_reason_str_and_score_int(self) -> None:
  41. data = {"reason": "this makes me sad", "score": -100}
  42. self._assert_status(200, data)
  43. def test_no_reason(self) -> None:
  44. data = {"score": 0}
  45. self._assert_status(200, data)
  46. def test_no_score(self) -> None:
  47. data = {"reason": "this makes me sad"}
  48. self._assert_status(200, data)
  49. def test_no_reason_and_no_score(self) -> None:
  50. data: JsonDict = {}
  51. self._assert_status(200, data)
  52. def test_reason_int_and_score_str(self) -> None:
  53. data = {"reason": 10, "score": "string"}
  54. self._assert_status(400, data)
  55. def test_reason_zero_and_score_blank(self) -> None:
  56. data = {"reason": 0, "score": ""}
  57. self._assert_status(400, data)
  58. def test_reason_and_score_null(self) -> None:
  59. data = {"reason": None, "score": None}
  60. self._assert_status(400, data)
  61. def _assert_status(self, response_status: int, data: JsonDict) -> None:
  62. channel = self.make_request(
  63. "POST", self.report_path, data, access_token=self.other_user_tok
  64. )
  65. self.assertEqual(
  66. response_status, int(channel.result["code"]), msg=channel.result["body"]
  67. )