Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • geant-swd/sage-validation
1 result
Show changes
......@@ -2,6 +2,14 @@
from sage_validation.settings import * # noqa: F403
DATABASES = {
"default": DATABASES["default"], # noqa: F405
}
if os.getenv("USE_SQLITE", "false").lower() == "true": # noqa: F405
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "sage_validation",
}
}
else:
DATABASES = {
"default": DATABASES["default"],
}
......@@ -5,9 +5,20 @@ from django.core.files.uploadedfile import SimpleUploadedFile
from django.urls.base import reverse
from rest_framework.test import APIClient
from sage_validation.accounts.models import UserActivityLog
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
......@@ -40,13 +51,40 @@ def test_csv_export_with_data(api_client: APIClient) -> None:
# Simulate session data
session = api_client.session
session["validated_csv"] = [
{"AccountNumber": "12345", "TransactionDate": "01/03/2024", "NominalAnalysisNominalAccountNumber/1": "N100"}
{"AccountNumber": "12345", "TransactionDate": "01/03/2024", "TransactionReference": "BK1234"},
]
session["input_file_hash"] = "123456"
session.save()
response = api_client.get(url)
assert UserActivityLog.objects.count() == 1
assert response.status_code == 200
assert response["Content-Disposition"] == "attachment; filename=Validated_BK1234.csv"
assert b"AccountNumber,TransactionDate,TransactionReference" in response.content
assert b"12345,01/03/2024,BK1234" in response.content
@pytest.mark.django_db
def test_activity_log_creation_on_csv_export(api_client: APIClient) -> None:
"""Test that a UserActivityLog is created when exporting a CSV file."""
url = reverse("export-file")
# Simulate session data
session = api_client.session
session["validated_csv"] = [
{"AccountNumber": "12345", "TransactionDate": "01/03/2024", "TransactionReference": "BK1234"},
]
session["input_file_hash"] = "123456"
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
log = UserActivityLog.objects.first()
assert log.action == "download"
assert log.input_file_hash == "123456"
assert log.output_file_hash == UserActivityLog.generate_file_hash(session["validated_csv"])
assert log.name == "Validated_BK1234.csv"
......@@ -6,6 +6,10 @@ deps =
mypy
ruff
-r requirements.txt
setenv =
USE_SQLITE = true
commands =
ruff check .
mypy .
......