<commit_before>import pytest
import json

from rest_framework import status

from grandchallenge.subdomains.utils import reverse
from tests.cases_tests.factories import ImageFactoryWithImageFile
from .helpers import (
    create_upload_image_test_data,
    create_upload_image_invalid_test_data,
    read_json_file,
    get_response_status,
    get_auth_token_header,
)


@pytest.mark.django_db
@pytest.mark.parametrize("valid", [True, False])
@pytest.mark.parametrize(
    "user,status_valid,status_invalid",
    [
        (
            "anonymous",
            status.HTTP_401_UNAUTHORIZED,
            status.HTTP_401_UNAUTHORIZED,
        ),
        ("normal", status.HTTP_403_FORBIDDEN, status.HTTP_403_FORBIDDEN),
        ("staff", status.HTTP_403_FORBIDDEN, status.HTTP_403_FORBIDDEN),
        ("import_user", status.HTTP_201_CREATED, status.HTTP_400_BAD_REQUEST),
    ],
)
@pytest.mark.parametrize(
    "endpoint_type,reverse_name,annotation_data",
    [
        ("upload_image_rsbmes", "retina:importers:upload-image", False),
        ("upload_image_kappa", "retina:importers:upload-image", False),
        ("upload_image_areds", "retina:importers:upload-image", False),
    ],
)
class TestCustomUploadEndpoints:
    def test_view(
        self,
        client,
        endpoint_type,
        reverse_name,
        annotation_data,
        user,
        status_valid,
        status_invalid,
        valid,
    ):
        if "upload_image" in endpoint_type:
            data_type = endpoint_type.lstrip("upload_image_")
            if valid:
                data = create_upload_image_test_data(data_type=data_type)
            else:
                data = create_upload_image_invalid_test_data(
                    data_type=data_type
                )
        else:
            valid_str = "valid" if valid else "invalid"
            data = read_json_file(f"{endpoint_type}_{valid_str}_data.json")

        response_status = get_response_status(
            client, reverse_name, data, user, annotation_data
        )
        if valid:
            assert response_status == status_valid
        else:
            assert response_status == status_invalid


@pytest.mark.django_db
@pytest.mark.parametrize(
    "user,expected_status, access",
    [
        ("anonymous", status.HTTP_401_UNAUTHORIZED, False),
        ("normal", status.HTTP_403_FORBIDDEN, False),
        ("staff", status.HTTP_403_FORBIDDEN, False),
        ("import_user", status.HTTP_200_OK, True),
    ],
)
class TestCheckImageEndpoint:
    def test_non_existing_image(self, client, user, expected_status, access):
        auth_header = get_auth_token_header(user)
        url = reverse("retina:importers:check-image")

        data = json.dumps(create_upload_image_test_data(with_image=False))

        response = client.post(
            url, data=data, content_type="application/json", **auth_header<commit_msg>Remove unused argument<commit_after>import pytest
import json

from rest_framework import status

from grandchallenge.subdomains.utils import reverse
from tests.cases_tests.factories import ImageFactoryWithImageFile
from .helpers import (
    create_upload_image_test_data,
    create_upload_image_invalid_test_data,
    read_json_file,
    get_response_status,
    get_auth_token_header,
)


@pytest.mark.django_db
@pytest.mark.parametrize("valid", [True, False])
@pytest.mark.parametrize(
    "user,status_valid,status_invalid",
    [
        (
            "anonymous",
            status.HTTP_401_UNAUTHORIZED,
            status.HTTP_401_UNAUTHORIZED,
        ),
        ("normal", status.HTTP_403_FORBIDDEN, status.HTTP_403_FORBIDDEN),
        ("staff", status.HTTP_403_FORBIDDEN, status.HTTP_403_FORBIDDEN),
        ("import_user", status.HTTP_201_CREATED, status.HTTP_400_BAD_REQUEST),
    ],
)
@pytest.mark.parametrize(
    "endpoint_type,reverse_name",
    [
        ("upload_image_rsbmes", "retina:importers:upload-image"),
        ("upload_image_kappa", "retina:importers:upload-image"),
        ("upload_image_areds", "retina:importers:upload-image"),
    ],
)
class TestCustomUploadEndpoints:
    def test_view(
        self,
        client,
        endpoint_type,
        reverse_name,
        user,
        status_valid,
        status_invalid,
        valid,
    ):
        if "upload_image" in endpoint_type:
            data_type = endpoint_type.lstrip("upload_image_")
            if valid:
                data = create_upload_image_test_data(data_type=data_type)
            else:
                data = create_upload_image_invalid_test_data(
                    data_type=data_type
                )
        else:
            valid_str = "valid" if valid else "invalid"
            data = read_json_file(f"{endpoint_type}_{valid_str}_data.json")

        response_status = get_response_status(client, reverse_name, data, user)
        if valid:
            assert response_status == status_valid
        else:
            assert response_status == status_invalid


@pytest.mark.django_db
@pytest.mark.parametrize(
    "user,expected_status, access",
    [
        ("anonymous", status.HTTP_401_UNAUTHORIZED, False),
        ("normal", status.HTTP_403_FORBIDDEN, False),
        ("staff", status.HTTP_403_FORBIDDEN, False),
        ("import_user", status.HTTP_200_OK, True),
    ],
)
class TestCheckImageEndpoint:
    def test_non_existing_image(self, client, user, expected_status, access):
        auth_header = get_auth_token_header(user)
        url = reverse("retina:importers:check-image")

        data = json.dumps(create_upload_image_test_data(with_image=False))

        response = client.post(
            url, data=data, content_type="application/json", **auth_header
{'fraction_non_alphanumeric': 0.09592410400562193, 'fraction_numerical': 0.012649332396345749, 'mean_word_length': 2.9154057771664372, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2003677', 'n_tokens_mistral': 1841, 'n_tokens_neox': 1768, 'n_words': 277}