diff --git a/apps/api/requirements.txt b/apps/api/requirements.txt index b361beec..4c38a422 100644 --- a/apps/api/requirements.txt +++ b/apps/api/requirements.txt @@ -11,6 +11,8 @@ botocore python-jose passlib fastapi-jwt-auth +pytest +httpx faker requests pyyaml diff --git a/apps/api/src/services/install/install.py b/apps/api/src/services/install/install.py index b4538b28..bc079957 100644 --- a/apps/api/src/services/install/install.py +++ b/apps/api/src/services/install/install.py @@ -93,7 +93,7 @@ async def update_install_instance( # Install Default roles -async def install_default_elements(request: Request, data: dict, db_session: Session): +async def install_default_elements( data: dict, db_session: Session): # remove all default roles statement = select(Role).where(Role.role_type == RoleTypeEnum.TYPE_GLOBAL) roles = db_session.exec(statement).all() @@ -279,7 +279,7 @@ async def install_default_elements(request: Request, data: dict, db_session: Ses # Organization creation async def install_create_organization( - request: Request, org_object: OrganizationCreate, db_session: Session + org_object: OrganizationCreate, db_session: Session ): org = Organization.from_orm(org_object) @@ -296,7 +296,7 @@ async def install_create_organization( async def install_create_organization_user( - request: Request, user_object: UserCreate, org_slug: str, db_session: Session + user_object: UserCreate, org_slug: str, db_session: Session ): user = User.from_orm(user_object) diff --git a/apps/api/src/tests/test_main.py b/apps/api/src/tests/test_main.py new file mode 100644 index 00000000..22a02f5f --- /dev/null +++ b/apps/api/src/tests/test_main.py @@ -0,0 +1,50 @@ +from fastapi.testclient import TestClient +from sqlalchemy import create_engine +from sqlalchemy.pool import StaticPool +from sqlmodel import SQLModel, Session +from src.tests.utils.init_data_for_tests import create_initial_data_for_tests +from src.core.events.database import get_db_session +import pytest +import asyncio +from app import app + +client = TestClient(app) + +# TODO : fix this later https://stackoverflow.com/questions/10253826/path-issue-with-pytest-importerror-no-module-named + + +@pytest.fixture(name="session", scope="session") +def session_fixture(): + engine = create_engine( + "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool + ) + SQLModel.metadata.create_all(engine) + with Session(engine) as session: + yield session + + +@pytest.fixture(name="client") +def client_fixture(session: Session): + def get_session_override(): + return session + + app.dependency_overrides[get_db_session] = get_session_override + + client = TestClient(app) + yield client + app.dependency_overrides.clear() + + +@pytest.fixture(scope="session", autouse=True) +def execute_before_all_tests(session: Session): + # This function will run once before all tests. + asyncio.run(create_initial_data_for_tests(session)) + + +def test_create_default_elements(client: TestClient, session: Session): + + response = client.get( + "/api/v1/orgs/slug/wayne", + ) + + assert response.status_code == 200 diff --git a/apps/api/src/tests/test_rbac.py b/apps/api/src/tests/test_rbac.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/api/src/tests/utils/__init__.py b/apps/api/src/tests/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/api/src/tests/utils/init_data_for_tests.py b/apps/api/src/tests/utils/init_data_for_tests.py new file mode 100644 index 00000000..8229cfe2 --- /dev/null +++ b/apps/api/src/tests/utils/init_data_for_tests.py @@ -0,0 +1,57 @@ +from sqlmodel import Session, select +from src.db.user_organizations import UserOrganization +from src.db.organizations import OrganizationCreate +from src.db.users import User, UserCreate +from src.services.install.install import ( + install_create_organization, + install_create_organization_user, + install_default_elements, +) + + +async def create_initial_data_for_tests(db_session: Session): + # Install default elements + await install_default_elements({}, db_session) + + # Initiate test Organization + test_org = OrganizationCreate( + name="Wayne Enterprises", + description=None, + slug="wayne", + email="hello@wayne.dev", + logo_image=None, + ) + + # Create test organization + await install_create_organization(test_org, db_session) + + users = [ + UserCreate( + username="batman", + first_name="Bruce", + last_name="Wayne", + email="bruce@wayne.com", + password="imbatman", + ), + UserCreate( + username="robin", + first_name="Richard John", + last_name="Grayson", + email="robin@wayne.com", + password="secret", + ), + ] + + # Create 2 users in that Organization + for user in users: + await install_create_organization_user(user, "wayne", db_session) + + # Make robin a normal user + statement = select(UserOrganization).join(User).where(User.username == "robin") + user_org = db_session.exec(statement).first() + + user_org.role_id = 3 # type: ignore + db_session.add(user_org) + db_session.commit() + + return True