diff --git a/app/api/compliance.py b/app/api/compliance.py index 12af7144..811faeaa 100644 --- a/app/api/compliance.py +++ b/app/api/compliance.py @@ -19,6 +19,7 @@ from sqlalchemy.orm import Session from app.database import get_db from app.utils.compliance_service import ( + COMPLIANCE_TEMPLATES, apply_template, evaluate_template_status, get_all_templates, @@ -153,6 +154,9 @@ async def apply_compliance_template(name: str, db: DbSession, admin: AdminUser) Writes all template settings to the database and evaluates the resulting compliance status. """ + if name not in COMPLIANCE_TEMPLATES: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Template '{name}' not found") + template = get_template_by_name(db, name) if template is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Template '{name}' not found") diff --git a/frontend/templates/compliance.html b/frontend/templates/compliance.html index aa41ab6e..06685253 100644 --- a/frontend/templates/compliance.html +++ b/frontend/templates/compliance.html @@ -155,9 +155,11 @@ diff --git a/tests/test_compliance.py b/tests/test_compliance.py index e8b42e62..31ef9030 100644 --- a/tests/test_compliance.py +++ b/tests/test_compliance.py @@ -14,6 +14,7 @@ import pytest from fastapi import status from fastapi.testclient import TestClient from sqlalchemy import create_engine +from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import sessionmaker from sqlalchemy.pool import StaticPool @@ -163,7 +164,7 @@ class TestComplianceTemplateModel: settings_json="{}", ) ct_session.add(t2) - with pytest.raises(Exception): + with pytest.raises(IntegrityError): ct_session.commit() ct_session.rollback()