def test_get_endpoint_invalid(self):
        # Test for a unknown section
        resp = tools.make_bot_request(
            tools.generate_guildapi_path() + r"config/%20%",
            method = "GET"
        )
        tools.assure_identical_response(resp, unknown_config_response)

        # Test for a invalid guild
        resp = tools.make_bot_request(
            f"/api/guilds/true/config/general",
            method = "GET"
        )
        # FastAPI itself denies this one as it doesnt match a proper type
        assert resp.status_code == 422
    def test_get_endpoint(self):
        resp = tools.make_bot_request(tools.generate_guildapi_path() + "config/general")
        # Assume that if this endpoint returns JSON and a 200 OK code then it returned the right data
        assert resp.status_code == 200

        # Due to the variants of what this can return, we can only check if its a proper dict
        assert type(resp.json()) == dict
 def make_mute_post(self, mute_body: dict):
     resp = tools.make_bot_request(
         tools.generate_guildapi_path() + "mute",
         method = "POST",
         body = mute_body
     )
     return resp
    def test_patch_endpoint_invalid(self):
        # Test for a unknown section
        resp = tools.make_bot_request(
            tools.generate_guildapi_path() + "config/spooky",
            method = "PATCH",
            body = patch_body
        )
        tools.assure_identical_response(resp, unknown_config_response)

        # Test for a invalid guild
        resp = tools.make_bot_request(
            f"/api/guilds/292782/config/general",
            method = "PATCH",
            body = patch_body
        )
        tools.assure_identical_response(resp, bad_request_response)
    def test_post_endpoint_invalid(self):
        # Test for an unknown section
        resp = tools.make_bot_request(
            tools.generate_guildapi_path() + "config/''",
            method = "POST",
            body = post_body
        )
        tools.assure_identical_response(resp, unknown_config_response)

        # Test for a invalid guild
        resp = tools.make_bot_request(
            f"/api/guilds/{-1111}/config/general",
            method = "POST",
            body = post_body
        )
        tools.assure_identical_response(resp, bad_request_response)
    def test_post_endpoint(self):
        resp = tools.make_bot_request(
            tools.generate_guildapi_path() + "config/general",
            method = "POST",
            body = post_body
        )
        
        tools.assure_ok_response(resp)

        tools.assure_fields_and_types(resp.json(), ["status", "modified_values"])
    def test_endpoint(self):
        resp = tools.make_bot_request("/api/guilds/")

        tools.assure_ok_response(resp)

        for _, guild_info in resp.json().items():
            # Assure the proper fields and types exist on all guilds
            tools.assure_fields_and_types(guild_info, ["id", "name", "permissions", "icon"])

            # Make sure the permissions are in the expected range, subject to change.
            assert guild_info["permissions"] <= 15
    def test_patch_endpoint(self):
        # Test proper usage
        resp = tools.make_bot_request(
            tools.generate_guildapi_path() + "config/general",
            method = "PATCH",
            body = patch_body
        )
        tools.assure_ok_response(resp)

        tools.assure_fields_and_types(resp.json(), ["status", "modified_values"])
        
        # Test duplicate update requests
        resp = tools.make_bot_request(
            tools.generate_guildapi_path() + "config/general",
            method = "PATCH",
            body = patch_body
        )
        assert resp.status_code == 400
        
        tools.assure_fields_and_types(resp.json(), ["status", "errors"])
示例#9
0
    def test_whoami(self):
        resp = tools.make_bot_request("/api/whoami")

        tools.assure_ok_response(resp)

        # Make sure all expected fields exist, and that they are the right type
        resp: dict = resp.json()
        tools.assure_fields_and_types(
            resp, ["username", "discrim", "avatar_url", "bot_admin_status"])
        # Make sure the discriminator can always be converted into a number
        assert int(resp["discrim"])
示例#10
0
    def test_endpoint(self):
        resp = tools.make_bot_request(tools.generate_guildapi_path() + "info")

        tools.assure_ok_response(resp)

        resp: dict = resp.json()
        # Assure the keys are the right type
        # Check the top-level type of the response
        tools.assure_fields_and_types(
            resp, 
            [
                "name",
                "id",
                "server_icon",
                "owner",
                "members",
                "text_channels",
                "additional_text_channels",
                "voice_channels",
                "creation_date",
                "age_days",
                "vip_features",
                "role_list",
                "emojis",
                "member_statuses",
                "user_perms",
                "user_level"
            ]
        )

        # Check the structures and their fields and types
        tools.assure_fields_and_types(resp["owner"], ["id", "name"])

        tools.assure_fields(resp["member_statuses"], ["online", "idle", "dnd", "offline"])
        for _, v in resp["member_statuses"].items():
            assert type(v) == tools.field_types["member_statuses_count"]

        tools.assure_fields_and_types(resp["text_channels"], ["name", "can_log"], deep=True)
        tools.assure_fields_and_types(resp["additional_text_channels"], ["name", "can_log"], deep=True)

        tools.assure_fields_and_types(
            resp["role_list"],
            ["id", "name", "color", "members", "is_admin", "is_mod", "can_be_self_role"],
            deep=True
        )
        for emoji in resp["emojis"]:
            tools.assure_types(emoji)
示例#11
0
    def test_mute_endpoint_invalid(self):
        # Test for a bad guild
        resp = tools.make_bot_request(
            f"/api/guilds/{-1111}/mute",
            method = "POST",
            body = {
                "action": "setup",
                "role_id": 9999
            }
        )
        tools.assure_identical_response(resp, bad_request_response)

        # Test for request body parts, grouped for clarity
        def validate_body_parts():
            # Has action, no role_id
            resp = self.make_mute_post({"action": "setup"})
            tools.assure_identical_response(resp, no_roleid_response)

            # Has role_id, no action
            resp = self.make_mute_post({"role_id": 99999})
            tools.assure_identical_response(resp, bad_request_response)

            # Extra, unknown, fields
            resp = self.make_mute_post({"action": "setup", "role_id": 99999, "test": True})
            tools.assure_identical_response(resp, bad_request_response)

        validate_body_parts()

        # Test for a bad role_id
        resp = self.make_mute_post({"action": "cleanup", "role_id": -11111})
        tools.assure_identical_response(resp, bad_request_response)

        # Test for improper role_id type
        resp = self.make_mute_post({"action": "cleanup", "role_id": "Spooky"})
        tools.assure_identical_response(resp, bad_request_response)

        # Test for improper action type
        resp = self.make_mute_post({"action": True, "role_id": 99999})
        tools.assure_identical_response(resp, bad_request_response)

        # Test for a non-existant action
        resp = self.make_mute_post({"action": "spooky", "role_id": 99999})
        tools.assure_identical_response(resp, bad_request_response)
示例#12
0
 def test_endpoint_invalid(self):
     # Test for a bad guild ID
     resp = tools.make_bot_request("/api/guilds/-1111/info")
     
     tools.assure_identical_response(resp, bad_request_response)
示例#13
0
    def test_generalinfo(self):
        resp = tools.make_bot_request("/api/general_info")

        tools.assure_ok_response(resp)

        tools.assure_fields_and_types(resp.json(), ["languages", "logging"])
示例#14
0
    def test_bot_offline(self):
        # Test to assure the API handles a bot outage properly
        resp = tools.make_bot_request("/api/whoami")

        tools.assure_identical_response(resp, no_reply_response)