aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_api_bot_infractions.py
blob: 58453e9ba83872331c1cc9702017c7666b06e9b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import json

from tests import SiteTest, app

TEST_USER_ID = "test"


class ApiBotInfractionsEndpoint(SiteTest):

    def test_infraction_create_invalid(self):
        # Invalid infraction type
        post_data_invalid_type = json.dumps(
            {"type": "not_a_type", "reason": "test", "user_id": TEST_USER_ID, "actor_id": TEST_USER_ID}
        )
        response = self.client.post("/bot/infractions", app.config["API_SUBDOMAIN"],
                                    headers=app.config["TEST_HEADER"],
                                    data=post_data_invalid_type)
        self.assert400(response)

    def test_infraction_kick(self):
        post_data_valid = json.dumps(
            {"type": "kick", "reason": "test", "user_id": TEST_USER_ID, "actor_id": TEST_USER_ID}
        )
        response = self.client.post("/bot/infractions", app.config["API_SUBDOMAIN"],
                                    headers=app.config["TEST_HEADER"],
                                    data=post_data_valid)
        self.assert200(response)
        self.assertTrue("infraction" in response.json)
        self.assertTrue("id" in response.json["infraction"])
        infraction_id = response.json["infraction"]["id"]
        response = self.client.get(f"/bot/infractions/id/{infraction_id}", app.config["API_SUBDOMAIN"],
                                   headers=app.config["TEST_HEADER"])
        self.assert200(response)
        self.assertTrue("infraction" in response.json)
        self.assertTrue("id" in response.json["infraction"])
        self.assertEqual(response.json["infraction"]["id"], infraction_id)
        self.assertTrue("active" in response.json["infraction"])
        self.assertFalse(response.json["infraction"]["active"])

    def test_infraction_ban(self):
        post_data_valid = json.dumps(
            {"type": "ban", "reason": "baddie", "user_id": TEST_USER_ID, "actor_id": TEST_USER_ID}
        )
        response = self.client.post("/bot/infractions", app.config["API_SUBDOMAIN"],
                                    headers=app.config["TEST_HEADER"],
                                    data=post_data_valid)
        self.assert200(response)
        self.assertTrue("infraction" in response.json)
        self.assertTrue("id" in response.json["infraction"])
        infraction_id = response.json["infraction"]["id"]

        # Check if the ban is currently applied
        response = self.client.get(f"/bot/infractions/user/{TEST_USER_ID}/ban/current", app.config["API_SUBDOMAIN"],
                                   headers=app.config["TEST_HEADER"])
        self.assert200(response)
        self.assertTrue("infraction" in response.json)
        self.assertIsNotNone(response.json["infraction"])
        self.assertTrue("id" in response.json["infraction"])
        self.assertEqual(response.json["infraction"]["id"], infraction_id)
        self.assertIsNone(response.json["infraction"]["expires_at"])
        self.assertTrue(response.json["infraction"]["active"])

        # Update the expiration to 1d
        patch_data_valid = json.dumps(
            {"id": infraction_id, "duration": "1d"}
        )
        response = self.client.patch("/bot/infractions", app.config["API_SUBDOMAIN"],
                                     headers=app.config["TEST_HEADER"],
                                     data=patch_data_valid)
        self.assert200(response)
        self.assertTrue("success" in response.json)
        self.assertTrue("infraction" in response.json)
        self.assertTrue(response.json["success"])
        self.assertIsNotNone(response.json["infraction"]["expires_at"])
        self.assertTrue(response.json["infraction"]["active"])

        # Disable the ban
        patch_data_valid = json.dumps(
            {"id": infraction_id, "active": False}
        )
        response = self.client.patch("/bot/infractions", app.config["API_SUBDOMAIN"],
                                     headers=app.config["TEST_HEADER"],
                                     data=patch_data_valid)
        self.assert200(response)
        self.assertTrue("success" in response.json)
        self.assertTrue("infraction" in response.json)
        self.assertTrue(response.json["success"])
        self.assertFalse(response.json["infraction"]["active"])

        # Check if there is no active ban anymore
        response = self.client.get(f"/bot/infractions/user/{TEST_USER_ID}/ban/current", app.config["API_SUBDOMAIN"],
                                   headers=app.config["TEST_HEADER"])
        self.assert200(response)
        self.assertTrue("infraction" in response.json)
        self.assertIsNone(response.json["infraction"])

        # Re-activate the ban
        patch_data_valid = json.dumps(
            {"id": infraction_id, "active": True}
        )
        response = self.client.patch("/bot/infractions", app.config["API_SUBDOMAIN"],
                                     headers=app.config["TEST_HEADER"],
                                     data=patch_data_valid)
        self.assert200(response)
        self.assertTrue("success" in response.json)
        self.assertTrue("infraction" in response.json)
        self.assertTrue(response.json["success"])
        self.assertTrue(response.json["infraction"]["active"])

        # Create a new ban
        post_data_valid = json.dumps(
            {"type": "ban", "reason": "baddie v2.0", "user_id": TEST_USER_ID, "actor_id": TEST_USER_ID}
        )
        response = self.client.post("/bot/infractions", app.config["API_SUBDOMAIN"],
                                    headers=app.config["TEST_HEADER"],
                                    data=post_data_valid)
        self.assert200(response)
        self.assertTrue("infraction" in response.json)
        self.assertTrue("id" in response.json["infraction"])
        new_infraction_id = response.json["infraction"]["id"]

        # Check if the old ban is now disabled
        response = self.client.get(f"/bot/infractions/id/{infraction_id}", app.config["API_SUBDOMAIN"],
                                   headers=app.config["TEST_HEADER"])
        self.assert200(response)
        self.assertTrue("infraction" in response.json)
        self.assertFalse(response.json["infraction"]["active"])

        # Check if the current ban infraction is the new infraction
        response = self.client.get(f"/bot/infractions/user/{TEST_USER_ID}/ban/current", app.config["API_SUBDOMAIN"],
                                   headers=app.config["TEST_HEADER"])
        self.assert200(response)
        self.assertTrue("infraction" in response.json)
        self.assertEqual(response.json["infraction"]["id"], new_infraction_id)