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

Update unit tests to support authentication.

parent b7456861
Branches
Tags
1 merge request!4Feature/activity log
"""All the tests for the file_validator app."""
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
from unittest.mock import MagicMock from unittest.mock import MagicMock
import pytest import pytest
from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile from django.core.files.uploadedfile import SimpleUploadedFile
from faker import Faker from faker import Faker
from rest_framework.test import APIClient from rest_framework.test import APIClient
...@@ -144,4 +145,8 @@ def mock_meo_database(mocker: MagicMock)-> None: ...@@ -144,4 +145,8 @@ def mock_meo_database(mocker: MagicMock)-> None:
@pytest.fixture @pytest.fixture
def api_client() -> APIClient: def api_client() -> APIClient:
"""Fixture to return Django API test client.""" """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
...@@ -8,6 +8,15 @@ from rest_framework.test import APIClient ...@@ -8,6 +8,15 @@ from rest_framework.test import APIClient
UPLOAD_FILE_URL = reverse("upload-file") 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 @pytest.mark.django_db
def test_csv_upload_valid( def test_csv_upload_valid(
api_client: APIClient, sample_input_file: SimpleUploadedFile, mock_meo_database: MagicMock api_client: APIClient, sample_input_file: SimpleUploadedFile, mock_meo_database: MagicMock
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment