|
| 1 | +# Copyright 2026 Dell Inc. or its subsidiaries. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Integration tests for Local Repository create API.""" |
| 16 | + |
| 17 | +from unittest.mock import patch |
| 18 | + |
| 19 | +from tests.integration.api.local_repo.conftest import setup_input_files |
| 20 | + |
| 21 | + |
| 22 | +class TestCreateLocalRepoSuccess: |
| 23 | + """Happy-path create local repository tests.""" |
| 24 | + |
| 25 | + def test_returns_202_with_valid_request( |
| 26 | + self, client, auth_headers, created_job, nfs_queue_dir, input_dir |
| 27 | + ): |
| 28 | + setup_input_files(input_dir, created_job) |
| 29 | + |
| 30 | + with patch( |
| 31 | + "infra.repositories.nfs_input_repository" |
| 32 | + ".NfsInputRepository.get_source_input_repository_path", |
| 33 | + return_value=input_dir / created_job / "input", |
| 34 | + ), patch( |
| 35 | + "infra.repositories.nfs_input_repository" |
| 36 | + ".NfsInputRepository.get_destination_input_repository_path", |
| 37 | + return_value=nfs_queue_dir / "dest_input", |
| 38 | + ), patch( |
| 39 | + "infra.repositories.nfs_input_repository" |
| 40 | + ".NfsInputRepository.validate_input_directory", |
| 41 | + return_value=True, |
| 42 | + ), patch( |
| 43 | + "infra.repositories.nfs_playbook_queue_request_repository" |
| 44 | + ".NfsPlaybookQueueRequestRepository.is_available", |
| 45 | + return_value=True, |
| 46 | + ), patch( |
| 47 | + "infra.repositories.nfs_playbook_queue_request_repository" |
| 48 | + ".NfsPlaybookQueueRequestRepository.write_request", |
| 49 | + return_value=nfs_queue_dir / "requests" / "test.json", |
| 50 | + ): |
| 51 | + response = client.post( |
| 52 | + f"/api/v1/jobs/{created_job}/stages/create-local-repository", |
| 53 | + headers=auth_headers, |
| 54 | + ) |
| 55 | + |
| 56 | + assert response.status_code == 202 |
| 57 | + data = response.json() |
| 58 | + assert data["job_id"] == created_job |
| 59 | + assert data["stage"] == "create-local-repository" |
| 60 | + assert data["status"] == "accepted" |
| 61 | + assert "submitted_at" in data |
| 62 | + assert "correlation_id" in data |
| 63 | + |
| 64 | + def test_returns_correlation_id( |
| 65 | + self, client, created_job, unique_correlation_id, |
| 66 | + nfs_queue_dir, input_dir |
| 67 | + ): |
| 68 | + setup_input_files(input_dir, created_job) |
| 69 | + headers = { |
| 70 | + "Authorization": "Bearer test-client-123", |
| 71 | + "X-Correlation-Id": unique_correlation_id, |
| 72 | + } |
| 73 | + |
| 74 | + with patch( |
| 75 | + "infra.repositories.nfs_input_repository" |
| 76 | + ".NfsInputRepository.get_source_input_repository_path", |
| 77 | + return_value=input_dir / created_job / "input", |
| 78 | + ), patch( |
| 79 | + "infra.repositories.nfs_input_repository" |
| 80 | + ".NfsInputRepository.get_destination_input_repository_path", |
| 81 | + return_value=nfs_queue_dir / "dest_input", |
| 82 | + ), patch( |
| 83 | + "infra.repositories.nfs_input_repository" |
| 84 | + ".NfsInputRepository.validate_input_directory", |
| 85 | + return_value=True, |
| 86 | + ), patch( |
| 87 | + "infra.repositories.nfs_playbook_queue_request_repository" |
| 88 | + ".NfsPlaybookQueueRequestRepository.is_available", |
| 89 | + return_value=True, |
| 90 | + ), patch( |
| 91 | + "infra.repositories.nfs_playbook_queue_request_repository" |
| 92 | + ".NfsPlaybookQueueRequestRepository.write_request", |
| 93 | + return_value=nfs_queue_dir / "requests" / "test.json", |
| 94 | + ): |
| 95 | + response = client.post( |
| 96 | + f"/api/v1/jobs/{created_job}/stages/create-local-repository", |
| 97 | + headers=headers, |
| 98 | + ) |
| 99 | + |
| 100 | + assert response.status_code == 202 |
| 101 | + assert response.json()["correlation_id"] == unique_correlation_id |
| 102 | + |
| 103 | + |
| 104 | +class TestCreateLocalRepoValidation: |
| 105 | + """Validation scenarios for create local repository.""" |
| 106 | + |
| 107 | + def test_invalid_job_id_returns_400(self, client, auth_headers): |
| 108 | + response = client.post( |
| 109 | + "/api/v1/jobs/invalid-uuid/stages/create-local-repository", |
| 110 | + headers=auth_headers, |
| 111 | + ) |
| 112 | + assert response.status_code == 400 |
| 113 | + detail = response.json()["detail"] |
| 114 | + assert detail["error"] == "INVALID_JOB_ID" |
| 115 | + |
| 116 | + def test_nonexistent_job_returns_404(self, client, auth_headers): |
| 117 | + fake_job_id = "018f3c4c-6a2e-7b2a-9c2a-3d8d2c4b9a11" |
| 118 | + response = client.post( |
| 119 | + f"/api/v1/jobs/{fake_job_id}/stages/create-local-repository", |
| 120 | + headers=auth_headers, |
| 121 | + ) |
| 122 | + assert response.status_code == 404 |
| 123 | + detail = response.json()["detail"] |
| 124 | + assert detail["error"] == "JOB_NOT_FOUND" |
| 125 | + |
| 126 | + |
| 127 | +class TestCreateLocalRepoAuthentication: |
| 128 | + """Authentication header tests.""" |
| 129 | + |
| 130 | + def test_missing_authorization_returns_422(self, client, created_job): |
| 131 | + headers = { |
| 132 | + "X-Correlation-Id": "019bf590-1234-7890-abcd-ef1234567890", |
| 133 | + } |
| 134 | + response = client.post( |
| 135 | + f"/api/v1/jobs/{created_job}/stages/create-local-repository", |
| 136 | + headers=headers, |
| 137 | + ) |
| 138 | + assert response.status_code == 422 |
| 139 | + |
| 140 | + def test_invalid_authorization_format_returns_401(self, client, created_job): |
| 141 | + headers = { |
| 142 | + "Authorization": "InvalidFormat test-token", |
| 143 | + "X-Correlation-Id": "019bf590-1234-7890-abcd-ef1234567890", |
| 144 | + } |
| 145 | + response = client.post( |
| 146 | + f"/api/v1/jobs/{created_job}/stages/create-local-repository", |
| 147 | + headers=headers, |
| 148 | + ) |
| 149 | + assert response.status_code == 401 |
| 150 | + |
| 151 | + def test_empty_bearer_token_returns_401(self, client, created_job): |
| 152 | + headers = { |
| 153 | + "Authorization": "Bearer ", |
| 154 | + "X-Correlation-Id": "019bf590-1234-7890-abcd-ef1234567890", |
| 155 | + } |
| 156 | + response = client.post( |
| 157 | + f"/api/v1/jobs/{created_job}/stages/create-local-repository", |
| 158 | + headers=headers, |
| 159 | + ) |
| 160 | + assert response.status_code == 401 |
| 161 | + |
| 162 | + |
| 163 | +class TestCreateLocalRepoInputValidation: |
| 164 | + """Input file validation tests.""" |
| 165 | + |
| 166 | + def test_missing_input_files_returns_400(self, client, auth_headers, created_job): |
| 167 | + with patch( |
| 168 | + "infra.repositories.nfs_input_repository" |
| 169 | + ".NfsInputRepository.validate_input_directory", |
| 170 | + return_value=False, |
| 171 | + ): |
| 172 | + response = client.post( |
| 173 | + f"/api/v1/jobs/{created_job}/stages/create-local-repository", |
| 174 | + headers=auth_headers, |
| 175 | + ) |
| 176 | + |
| 177 | + assert response.status_code == 400 |
| 178 | + detail = response.json()["detail"] |
| 179 | + assert detail["error"] == "INPUT_FILES_MISSING" |
0 commit comments