diff --git a/tests/routers/test_channels.py b/tests/routers/test_channels.py index 1c78125..db3864b 100644 --- a/tests/routers/test_channels.py +++ b/tests/routers/test_channels.py @@ -7,11 +7,17 @@ from sqlalchemy import String 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.utils.database import get_db -# Import mocks from db_mocks +# Import mocks and fixtures +from tests.utils.auth_test_fixtures import ( + admin_user_client, + db_session, + mock_get_current_user_admin, + mock_get_current_user_non_admin, + non_admin_user_client, +) from tests.utils.db_mocks import ( MockBase, MockChannelDB, @@ -25,28 +31,6 @@ from tests.utils.db_mocks import session_mock as TestingSessionLocal # Create a FastAPI instance for testing app = FastAPI() - -# Mock current user -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, - ) - - # Override dependencies app.dependency_overrides[get_db] = mock_get_db app.include_router(channels_router) @@ -54,41 +38,6 @@ app.include_router(channels_router) client = TestClient(app) -@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.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.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 - - # --- Test Cases For Channel Creation --- diff --git a/tests/utils/auth_test_fixtures.py b/tests/utils/auth_test_fixtures.py new file mode 100644 index 0000000..cb983d7 --- /dev/null +++ b/tests/utils/auth_test_fixtures.py @@ -0,0 +1,73 @@ +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.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.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.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