Assure that if a form has an enumeration it errors out if that enumeration is empty.

This commit is contained in:
Dan Funk 2020-04-27 15:10:09 -04:00
parent 8ed520c6f1
commit 1b9743a4d1
2 changed files with 27 additions and 1 deletions

View File

@ -281,7 +281,12 @@ class WorkflowProcessor(object):
form_data = {}
for field in task.task_spec.form.fields:
if field.type == "enum":
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':

View File

@ -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)