Skip to content

Commit

Permalink
✨ Apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Oct 26, 2023
1 parent 37f725f commit 7c949b9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/openforms/forms/api/serializers/form_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ...models import FormDefinition, FormStep
from ...validators import validate_no_duplicate_keys_across_steps
from .button_text import ButtonTextSerializer
from ..validators import FormStepIsApplicableIfFirstValidator


class FormStepLiteralsSerializer(serializers.Serializer):
Expand Down Expand Up @@ -54,6 +55,7 @@ class Meta:
"index",
"literals",
"url",
"is_applicable",
)
extra_kwargs = {
"uuid": {
Expand Down Expand Up @@ -140,6 +142,7 @@ class Meta:
"read_only": True,
},
}
validators = [FormStepIsApplicableIfFirstValidator()]

def create(self, validated_data):
validated_data["form"] = self.context["form"]
Expand Down
13 changes: 13 additions & 0 deletions src/openforms/forms/api/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ def __call__(self, attrs: dict, serializer: serializers.Serializer):
)


class FormStepIsApplicableIfFirstValidator:
def __call__(self, attrs: dict):
if not attrs.get("is_applicable", True) and attrs.get("order") == 0:
raise serializers.ValidationError(
{
"is_applicable": serializers.ErrorDetail(
_("First form step must be applicable."),
code="invalid",
),
}
)


def validate_template_expressions(configuration: JSONObject) -> None:
"""
Validate that any template expressions in supported properties are correct.
Expand Down
39 changes: 39 additions & 0 deletions src/openforms/forms/tests/test_api_formsteps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,3 +1092,42 @@ def test_update_with_translations_validate_literals(self, _mock):
for error in expected:
with self.subTest(field=error["name"], code=error["code"]):
self.assertIn(error, invalid_params)


class FormStepsAPIApplicabilityTests(APITestCase):
def setUp(self):
super().setUp()

self.user = UserFactory.create()
self.form = FormFactory.create()
self.form_definition = FormDefinitionFactory.create()
self.client.force_authenticate(user=self.user)

def test_create_form_step_not_applicable_as_first_unsucessful(self):
self.user.user_permissions.add(Permission.objects.get(codename="change_form"))
self.user.is_staff = True
self.user.save()
url = reverse(
"api:form-steps-list", kwargs={"form_uuid_or_slug": self.form.uuid}
)

form_detail_url = reverse(
"api:formdefinition-detail",
kwargs={"uuid": self.form_definition.uuid},
)
data = {
"formDefinition": f"http://testserver{form_detail_url}",
"index": 0,
"isApplicable": False,
}
response = self.client.post(url, data=data)

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
response.json()["invalidParams"][0],
{
"name": "isApplicable",
"code": "invalid",
"reason": "First form step must be applicable.",
},
)
7 changes: 6 additions & 1 deletion src/openforms/forms/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,15 @@ def test_clean(self):
order=0,
is_applicable=True,
)
step_ok_order_1 = FormStepFactory.create(
order=1,
is_applicable=False,
)
with self.subTest("clean raises"):
self.assertRaises(ValidationError, step_raises.clean)
with self.subTest("clean does not raises"):
with self.subTest("clean does not raise"):
step_ok.clean()
step_ok_order_1.clean()


class FormLogicTests(TestCase):
Expand Down
1 change: 0 additions & 1 deletion src/openforms/js/components/admin/form_design/FormStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const FormStep = ({data, onEdit, onComponentMutated, onFieldChange, onReplace})
validationErrors = [],
componentTranslations,
} = data;
console.log(_generatedId);
const previousFormDefinition = usePrevious(formDefinition);
let forceBuilderUpdate = false;
if (previousFormDefinition && previousFormDefinition != formDefinition) {
Expand Down

0 comments on commit 7c949b9

Please sign in to comment.