2020-02-04 20:44:06 +00:00
|
|
|
import json
|
2020-03-05 18:25:28 +00:00
|
|
|
import os
|
2020-02-04 20:44:06 +00:00
|
|
|
|
2020-03-05 18:25:28 +00:00
|
|
|
from crc import session, app
|
2020-03-05 16:23:22 +00:00
|
|
|
from crc.models.api_models import WorkflowApiSchema, Task
|
2020-02-11 01:54:22 +00:00
|
|
|
from crc.models.file import FileModelSchema
|
2020-02-10 21:19:23 +00:00
|
|
|
from crc.models.study import StudyModel
|
2020-03-05 16:23:22 +00:00
|
|
|
from crc.models.workflow import WorkflowSpecModelSchema, WorkflowModel, WorkflowStatus
|
2020-03-03 18:50:22 +00:00
|
|
|
from crc.services.workflow_processor import WorkflowProcessor
|
2020-03-11 16:35:37 +00:00
|
|
|
from crc.models.stats import WorkflowStatsModel, TaskEventModel
|
2020-02-04 20:44:06 +00:00
|
|
|
from tests.base_test import BaseTest
|
|
|
|
|
|
|
|
|
|
|
|
class TestTasksApi(BaseTest):
|
|
|
|
|
|
|
|
def create_workflow(self, workflow_name):
|
|
|
|
study = session.query(StudyModel).first()
|
2020-03-03 18:50:22 +00:00
|
|
|
spec = self.load_test_spec(workflow_name)
|
|
|
|
processor = WorkflowProcessor.create(study.id, spec.id)
|
2020-02-28 16:14:30 +00:00
|
|
|
rv = self.app.post(
|
|
|
|
'/v1.0/study/%i/workflows' % study.id,
|
|
|
|
headers=self.logged_in_headers(),
|
|
|
|
content_type="application/json",
|
|
|
|
data=json.dumps(WorkflowSpecModelSchema().dump(spec)))
|
|
|
|
self.assert_success(rv)
|
2020-02-18 15:14:03 +00:00
|
|
|
workflow = session.query(WorkflowModel).filter_by(study_id=study.id, workflow_spec_id=workflow_name).first()
|
2020-02-04 20:44:06 +00:00
|
|
|
return workflow
|
|
|
|
|
2020-03-05 21:45:44 +00:00
|
|
|
def get_workflow_api(self, workflow, soft_reset=False, hard_reset=False):
|
|
|
|
rv = self.app.get('/v1.0/workflow/%i?soft_reset=%s&hard_reset=%s' %
|
|
|
|
(workflow.id, str(soft_reset), str(hard_reset)),
|
|
|
|
content_type="application/json")
|
2020-02-04 20:44:06 +00:00
|
|
|
json_data = json.loads(rv.get_data(as_text=True))
|
2020-02-07 16:34:44 +00:00
|
|
|
workflow_api = WorkflowApiSchema().load(json_data)
|
2020-02-07 17:36:08 +00:00
|
|
|
self.assertEqual(workflow.workflow_spec_id, workflow_api.workflow_spec_id)
|
2020-02-07 16:34:44 +00:00
|
|
|
return workflow_api
|
2020-02-04 20:44:06 +00:00
|
|
|
|
|
|
|
def complete_form(self, workflow, task, dict_data):
|
|
|
|
rv = self.app.put('/v1.0/workflow/%i/task/%s/data' % (workflow.id, task.id),
|
|
|
|
content_type="application/json",
|
|
|
|
data=json.dumps(dict_data))
|
|
|
|
self.assert_success(rv)
|
|
|
|
json_data = json.loads(rv.get_data(as_text=True))
|
2020-03-11 16:35:37 +00:00
|
|
|
|
|
|
|
num_stats = session.query(WorkflowStatsModel)\
|
|
|
|
.filter_by(workflow_id=workflow.id)\
|
|
|
|
.filter_by(workflow_spec_id=workflow.workflow_spec_id)\
|
|
|
|
.count()
|
|
|
|
self.assertGreater(num_stats, 0)
|
|
|
|
|
|
|
|
num_task_events = session.query(TaskEventModel)\
|
|
|
|
.filter_by(workflow_id=workflow.id)\
|
|
|
|
.filter_by(task_id=task.id)\
|
|
|
|
.count()
|
|
|
|
self.assertGreater(num_task_events, 0)
|
|
|
|
|
2020-02-07 16:34:44 +00:00
|
|
|
workflow = WorkflowApiSchema().load(json_data)
|
2020-02-04 20:44:06 +00:00
|
|
|
return workflow
|
|
|
|
|
|
|
|
def test_get_current_user_tasks(self):
|
|
|
|
self.load_example_data()
|
|
|
|
workflow = self.create_workflow('random_fact')
|
2020-02-07 16:34:44 +00:00
|
|
|
tasks = self.get_workflow_api(workflow).user_tasks
|
2020-02-04 20:44:06 +00:00
|
|
|
self.assertEqual("Task_User_Select_Type", tasks[0].name)
|
|
|
|
self.assertEqual(3, len(tasks[0].form["fields"][0]["options"]))
|
2020-02-20 19:35:21 +00:00
|
|
|
self.assertIsNotNone(tasks[0].documentation)
|
2020-02-21 17:02:35 +00:00
|
|
|
expected_docs = """# h1 Heading 8-)
|
|
|
|
## h2 Heading
|
|
|
|
### h3 Heading
|
|
|
|
#### h4 Heading
|
|
|
|
##### h5 Heading
|
|
|
|
###### h6 Heading
|
2020-02-20 19:35:21 +00:00
|
|
|
"""
|
2020-02-21 17:02:35 +00:00
|
|
|
self.assertTrue(str.startswith(tasks[0].documentation, expected_docs))
|
2020-02-04 20:44:06 +00:00
|
|
|
|
|
|
|
def test_two_forms_task(self):
|
|
|
|
# Set up a new workflow
|
|
|
|
self.load_example_data()
|
|
|
|
workflow = self.create_workflow('two_forms')
|
|
|
|
# get the first form in the two form workflow.
|
2020-02-07 16:34:44 +00:00
|
|
|
workflow_api = self.get_workflow_api(workflow)
|
2020-02-07 17:36:08 +00:00
|
|
|
self.assertEqual('two_forms', workflow_api.workflow_spec_id)
|
2020-02-07 16:34:44 +00:00
|
|
|
self.assertEqual(2, len(workflow_api.user_tasks))
|
|
|
|
self.assertIsNotNone(workflow_api.user_tasks[0].form)
|
|
|
|
self.assertEqual("UserTask", workflow_api.next_task['type'])
|
|
|
|
self.assertEqual("StepOne", workflow_api.next_task['name'])
|
|
|
|
self.assertEqual(1, len(workflow_api.next_task['form']['fields']))
|
2020-02-04 20:44:06 +00:00
|
|
|
|
|
|
|
# Complete the form for Step one and post it.
|
2020-02-07 16:34:44 +00:00
|
|
|
self.complete_form(workflow, workflow_api.user_tasks[0], {"color": "blue"})
|
2020-02-04 20:44:06 +00:00
|
|
|
|
|
|
|
# Get the next Task
|
2020-02-07 16:34:44 +00:00
|
|
|
workflow_api = self.get_workflow_api(workflow)
|
|
|
|
self.assertEqual("StepTwo", workflow_api.next_task['name'])
|
2020-02-04 20:44:06 +00:00
|
|
|
|
|
|
|
# Get all user Tasks and check that the data have been saved
|
2020-02-07 16:34:44 +00:00
|
|
|
for task in workflow_api.user_tasks:
|
2020-02-04 20:44:06 +00:00
|
|
|
self.assertIsNotNone(task.data)
|
|
|
|
for val in task.data.values():
|
|
|
|
self.assertIsNotNone(val)
|
|
|
|
|
|
|
|
def test_error_message_on_bad_gateway_expression(self):
|
|
|
|
self.load_example_data()
|
|
|
|
workflow = self.create_workflow('exclusive_gateway')
|
|
|
|
|
|
|
|
# get the first form in the two form workflow.
|
2020-02-07 16:34:44 +00:00
|
|
|
tasks = self.get_workflow_api(workflow).user_tasks
|
2020-02-04 20:44:06 +00:00
|
|
|
self.complete_form(workflow, tasks[0], {"has_bananas": True})
|
|
|
|
|
|
|
|
def test_workflow_with_parallel_forms(self):
|
|
|
|
self.load_example_data()
|
|
|
|
workflow = self.create_workflow('exclusive_gateway')
|
|
|
|
|
|
|
|
# get the first form in the two form workflow.
|
2020-02-07 16:34:44 +00:00
|
|
|
tasks = self.get_workflow_api(workflow).user_tasks
|
2020-02-04 20:44:06 +00:00
|
|
|
self.complete_form(workflow, tasks[0], {"has_bananas": True})
|
|
|
|
|
|
|
|
# Get the next Task
|
2020-02-07 16:34:44 +00:00
|
|
|
workflow_api = self.get_workflow_api(workflow)
|
|
|
|
self.assertEqual("Task_Num_Bananas", workflow_api.next_task['name'])
|
|
|
|
|
|
|
|
def test_get_workflow_contains_details_about_last_task_data(self):
|
|
|
|
self.load_example_data()
|
|
|
|
workflow = self.create_workflow('exclusive_gateway')
|
|
|
|
|
|
|
|
# get the first form in the two form workflow.
|
|
|
|
tasks = self.get_workflow_api(workflow).user_tasks
|
|
|
|
workflow_api = self.complete_form(workflow, tasks[0], {"has_bananas": True})
|
|
|
|
|
|
|
|
self.assertIsNotNone(workflow_api.last_task)
|
2020-02-18 15:14:03 +00:00
|
|
|
self.assertEqual({"has_bananas": True}, workflow_api.last_task['data'])
|
2020-02-07 16:34:44 +00:00
|
|
|
|
|
|
|
def test_get_workflow_contains_reference_to_last_task_and_next_task(self):
|
|
|
|
self.load_example_data()
|
|
|
|
workflow = self.create_workflow('exclusive_gateway')
|
|
|
|
|
|
|
|
# get the first form in the two form workflow.
|
|
|
|
tasks = self.get_workflow_api(workflow).user_tasks
|
|
|
|
self.complete_form(workflow, tasks[0], {"has_bananas": True})
|
|
|
|
|
|
|
|
workflow_api = self.get_workflow_api(workflow)
|
|
|
|
self.assertIsNotNone(workflow_api.last_task)
|
|
|
|
self.assertIsNotNone(workflow_api.next_task)
|
2020-02-11 01:54:22 +00:00
|
|
|
|
|
|
|
def test_document_added_to_workflow_shows_up_in_file_list(self):
|
|
|
|
self.load_example_data()
|
|
|
|
workflow = self.create_workflow('docx')
|
|
|
|
# get the first form in the two form workflow.
|
|
|
|
tasks = self.get_workflow_api(workflow).user_tasks
|
|
|
|
data = {
|
|
|
|
"full_name": "Buck of the Wild",
|
|
|
|
"date": "5/1/2020",
|
|
|
|
"title": "Leader of the Pack",
|
|
|
|
"company": "In the company of wolves",
|
|
|
|
"last_name": "Mr. Wolf"
|
|
|
|
}
|
|
|
|
workflow_api = self.complete_form(workflow, tasks[0], data)
|
2020-02-25 17:01:25 +00:00
|
|
|
self.assertIsNotNone(workflow_api.next_task)
|
|
|
|
self.assertEquals("EndEvent_0evb22x", workflow_api.next_task['name'])
|
2020-02-11 01:54:22 +00:00
|
|
|
self.assertTrue(workflow_api.status == WorkflowStatus.complete)
|
|
|
|
rv = self.app.get('/v1.0/file?workflow_id=%i' % workflow.id)
|
|
|
|
self.assert_success(rv)
|
|
|
|
json_data = json.loads(rv.get_data(as_text=True))
|
|
|
|
files = FileModelSchema(many=True).load(json_data, session=session)
|
2020-02-18 15:14:03 +00:00
|
|
|
self.assertTrue(len(files) == 1)
|
2020-02-25 17:01:25 +00:00
|
|
|
|
|
|
|
def test_documentation_processing_handles_replacements(self):
|
|
|
|
|
|
|
|
docs = "Some simple docs"
|
|
|
|
task = Task(1, "bill", "bill", "", "started", {}, docs, {})
|
|
|
|
task.process_documentation(docs)
|
|
|
|
self.assertEqual(docs, task.documentation)
|
|
|
|
|
|
|
|
task.data = {"replace_me": "new_thing"}
|
|
|
|
task.process_documentation("{{replace_me}}")
|
|
|
|
self.assertEqual("new_thing", task.documentation)
|
|
|
|
|
|
|
|
documentation = """
|
|
|
|
# Bigger Test
|
|
|
|
|
|
|
|
* bullet one
|
|
|
|
* bullet two has {{replace_me}}
|
|
|
|
|
|
|
|
# other stuff.
|
|
|
|
"""
|
|
|
|
expected = """
|
|
|
|
# Bigger Test
|
|
|
|
|
|
|
|
* bullet one
|
|
|
|
* bullet two has new_thing
|
|
|
|
|
|
|
|
# other stuff.
|
|
|
|
"""
|
|
|
|
task.process_documentation(documentation)
|
|
|
|
self.assertEqual(expected, task.documentation)
|
|
|
|
|
2020-02-29 22:22:38 +00:00
|
|
|
def test_documentation_processing_handles_conditionals(self):
|
|
|
|
|
|
|
|
docs = "This test {% if works == 'yes' %}works{% endif %}"
|
|
|
|
task = Task(1, "bill", "bill", "", "started", {}, docs, {})
|
|
|
|
task.process_documentation(docs)
|
|
|
|
self.assertEqual("This test ", task.documentation)
|
|
|
|
|
|
|
|
task.data = {"works": 'yes'}
|
|
|
|
task.process_documentation(docs)
|
|
|
|
self.assertEqual("This test works", task.documentation)
|
|
|
|
|
2020-02-25 17:01:25 +00:00
|
|
|
def test_get_documentation_populated_in_end(self):
|
|
|
|
self.load_example_data()
|
|
|
|
workflow = self.create_workflow('random_fact')
|
|
|
|
workflow_api = self.get_workflow_api(workflow)
|
|
|
|
tasks = workflow_api.user_tasks
|
|
|
|
self.assertEqual("Task_User_Select_Type", tasks[0].name)
|
|
|
|
self.assertEqual(3, len(tasks[0].form["fields"][0]["options"]))
|
|
|
|
self.assertIsNotNone(tasks[0].documentation)
|
|
|
|
self.complete_form(workflow, workflow_api.user_tasks[0], {"type": "norris"})
|
|
|
|
workflow_api = self.get_workflow_api(workflow)
|
|
|
|
self.assertEqual("EndEvent_0u1cgrf", workflow_api.next_task['name'])
|
|
|
|
self.assertIsNotNone(workflow_api.next_task['documentation'])
|
2020-02-28 16:14:30 +00:00
|
|
|
self.assertTrue("norris" in workflow_api.next_task['documentation'])
|
2020-03-03 18:50:22 +00:00
|
|
|
|
2020-03-05 18:25:28 +00:00
|
|
|
def test_load_workflow_from_outdated_spec(self):
|
|
|
|
|
|
|
|
# Start the basic two_forms workflow and complete a task.
|
|
|
|
self.load_example_data()
|
|
|
|
workflow = self.create_workflow('two_forms')
|
|
|
|
workflow_api = self.get_workflow_api(workflow)
|
|
|
|
self.complete_form(workflow, workflow_api.user_tasks[0], {"color": "blue"})
|
2020-03-05 21:45:44 +00:00
|
|
|
self.assertTrue(workflow_api.is_latest_spec)
|
2020-03-05 18:25:28 +00:00
|
|
|
|
2020-03-05 20:35:55 +00:00
|
|
|
# Modify the specification, with a major change that alters the flow and can't be deserialized
|
|
|
|
# effectively, if it uses the latest spec files.
|
2020-03-05 18:25:28 +00:00
|
|
|
file_path = os.path.join(app.root_path, '..', 'tests', 'data', 'two_forms', 'mods', 'two_forms_struc_mod.bpmn')
|
|
|
|
self.replace_file("two_forms.bpmn", file_path)
|
|
|
|
|
2020-03-05 20:35:55 +00:00
|
|
|
workflow_api = self.get_workflow_api(workflow)
|
|
|
|
self.assertTrue(workflow_api.spec_version.startswith("v1 "))
|
2020-03-05 21:45:44 +00:00
|
|
|
self.assertFalse(workflow_api.is_latest_spec)
|
|
|
|
|
|
|
|
workflow_api = self.get_workflow_api(workflow, hard_reset=True)
|
|
|
|
self.assertTrue(workflow_api.spec_version.startswith("v2 "))
|
|
|
|
self.assertTrue(workflow_api.is_latest_spec)
|