From 1b9743a4d1061da316a7096b236df4580110acc2 Mon Sep 17 00:00:00 2001 From: Dan Funk Date: Mon, 27 Apr 2020 15:10:09 -0400 Subject: [PATCH] Assure that if a form has an enumeration it errors out if that enumeration is empty. --- crc/services/workflow_processor.py | 7 ++++++- tests/test_workflow_processor.py | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/crc/services/workflow_processor.py b/crc/services/workflow_processor.py index 23dd572a..c2987154 100644 --- a/crc/services/workflow_processor.py +++ b/crc/services/workflow_processor.py @@ -281,7 +281,12 @@ class WorkflowProcessor(object): form_data = {} for field in task.task_spec.form.fields: if field.type == "enum": - form_data[field.id] = random.choice(field.options) + if len(field.options) > 0: + form_data[field.id] = random.choice(field.options) + else: + raise ApiError.from_task("invalid_enum", "You specified an enumeration field (%s)," + " with no options" % field.id, + task) elif field.type == "long": form_data[field.id] = random.randint(1, 1000) elif field.type == 'boolean': diff --git a/tests/test_workflow_processor.py b/tests/test_workflow_processor.py index fb03fc59..9973b6dd 100644 --- a/tests/test_workflow_processor.py +++ b/tests/test_workflow_processor.py @@ -4,7 +4,10 @@ import string import random from unittest.mock import patch +from SpiffWorkflow import Task as SpiffTask from SpiffWorkflow.bpmn.specs.EndEvent import EndEvent +from SpiffWorkflow.camunda.specs.UserTask import Form, FormField +from SpiffWorkflow.specs import TaskSpec from crc import session, db, app from crc.api.common import ApiError @@ -378,3 +381,21 @@ class TestWorkflowProcessor(BaseTest): self.assertTrue("sponsor_funding_source" in data) self.assertEqual("disabled", data["sponsor_funding_source"]) + def test_enum_with_no_choices_raises_api_error(self): + self.load_example_data() + workflow_spec_model = self.load_test_spec("random_fact") + study = session.query(StudyModel).first() + processor = self.get_processor(study, workflow_spec_model) + processor.do_engine_steps() + tasks = processor.next_user_tasks() + task = tasks[0] + + + field = FormField() + field.id = "test_enum_field" + field.type = "enum" + field.options = [] + task.task_spec.form.fields.append(field) + + with self.assertRaises(ApiError): + processor.populate_form_with_random_data(task) \ No newline at end of file