83 lines
2.6 KiB
Python
83 lines
2.6 KiB
Python
import pytest
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.auth.dependencies import get_current_user
|
|
from app.models.auth import CognitoUser
|
|
from app.routers.channels import router as channels_router
|
|
from app.routers.groups import router as groups_router
|
|
from app.routers.playlist import router as playlist_router
|
|
from app.routers.priorities import router as priorities_router
|
|
from app.utils.database import get_db
|
|
from tests.utils.db_mocks import (
|
|
MockBase,
|
|
MockChannelDB,
|
|
MockChannelURL,
|
|
MockPriority,
|
|
engine_mock,
|
|
mock_get_db,
|
|
)
|
|
from tests.utils.db_mocks import session_mock as TestingSessionLocal
|
|
|
|
|
|
def mock_get_current_user_admin():
|
|
return CognitoUser(
|
|
username="testadmin",
|
|
email="testadmin@example.com",
|
|
roles=["admin"],
|
|
user_status="CONFIRMED",
|
|
enabled=True,
|
|
)
|
|
|
|
|
|
def mock_get_current_user_non_admin():
|
|
return CognitoUser(
|
|
username="testuser",
|
|
email="testuser@example.com",
|
|
roles=["user"], # Or any role other than admin
|
|
user_status="CONFIRMED",
|
|
enabled=True,
|
|
)
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def db_session():
|
|
# Create tables for each test function
|
|
MockBase.metadata.create_all(bind=engine_mock)
|
|
db = TestingSessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
# Drop tables after each test function
|
|
MockBase.metadata.drop_all(bind=engine_mock)
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def admin_user_client(db_session: Session):
|
|
"""Yields a TestClient configured with an admin user."""
|
|
test_app = FastAPI()
|
|
test_app.include_router(channels_router)
|
|
test_app.include_router(priorities_router)
|
|
test_app.include_router(playlist_router)
|
|
test_app.include_router(groups_router)
|
|
test_app.dependency_overrides[get_db] = mock_get_db
|
|
test_app.dependency_overrides[get_current_user] = mock_get_current_user_admin
|
|
with TestClient(test_app) as test_client:
|
|
yield test_client
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def non_admin_user_client(db_session: Session):
|
|
"""Yields a TestClient configured with a non-admin user."""
|
|
test_app = FastAPI()
|
|
test_app.include_router(channels_router)
|
|
test_app.include_router(priorities_router)
|
|
test_app.include_router(playlist_router)
|
|
test_app.include_router(groups_router)
|
|
test_app.dependency_overrides[get_db] = mock_get_db
|
|
test_app.dependency_overrides[get_current_user] = mock_get_current_user_non_admin
|
|
with TestClient(test_app) as test_client:
|
|
yield test_client
|