11"""
22Unit tests for job management API.
33"""
4+ import os
45import pytest
56from unittest .mock import Mock , patch , MagicMock
67from fastapi import status
78from fastapi .testclient import TestClient
89from datetime import datetime
10+
11+ os .environ ["INTERNAL_API_KEY" ] = "test-key"
12+
913from src .sum_impact_assessment .api .main import app
1014from sum_impact_assessment .database .models .job import JobRun
1115from src .sum_impact_assessment .schemas .job import JobNameEnum , JobStatusEnum
1216
1317
1418# Create test client
1519client = TestClient (app )
20+ AUTH_HEADERS = {"X-Internal-API-Key" : "test-key" }
1621
1722
1823class TestJobsAPI :
@@ -43,7 +48,7 @@ def test_trigger_job_success(self, mock_get_job_class, mock_job_repo_class):
4348 mock_get_job_class .return_value = mock_job_class
4449
4550 # Make request
46- response = client .post ("/jobs/runs/kpi_measures_analysis" )
51+ response = client .post ("/jobs/runs/kpi_measures_analysis" , headers = AUTH_HEADERS )
4752
4853 # Assertions
4954 assert response .status_code == status .HTTP_201_CREATED
@@ -68,7 +73,7 @@ def test_trigger_job_not_found_in_registry(self, mock_get_job_class, mock_job_re
6873 mock_get_job_class .side_effect = KeyError ("Job not found" )
6974
7075 # Make request
71- response = client .post ("/jobs/runs/kpi_measures_analysis" )
76+ response = client .post ("/jobs/runs/kpi_measures_analysis" , headers = AUTH_HEADERS )
7277
7378 # Assertions
7479 assert response .status_code == status .HTTP_404_NOT_FOUND
@@ -80,7 +85,7 @@ def test_trigger_job_not_found_in_registry(self, mock_get_job_class, mock_job_re
8085 def test_trigger_job_invalid_job_name (self ):
8186 """Test triggering a job with invalid job name returns 422."""
8287 # Make request with invalid job name
83- response = client .post ("/jobs/runs/invalid_job_name" )
88+ response = client .post ("/jobs/runs/invalid_job_name" , headers = AUTH_HEADERS )
8489
8590 # Assertions
8691 assert response .status_code == status .HTTP_422_UNPROCESSABLE_ENTITY
@@ -100,7 +105,7 @@ def test_trigger_job_database_error(self, mock_get_job_class, mock_job_repo_clas
100105 mock_job_repo_class .return_value = mock_repo_instance
101106
102107 # Make request
103- response = client .post ("/jobs/runs/kpi_measures_analysis" )
108+ response = client .post ("/jobs/runs/kpi_measures_analysis" , headers = AUTH_HEADERS )
104109
105110 # Assertions
106111 assert response .status_code == status .HTTP_500_INTERNAL_SERVER_ERROR
@@ -133,6 +138,7 @@ def test_trigger_job_with_params(self, mock_get_job_class, mock_job_repo_class):
133138 # Make request with params
134139 response = client .post (
135140 "/jobs/runs/mcda_analysis_quantitative" ,
141+ headers = AUTH_HEADERS ,
136142 json = {"params" : {"kpi_group_type" : "MCDA_GOALS" }}
137143 )
138144
@@ -168,7 +174,7 @@ def test_trigger_mcda_analysis_job(self, mock_get_job_class, mock_job_repo_class
168174 mock_get_job_class .return_value = mock_job_class
169175
170176 # Make request
171- response = client .post ("/jobs/runs/mcda_analysis_quantitative" )
177+ response = client .post ("/jobs/runs/mcda_analysis_quantitative" , headers = AUTH_HEADERS )
172178
173179 # Assertions
174180 assert response .status_code == status .HTTP_201_CREATED
@@ -207,6 +213,7 @@ def test_trigger_mcda_analysis_job_with_perspective(self, mock_get_job_class, mo
207213 # Make request with perspective parameter
208214 response = client .post (
209215 "/jobs/runs/mcda_analysis_quantitative" ,
216+ headers = AUTH_HEADERS ,
210217 json = {"params" : {"perspective" : "regulatory" }}
211218 )
212219
0 commit comments