mathpulse-api-v3test / tests /role_policy_expectations.py
github-actions[bot]
deploy: backend from 0338469
d66d64c
Raw History Blame Contribute Delete
18 kB
"""Independent auth contract and route registry reviewed for regression tests."""
ALL_ROLES = frozenset({"student", "teacher", "admin"})
TEACHER_ADMIN = frozenset({"teacher", "admin"})
ADMIN_ONLY = frozenset({"admin"})
# Curated contract values, intentionally literal and independent of main.ROLE_POLICIES.
EXPECTED_ROLE_POLICIES = {
"/api/chat": ALL_ROLES,
"/api/chat/stream": ALL_ROLES,
"/api/verify-solution": ALL_ROLES,
"/api/predict-risk": TEACHER_ADMIN,
"/api/predict-risk/batch": TEACHER_ADMIN,
"/api/learning-path": ALL_ROLES,
"/api/analytics/daily-insight": TEACHER_ADMIN,
"/api/analytics/class/{class_id}": TEACHER_ADMIN,
"/api/analytics/class/{class_id}/students": TEACHER_ADMIN,
"/api/analytics/class/{class_id}/topics": TEACHER_ADMIN,
"/api/analytics/class/{class_id}/refresh-insights": TEACHER_ADMIN,
"/api/analytics/class/{class_id}/invalidate-cache": ALL_ROLES,
"/api/intervention/generate": TEACHER_ADMIN,
"/api/intervention/{student_id}": TEACHER_ADMIN,
"/api/intervention/{student_id}/step/{step_number}/complete": ALL_ROLES,
"/api/intervention/{student_id}/export-pdf": TEACHER_ADMIN,
"/api/pipeline/event": ALL_ROLES,
"/api/pipeline/profile/{student_id}": ALL_ROLES,
"/api/pipeline/profile/{student_id}/recompute": TEACHER_ADMIN,
"/api/pipeline/admin/backfill": ADMIN_ONLY,
"/api/upload/class-records": TEACHER_ADMIN,
"/api/class-section/{class_section_id}": TEACHER_ADMIN,
"/api/upload/class-records/risk-refresh/recent": TEACHER_ADMIN,
"/api/import/student-accounts/preview": TEACHER_ADMIN,
"/api/import/student-accounts/commit": TEACHER_ADMIN,
"/api/admin/users": ADMIN_ONLY,
"/api/admin/users/bulk-action": ADMIN_ONLY,
"/api/admin/school-analytics": ADMIN_ONLY,
"/api/teacher/create-student-account": TEACHER_ADMIN,
"/api/upload/course-materials": TEACHER_ADMIN,
"/api/upload/course-materials/recent": TEACHER_ADMIN,
"/api/teacher-materials/upload": TEACHER_ADMIN,
"/api/course-materials/topics": TEACHER_ADMIN,
"/api/quiz/generate": ALL_ROLES,
"/api/quiz/generate-async": ALL_ROLES,
"/api/quiz/preview": ALL_ROLES,
"/api/lesson/generate": TEACHER_ADMIN,
"/api/lesson/generate-async": TEACHER_ADMIN,
"/api/rag/lesson": ALL_ROLES,
"/api/rag/lesson/stream": ALL_ROLES,
"/api/rag/lesson/prefetch": ALL_ROLES,
"/api/rag/generate-problem": TEACHER_ADMIN,
"/api/rag/analysis-context": ALL_ROLES,
"/api/rag/documents/by-subject/{subject}": ADMIN_ONLY,
"/api/rag/documents/by-source": ADMIN_ONLY,
"/api/rag/documents/all": ADMIN_ONLY,
"/api/feedback/import-grounded": TEACHER_ADMIN,
"/api/feedback/import-grounded/summary": TEACHER_ADMIN,
"/api/import-grounded/access-audit": TEACHER_ADMIN,
"/api/quiz/student-competency": TEACHER_ADMIN,
"/api/calculator/evaluate": ALL_ROLES,
"/api/diagnostic/generate": ALL_ROLES,
"/api/diagnostic/submit": ALL_ROLES,
"/api/diagnostic/analyze": ALL_ROLES,
"/api/student/competency-analysis": TEACHER_ADMIN,
"/api/risk/train-model": ADMIN_ONLY,
"/api/predict-risk/enhanced": TEACHER_ADMIN,
"/api/quiz/calibrate-difficulty": TEACHER_ADMIN,
"/api/quiz/adaptive-select": TEACHER_ADMIN,
"/api/learning/recommend-topics": TEACHER_ADMIN,
"/api/analytics/student-summary": ALL_ROLES,
"/api/analytics/class-insights": TEACHER_ADMIN,
"/api/analytics/refresh-cache": ADMIN_ONLY,
"/api/testing/reset-data": ALL_ROLES,
"/api/dev/generate-mock-data": ADMIN_ONLY,
"/api/analytics/config": TEACHER_ADMIN,
"/api/analytics/imported-class-overview": TEACHER_ADMIN,
"/api/analytics/topic-mastery": TEACHER_ADMIN,
"/api/automation/diagnostic-completed": ADMIN_ONLY,
"/api/automation/quiz-submitted": ADMIN_ONLY,
"/api/automation/student-enrolled": ADMIN_ONLY,
"/api/automation/data-imported": ADMIN_ONLY,
"/api/automation/content-updated": ADMIN_ONLY,
"/api/admin/model-config": ADMIN_ONLY,
"/api/admin/upload-pdf": ADMIN_ONLY,
"/api/admin/reingest-pdf": ADMIN_ONLY,
"/api/admin/delete-file": ADMIN_ONLY,
"/api/admin/model-config/profile": ADMIN_ONLY,
"/api/admin/model-config/override": ADMIN_ONLY,
"/api/admin/model-config/reset": ADMIN_ONLY,
"/api/lessons/videos/search": ALL_ROLES,
"/api/lesson/personalized": ALL_ROLES,
"/api/at-risk/resolve": ALL_ROLES,
"/api/at-risk/fallback/{uid}/{topic_id}": ALL_ROLES,
"/api/quiz-battle/generate": ALL_ROLES,
"/api/quiz-battle/ingest-pdf": TEACHER_ADMIN,
"/api/quiz-battle/bank-status": TEACHER_ADMIN,
"/api/class-records/upload": TEACHER_ADMIN,
"/api/jev/verify": TEACHER_ADMIN,
}
# Fixed route/method manifest. New endpoints require an explicit review and
# classification; no route becomes public merely by being absent from policy.
EXPECTED_REGISTERED_ROUTE_METHODS = {
"/openapi.json": frozenset({"GET", "HEAD"}),
"/docs": frozenset({"GET", "HEAD"}),
"/docs/oauth2-redirect": frozenset({"GET", "HEAD"}),
"/redoc": frozenset({"GET", "HEAD"}),
"/api/rag/health": frozenset({"GET"}),
"/api/rag/lesson": frozenset({"POST"}),
"/api/rag/lesson/stream": frozenset({"POST"}),
"/api/rag/lesson/prefetch": frozenset({"POST"}),
"/api/rag/generate-problem": frozenset({"POST"}),
"/api/rag/analysis-context": frozenset({"POST"}),
"/api/rag/documents": frozenset({"GET"}),
"/api/rag/documents/by-subject/{subject}": frozenset({"DELETE"}),
"/api/rag/documents/by-source": frozenset({"DELETE"}),
"/api/rag/documents/all": frozenset({"DELETE"}),
"/api/admin/model-config": frozenset({"GET"}),
"/api/admin/model-config/profile": frozenset({"POST"}),
"/api/admin/model-config/override": frozenset({"POST"}),
"/api/admin/model-config/reset": frozenset({"DELETE"}),
"/api/admin/reingest-status": frozenset({"GET"}),
"/api/admin/upload-pdf": frozenset({"POST"}),
"/api/admin/reingest-pdf": frozenset({"POST"}),
"/api/admin/delete-file": frozenset({"POST"}),
"/api/admin/dashboard-analytics": frozenset({"GET"}),
"/api/admin/school-analytics": frozenset({"GET"}),
"/api/curriculum/subjects": frozenset({"GET"}),
"/api/curriculum/subjects/{subject_id}": frozenset({"GET"}),
"/api/curriculum/subjects/{subject_id}/topics": frozenset({"GET"}),
"/api/curriculum/subjects/{subject_id}/topics/{topic_id}": frozenset({"GET"}),
"/api/diagnostic/generate": frozenset({"POST"}),
"/api/diagnostic/submit": frozenset({"POST"}),
"/api/diagnostic/analyze": frozenset({"POST"}),
"/api/diagnostic/results/{user_id}": frozenset({"GET"}),
"/api/lessons/videos/search": frozenset({"POST"}),
"/api/quiz-battle/generate": frozenset({"POST"}),
"/api/quiz-battle/ingest-pdf": frozenset({"POST"}),
"/api/quiz-battle/bank-status": frozenset({"GET"}),
"/api/quiz-battle/results": frozenset({"GET"}),
"/api/teacher-materials/upload": frozenset({"POST"}),
"/api/teacher-materials/{moduleId}/generate-quiz": frozenset({"POST"}),
"/api/templates/class-records": frozenset({"GET"}),
"/api/class-records/upload": frozenset({"POST"}),
"/api/class-records/intervention-plan": frozenset({"POST"}),
"/api/class-records": frozenset({"GET"}),
"/api/class-records/{uploadId}/students": frozenset({"GET"}),
"/api/class-records/{uploadId}/ai-report": frozenset({"POST"}),
"/api/risk/compute": frozenset({"POST"}),
"/api/risk/compute/batch": frozenset({"POST"}),
"/api/tutor-checkin": frozenset({"POST"}),
"/api/practice/generate": frozenset({"POST"}),
"/api/practice/submit": frozenset({"POST"}),
"/api/practice/stats/{userId}": frozenset({"GET"}),
"/api/practice/history/{userId}": frozenset({"GET"}),
"/api/try-it-yourself/resolve-question": frozenset({"POST"}),
"/api/try-it-yourself/use-hint": frozenset({"POST"}),
"/api/try-it-yourself/complete-session": frozenset({"POST"}),
"/api/try-it-yourself/generate-round": frozenset({"POST"}),
"/api/try-it-yourself/shadow-retry": frozenset({"POST"}),
"/api/analytics/class/{class_id}": frozenset({"GET"}),
"/api/analytics/class/{class_id}/students": frozenset({"GET"}),
"/api/analytics/class/{class_id}/topics": frozenset({"GET"}),
"/api/analytics/class/{class_id}/refresh-insights": frozenset({"POST"}),
"/api/analytics/class/{class_id}/invalidate-cache": frozenset({"POST"}),
"/api/intervention/generate": frozenset({"POST"}),
"/api/intervention/{student_id}": frozenset({"GET"}),
"/api/intervention/{student_id}/step/{step_number}/complete": frozenset({"POST"}),
"/api/intervention/{student_id}/export-pdf": frozenset({"GET"}),
"/api/pipeline/event": frozenset({"POST"}),
"/api/pipeline/nudge/{student_id}": frozenset({"POST"}),
"/api/pipeline/profile/{student_id}": frozenset({"GET"}),
"/api/pipeline/profile/{student_id}/recompute": frozenset({"POST"}),
"/api/pipeline/admin/backfill": frozenset({"POST"}),
"/api/ai/weakness-detection": frozenset({"POST"}),
"/api/ai/module-preview": frozenset({"POST"}),
"/api/ai/study-tips": frozenset({"POST"}),
"/api/at-risk/resolve": frozenset({"POST"}),
"/api/at-risk/fallback/{uid}/{topic_id}": frozenset({"GET"}),
"/api/curriculum/generate-module": frozenset({"POST"}),
"/api/mastery/record": frozenset({"POST"}),
"/api/jev/verify": frozenset({"POST"}),
"/health": frozenset({"GET"}),
"/api/memory/health": frozenset({"GET"}),
"/debug/scope-info": frozenset({"GET"}),
"/": frozenset({"GET"}),
"/api/chat": frozenset({"POST"}),
"/api/chat/stream": frozenset({"POST"}),
"/api/verify-solution": frozenset({"POST"}),
"/api/predict-risk": frozenset({"POST"}),
"/api/predict-risk/batch": frozenset({"POST"}),
"/api/learning-path": frozenset({"POST"}),
"/api/analytics/daily-insight": frozenset({"POST"}),
"/api/import/student-accounts/preview": frozenset({"POST"}),
"/api/import/student-accounts/commit": frozenset({"POST"}),
"/api/admin/audit-log": frozenset({"POST"}),
"/api/admin/users": frozenset({"GET", "PATCH", "POST", "DELETE"}),
"/api/admin/users/bulk-action": frozenset({"POST"}),
"/api/teacher/create-student-account": frozenset({"POST"}),
"/api/upload/class-records/risk-refresh/recent": frozenset({"GET"}),
"/api/class-section/{class_section_id}": frozenset({"DELETE"}),
"/api/upload/class-records": frozenset({"POST"}),
"/api/upload/course-materials": frozenset({"POST"}),
"/api/upload/course-materials/recent": frozenset({"GET"}),
"/api/course-materials/topics": frozenset({"GET"}),
"/api/lesson/generate": frozenset({"POST"}),
"/api/lesson-plans/{lesson_id}/publish": frozenset({"POST"}),
"/api/lesson/generate-async": frozenset({"POST"}),
"/api/feedback/import-grounded": frozenset({"POST"}),
"/api/feedback/import-grounded/summary": frozenset({"GET"}),
"/api/import-grounded/access-audit": frozenset({"GET"}),
"/api/quiz/generate": frozenset({"POST"}),
"/api/quiz/preview": frozenset({"POST"}),
"/api/quiz/generate-async": frozenset({"POST"}),
"/api/tasks/{task_id}": frozenset({"GET"}),
"/api/tasks": frozenset({"GET"}),
"/api/tasks/{task_id}/cancel": frozenset({"POST"}),
"/api/quiz/topics": frozenset({"GET"}),
"/api/quiz/student-competency": frozenset({"POST"}),
"/api/calculator/evaluate": frozenset({"POST"}),
"/api/student/competency-analysis": frozenset({"POST"}),
"/api/risk/train-model": frozenset({"POST"}),
"/api/predict-risk/enhanced": frozenset({"POST"}),
"/api/quiz/calibrate-difficulty": frozenset({"POST"}),
"/api/quiz/adaptive-select": frozenset({"POST"}),
"/api/learning/recommend-topics": frozenset({"POST"}),
"/api/analytics/student-summary": frozenset({"GET"}),
"/api/analytics/class-insights": frozenset({"POST"}),
"/api/analytics/refresh-cache": frozenset({"POST"}),
"/api/dev/generate-mock-data": frozenset({"POST"}),
"/api/testing/reset-data": frozenset({"POST"}),
"/api/analytics/config": frozenset({"GET"}),
"/api/analytics/imported-class-overview": frozenset({"GET"}),
"/api/analytics/topic-mastery": frozenset({"GET"}),
"/api/automation/diagnostic-completed": frozenset({"POST"}),
"/api/automation/quiz-submitted": frozenset({"POST"}),
"/api/automation/student-enrolled": frozenset({"POST"}),
"/api/automation/data-imported": frozenset({"POST"}),
"/api/automation/content-updated": frozenset({"POST"}),
"/api/lesson/diagnostic": frozenset({"POST"}),
"/api/lessons/generate": frozenset({"POST"}),
"/api/progress/evaluate": frozenset({"POST"}),
"/api/quiz/adaptive": frozenset({"POST"}),
"/api/learning/path": frozenset({"POST"}),
"/api/lesson/personalized": frozenset({"POST"}),
}
PUBLIC_ROUTE_EXCEPTIONS = {
("GET", "/openapi.json"): "OpenAPI schema endpoint",
("HEAD", "/openapi.json"): "OpenAPI schema endpoint",
("GET", "/docs"): "Swagger UI endpoint",
("HEAD", "/docs"): "Swagger UI endpoint",
("GET", "/docs/oauth2-redirect"): "Swagger OAuth redirect endpoint",
("HEAD", "/docs/oauth2-redirect"): "Swagger OAuth redirect endpoint",
("GET", "/redoc"): "ReDoc endpoint",
("HEAD", "/redoc"): "ReDoc endpoint",
("GET", "/health"): "Unauthenticated liveness endpoint",
("GET", "/"): "Unauthenticated API identity endpoint",
("GET", "/api/rag/health"): "Unauthenticated RAG health endpoint",
("GET", "/api/templates/class-records"): "Unauthenticated static import template",
("GET", "/api/quiz/topics"): "Public topic catalog",
("GET", "/debug/scope-info"): "Non-API diagnostic route bypasses AuthMiddleware",
}
# AuthMiddleware authenticates these endpoints, but they have no route-specific
# role restriction. Keep that distinct from endpoints with in-handler guards.
AUTHENTICATED_ROUTE_EXCEPTIONS = {
("GET", "/api/curriculum/subjects"): "Authenticated curriculum catalog; no role restriction",
("GET", "/api/curriculum/subjects/{subject_id}"): "Authenticated curriculum catalog; no role restriction",
("GET", "/api/curriculum/subjects/{subject_id}/topics"): "Authenticated curriculum catalog; no role restriction",
("GET", "/api/curriculum/subjects/{subject_id}/topics/{topic_id}"): "Authenticated curriculum catalog; no role restriction",
("GET", "/api/memory/health"): "Authenticated health endpoint; no role restriction",
("GET", "/api/rag/documents"): "Authenticated RAG document listing; no role restriction",
("POST", "/api/curriculum/generate-module"): "Authenticated module generation; no role restriction",
("POST", "/api/ai/module-preview"): "Authenticated preview generation; no role restriction",
("POST", "/api/ai/study-tips"): "Authenticated study tips; no role restriction",
("POST", "/api/ai/weakness-detection"): "Authenticated weakness detection; no role restriction",
("POST", "/api/learning/path"): "Authenticated learning path; no role restriction",
("POST", "/api/lesson/diagnostic"): "Authenticated diagnostic lesson; no role restriction",
("POST", "/api/lessons/generate"): "Authenticated lesson generation; no role restriction",
("POST", "/api/mastery/record"): "Authenticated mastery record; no role restriction",
("POST", "/api/progress/evaluate"): "Authenticated progress evaluation; no role restriction",
("POST", "/api/quiz/adaptive"): "Authenticated adaptive quiz; no role restriction",
("POST", "/api/risk/compute"): "Authenticated risk computation; no role restriction",
("POST", "/api/risk/compute/batch"): "Authenticated batch risk computation; no role restriction",
("POST", "/api/tutor-checkin"): "Authenticated tutor check-in; no role restriction",
}
# These routes perform an explicit handler-level ownership or role check.
HANDLER_GUARDED_ROUTE_EXCEPTIONS = {
("GET", "/api/diagnostic/results/{user_id}"): "student UID ownership check",
("POST", "/api/pipeline/event"): "student event owner check",
("POST", "/api/pipeline/nudge/{student_id}"): "student target ownership check",
("POST", "/api/admin/audit-log"): "admin/teacher role check",
("GET", "/api/admin/reingest-status"): "admin role check",
("GET", "/api/admin/dashboard-analytics"): "admin role check",
("GET", "/api/quiz-battle/results"): "teacher/admin role check",
("POST", "/api/teacher-materials/{moduleId}/generate-quiz"): "teacher/admin role check",
("POST", "/api/class-records/intervention-plan"): "teacher/admin role check",
("GET", "/api/class-records"): "teacher/admin role check",
("GET", "/api/class-records/{uploadId}/students"): "teacher/admin role check",
("POST", "/api/class-records/{uploadId}/ai-report"): "teacher/admin role check",
("POST", "/api/class-records/upload"): "teacher/admin role check",
("POST", "/api/practice/generate"): "authenticated user and owner checks",
("POST", "/api/practice/submit"): "authenticated user and owner checks",
("GET", "/api/practice/stats/{userId}"): "authenticated user and owner checks",
("GET", "/api/practice/history/{userId}"): "authenticated user and owner checks",
("POST", "/api/tasks/{task_id}/cancel"): "task owner or admin check",
("GET", "/api/tasks/{task_id}"): "task owner or admin check",
("GET", "/api/tasks"): "task owner filtering or admin check",
("POST", "/api/lesson-plans/{lesson_id}/publish"): "teacher/admin role and lesson owner check",
("POST", "/api/try-it-yourself/resolve-question"): "authenticated user ownership check",
("POST", "/api/try-it-yourself/use-hint"): "authenticated user ownership check",
("POST", "/api/try-it-yourself/complete-session"): "authenticated user ownership check",
("POST", "/api/try-it-yourself/generate-round"): "authenticated user ownership check",
("POST", "/api/try-it-yourself/shadow-retry"): "authenticated user ownership check",
}