From e8f453a6b7def58da9e1a31e57a0885671092fbd Mon Sep 17 00:00:00 2001 From: Winston Hsiao Date: Sat, 3 Aug 2024 10:28:46 -0400 Subject: [PATCH] added tests for oauth user sign ups --- store/app/crud/users.py | 20 +++++++++++++++++++- store/app/model.py | 11 ++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/store/app/crud/users.py b/store/app/crud/users.py index e4a293db..6620f13c 100644 --- a/store/app/crud/users.py +++ b/store/app/crud/users.py @@ -37,11 +37,15 @@ async def get_user(self, id: str, throw_if_missing: bool = False) -> User | None async def get_user(self, id: str, throw_if_missing: bool = False) -> User | None: return await self._get_item(id, User, throw_if_missing=throw_if_missing) + """Standard sign up with email and password, leaves oauth providers empty""" + async def _create_user_from_email(self, email: str, password: str) -> User: user = User.create(email=email, password=password) await self._add_item(user, unique_fields=["email"]) return user + """OAuth sign up, creates user and links OAuthKey""" + async def _create_user_from_oauth(self, email: str, provider: str, token: str) -> User: user = User.create(email=email, password=None) if provider == "github": @@ -131,7 +135,21 @@ async def delete_api_key(self, token: APIKey | str) -> None: async def test_adhoc() -> None: async with UserCrud() as crud: - await crud._create_user_from_email(email="ben@kscale.dev", password="examplepas$w0rd") + # Test standard user creation + user = await crud._create_user_from_email(email="ben@kscale.dev", password="examplepas$w0rd") + print(f"User created: {user}") + + # Test OAuth user creation (GitHub) + oauth_user_github = await crud.get_user_from_github_token( + token="gh_token_example", email="oauth_github@kscale.dev" + ) + print(f"OAuth GitHub User created: {oauth_user_github}") + + # Test OAuth user creation (Google) + oauth_user_google = await crud.get_user_from_google_token( + token="google_token_example", email="oauth_google@kscale.dev" + ) + print(f"OAuth Google User created: {oauth_user_google}") if __name__ == "__main__": diff --git a/store/app/model.py b/store/app/model.py index ca8cc21d..4d427ebc 100644 --- a/store/app/model.py +++ b/store/app/model.py @@ -76,6 +76,7 @@ def verify_email(self) -> None: class OAuthKey(RobolistBaseModel): """Keys for OAuth providers which identify users.""" + user_id: str provider: str token: str @@ -86,8 +87,7 @@ def create(cls, user_id: str, provider: str, token: str) -> Self: id=new_uuid(), user_id=user_id, provider=provider, - token=token - ) + token=token), APIKeySource = Literal["user", "oauth"] @@ -118,7 +118,12 @@ def create( if permissions == "full": permissions = {"read", "write", "admin"} ttl_timestamp = int((datetime.utcnow() + timedelta(days=90)).timestamp()) - return cls(id=new_uuid(), user_id=user_id, source=source, permissions=permissions, ttl=ttl_timestamp) + return cls( + id=new_uuid(), + user_id=user_id, + source=source, + permissions=permissions, + ttl=ttl_timestamp), ArtifactSize = Literal["small", "large"]