2019-12-18 19:02:17 +00:00
|
|
|
# Set environment variable to testing before loading.
|
|
|
|
# IMPORTANT - Environment must be loaded before app, models, etc....
|
|
|
|
import os
|
2020-05-25 16:29:05 +00:00
|
|
|
|
2020-05-22 18:37:49 +00:00
|
|
|
os.environ["TESTING"] = "true"
|
|
|
|
|
|
|
|
import json
|
2020-01-24 14:35:14 +00:00
|
|
|
import unittest
|
2020-02-27 15:30:16 +00:00
|
|
|
import urllib.parse
|
Created a "StudyService" and moved all complex logic around study manipulation out of the study api, and this service, as things were getting complicated. The Workflow Processor no longer creates the WorkflowModel, the study object handles that, and only passes the model into the workflow processor when it is ready to start the workflow.
Created a Study object (seperate from the StudyModel) that can cronstructed on request, and contains a different data structure than we store in the DB. This allows us to return underlying Categories and Workflows in a clean way.
Added a new status to workflows called "not_started", meaning we have not yet instantiated a processor or created a BPMN, they have no version yet and no stored data, just the possiblity of being started.
The Top Level Workflow or "Master" workflow is now a part of the sample data, and loaded at all times.
Removed the ability to "add a workflow to a study" and "remove a workflow from a study", a study contains all possible workflows by definition.
Example data no longer creates users or studies, it just creates the specs.
2020-03-30 12:00:16 +00:00
|
|
|
import datetime
|
2020-06-12 17:46:10 +00:00
|
|
|
from flask import g
|
|
|
|
from sqlalchemy import Sequence
|
2020-02-27 15:30:16 +00:00
|
|
|
|
2020-06-12 17:46:10 +00:00
|
|
|
from crc import app, db, session
|
|
|
|
from crc.models.api_models import WorkflowApiSchema, MultiInstanceType
|
|
|
|
from crc.models.file import FileModel, FileDataModel, CONTENT_TYPES
|
2020-07-15 02:16:44 +00:00
|
|
|
from crc.models.task_event import TaskEventModel
|
2020-07-31 03:03:11 +00:00
|
|
|
from crc.models.study import StudyModel, StudyStatus
|
2020-11-10 14:33:54 +00:00
|
|
|
from crc.models.data_store import DataStoreModel
|
2020-06-12 17:46:10 +00:00
|
|
|
from crc.models.user import UserModel
|
2020-07-30 02:47:47 +00:00
|
|
|
from crc.models.workflow import WorkflowSpecModel, WorkflowSpecCategoryModel
|
2020-03-05 18:25:28 +00:00
|
|
|
from crc.services.file_service import FileService
|
Created a "StudyService" and moved all complex logic around study manipulation out of the study api, and this service, as things were getting complicated. The Workflow Processor no longer creates the WorkflowModel, the study object handles that, and only passes the model into the workflow processor when it is ready to start the workflow.
Created a Study object (seperate from the StudyModel) that can cronstructed on request, and contains a different data structure than we store in the DB. This allows us to return underlying Categories and Workflows in a clean way.
Added a new status to workflows called "not_started", meaning we have not yet instantiated a processor or created a BPMN, they have no version yet and no stored data, just the possiblity of being started.
The Top Level Workflow or "Master" workflow is now a part of the sample data, and loaded at all times.
Removed the ability to "add a workflow to a study" and "remove a workflow from a study", a study contains all possible workflows by definition.
Example data no longer creates users or studies, it just creates the specs.
2020-03-30 12:00:16 +00:00
|
|
|
from crc.services.study_service import StudyService
|
2020-07-30 02:47:47 +00:00
|
|
|
from crc.services.user_service import UserService
|
2020-06-12 17:46:10 +00:00
|
|
|
from crc.services.workflow_service import WorkflowService
|
2019-12-30 18:03:57 +00:00
|
|
|
from example_data import ExampleDataLoader
|
2019-12-18 19:02:17 +00:00
|
|
|
|
2020-07-30 02:47:47 +00:00
|
|
|
# UNCOMMENT THIS FOR DEBUGGING SQL ALCHEMY QUERIES
|
2020-05-27 18:36:10 +00:00
|
|
|
import logging
|
2020-07-30 02:47:47 +00:00
|
|
|
|
2020-05-27 18:36:10 +00:00
|
|
|
logging.basicConfig()
|
2019-12-27 18:50:03 +00:00
|
|
|
|
|
|
|
|
2020-01-24 14:35:14 +00:00
|
|
|
class BaseTest(unittest.TestCase):
|
2020-02-27 15:30:16 +00:00
|
|
|
""" Great class to inherit from, as it sets up and tears down classes
|
|
|
|
efficiently when we have a database in place.
|
|
|
|
"""
|
2019-11-21 14:22:42 +00:00
|
|
|
|
2020-05-22 18:37:49 +00:00
|
|
|
if not app.config['TESTING']:
|
2020-05-22 22:25:00 +00:00
|
|
|
raise (Exception("INVALID TEST CONFIGURATION. This is almost always in import order issue."
|
2020-07-30 02:47:47 +00:00
|
|
|
"The first class to import in each test should be the base_test.py file."))
|
2020-05-22 18:37:49 +00:00
|
|
|
|
2019-11-21 14:22:42 +00:00
|
|
|
auths = {}
|
2020-02-27 15:30:16 +00:00
|
|
|
test_uid = "dhf8r"
|
2019-11-21 14:22:42 +00:00
|
|
|
|
Created a "StudyService" and moved all complex logic around study manipulation out of the study api, and this service, as things were getting complicated. The Workflow Processor no longer creates the WorkflowModel, the study object handles that, and only passes the model into the workflow processor when it is ready to start the workflow.
Created a Study object (seperate from the StudyModel) that can cronstructed on request, and contains a different data structure than we store in the DB. This allows us to return underlying Categories and Workflows in a clean way.
Added a new status to workflows called "not_started", meaning we have not yet instantiated a processor or created a BPMN, they have no version yet and no stored data, just the possiblity of being started.
The Top Level Workflow or "Master" workflow is now a part of the sample data, and loaded at all times.
Removed the ability to "add a workflow to a study" and "remove a workflow from a study", a study contains all possible workflows by definition.
Example data no longer creates users or studies, it just creates the specs.
2020-03-30 12:00:16 +00:00
|
|
|
users = [
|
|
|
|
{
|
2020-07-30 02:47:47 +00:00
|
|
|
'uid': 'dhf8r',
|
|
|
|
'email_address': 'dhf8r@virginia.EDU',
|
|
|
|
'display_name': 'Daniel Harold Funk',
|
|
|
|
'affiliation': 'staff@virginia.edu;member@virginia.edu',
|
|
|
|
'eppn': 'dhf8r@virginia.edu',
|
|
|
|
'first_name': 'Daniel',
|
|
|
|
'last_name': 'Funk',
|
|
|
|
'title': 'SOFTWARE ENGINEER V'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'uid': 'lbd3p',
|
|
|
|
'email_address': 'lbd3p@virginia.EDU',
|
|
|
|
'display_name': 'Laura Barnes',
|
|
|
|
'affiliation': 'staff@virginia.edu;member@virginia.edu',
|
|
|
|
'eppn': 'lbd3p@virginia.edu',
|
|
|
|
'first_name': 'Laura',
|
|
|
|
'last_name': 'Barnes',
|
|
|
|
'title': 'Associate Professor of Systems and Information Engineering'
|
|
|
|
},
|
Created a "StudyService" and moved all complex logic around study manipulation out of the study api, and this service, as things were getting complicated. The Workflow Processor no longer creates the WorkflowModel, the study object handles that, and only passes the model into the workflow processor when it is ready to start the workflow.
Created a Study object (seperate from the StudyModel) that can cronstructed on request, and contains a different data structure than we store in the DB. This allows us to return underlying Categories and Workflows in a clean way.
Added a new status to workflows called "not_started", meaning we have not yet instantiated a processor or created a BPMN, they have no version yet and no stored data, just the possiblity of being started.
The Top Level Workflow or "Master" workflow is now a part of the sample data, and loaded at all times.
Removed the ability to "add a workflow to a study" and "remove a workflow from a study", a study contains all possible workflows by definition.
Example data no longer creates users or studies, it just creates the specs.
2020-03-30 12:00:16 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
studies = [
|
|
|
|
{
|
2021-03-13 01:41:07 +00:00
|
|
|
'id': 0,
|
|
|
|
'title': 'The impact of fried pickles on beer consumption in bipedal software developers.',
|
|
|
|
'last_updated': datetime.datetime.now(),
|
|
|
|
'status': StudyStatus.in_progress,
|
|
|
|
'primary_investigator_id': 'dhf8r',
|
|
|
|
'sponsor': 'Sartography Pharmaceuticals',
|
|
|
|
'ind_number': '1234',
|
|
|
|
'user_uid': 'dhf8r'
|
Created a "StudyService" and moved all complex logic around study manipulation out of the study api, and this service, as things were getting complicated. The Workflow Processor no longer creates the WorkflowModel, the study object handles that, and only passes the model into the workflow processor when it is ready to start the workflow.
Created a Study object (seperate from the StudyModel) that can cronstructed on request, and contains a different data structure than we store in the DB. This allows us to return underlying Categories and Workflows in a clean way.
Added a new status to workflows called "not_started", meaning we have not yet instantiated a processor or created a BPMN, they have no version yet and no stored data, just the possiblity of being started.
The Top Level Workflow or "Master" workflow is now a part of the sample data, and loaded at all times.
Removed the ability to "add a workflow to a study" and "remove a workflow from a study", a study contains all possible workflows by definition.
Example data no longer creates users or studies, it just creates the specs.
2020-03-30 12:00:16 +00:00
|
|
|
},
|
|
|
|
{
|
2021-03-13 01:41:07 +00:00
|
|
|
'id': 1,
|
|
|
|
'title': 'Requirement of hippocampal neurogenesis for the behavioral effects of soft pretzels',
|
|
|
|
'last_updated': datetime.datetime.now(),
|
|
|
|
'status': StudyStatus.in_progress,
|
|
|
|
'primary_investigator_id': 'dhf8r',
|
|
|
|
'sponsor': 'Makerspace & Co.',
|
|
|
|
'ind_number': '5678',
|
|
|
|
'user_uid': 'dhf8r'
|
Created a "StudyService" and moved all complex logic around study manipulation out of the study api, and this service, as things were getting complicated. The Workflow Processor no longer creates the WorkflowModel, the study object handles that, and only passes the model into the workflow processor when it is ready to start the workflow.
Created a Study object (seperate from the StudyModel) that can cronstructed on request, and contains a different data structure than we store in the DB. This allows us to return underlying Categories and Workflows in a clean way.
Added a new status to workflows called "not_started", meaning we have not yet instantiated a processor or created a BPMN, they have no version yet and no stored data, just the possiblity of being started.
The Top Level Workflow or "Master" workflow is now a part of the sample data, and loaded at all times.
Removed the ability to "add a workflow to a study" and "remove a workflow from a study", a study contains all possible workflows by definition.
Example data no longer creates users or studies, it just creates the specs.
2020-03-30 12:00:16 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2019-11-21 14:22:42 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
app.config.from_object('config.testing')
|
|
|
|
cls.ctx = app.test_request_context()
|
|
|
|
cls.app = app.test_client()
|
2020-05-19 20:11:43 +00:00
|
|
|
cls.ctx.push()
|
2019-12-18 19:02:17 +00:00
|
|
|
db.create_all()
|
2019-11-21 14:22:42 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2020-05-19 20:11:43 +00:00
|
|
|
cls.ctx.pop()
|
2020-05-23 19:08:17 +00:00
|
|
|
db.drop_all()
|
2019-11-21 14:22:42 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def setUp(self):
|
2020-05-19 20:11:43 +00:00
|
|
|
pass
|
2019-11-21 14:22:42 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2020-05-19 20:11:43 +00:00
|
|
|
ExampleDataLoader.clean_db()
|
2020-07-30 02:47:47 +00:00
|
|
|
self.logout()
|
2019-11-21 14:22:42 +00:00
|
|
|
self.auths = {}
|
|
|
|
|
2020-02-27 15:30:16 +00:00
|
|
|
def logged_in_headers(self, user=None, redirect_url='http://some/frontend/url'):
|
|
|
|
if user is None:
|
|
|
|
uid = self.test_uid
|
2020-05-26 19:47:41 +00:00
|
|
|
user_info = {'uid': self.test_uid}
|
2020-02-27 15:30:16 +00:00
|
|
|
else:
|
|
|
|
uid = user.uid
|
2020-05-26 19:47:41 +00:00
|
|
|
user_info = {'uid': user.uid}
|
2020-02-27 15:30:16 +00:00
|
|
|
|
|
|
|
query_string = self.user_info_to_query_string(user_info, redirect_url)
|
2020-05-31 20:49:39 +00:00
|
|
|
rv = self.app.get("/v1.0/login%s" % query_string, follow_redirects=False)
|
2020-02-27 15:30:16 +00:00
|
|
|
self.assertTrue(rv.status_code == 302)
|
|
|
|
self.assertTrue(str.startswith(rv.location, redirect_url))
|
|
|
|
|
|
|
|
user_model = session.query(UserModel).filter_by(uid=uid).first()
|
|
|
|
self.assertIsNotNone(user_model.display_name)
|
2020-06-12 17:46:10 +00:00
|
|
|
self.assertEqual(user_model.uid, uid)
|
2020-06-12 18:09:08 +00:00
|
|
|
self.assertTrue('user' in g, 'User should be in Flask globals')
|
2020-07-27 18:38:57 +00:00
|
|
|
user = UserService.current_user(allow_admin_impersonate=True)
|
|
|
|
self.assertEqual(uid, user.uid, 'Logged in user should match given user uid')
|
2020-06-12 17:46:10 +00:00
|
|
|
|
2020-02-27 15:30:16 +00:00
|
|
|
return dict(Authorization='Bearer ' + user_model.encode_auth_token().decode())
|
|
|
|
|
2021-01-12 15:18:35 +00:00
|
|
|
def delete_example_data(self, use_crc_data=False, use_rrt_data=False):
|
|
|
|
"""
|
|
|
|
delete everything that matters in the local database - this is used to
|
|
|
|
test ground zero copy of workflow specs.
|
|
|
|
"""
|
|
|
|
session.execute("delete from workflow; delete from file_data; delete from file; delete from workflow_spec;")
|
|
|
|
session.commit()
|
|
|
|
|
2020-05-30 22:43:20 +00:00
|
|
|
def load_example_data(self, use_crc_data=False, use_rrt_data=False):
|
2020-05-25 16:29:05 +00:00
|
|
|
"""use_crc_data will cause this to load the mammoth collection of documents
|
2020-05-30 22:43:20 +00:00
|
|
|
we built up developing crc, use_rrt_data will do the same for hte rrt project,
|
|
|
|
otherwise it depends on a small setup for running tests."""
|
2019-12-18 19:02:17 +00:00
|
|
|
from example_data import ExampleDataLoader
|
2019-12-30 18:15:39 +00:00
|
|
|
ExampleDataLoader.clean_db()
|
2020-05-30 22:43:20 +00:00
|
|
|
if use_crc_data:
|
2020-05-25 16:29:05 +00:00
|
|
|
ExampleDataLoader().load_all()
|
2020-05-30 22:43:20 +00:00
|
|
|
elif use_rrt_data:
|
|
|
|
ExampleDataLoader().load_rrt()
|
2020-05-25 16:29:05 +00:00
|
|
|
else:
|
|
|
|
ExampleDataLoader().load_test_data()
|
2019-12-18 19:02:17 +00:00
|
|
|
|
2020-07-30 02:47:47 +00:00
|
|
|
# If in production mode, only add the first user.
|
|
|
|
if app.config['PRODUCTION']:
|
2020-07-30 16:40:53 +00:00
|
|
|
session.add(UserModel(**self.users[0]))
|
2020-07-30 02:47:47 +00:00
|
|
|
else:
|
|
|
|
for user_json in self.users:
|
2020-07-30 16:40:53 +00:00
|
|
|
session.add(UserModel(**user_json))
|
2020-07-30 02:47:47 +00:00
|
|
|
|
2020-07-30 16:40:53 +00:00
|
|
|
session.commit()
|
Created a "StudyService" and moved all complex logic around study manipulation out of the study api, and this service, as things were getting complicated. The Workflow Processor no longer creates the WorkflowModel, the study object handles that, and only passes the model into the workflow processor when it is ready to start the workflow.
Created a Study object (seperate from the StudyModel) that can cronstructed on request, and contains a different data structure than we store in the DB. This allows us to return underlying Categories and Workflows in a clean way.
Added a new status to workflows called "not_started", meaning we have not yet instantiated a processor or created a BPMN, they have no version yet and no stored data, just the possiblity of being started.
The Top Level Workflow or "Master" workflow is now a part of the sample data, and loaded at all times.
Removed the ability to "add a workflow to a study" and "remove a workflow from a study", a study contains all possible workflows by definition.
Example data no longer creates users or studies, it just creates the specs.
2020-03-30 12:00:16 +00:00
|
|
|
for study_json in self.studies:
|
|
|
|
study_model = StudyModel(**study_json)
|
2020-07-30 16:40:53 +00:00
|
|
|
session.add(study_model)
|
Created a "StudyService" and moved all complex logic around study manipulation out of the study api, and this service, as things were getting complicated. The Workflow Processor no longer creates the WorkflowModel, the study object handles that, and only passes the model into the workflow processor when it is ready to start the workflow.
Created a Study object (seperate from the StudyModel) that can cronstructed on request, and contains a different data structure than we store in the DB. This allows us to return underlying Categories and Workflows in a clean way.
Added a new status to workflows called "not_started", meaning we have not yet instantiated a processor or created a BPMN, they have no version yet and no stored data, just the possiblity of being started.
The Top Level Workflow or "Master" workflow is now a part of the sample data, and loaded at all times.
Removed the ability to "add a workflow to a study" and "remove a workflow from a study", a study contains all possible workflows by definition.
Example data no longer creates users or studies, it just creates the specs.
2020-03-30 12:00:16 +00:00
|
|
|
StudyService._add_all_workflow_specs_to_study(study_model)
|
2021-04-02 21:10:05 +00:00
|
|
|
session.commit()
|
|
|
|
update_seq = f"ALTER SEQUENCE %s RESTART WITH %s" % (StudyModel.__tablename__ + '_id_seq', study_model.id + 1)
|
|
|
|
print("Update Sequence." + update_seq)
|
|
|
|
session.execute(update_seq)
|
2020-07-30 16:40:53 +00:00
|
|
|
session.flush()
|
Created a "StudyService" and moved all complex logic around study manipulation out of the study api, and this service, as things were getting complicated. The Workflow Processor no longer creates the WorkflowModel, the study object handles that, and only passes the model into the workflow processor when it is ready to start the workflow.
Created a Study object (seperate from the StudyModel) that can cronstructed on request, and contains a different data structure than we store in the DB. This allows us to return underlying Categories and Workflows in a clean way.
Added a new status to workflows called "not_started", meaning we have not yet instantiated a processor or created a BPMN, they have no version yet and no stored data, just the possiblity of being started.
The Top Level Workflow or "Master" workflow is now a part of the sample data, and loaded at all times.
Removed the ability to "add a workflow to a study" and "remove a workflow from a study", a study contains all possible workflows by definition.
Example data no longer creates users or studies, it just creates the specs.
2020-03-30 12:00:16 +00:00
|
|
|
|
2020-01-24 14:35:14 +00:00
|
|
|
specs = session.query(WorkflowSpecModel).all()
|
|
|
|
self.assertIsNotNone(specs)
|
|
|
|
|
Created a "StudyService" and moved all complex logic around study manipulation out of the study api, and this service, as things were getting complicated. The Workflow Processor no longer creates the WorkflowModel, the study object handles that, and only passes the model into the workflow processor when it is ready to start the workflow.
Created a Study object (seperate from the StudyModel) that can cronstructed on request, and contains a different data structure than we store in the DB. This allows us to return underlying Categories and Workflows in a clean way.
Added a new status to workflows called "not_started", meaning we have not yet instantiated a processor or created a BPMN, they have no version yet and no stored data, just the possiblity of being started.
The Top Level Workflow or "Master" workflow is now a part of the sample data, and loaded at all times.
Removed the ability to "add a workflow to a study" and "remove a workflow from a study", a study contains all possible workflows by definition.
Example data no longer creates users or studies, it just creates the specs.
2020-03-30 12:00:16 +00:00
|
|
|
for spec in specs:
|
|
|
|
files = session.query(FileModel).filter_by(workflow_spec_id=spec.id).all()
|
|
|
|
self.assertIsNotNone(files)
|
|
|
|
self.assertGreater(len(files), 0)
|
2020-01-24 14:35:14 +00:00
|
|
|
for file in files:
|
|
|
|
file_data = session.query(FileDataModel).filter_by(file_model_id=file.id).all()
|
|
|
|
self.assertIsNotNone(file_data)
|
|
|
|
self.assertGreater(len(file_data), 0)
|
|
|
|
|
2020-02-27 15:30:16 +00:00
|
|
|
@staticmethod
|
2020-07-21 19:18:08 +00:00
|
|
|
def load_test_spec(dir_name, display_name=None, master_spec=False, category_id=None):
|
2020-02-04 21:49:28 +00:00
|
|
|
"""Loads a spec into the database based on a directory in /tests/data"""
|
2020-07-21 19:18:08 +00:00
|
|
|
if category_id is None:
|
|
|
|
category = WorkflowSpecCategoryModel(name="test", display_name="Test Workflows", display_order=0)
|
2020-07-30 16:40:53 +00:00
|
|
|
session.add(category)
|
|
|
|
session.commit()
|
2020-07-21 19:18:08 +00:00
|
|
|
category_id = category.id
|
2020-06-12 17:46:10 +00:00
|
|
|
|
2020-02-11 16:11:21 +00:00
|
|
|
if session.query(WorkflowSpecModel).filter_by(id=dir_name).count() > 0:
|
2020-05-23 19:08:17 +00:00
|
|
|
return session.query(WorkflowSpecModel).filter_by(id=dir_name).first()
|
2020-02-04 21:49:28 +00:00
|
|
|
filepath = os.path.join(app.root_path, '..', 'tests', 'data', dir_name, "*")
|
2020-07-21 19:18:08 +00:00
|
|
|
if display_name is None:
|
|
|
|
display_name = dir_name
|
Created a "StudyService" and moved all complex logic around study manipulation out of the study api, and this service, as things were getting complicated. The Workflow Processor no longer creates the WorkflowModel, the study object handles that, and only passes the model into the workflow processor when it is ready to start the workflow.
Created a Study object (seperate from the StudyModel) that can cronstructed on request, and contains a different data structure than we store in the DB. This allows us to return underlying Categories and Workflows in a clean way.
Added a new status to workflows called "not_started", meaning we have not yet instantiated a processor or created a BPMN, they have no version yet and no stored data, just the possiblity of being started.
The Top Level Workflow or "Master" workflow is now a part of the sample data, and loaded at all times.
Removed the ability to "add a workflow to a study" and "remove a workflow from a study", a study contains all possible workflows by definition.
Example data no longer creates users or studies, it just creates the specs.
2020-03-30 12:00:16 +00:00
|
|
|
return ExampleDataLoader().create_spec(id=dir_name, name=dir_name, filepath=filepath, master_spec=master_spec,
|
2020-07-21 19:18:08 +00:00
|
|
|
display_name=display_name, category_id=category_id)
|
2020-02-04 21:49:28 +00:00
|
|
|
|
2020-02-27 15:30:16 +00:00
|
|
|
@staticmethod
|
|
|
|
def protocol_builder_response(file_name):
|
2020-02-20 18:30:04 +00:00
|
|
|
filepath = os.path.join(app.root_path, '..', 'tests', 'data', 'pb_responses', file_name)
|
|
|
|
with open(filepath, 'r') as myfile:
|
|
|
|
data = myfile.read()
|
|
|
|
return data
|
|
|
|
|
2020-12-11 13:34:59 +00:00
|
|
|
@staticmethod
|
|
|
|
def workflow_sync_response(file_name):
|
|
|
|
filepath = os.path.join(app.root_path, '..', 'tests', 'data', 'workflow_sync_responses', file_name)
|
2020-12-14 15:27:40 +00:00
|
|
|
with open(filepath, 'rb') as myfile:
|
2020-12-11 13:34:59 +00:00
|
|
|
data = myfile.read()
|
|
|
|
return data
|
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
def assert_success(self, rv, msg=""):
|
|
|
|
try:
|
|
|
|
data = json.loads(rv.get_data(as_text=True))
|
2020-02-18 21:38:56 +00:00
|
|
|
self.assertTrue(200 <= rv.status_code < 300,
|
2019-12-18 19:02:17 +00:00
|
|
|
"BAD Response: %i. \n %s" %
|
|
|
|
(rv.status_code, json.dumps(data)) + ". " + msg)
|
|
|
|
except:
|
2020-02-18 21:38:56 +00:00
|
|
|
self.assertTrue(200 <= rv.status_code < 300,
|
2019-12-18 19:02:17 +00:00
|
|
|
"BAD Response: %i." % rv.status_code + ". " + msg)
|
2020-02-18 21:38:56 +00:00
|
|
|
|
2020-03-09 19:12:40 +00:00
|
|
|
def assert_failure(self, rv, status_code=0, error_code=""):
|
2020-02-18 21:38:56 +00:00
|
|
|
self.assertFalse(200 <= rv.status_code < 300,
|
|
|
|
"Incorrect Valid Response:" + rv.status + ".")
|
2020-03-09 19:12:40 +00:00
|
|
|
if status_code != 0:
|
|
|
|
self.assertEqual(status_code, rv.status_code)
|
|
|
|
if error_code != "":
|
|
|
|
data = json.loads(rv.get_data(as_text=True))
|
|
|
|
self.assertEqual(error_code, data["code"])
|
2020-02-27 15:30:16 +00:00
|
|
|
|
2020-08-21 17:34:37 +00:00
|
|
|
def assert_dict_contains_subset(self, container, subset):
|
|
|
|
def extract_dict_a_from_b(a, b):
|
|
|
|
return dict([(k, b[k]) for k in a.keys() if k in b.keys()])
|
|
|
|
|
|
|
|
extract = extract_dict_a_from_b(subset, container)
|
|
|
|
self.assertEqual(subset, extract)
|
|
|
|
|
2020-02-27 15:30:16 +00:00
|
|
|
@staticmethod
|
|
|
|
def user_info_to_query_string(user_info, redirect_url):
|
|
|
|
query_string_list = []
|
|
|
|
items = user_info.items()
|
|
|
|
for key, value in items:
|
|
|
|
query_string_list.append('%s=%s' % (key, urllib.parse.quote(value)))
|
|
|
|
|
2020-05-31 20:49:39 +00:00
|
|
|
query_string_list.append('redirect_url=%s' % redirect_url)
|
2020-02-27 15:30:16 +00:00
|
|
|
|
|
|
|
return '?%s' % '&'.join(query_string_list)
|
|
|
|
|
2020-03-05 18:25:28 +00:00
|
|
|
def replace_file(self, name, file_path):
|
|
|
|
"""Replaces a stored file with the given name with the contents of the file at the given path."""
|
|
|
|
file_service = FileService()
|
|
|
|
file = open(file_path, "rb")
|
|
|
|
data = file.read()
|
|
|
|
|
2020-07-30 16:40:53 +00:00
|
|
|
file_model = session.query(FileModel).filter(FileModel.name == name).first()
|
2020-03-05 18:25:28 +00:00
|
|
|
noise, file_extension = os.path.splitext(file_path)
|
|
|
|
content_type = CONTENT_TYPES[file_extension[1:]]
|
|
|
|
file_service.update_file(file_model, data, content_type)
|
2020-03-19 21:13:30 +00:00
|
|
|
|
2020-05-23 19:08:17 +00:00
|
|
|
def create_user(self, uid="dhf8r", email="daniel.h.funk@gmail.com", display_name="Hoopy Frood"):
|
|
|
|
user = session.query(UserModel).filter(UserModel.uid == uid).first()
|
|
|
|
if user is None:
|
|
|
|
user = UserModel(uid=uid, email_address=email, display_name=display_name)
|
2020-07-30 16:40:53 +00:00
|
|
|
session.add(user)
|
|
|
|
session.commit()
|
2020-05-23 19:08:17 +00:00
|
|
|
return user
|
|
|
|
|
2020-07-30 02:47:47 +00:00
|
|
|
def create_study(self, uid="dhf8r", title="Beer consumption in the bipedal software engineer",
|
|
|
|
primary_investigator_id="lb3dp"):
|
2020-06-11 20:39:00 +00:00
|
|
|
study = session.query(StudyModel).filter_by(user_uid=uid).filter_by(title=title).first()
|
2020-05-23 19:08:17 +00:00
|
|
|
if study is None:
|
|
|
|
user = self.create_user(uid=uid)
|
2020-07-31 03:03:11 +00:00
|
|
|
study = StudyModel(title=title, status=StudyStatus.in_progress,
|
2020-06-11 20:39:00 +00:00
|
|
|
user_uid=user.uid, primary_investigator_id=primary_investigator_id)
|
2020-07-30 16:40:53 +00:00
|
|
|
session.add(study)
|
|
|
|
session.commit()
|
2020-05-23 19:08:17 +00:00
|
|
|
return study
|
|
|
|
|
2020-07-06 19:06:50 +00:00
|
|
|
def _create_study_workflow_approvals(self, user_uid, title, primary_investigator_id, approver_uids, statuses,
|
|
|
|
workflow_spec_name="random_fact"):
|
|
|
|
study = self.create_study(uid=user_uid, title=title, primary_investigator_id=primary_investigator_id)
|
|
|
|
workflow = self.create_workflow(workflow_name=workflow_spec_name, study=study)
|
|
|
|
approvals = []
|
|
|
|
|
|
|
|
for i in range(len(approver_uids)):
|
|
|
|
approvals.append(self.create_approval(
|
|
|
|
study=study,
|
|
|
|
workflow=workflow,
|
|
|
|
approver_uid=approver_uids[i],
|
|
|
|
status=statuses[i],
|
|
|
|
version=1
|
|
|
|
))
|
|
|
|
|
|
|
|
full_study = {
|
|
|
|
'study': study,
|
|
|
|
'workflow': workflow,
|
|
|
|
'approvals': approvals,
|
|
|
|
}
|
|
|
|
|
|
|
|
return full_study
|
|
|
|
|
2020-07-21 19:18:08 +00:00
|
|
|
def create_workflow(self, workflow_name, display_name=None, study=None, category_id=None, as_user="dhf8r"):
|
2020-07-30 16:40:53 +00:00
|
|
|
session.flush()
|
|
|
|
spec = session.query(WorkflowSpecModel).filter(WorkflowSpecModel.name == workflow_name).first()
|
2020-05-25 16:29:05 +00:00
|
|
|
if spec is None:
|
2020-07-21 19:18:08 +00:00
|
|
|
if display_name is None:
|
|
|
|
display_name = workflow_name
|
|
|
|
spec = self.load_test_spec(workflow_name, display_name, category_id=category_id)
|
2020-05-23 19:08:17 +00:00
|
|
|
if study is None:
|
2020-07-14 14:29:25 +00:00
|
|
|
study = self.create_study(uid=as_user)
|
Created a "StudyService" and moved all complex logic around study manipulation out of the study api, and this service, as things were getting complicated. The Workflow Processor no longer creates the WorkflowModel, the study object handles that, and only passes the model into the workflow processor when it is ready to start the workflow.
Created a Study object (seperate from the StudyModel) that can cronstructed on request, and contains a different data structure than we store in the DB. This allows us to return underlying Categories and Workflows in a clean way.
Added a new status to workflows called "not_started", meaning we have not yet instantiated a processor or created a BPMN, they have no version yet and no stored data, just the possiblity of being started.
The Top Level Workflow or "Master" workflow is now a part of the sample data, and loaded at all times.
Removed the ability to "add a workflow to a study" and "remove a workflow from a study", a study contains all possible workflows by definition.
Example data no longer creates users or studies, it just creates the specs.
2020-03-30 12:00:16 +00:00
|
|
|
workflow_model = StudyService._create_workflow_model(study, spec)
|
2020-03-30 18:01:57 +00:00
|
|
|
return workflow_model
|
2020-03-19 21:13:30 +00:00
|
|
|
|
|
|
|
def create_reference_document(self):
|
2020-05-07 17:57:24 +00:00
|
|
|
file_path = os.path.join(app.root_path, 'static', 'reference', 'irb_documents.xlsx')
|
2020-03-19 21:13:30 +00:00
|
|
|
file = open(file_path, "rb")
|
2020-05-07 17:57:24 +00:00
|
|
|
FileService.add_reference_file(FileService.DOCUMENT_LIST,
|
2020-03-19 21:13:30 +00:00
|
|
|
binary_data=file.read(),
|
|
|
|
content_type=CONTENT_TYPES['xls'])
|
|
|
|
file.close()
|
2020-06-11 20:39:00 +00:00
|
|
|
|
|
|
|
def create_approval(
|
2020-07-30 02:47:47 +00:00
|
|
|
self,
|
|
|
|
study=None,
|
|
|
|
workflow=None,
|
|
|
|
approver_uid=None,
|
|
|
|
status=None,
|
|
|
|
version=None,
|
2020-06-11 20:39:00 +00:00
|
|
|
):
|
|
|
|
study = study or self.create_study()
|
|
|
|
workflow = workflow or self.create_workflow()
|
|
|
|
approver_uid = approver_uid or self.test_uid
|
|
|
|
status = status or ApprovalStatus.PENDING.value
|
|
|
|
version = version or 1
|
2020-07-30 02:47:47 +00:00
|
|
|
approval = ApprovalModel(study=study, workflow=workflow, approver_uid=approver_uid, status=status,
|
|
|
|
version=version)
|
2020-07-30 16:40:53 +00:00
|
|
|
session.add(approval)
|
|
|
|
session.commit()
|
2020-06-11 20:39:00 +00:00
|
|
|
return approval
|
|
|
|
|
2021-01-19 20:27:44 +00:00
|
|
|
def get_workflow_common(self, url, user):
|
2021-01-14 20:32:14 +00:00
|
|
|
rv = self.app.get(url,
|
2020-06-12 17:46:10 +00:00
|
|
|
headers=self.logged_in_headers(user),
|
|
|
|
content_type="application/json")
|
|
|
|
self.assert_success(rv)
|
|
|
|
json_data = json.loads(rv.get_data(as_text=True))
|
|
|
|
workflow_api = WorkflowApiSchema().load(json_data)
|
2021-01-19 20:27:44 +00:00
|
|
|
return workflow_api
|
|
|
|
|
|
|
|
def get_workflow_api(self, workflow, do_engine_steps=True, user_uid="dhf8r"):
|
|
|
|
user = session.query(UserModel).filter_by(uid=user_uid).first()
|
|
|
|
self.assertIsNotNone(user)
|
2021-03-13 01:41:07 +00:00
|
|
|
url = (f'/v1.0/workflow/{workflow.id}'
|
2021-01-19 20:27:44 +00:00
|
|
|
f'?do_engine_steps={str(do_engine_steps)}')
|
|
|
|
workflow_api = self.get_workflow_common(url, user)
|
|
|
|
self.assertEqual(workflow.workflow_spec_id, workflow_api.workflow_spec_id)
|
|
|
|
return workflow_api
|
|
|
|
|
|
|
|
def restart_workflow_api(self, workflow, clear_data=False, user_uid="dhf8r"):
|
|
|
|
user = session.query(UserModel).filter_by(uid=user_uid).first()
|
|
|
|
self.assertIsNotNone(user)
|
2021-03-13 01:41:07 +00:00
|
|
|
url = (f'/v1.0/workflow/{workflow.id}/restart'
|
2021-01-19 20:27:44 +00:00
|
|
|
f'?clear_data={str(clear_data)}')
|
|
|
|
workflow_api = self.get_workflow_common(url, user)
|
2020-06-12 17:46:10 +00:00
|
|
|
self.assertEqual(workflow.workflow_spec_id, workflow_api.workflow_spec_id)
|
|
|
|
return workflow_api
|
|
|
|
|
2021-03-13 01:41:07 +00:00
|
|
|
def complete_form(self, workflow_in, task_in, dict_data, update_all=False, error_code=None, terminate_loop=None,
|
|
|
|
user_uid="dhf8r"):
|
2021-04-26 12:50:46 +00:00
|
|
|
# workflow_in should be a workflow, not a workflow_api
|
|
|
|
# we were passing in workflow_api in many of our tests, and
|
|
|
|
# this caused problems testing standalone workflows
|
|
|
|
standalone = getattr(workflow_in.workflow_spec, 'standalone', False)
|
2020-06-12 17:46:10 +00:00
|
|
|
prev_completed_task_count = workflow_in.completed_tasks
|
|
|
|
if isinstance(task_in, dict):
|
|
|
|
task_id = task_in["id"]
|
|
|
|
else:
|
|
|
|
task_id = task_in.id
|
|
|
|
|
|
|
|
user = session.query(UserModel).filter_by(uid=user_uid).first()
|
|
|
|
self.assertIsNotNone(user)
|
2021-03-13 01:41:07 +00:00
|
|
|
args = ""
|
2020-06-17 15:35:06 +00:00
|
|
|
if terminate_loop:
|
2021-03-13 01:41:07 +00:00
|
|
|
args += "?terminate_loop=true"
|
|
|
|
if update_all:
|
|
|
|
args += "?update_all=true"
|
|
|
|
|
|
|
|
rv = self.app.put('/v1.0/workflow/%i/task/%s/data%s' % (workflow_in.id, task_id, args),
|
|
|
|
headers=self.logged_in_headers(user=user),
|
|
|
|
content_type="application/json",
|
|
|
|
data=json.dumps(dict_data))
|
2020-06-12 17:46:10 +00:00
|
|
|
if error_code:
|
|
|
|
self.assert_failure(rv, error_code=error_code)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.assert_success(rv)
|
|
|
|
json_data = json.loads(rv.get_data(as_text=True))
|
|
|
|
|
2020-07-15 02:16:44 +00:00
|
|
|
# Assure task events are updated on the model
|
2020-06-12 17:46:10 +00:00
|
|
|
workflow = WorkflowApiSchema().load(json_data)
|
|
|
|
# The total number of tasks may change over time, as users move through gateways
|
|
|
|
# branches may be pruned. As we hit parallel Multi-Instance new tasks may be created...
|
|
|
|
self.assertIsNotNone(workflow.total_tasks)
|
2020-06-17 15:35:06 +00:00
|
|
|
# presumably, we also need to deal with sequential items here too . .
|
2021-03-13 01:41:07 +00:00
|
|
|
if not task_in.multi_instance_type == 'looping' and not update_all:
|
2020-06-17 15:35:06 +00:00
|
|
|
self.assertEqual(prev_completed_task_count + 1, workflow.completed_tasks)
|
2020-06-12 17:46:10 +00:00
|
|
|
|
|
|
|
# Assure a record exists in the Task Events
|
|
|
|
task_events = session.query(TaskEventModel) \
|
|
|
|
.filter_by(workflow_id=workflow.id) \
|
|
|
|
.filter_by(task_id=task_id) \
|
2020-07-15 02:16:44 +00:00
|
|
|
.filter_by(action=WorkflowService.TASK_ACTION_COMPLETE) \
|
2020-06-12 17:46:10 +00:00
|
|
|
.order_by(TaskEventModel.date.desc()).all()
|
|
|
|
self.assertGreater(len(task_events), 0)
|
|
|
|
event = task_events[0]
|
2021-04-26 12:50:46 +00:00
|
|
|
if not standalone:
|
|
|
|
self.assertIsNotNone(event.study_id)
|
2020-06-12 17:46:10 +00:00
|
|
|
self.assertEqual(user_uid, event.user_uid)
|
|
|
|
self.assertEqual(workflow.id, event.workflow_id)
|
|
|
|
self.assertEqual(workflow.workflow_spec_id, event.workflow_spec_id)
|
|
|
|
self.assertEqual(workflow.spec_version, event.spec_version)
|
|
|
|
self.assertEqual(WorkflowService.TASK_ACTION_COMPLETE, event.action)
|
|
|
|
self.assertEqual(task_in.id, task_id)
|
|
|
|
self.assertEqual(task_in.name, event.task_name)
|
|
|
|
self.assertEqual(task_in.title, event.task_title)
|
|
|
|
self.assertEqual(task_in.type, event.task_type)
|
2020-06-17 15:35:06 +00:00
|
|
|
if not task_in.multi_instance_type == 'looping':
|
|
|
|
self.assertEqual("COMPLETED", event.task_state)
|
2020-06-12 17:46:10 +00:00
|
|
|
|
|
|
|
# Not sure what voodoo is happening inside of marshmallow to get me in this state.
|
|
|
|
if isinstance(task_in.multi_instance_type, MultiInstanceType):
|
|
|
|
self.assertEqual(task_in.multi_instance_type.value, event.mi_type)
|
|
|
|
else:
|
|
|
|
self.assertEqual(task_in.multi_instance_type, event.mi_type)
|
|
|
|
|
|
|
|
self.assertEqual(task_in.multi_instance_count, event.mi_count)
|
2020-06-17 15:35:06 +00:00
|
|
|
if task_in.multi_instance_type == 'looping' and not terminate_loop:
|
2020-07-30 02:47:47 +00:00
|
|
|
self.assertEqual(task_in.multi_instance_index + 1, event.mi_index)
|
2020-06-17 15:35:06 +00:00
|
|
|
else:
|
|
|
|
self.assertEqual(task_in.multi_instance_index, event.mi_index)
|
2020-06-12 17:46:10 +00:00
|
|
|
self.assertEqual(task_in.process_name, event.process_name)
|
|
|
|
self.assertIsNotNone(event.date)
|
|
|
|
|
|
|
|
workflow = WorkflowApiSchema().load(json_data)
|
|
|
|
return workflow
|
2020-07-30 02:47:47 +00:00
|
|
|
|
|
|
|
def logout(self):
|
2020-07-30 14:40:06 +00:00
|
|
|
if 'user' in g:
|
|
|
|
del g.user
|
|
|
|
|
|
|
|
if 'impersonate_user' in g:
|
|
|
|
del g.impersonate_user
|