2020-05-24 07:22:14 +00:00
|
|
|
import json
|
2020-05-24 05:53:48 +00:00
|
|
|
from tests.base_test import BaseTest
|
|
|
|
|
2020-06-02 22:17:00 +00:00
|
|
|
from crc import session
|
2020-05-26 16:21:36 +00:00
|
|
|
from crc.models.approval import ApprovalModel, ApprovalSchema, ApprovalStatus
|
2020-06-02 22:17:00 +00:00
|
|
|
from crc.models.protocol_builder import ProtocolBuilderStatus
|
|
|
|
from crc.models.study import StudyModel
|
2020-05-24 07:22:14 +00:00
|
|
|
|
2020-05-24 05:53:48 +00:00
|
|
|
|
|
|
|
class TestApprovals(BaseTest):
|
|
|
|
def setUp(self):
|
|
|
|
"""Initial setup shared by all TestApprovals tests"""
|
|
|
|
self.load_example_data()
|
2020-05-24 07:22:14 +00:00
|
|
|
self.study = self.create_study()
|
2020-05-24 05:53:48 +00:00
|
|
|
self.workflow = self.create_workflow('random_fact')
|
2020-06-02 22:17:00 +00:00
|
|
|
self.unrelated_study = StudyModel(title="second study",
|
|
|
|
protocol_builder_status=ProtocolBuilderStatus.ACTIVE,
|
|
|
|
user_uid="dhf8r", primary_investigator_id="dhf8r")
|
|
|
|
self.unrelated_workflow = self.create_workflow('random_fact', study=self.unrelated_study)
|
|
|
|
|
2020-05-24 07:22:14 +00:00
|
|
|
# TODO: Move to base_test as a helper
|
|
|
|
self.approval = ApprovalModel(
|
|
|
|
study=self.study,
|
|
|
|
workflow=self.workflow,
|
2020-06-02 22:17:00 +00:00
|
|
|
approver_uid='lb3dp',
|
2020-05-31 19:35:42 +00:00
|
|
|
status=ApprovalStatus.PENDING.value,
|
2020-05-24 07:22:14 +00:00
|
|
|
version=1
|
|
|
|
)
|
|
|
|
session.add(self.approval)
|
2020-05-26 16:21:36 +00:00
|
|
|
|
|
|
|
self.approval_2 = ApprovalModel(
|
|
|
|
study=self.study,
|
|
|
|
workflow=self.workflow,
|
|
|
|
approver_uid='dhf8r',
|
2020-05-31 19:35:42 +00:00
|
|
|
status=ApprovalStatus.PENDING.value,
|
2020-05-26 16:21:36 +00:00
|
|
|
version=1
|
|
|
|
)
|
|
|
|
session.add(self.approval_2)
|
|
|
|
|
2020-06-02 22:17:00 +00:00
|
|
|
# A third study, unrelated to the first.
|
|
|
|
self.approval_3 = ApprovalModel(
|
|
|
|
study=self.unrelated_study,
|
|
|
|
workflow=self.unrelated_workflow,
|
|
|
|
approver_uid='lb3dp',
|
|
|
|
status=ApprovalStatus.PENDING.value,
|
|
|
|
version=1
|
|
|
|
)
|
|
|
|
session.add(self.approval_3)
|
|
|
|
|
2020-05-24 07:22:14 +00:00
|
|
|
session.commit()
|
2020-05-24 05:53:48 +00:00
|
|
|
|
|
|
|
def test_list_approvals_per_approver(self):
|
|
|
|
"""Only approvals associated with approver should be returned"""
|
2020-05-26 16:21:36 +00:00
|
|
|
approver_uid = self.approval_2.approver_uid
|
2020-06-02 01:45:09 +00:00
|
|
|
rv = self.app.get(f'/v1.0/approval', headers=self.logged_in_headers())
|
2020-05-24 05:53:48 +00:00
|
|
|
self.assert_success(rv)
|
|
|
|
|
2020-05-26 16:21:36 +00:00
|
|
|
response = json.loads(rv.get_data(as_text=True))
|
|
|
|
|
2020-06-02 22:17:00 +00:00
|
|
|
# Stored approvals are 3
|
2020-05-26 16:21:36 +00:00
|
|
|
approvals_count = ApprovalModel.query.count()
|
2020-06-02 22:17:00 +00:00
|
|
|
self.assertEqual(approvals_count, 3)
|
2020-05-26 16:21:36 +00:00
|
|
|
|
|
|
|
# but Dan's approvals should be only 1
|
|
|
|
self.assertEqual(len(response), 1)
|
|
|
|
|
|
|
|
# Confirm approver UID matches returned payload
|
2020-06-03 00:07:56 +00:00
|
|
|
approval = response[0]
|
|
|
|
self.assertEqual(approval['approver']['uid'], approver_uid)
|
2020-05-26 16:21:36 +00:00
|
|
|
|
2020-05-24 05:53:48 +00:00
|
|
|
def test_list_approvals_per_admin(self):
|
|
|
|
"""All approvals will be returned"""
|
2020-06-02 01:45:09 +00:00
|
|
|
rv = self.app.get('/v1.0/approval?everything=true', headers=self.logged_in_headers())
|
2020-05-24 05:53:48 +00:00
|
|
|
self.assert_success(rv)
|
|
|
|
|
2020-05-26 16:21:36 +00:00
|
|
|
response = json.loads(rv.get_data(as_text=True))
|
|
|
|
|
2020-06-02 22:17:00 +00:00
|
|
|
# Returned approvals should match what's in the db, we should get one approval back
|
|
|
|
# per study (2 studies), and that approval should have one related approval.
|
2020-05-26 16:21:36 +00:00
|
|
|
approvals_count = ApprovalModel.query.count()
|
|
|
|
response_count = len(response)
|
2020-06-02 01:45:09 +00:00
|
|
|
self.assertEqual(2, response_count)
|
|
|
|
|
|
|
|
rv = self.app.get('/v1.0/approval', headers=self.logged_in_headers())
|
|
|
|
self.assert_success(rv)
|
2020-06-02 11:08:29 +00:00
|
|
|
response = json.loads(rv.get_data(as_text=True))
|
2020-06-02 01:45:09 +00:00
|
|
|
response_count = len(response)
|
|
|
|
self.assertEqual(1, response_count)
|
2020-06-02 22:17:00 +00:00
|
|
|
self.assertEqual(1, len(response[0]['related_approvals'])) # this approval has a related approval.
|
2020-06-02 01:45:09 +00:00
|
|
|
|
2020-06-02 11:43:19 +00:00
|
|
|
def test_update_approval_fails_if_not_the_approver(self):
|
2020-06-02 22:17:00 +00:00
|
|
|
approval = session.query(ApprovalModel).filter_by(approver_uid='lb3dp').first()
|
2020-06-02 11:43:19 +00:00
|
|
|
data = {'id': approval.id,
|
|
|
|
"approver_uid": "dhf8r",
|
|
|
|
'message': "Approved. I like the cut of your jib.",
|
|
|
|
'status': ApprovalStatus.APPROVED.value}
|
|
|
|
|
|
|
|
self.assertEqual(approval.status, ApprovalStatus.PENDING.value)
|
|
|
|
|
|
|
|
rv = self.app.put(f'/v1.0/approval/{approval.id}',
|
|
|
|
content_type="application/json",
|
|
|
|
headers=self.logged_in_headers(), # As dhf8r
|
|
|
|
data=json.dumps(data))
|
|
|
|
self.assert_failure(rv)
|
|
|
|
|
|
|
|
def test_accept_approval(self):
|
|
|
|
approval = session.query(ApprovalModel).filter_by(approver_uid='dhf8r').first()
|
|
|
|
data = {'id': approval.id,
|
|
|
|
"approver_uid": "dhf8r",
|
|
|
|
'message': "Approved. I like the cut of your jib.",
|
|
|
|
'status': ApprovalStatus.APPROVED.value}
|
2020-06-02 01:45:09 +00:00
|
|
|
|
2020-06-02 11:43:19 +00:00
|
|
|
self.assertEqual(approval.status, ApprovalStatus.PENDING.value)
|
2020-06-02 01:45:09 +00:00
|
|
|
|
2020-06-02 11:43:19 +00:00
|
|
|
rv = self.app.put(f'/v1.0/approval/{approval.id}',
|
|
|
|
content_type="application/json",
|
|
|
|
headers=self.logged_in_headers(), # As dhf8r
|
|
|
|
data=json.dumps(data))
|
|
|
|
self.assert_success(rv)
|
|
|
|
|
|
|
|
session.refresh(approval)
|
|
|
|
|
|
|
|
# Updated record should now have the data sent to the endpoint
|
|
|
|
self.assertEqual(approval.message, data['message'])
|
|
|
|
self.assertEqual(approval.status, ApprovalStatus.APPROVED.value)
|
2020-05-26 16:21:36 +00:00
|
|
|
|
2020-06-02 11:43:19 +00:00
|
|
|
def test_decline_approval(self):
|
|
|
|
approval = session.query(ApprovalModel).filter_by(approver_uid='dhf8r').first()
|
|
|
|
data = {'id': approval.id,
|
|
|
|
"approver_uid": "dhf8r",
|
|
|
|
'message': "Approved. I find the cut of your jib lacking.",
|
|
|
|
'status': ApprovalStatus.DECLINED.value}
|
2020-05-27 18:06:32 +00:00
|
|
|
|
2020-06-02 11:43:19 +00:00
|
|
|
self.assertEqual(approval.status, ApprovalStatus.PENDING.value)
|
2020-05-27 18:06:32 +00:00
|
|
|
|
2020-06-02 11:43:19 +00:00
|
|
|
rv = self.app.put(f'/v1.0/approval/{approval.id}',
|
2020-05-24 07:22:14 +00:00
|
|
|
content_type="application/json",
|
2020-06-02 11:43:19 +00:00
|
|
|
headers=self.logged_in_headers(), # As dhf8r
|
2020-05-27 18:06:32 +00:00
|
|
|
data=json.dumps(data))
|
2020-05-24 05:53:48 +00:00
|
|
|
self.assert_success(rv)
|
2020-05-27 18:06:32 +00:00
|
|
|
|
2020-06-02 11:43:19 +00:00
|
|
|
session.refresh(approval)
|
2020-05-27 18:06:32 +00:00
|
|
|
|
|
|
|
# Updated record should now have the data sent to the endpoint
|
2020-06-02 11:43:19 +00:00
|
|
|
self.assertEqual(approval.message, data['message'])
|
|
|
|
self.assertEqual(approval.status, ApprovalStatus.DECLINED.value)
|