Skip to content

Commit

Permalink
added tests for oauth user sign ups
Browse files Browse the repository at this point in the history
  • Loading branch information
Winston-Hsiao committed Aug 3, 2024
1 parent d6971d0 commit e8f453a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
20 changes: 19 additions & 1 deletion store/app/crud/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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="[email protected]", password="examplepas$w0rd")
# Test standard user creation
user = await crud._create_user_from_email(email="[email protected]", 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="[email protected]"
)
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="[email protected]"
)
print(f"OAuth Google User created: {oauth_user_google}")


if __name__ == "__main__":
Expand Down
11 changes: 8 additions & 3 deletions store/app/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]
Expand Down Expand Up @@ -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"]
Expand Down

0 comments on commit e8f453a

Please sign in to comment.