test_report_event.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 test_cannot_report_nonexistent_event(self) -> None:
  62. """
  63. Tests that we don't accept event reports for events which do not exist.
  64. """
  65. channel = self.make_request(
  66. "POST",
  67. f"rooms/{self.room_id}/report/$nonsenseeventid:test",
  68. {"reason": "i am very sad"},
  69. access_token=self.other_user_tok,
  70. )
  71. self.assertEqual(404, channel.code, msg=channel.result["body"])
  72. self.assertEqual(
  73. "Unable to report event: it does not exist or you aren't able to see it.",
  74. channel.json_body["error"],
  75. msg=channel.result["body"],
  76. )
  77. def test_cannot_report_event_if_not_in_room(self) -> None:
  78. """
  79. Tests that we don't accept event reports for events that exist, but for which
  80. the reporter should not be able to view (because they are not in the room).
  81. """
  82. # Have the admin user create a room (the "other" user will not join this room).
  83. new_room_id = self.helper.create_room_as(tok=self.admin_user_tok)
  84. # Have the admin user send an event in this room.
  85. response = self.helper.send_event(
  86. new_room_id,
  87. "m.room.message",
  88. content={
  89. "msgtype": "m.text",
  90. "body": "This event has some bad words in it! Flip!",
  91. },
  92. tok=self.admin_user_tok,
  93. )
  94. event_id = response["event_id"]
  95. # Have the "other" user attempt to report it. Perhaps they found the event ID
  96. # in a screenshot or something...
  97. channel = self.make_request(
  98. "POST",
  99. f"rooms/{new_room_id}/report/{event_id}",
  100. {"reason": "I'm not in this room but I have opinions anyways!"},
  101. access_token=self.other_user_tok,
  102. )
  103. # The "other" user is not in the room, so their report should be rejected.
  104. self.assertEqual(404, channel.code, msg=channel.result["body"])
  105. self.assertEqual(
  106. "Unable to report event: it does not exist or you aren't able to see it.",
  107. channel.json_body["error"],
  108. msg=channel.result["body"],
  109. )
  110. def _assert_status(self, response_status: int, data: JsonDict) -> None:
  111. channel = self.make_request(
  112. "POST", self.report_path, data, access_token=self.other_user_tok
  113. )
  114. self.assertEqual(response_status, channel.code, msg=channel.result["body"])