Skip to content

Commit

Permalink
Fix Hanging Protocols json field raised exception (#3727)
Browse files Browse the repository at this point in the history
closes #3724
  • Loading branch information
ammar257ammar authored Dec 12, 2024
1 parent 069a690 commit cc9642f
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 5 deletions.
18 changes: 13 additions & 5 deletions app/grandchallenge/hanging_protocols/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,21 @@ def __init__(self, *args, **kwargs):
)

def clean_json(self):
json = self.cleaned_data["json"]
viewport_names = [x["viewport_name"] for x in json]
hanging_protocol_json = self.cleaned_data["json"]

try:
viewport_names = [
viewport["viewport_name"] for viewport in hanging_protocol_json
]
except (KeyError, TypeError):
raise ValidationError(
"Hanging protocol definition is invalid. Have a look at the example in the helptext."
)

self._validate_viewport_uniqueness(viewport_names=viewport_names)
self._validate_dimensions(value=json)
self._validate_dimensions(value=hanging_protocol_json)

for viewport in json:
for viewport in hanging_protocol_json:
if "parent_id" in viewport:
self._validate_parent_id(
viewport=viewport, viewport_names=viewport_names
Expand All @@ -74,7 +82,7 @@ def clean_json(self):
viewport=viewport, viewport_names=viewport_names
)

return json
return hanging_protocol_json

def _validate_viewport_uniqueness(self, *, viewport_names):
if len(set(viewport_names)) != len(viewport_names):
Expand Down
82 changes: 82 additions & 0 deletions app/tests/hanging_protocols_tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,88 @@ def test_hanging_protocol_clientside():
assert form.is_valid()


@pytest.mark.django_db
@pytest.mark.parametrize(
"hanging_protocol_json, form_is_valid, expected_json_error",
(
("[]", False, "This field is required."),
("{}", False, "This field is required."),
(
'[{"viewport_name": "main"}]',
True,
None,
),
(
12345,
False,
"Hanging protocol definition is invalid. Have a look at the example in the helptext.",
),
("main", False, "Enter a valid JSON."),
(
"[1,2,3,4,5]",
False,
"Hanging protocol definition is invalid. Have a look at the example in the helptext.",
),
(
'["test1", "test2", "test3"]',
False,
"Hanging protocol definition is invalid. Have a look at the example in the helptext.",
),
(
"[[],[],[]]",
False,
"Hanging protocol definition is invalid. Have a look at the example in the helptext.",
),
(
"[{},{},{}]",
False,
"Hanging protocol definition is invalid. Have a look at the example in the helptext.",
),
(
"true",
False,
"Hanging protocol definition is invalid. Have a look at the example in the helptext.",
),
(
"false",
False,
"Hanging protocol definition is invalid. Have a look at the example in the helptext.",
),
(
'{"viewport_name": "main"}',
False,
"Hanging protocol definition is invalid. Have a look at the example in the helptext.",
),
(
"[{}]",
False,
"Hanging protocol definition is invalid. Have a look at the example in the helptext.",
),
(
'[{"test":1}]',
False,
"Hanging protocol definition is invalid. Have a look at the example in the helptext.",
),
(
'[{"test1":"main"},{"test2":"secondary"}]',
False,
"Hanging protocol definition is invalid. Have a look at the example in the helptext.",
),
),
)
def test_hanging_protocol_form_json_validation(
hanging_protocol_json, form_is_valid, expected_json_error
):
form = HangingProtocolForm(
{
"title": "main",
"json": hanging_protocol_json,
}
)
assert form.is_valid() is form_is_valid
assert form.errors.get("json", [None])[0] == expected_json_error


def make_ci_list(
*,
number_of_images,
Expand Down

0 comments on commit cc9642f

Please sign in to comment.