From 1a7d601dc08df0dd18d8a4aba06e922af891d719 Mon Sep 17 00:00:00 2001 From: Neda Moeini <neda.moeini@geant.org> Date: Tue, 4 Mar 2025 13:33:31 +0100 Subject: [PATCH] Update unit tests to support authentication. --- sage_validation/file_validator/tests.py | 1 - test/conftest.py | 7 ++++++- .../test_file_validator/test_file_validator_endpoints.py | 9 +++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) delete mode 100644 sage_validation/file_validator/tests.py diff --git a/sage_validation/file_validator/tests.py b/sage_validation/file_validator/tests.py deleted file mode 100644 index 676ebd6..0000000 --- a/sage_validation/file_validator/tests.py +++ /dev/null @@ -1 +0,0 @@ -"""All the tests for the file_validator app.""" diff --git a/test/conftest.py b/test/conftest.py index f32a594..9f0053b 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -2,6 +2,7 @@ from unittest.mock import MagicMock import pytest +from django.contrib.auth.models import User from django.core.files.uploadedfile import SimpleUploadedFile from faker import Faker from rest_framework.test import APIClient @@ -144,4 +145,8 @@ def mock_meo_database(mocker: MagicMock)-> None: @pytest.fixture def api_client() -> APIClient: """Fixture to return Django API test client.""" - return APIClient() + fake = Faker() + user = User.objects.create_user(username=fake.user_name(), password=fake.password()) + client = APIClient() + client.force_authenticate(user=user) + return client diff --git a/test/test_file_validator/test_file_validator_endpoints.py b/test/test_file_validator/test_file_validator_endpoints.py index 3193a62..ac78502 100644 --- a/test/test_file_validator/test_file_validator_endpoints.py +++ b/test/test_file_validator/test_file_validator_endpoints.py @@ -8,6 +8,15 @@ from rest_framework.test import APIClient UPLOAD_FILE_URL = reverse("upload-file") +@pytest.mark.django_db +def test_csv_upload_unauthenticated(sample_input_file: SimpleUploadedFile) -> None: + """Test that a valid CSV upload succeeds.""" + api_client = APIClient() + response = api_client.post(UPLOAD_FILE_URL, {"file": sample_input_file}, format="multipart") + + assert response.status_code == 403 + assert response.json()["detail"] == "Authentication credentials were not provided." + @pytest.mark.django_db def test_csv_upload_valid( api_client: APIClient, sample_input_file: SimpleUploadedFile, mock_meo_database: MagicMock -- GitLab