diff --git a/tests/routers/test_playlist.py b/tests/routers/test_playlist.py new file mode 100644 index 0000000..ac05124 --- /dev/null +++ b/tests/routers/test_playlist.py @@ -0,0 +1,43 @@ +import pytest +from fastapi import status + +from tests.utils.auth_test_fixtures import ( + admin_user_client, + db_session, + non_admin_user_client, +) + + +def test_protected_route_admin_access(db_session, admin_user_client): + """Test that admin users can access the protected route""" + response = admin_user_client.get("/playlist/protected") + assert response.status_code == status.HTTP_200_OK + data = response.json() + assert "access to support resources" in data["message"] + assert "testadmin" in data["message"] + + +def test_protected_route_non_admin_access(db_session, non_admin_user_client): + """Test that non-admin users can access the protected route + (just requires authentication)""" + response = non_admin_user_client.get("/playlist/protected") + assert response.status_code == status.HTTP_200_OK + data = response.json() + assert "access to support resources" in data["message"] + assert "testuser" in data["message"] + + +def test_protected_route_no_auth(): + """Test that unauthenticated users cannot access the protected route""" + from fastapi import FastAPI + from fastapi.testclient import TestClient + + from app.routers.playlist import router as playlist_router + + app = FastAPI() + app.include_router(playlist_router) + client = TestClient(app) + + response = client.get("/playlist/protected") + assert response.status_code == status.HTTP_401_UNAUTHORIZED + assert "Not authenticated" in response.json()["detail"] diff --git a/tests/utils/auth_test_fixtures.py b/tests/utils/auth_test_fixtures.py index e1ff564..0f95630 100644 --- a/tests/utils/auth_test_fixtures.py +++ b/tests/utils/auth_test_fixtures.py @@ -6,6 +6,7 @@ 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.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 ( @@ -58,6 +59,7 @@ def admin_user_client(db_session: Session): test_app = FastAPI() test_app.include_router(channels_router) test_app.include_router(priorities_router) + test_app.include_router(playlist_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: @@ -70,6 +72,7 @@ def non_admin_user_client(db_session: Session): test_app = FastAPI() test_app.include_router(channels_router) test_app.include_router(priorities_router) + test_app.include_router(playlist_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: