Created unit tests for playlist.py
All checks were successful
AWS Deploy on Push / build (push) Successful in 1m8s

This commit is contained in:
2025-05-28 22:58:29 -05:00
parent bf6f156fec
commit f7a1c20066
2 changed files with 46 additions and 0 deletions

View File

@@ -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"]

View File

@@ -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: