Skip to content
Snippets Groups Projects
Commit a4f613fe authored by Neda Moeini's avatar Neda Moeini
Browse files

Add more tests for endpoints

parent 144ca6ca
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !2. Comments created here will be created in the context of that merge request.
......@@ -18,7 +18,7 @@ BASE_DIR = Path(__file__).resolve().parent
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv("SECRET_KEY")
SECRET_KEY = os.getenv("SECRET_KEY", "test-secret-key")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
......
......@@ -4,6 +4,7 @@ from unittest.mock import MagicMock
import pytest
from django.core.files.uploadedfile import SimpleUploadedFile
from faker import Faker
from rest_framework.test import APIClient
from sage_validation.file_validator.models import MeoCostCentres, MeoValidSuppliers, XxData
......@@ -139,3 +140,8 @@ def mock_meo_database(mocker: MagicMock)-> None:
nominal_mock.filter.return_value.exists.return_value = True
mocker.patch("sage_validation.file_validator.models.MeoNominal.objects.using", return_value=nominal_mock)
@pytest.fixture
def api_client() -> APIClient:
"""Fixture to return Django API test client."""
return APIClient()
from unittest.mock import MagicMock
import pytest
from django.core.files.uploadedfile import SimpleUploadedFile
from django.urls.base import reverse
from rest_framework.test import APIClient
UPLOAD_FILE_URL = reverse("upload-file")
@pytest.mark.django_db
def test_csv_upload_valid(
api_client: APIClient, sample_input_file: SimpleUploadedFile, mock_meo_database: MagicMock
) -> None:
"""Test that a valid CSV upload succeeds."""
response = api_client.post(UPLOAD_FILE_URL, {"file": sample_input_file}, format="multipart")
assert response.status_code == 200
assert response.json()["status"] == "success"
assert "download_url" in response.json()
@pytest.mark.django_db
def test_csv_upload_invalid_extension(api_client: APIClient) -> None:
"""Test that uploading a non-CSV file fails."""
bad_file = SimpleUploadedFile("test.txt", b"Invalid content", content_type="text/plain")
response = api_client.post(UPLOAD_FILE_URL, {"file": bad_file}, format="multipart")
assert response.status_code == 400
assert response.json()["status"] == "error"
assert "errors" in response.json()
@pytest.mark.django_db
def test_csv_export_with_data(api_client: APIClient) -> None:
"""Test exporting a processed CSV file."""
url = reverse("export-file")
# Simulate session data
session = api_client.session
session["validated_csv"] = [
{"AccountNumber": "12345", "TransactionDate": "01/03/2024", "NominalAnalysisNominalAccountNumber/1": "N100"}
]
session.save()
response = api_client.get(url)
assert response.status_code == 200
assert response["Content-Disposition"] == 'attachment; filename="updated_file.csv"'
assert b"AccountNumber,TransactionDate,NominalAnalysisNominalAccountNumber/1" in response.content
assert b"12345,01/03/2024,N100" in response.content
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment