Skip to content

Commit

Permalink
Add tests for views and form
Browse files Browse the repository at this point in the history
  • Loading branch information
amickan committed May 27, 2024
1 parent 79e4da5 commit 7e3fb33
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
45 changes: 45 additions & 0 deletions app/tests/algorithms_tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from pathlib import Path

import pytest
from actstream.actions import is_following

from grandchallenge.algorithms.forms import (
AlgorithmForm,
AlgorithmModelForm,
AlgorithmPublishForm,
ImageActivateForm,
JobCreateForm,
Expand All @@ -21,15 +24,21 @@
from grandchallenge.core.utils.access_requests import (
AccessRequestHandlingOptions,
)
from grandchallenge.verifications.models import Verification
from tests.algorithms_tests.factories import (
AlgorithmFactory,
AlgorithmImageFactory,
AlgorithmJobFactory,
AlgorithmModelFactory,
AlgorithmPermissionRequestFactory,
)
from tests.algorithms_tests.utils import get_algorithm_creator
from tests.components_tests.factories import ComponentInterfaceFactory
from tests.factories import UserFactory, WorkstationFactory
from tests.uploads_tests.factories import (
UserUploadFactory,
create_upload_from_file,
)
from tests.utils import get_view_for_user
from tests.verification_tests.factories import VerificationFactory

Expand Down Expand Up @@ -521,3 +530,39 @@ def test_image_activate_form():
assert "Image updating already in progress." in str(
form.errors["algorithm_image"]
)


@pytest.mark.django_db
def test_algorithm_model_form():
user = UserFactory()
alg = AlgorithmFactory()
user_upload = UserUploadFactory(creator=user)
user_upload.status = user_upload.StatusChoices.COMPLETED
user_upload.save()

form = AlgorithmModelForm(
user=user,
algorithm=alg,
data={"user_upload": user_upload, "creator": user, "algorithm": alg},
)
assert not form.is_valid()
assert "This upload is not a valid .tar.gz file" in str(form.errors)
assert "Select a valid choice" in str(form.errors["creator"])

Verification.objects.create(user=user, is_verified=True)
upload = create_upload_from_file(
creator=user,
file_path=Path(__file__).parent / "resources" / "model.tar.gz",
)
AlgorithmModelFactory(creator=user)

form2 = AlgorithmModelForm(
user=user,
algorithm=alg,
data={"user_upload": upload, "creator": user, "algorithm": alg},
)
assert not form2.is_valid()
assert (
"You have an existing model importing, please wait for it to complete"
in str(form2.errors)
)
52 changes: 52 additions & 0 deletions app/tests/algorithms_tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
from grandchallenge.evaluation.tasks import (
create_algorithm_jobs_for_evaluation,
)
from grandchallenge.verifications.models import Verification
from tests.algorithms_tests.factories import (
AlgorithmFactory,
AlgorithmImageFactory,
AlgorithmJobFactory,
AlgorithmModelFactory,
)
from tests.algorithms_tests.utils import TwoAlgorithms
from tests.archives_tests.factories import ArchiveFactory, ArchiveItemFactory
Expand Down Expand Up @@ -534,3 +536,53 @@ def test_job_permissions_for_debug_phase(
)
# No-one should be in the viewers group
assert {*job.viewers.user_set.all()} == set()


@pytest.mark.django_db
def test_algorithm_model_view_permissions(client):
user, editor = UserFactory.create_batch(2)
alg = AlgorithmFactory()
alg.add_editor(editor)

am = AlgorithmModelFactory(algorithm=alg)

response = get_view_for_user(
viewname="algorithms:model-create",
client=client,
reverse_kwargs={"slug": alg.slug},
user=user,
)
assert response.status_code == 403

response = get_view_for_user(
viewname="algorithms:model-detail",
client=client,
reverse_kwargs={"slug": alg.slug, "pk": am.pk},
user=user,
)
assert response.status_code == 403

response = get_view_for_user(
viewname="algorithms:model-create",
client=client,
reverse_kwargs={"slug": alg.slug},
user=editor,
)
assert response.status_code == 403

Verification.objects.create(user=editor, is_verified=True)
response = get_view_for_user(
viewname="algorithms:model-create",
client=client,
reverse_kwargs={"slug": alg.slug},
user=editor,
)
assert response.status_code == 200

response = get_view_for_user(
viewname="algorithms:model-detail",
client=client,
reverse_kwargs={"slug": alg.slug, "pk": am.pk},
user=editor,
)
assert response.status_code == 200

0 comments on commit 7e3fb33

Please sign in to comment.