From cc156c2f1b880a250b8011dd97c7812c379b2e7c Mon Sep 17 00:00:00 2001 From: Momo Date: Sat, 21 Jul 2018 20:59:49 +0000 Subject: Infraction system API to replace Rowboat --- tests/test_api_bot_infractions.py | 134 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 tests/test_api_bot_infractions.py (limited to 'tests') diff --git a/tests/test_api_bot_infractions.py b/tests/test_api_bot_infractions.py new file mode 100644 index 00000000..58453e9b --- /dev/null +++ b/tests/test_api_bot_infractions.py @@ -0,0 +1,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) -- cgit v1.2.3 From 5c90069fef25907c8299b1e50235cc15ff3fa8d3 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Fri, 27 Jul 2018 17:20:41 +0100 Subject: Fix unit test --- pysite/views/api/bot/user.py | 2 +- tests/test_api_bot_users.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/pysite/views/api/bot/user.py b/pysite/views/api/bot/user.py index 5a096f2c..a3a0c7a8 100644 --- a/pysite/views/api/bot/user.py +++ b/pysite/views/api/bot/user.py @@ -68,7 +68,7 @@ class UserView(APIView, DBMixin): coerce=list ) - return jsonify({"result": result}) # pragma: no cover + return jsonify({"data": result}) # pragma: no cover @api_key @api_params(schema=SCHEMA, validation_type=ValidationTypes.json) diff --git a/tests/test_api_bot_users.py b/tests/test_api_bot_users.py index 9ad46071..eda3713e 100644 --- a/tests/test_api_bot_users.py +++ b/tests/test_api_bot_users.py @@ -1,7 +1,7 @@ -import os import json from tests import SiteTest, app + class ApiBotUsersEndpoint(SiteTest): def test_api_user(self): """ Check insert user """ @@ -12,8 +12,8 @@ class ApiBotUsersEndpoint(SiteTest): {'user_id': "1234", 'roles': ["5678"], "username": "test", "discriminator": "0000", "avatar": "http://some/url"} ]) - response = self.client.get('/bot/users', app.config['API_SUBDOMAIN'], headers=app.config['TEST_HEADER']) - self.assertEqual(response.status_code, 405) + response = self.client.get('/bot/users?user_id=1234', app.config['API_SUBDOMAIN'], headers=app.config['TEST_HEADER']) + self.assertTrue("data" in response.json) response = self.client.post('/bot/users', app.config['API_SUBDOMAIN'], headers=app.config['TEST_HEADER'], data=single_data) self.assertTrue("success" in response.json) -- cgit v1.2.3