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