-
Notifications
You must be signed in to change notification settings - Fork 7
Onboarding : Unified API call #152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4ea4d1d
first commit:automation onboarding
nishika26 9d18b5d
Merge branch 'main' into feature/onboarding
nishika26 45c53e0
Merge branch 'main' into feature/onboarding
nishika26 256d0da
cascade delete
nishika26 3880a43
test cases
nishika26 d3925b6
hashing part removed-redundent
nishika26 b01a260
rollback error
nishika26 31809d5
authorization
nishika26 a97a4e3
function doc
nishika26 a316fd0
function doc
nishika26 8112e08
Merge branch 'main' into feature/onboarding
nishika26 fa5847f
test cases update for hashed api key
nishika26 88de6cc
changes
nishika26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import uuid | ||
|
|
||
| from fastapi import APIRouter, HTTPException, Depends | ||
| from pydantic import BaseModel, EmailStr | ||
| from sqlmodel import Session | ||
|
|
||
| from app.crud import ( | ||
| create_organization, | ||
| get_organization_by_name, | ||
| create_project, | ||
| create_user, | ||
| create_api_key, | ||
| get_api_key_by_user_org, | ||
| ) | ||
| from app.models import ( | ||
| OrganizationCreate, | ||
| ProjectCreate, | ||
| UserCreate, | ||
| APIKeyPublic, | ||
| Organization, | ||
| Project, | ||
| User, | ||
| APIKey, | ||
| ) | ||
| from app.core.security import get_password_hash | ||
| from app.api.deps import ( | ||
| CurrentUser, | ||
| SessionDep, | ||
| get_current_active_superuser, | ||
| ) | ||
|
|
||
| router = APIRouter(tags=["onboarding"]) | ||
|
|
||
|
|
||
| # Pydantic models for input validation | ||
| class OnboardingRequest(BaseModel): | ||
| organization_name: str | ||
| project_name: str | ||
| email: EmailStr | ||
| password: str | ||
| user_name: str | ||
|
|
||
|
|
||
| class OnboardingResponse(BaseModel): | ||
| organization_id: int | ||
| project_id: int | ||
| user_id: uuid.UUID | ||
| api_key: str | ||
|
|
||
|
|
||
| @router.post( | ||
| "/onboard", | ||
| dependencies=[Depends(get_current_active_superuser)], | ||
| response_model=OnboardingResponse, | ||
| ) | ||
| def onboard_user(request: OnboardingRequest, session: SessionDep): | ||
nishika26 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Handles quick onboarding of a new user : Accepts Organization name, project name, email, password and user name, then gives back an API key which | ||
| will be further used for authentication. | ||
| """ | ||
| try: | ||
nishika26 marked this conversation as resolved.
Show resolved
Hide resolved
nishika26 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| existing_organization = get_organization_by_name( | ||
| session=session, name=request.organization_name | ||
| ) | ||
| if existing_organization: | ||
| organization = existing_organization | ||
| else: | ||
| org_create = OrganizationCreate(name=request.organization_name) | ||
| organization = create_organization(session=session, org_create=org_create) | ||
|
|
||
| existing_project = ( | ||
| session.query(Project).filter(Project.name == request.project_name).first() | ||
| ) | ||
| if existing_project: | ||
| project = existing_project # Use the existing project | ||
| else: | ||
| project_create = ProjectCreate( | ||
| name=request.project_name, organization_id=organization.id | ||
| ) | ||
| project = create_project(session=session, project_create=project_create) | ||
|
|
||
| existing_user = session.query(User).filter(User.email == request.email).first() | ||
| if existing_user: | ||
| user = existing_user | ||
| else: | ||
| user_create = UserCreate( | ||
| name=request.user_name, | ||
| email=request.email, | ||
| password=request.password, | ||
| ) | ||
| user = create_user(session=session, user_create=user_create) | ||
|
|
||
| existing_key = get_api_key_by_user_org( | ||
| db=session, organization_id=organization.id, user_id=user.id | ||
| ) | ||
|
|
||
| if existing_key: | ||
| raise HTTPException( | ||
| status_code=400, | ||
| detail="API key already exists for this user and organization", | ||
| ) | ||
|
|
||
| api_key_public = create_api_key( | ||
| session=session, organization_id=organization.id, user_id=user.id | ||
| ) | ||
|
|
||
| user.is_superuser = False | ||
| session.add(user) | ||
| session.commit() | ||
|
|
||
| return OnboardingResponse( | ||
| organization_id=organization.id, | ||
| project_id=project.id, | ||
| user_id=user.id, | ||
| api_key=api_key_public.key, | ||
| ) | ||
|
|
||
| except Exception as e: | ||
| session.rollback() | ||
| raise HTTPException(status_code=400, detail=str(e)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import pytest | ||
| from fastapi.testclient import TestClient | ||
| from app.main import app # Assuming your FastAPI app is in app/main.py | ||
| from app.models import Organization, Project, User, APIKey | ||
| from app.crud import create_organization, create_project, create_user, create_api_key | ||
| from app.api.deps import SessionDep | ||
| from sqlalchemy import create_engine | ||
| from sqlmodel import Session, SQLModel | ||
| from app.core.config import settings | ||
| from app.tests.utils.utils import random_email, random_lower_string | ||
| from app.core.security import decrypt_api_key | ||
|
|
||
| client = TestClient(app) | ||
|
|
||
|
|
||
| def test_onboard_user(client, db: Session, superuser_token_headers: dict[str, str]): | ||
| data = { | ||
| "organization_name": "TestOrg", | ||
| "project_name": "TestProject", | ||
| "email": random_email(), | ||
| "password": "testpassword123", | ||
| "user_name": "Test User", | ||
| } | ||
|
|
||
| response = client.post( | ||
| f"{settings.API_V1_STR}/onboard", json=data, headers=superuser_token_headers | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
|
|
||
| response_data = response.json() | ||
| assert "organization_id" in response_data | ||
| assert "project_id" in response_data | ||
| assert "user_id" in response_data | ||
| assert "api_key" in response_data | ||
|
|
||
| organization = ( | ||
| db.query(Organization) | ||
| .filter(Organization.name == data["organization_name"]) | ||
| .first() | ||
| ) | ||
| project = db.query(Project).filter(Project.name == data["project_name"]).first() | ||
| user = db.query(User).filter(User.email == data["email"]).first() | ||
| api_key = db.query(APIKey).filter(APIKey.user_id == user.id).first() | ||
|
|
||
| assert organization is not None | ||
| assert project is not None | ||
| assert user is not None | ||
| assert api_key is not None | ||
|
|
||
| plain_token = response_data["api_key"] | ||
| encrypted_stored = api_key.key | ||
|
|
||
| assert decrypt_api_key(encrypted_stored) == plain_token # main check | ||
| assert encrypted_stored != plain_token | ||
|
|
||
| assert user.is_superuser is False | ||
|
|
||
|
|
||
| def test_create_user_existing_email( | ||
| client, db: Session, superuser_token_headers: dict[str, str] | ||
| ): | ||
| data = { | ||
| "organization_name": "TestOrg", | ||
| "project_name": "TestProject", | ||
| "email": random_email(), | ||
| "password": "testpassword123", | ||
| "user_name": "Test User", | ||
| } | ||
|
|
||
| client.post( | ||
| f"{settings.API_V1_STR}/onboard", json=data, headers=superuser_token_headers | ||
| ) | ||
|
|
||
| response = client.post( | ||
| f"{settings.API_V1_STR}/onboard", json=data, headers=superuser_token_headers | ||
| ) | ||
|
|
||
| assert response.status_code == 400 | ||
| assert ( | ||
| response.json()["detail"] | ||
| == "400: API key already exists for this user and organization" | ||
| ) | ||
|
|
||
|
|
||
| def test_is_superuser_flag( | ||
| client, db: Session, superuser_token_headers: dict[str, str] | ||
| ): | ||
| data = { | ||
| "organization_name": "TestOrg", | ||
| "project_name": "TestProject", | ||
| "email": random_email(), | ||
| "password": "testpassword123", | ||
| "user_name": "Test User", | ||
| } | ||
|
|
||
| response = client.post( | ||
| f"{settings.API_V1_STR}/onboard", json=data, headers=superuser_token_headers | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
|
|
||
| response_data = response.json() | ||
| user = db.query(User).filter(User.id == response_data["user_id"]).first() | ||
| assert user is not None | ||
| assert user.is_superuser is False | ||
|
|
||
|
|
||
| def test_organization_and_project_creation( | ||
| client, db: Session, superuser_token_headers: dict[str, str] | ||
| ): | ||
| data = { | ||
| "organization_name": "NewOrg", | ||
| "project_name": "NewProject", | ||
| "email": random_email(), | ||
| "password": "newpassword123", | ||
| "user_name": "New User", | ||
| } | ||
|
|
||
| response = client.post( | ||
| f"{settings.API_V1_STR}/onboard", json=data, headers=superuser_token_headers | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
|
|
||
| organization = ( | ||
| db.query(Organization) | ||
| .filter(Organization.name == data["organization_name"]) | ||
| .first() | ||
| ) | ||
| project = db.query(Project).filter(Project.name == data["project_name"]).first() | ||
|
|
||
| assert organization is not None | ||
| assert project is not None |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it common practice to include DB calls within the routes? I thought this was what the CRUD module was for. @AkhileshNegi what are your thoughts?