test_report_event.py 3.0 KB

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