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