Skip to content

Commit

Permalink
Complement permission test
Browse files Browse the repository at this point in the history
  • Loading branch information
amickan committed Dec 9, 2024
1 parent 6cd94ed commit a754590
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 58 deletions.
25 changes: 13 additions & 12 deletions app/grandchallenge/algorithms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,19 @@ def clean_algorithm_model(self):


class AlgorithmInterfaceBaseForm(SaveFormInitMixin, ModelForm):
inputs = ModelMultipleChoiceField(
queryset=ComponentInterface.objects.exclude(
slug__in=[*NON_ALGORITHM_INTERFACES, "results-json-file"]
),
widget=Select2MultipleWidget,
)
outputs = ModelMultipleChoiceField(
queryset=ComponentInterface.objects.exclude(
slug__in=[*NON_ALGORITHM_INTERFACES, "results-json-file"]
),
widget=Select2MultipleWidget,
)

class Meta:
model = AlgorithmInterface
fields = ("inputs", "outputs")
Expand Down Expand Up @@ -1383,18 +1396,6 @@ def clean(self):


class AlgorithmInterfaceGetOrCreateForm(AlgorithmInterfaceBaseForm):
inputs = ModelMultipleChoiceField(
queryset=ComponentInterface.objects.exclude(
slug__in=[*NON_ALGORITHM_INTERFACES, "results-json-file"]
),
widget=Select2MultipleWidget,
)
outputs = ModelMultipleChoiceField(
queryset=ComponentInterface.objects.exclude(
slug__in=[*NON_ALGORITHM_INTERFACES, "results-json-file"]
),
widget=Select2MultipleWidget,
)
set_as_default = BooleanField(required=False)

class Meta(AlgorithmInterfaceBaseForm.Meta):
Expand Down
84 changes: 38 additions & 46 deletions app/tests/algorithms_tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2153,52 +2153,45 @@ def test_algorithm_template_download(client):
@pytest.mark.django_db
def test_algorithm_interface_view_permission(client, viewname):
(
user_with_perm,
user_without_perm,
algorithm_editor_with_perm,
verified_alg_editor_with_perm,
) = UserFactory.create_batch(4)
assign_perm("algorithms.add_algorithm", user_with_perm)
assign_perm("algorithms.add_algorithm", algorithm_editor_with_perm)
assign_perm("algorithms.add_algorithm", verified_alg_editor_with_perm)
user_with_alg_add_perm,
user_without_alg_add_perm,
algorithm_editor_with_alg_add,
algorithm_editor_without_alg_add,
verified_alg_editor_with_alg_add,
verified_alg_editor_without_alg_add,
) = UserFactory.create_batch(6)
assign_perm("algorithms.add_algorithm", user_with_alg_add_perm)
assign_perm("algorithms.add_algorithm", algorithm_editor_with_alg_add)
assign_perm("algorithms.add_algorithm", verified_alg_editor_with_alg_add)

alg = AlgorithmFactory()
alg.add_editor(algorithm_editor_with_perm)
alg.add_editor(verified_alg_editor_with_perm)

VerificationFactory(user=verified_alg_editor_with_perm, is_verified=True)

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

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

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

response = get_view_for_user(
viewname=viewname,
client=client,
reverse_kwargs={"slug": alg.slug},
user=verified_alg_editor_with_perm,
)
assert response.status_code == 200
alg.add_editor(algorithm_editor_with_alg_add)
alg.add_editor(algorithm_editor_without_alg_add)
alg.add_editor(verified_alg_editor_with_alg_add)
alg.add_editor(verified_alg_editor_without_alg_add)

VerificationFactory(
user=verified_alg_editor_with_alg_add, is_verified=True
)
VerificationFactory(
user=verified_alg_editor_without_alg_add, is_verified=True
)

for user, status in [
[user_with_alg_add_perm, 403],
[user_without_alg_add_perm, 403],
[algorithm_editor_with_alg_add, 403],
[algorithm_editor_without_alg_add, 403],
[verified_alg_editor_with_alg_add, 200],
[verified_alg_editor_without_alg_add, 403],
]:
response = get_view_for_user(
viewname=viewname,
client=client,
reverse_kwargs={"slug": alg.slug},
user=user,
)
assert response.status_code == status


@pytest.mark.django_db
Expand Down Expand Up @@ -2245,6 +2238,7 @@ def test_algorithm_interfaces_list_queryset(client):
assign_perm("algorithms.add_algorithm", user)
alg, alg2 = AlgorithmFactory.create_batch(2)
alg.add_editor(user)
VerificationFactory(user=user, is_verified=True)

io1, io2, io3, io4 = AlgorithmInterfaceFactory.create_batch(4)

Expand All @@ -2253,8 +2247,6 @@ def test_algorithm_interfaces_list_queryset(client):

iots = AlgorithmAlgorithmInterface.objects.order_by("id").all()

VerificationFactory(user=user, is_verified=True)

response = get_view_for_user(
viewname="algorithms:interface-list",
client=client,
Expand Down

0 comments on commit a754590

Please sign in to comment.