From 494a6b5c63b927a95898262d16b0b1161c8fbb3e Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 5 Sep 2021 15:50:27 -0300 Subject: [PATCH] tests: add support for test guild resources --- tests/common.py | 113 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 109 insertions(+), 4 deletions(-) diff --git a/tests/common.py b/tests/common.py index ddb4ac2..7f00fc7 100644 --- a/tests/common.py +++ b/tests/common.py @@ -22,7 +22,9 @@ from typing import Optional from dataclasses import dataclass from litecord.common.users import create_user, delete_user +from litecord.common.guilds import delete_guild from litecord.blueprints.auth import make_token +from litecord.storage import int_ def email() -> str: @@ -38,13 +40,92 @@ class WrappedUser: password: str token: str - async def refetch(self): + async def refetch(self) -> dict: async with self.test_cli.app.app_context(): return await self.test_cli.app.storage.get_user(self.id) async def delete(self): + return await delete_user(self.id) + + +@dataclass +class WrappedGuild: + test_cli: "TestClient" + id: int + owner: bool # value depends on the user that fetched guild + owner_id: int + name: str + unavailable: bool + icon: Optional[str] + splash: Optional[str] + region: Optional[str] + afk_timeout: int + afk_channel_id: Optional[str] + afk_timeout: int + verification_level: int + default_message_notifications: int + explicit_content_filter: int + mfa_level: int + embed_enabled: bool + embed_channel_id: int + widget_enabled: bool + widget_channel_id: int + system_channel_id: int + rules_channel_id: int + public_updates_channel_id: int + features: str + features: str + banner: Optional[str] + description: Optional[str] + preferred_locale: Optional[str] + discovery_splash: Optional[str] + + vanity_url_code: Optional[str] + max_presences: int + max_members: int + guild_scheduled_events: list + + joined_at: str # value depends on the user that fetched the guild + + member_count: int + members: list + channels: list + roles: list + presences: list + emojis: list + voice_states: list + + large: Optional[bool] = None + + async def delete(self): + await delete_guild(self.id) + + async def refetch(self) -> "WrappedGuild": async with self.test_cli.app.app_context(): - return await delete_user(self.id) + guild = await self.test_cli.app.storage.get_guild_full(self.id) + return WrappedGuild.from_json(self.test_cli, guild) + + @classmethod + def from_json(cls, test_cli, rjson): + return cls( + test_cli, + **{ + **rjson, + **{ + "id": int(rjson["id"]), + "owner_id": int(rjson["owner_id"]), + "afk_channel_id": int_(rjson["afk_channel_id"]), + "embed_channel_id": int_(rjson["embed_channel_id"]), + "widget_enabled": int_(rjson["widget_enabled"]), + "widget_channel_id": int_(rjson["widget_channel_id"]), + "system_channel_id": int_(rjson["system_channel_id"]), + "rules_channel_id": int_(rjson["rules_channel_id"]), + "public_updates_channel_id": int_( + rjson["public_updates_channel_id"] + ), + }, + }, + ) class TestClient: @@ -66,7 +147,8 @@ class TestClient: async def cleanup(self): for resource in self.resources: - await resource.delete() + async with self.app.app_context(): + await resource.delete() async def create_user( self, @@ -85,11 +167,34 @@ class TestClient: WrappedUser(self, user_id, username, email, password, user_token) ) + async def create_guild( + self, + *, + name: Optional[str] = None, + region: Optional[str] = None, + owner: Optional["WrappedUser"] = None, + ) -> WrappedGuild: + name = name or secrets.token_hex(6) + owner_token = owner.token if owner else self.user["token"] + + async with self.app.app_context(): + # TODO move guild creation logic to litecord.common.guild + # TODO make tests use aiosqlite on memory for db + resp = await self.post( + "/api/v6/guilds", + json={"name": name, "region": region}, + headers={"authorization": owner_token}, + ) + rjson = await resp.json + + return self.add_resource(WrappedGuild.from_json(self, rjson)) + def _inject_auth(self, kwargs: dict) -> list: """Inject the test user's API key into the test request before passing the request on to the underlying TestClient.""" headers = kwargs.get("headers", {}) - headers["authorization"] = self.user["token"] + if "authorization" not in headers: + headers["authorization"] = self.user["token"] return headers async def get(self, *args, **kwargs):