def get_one(self, test_id): """Handler for getting item.""" user_role = api_utils.get_user_role(test_id) if user_role in (const.ROLE_FOUNDATION, const.ROLE_OWNER): test_info = db.get_test(test_id, allowed_keys=[ 'id', 'cpid', 'created_at', 'duration_seconds', 'meta', 'product_version', 'verification_status' ]) else: test_info = db.get_test(test_id) test_list = db.get_test_results(test_id) test_name_list = [test_dict['name'] for test_dict in test_list] test_info.update({'results': test_name_list, 'user_role': user_role}) if user_role not in (const.ROLE_FOUNDATION, const.ROLE_OWNER): # Don't expose product information if product is not public. if (test_info.get('product_version') and not test_info['product_version']['product_info']['public'] ): test_info['product_version'] = None test_info['meta'] = { k: v for k, v in test_info['meta'].items() if k in MetadataController.rw_access_keys } return test_info
def get_one(self, test_id): """Handler for getting item.""" if api_utils.get_user_role(test_id) == const.ROLE_OWNER: test_info = db.get_test(test_id, allowed_keys=["id", "cpid", "created_at", "duration_seconds", "meta"]) else: test_info = db.get_test(test_id) test_list = db.get_test_results(test_id) test_name_list = [test_dict["name"] for test_dict in test_list] test_info.update({"results": test_name_list, "user_role": api_utils.get_user_role(test_id)}) return test_info
def get_one(self, test_id): """Handler for getting item.""" if api_utils.get_user_role(test_id) == const.ROLE_OWNER: test_info = db.get_test( test_id, allowed_keys=['id', 'cpid', 'created_at', 'duration_seconds', 'meta'] ) else: test_info = db.get_test(test_id) test_list = db.get_test_results(test_id) test_name_list = [test_dict['name'] for test_dict in test_list] test_info.update({'results': test_name_list, 'user_role': api_utils.get_user_role(test_id)}) return test_info
def delete(self, test_id): """Delete test run.""" test = db.get_test(test_id) if test['verification_status'] == const.TEST_VERIFIED: pecan.abort(403, 'Can not delete a verified test run.') db.delete_test(test_id) pecan.response.status = 204
def put(self, test_id, **kw): """Update a test result.""" test_info = {'id': test_id} is_foundation_admin = api_utils.check_user_is_foundation_admin() if 'product_version_id' in kw: test = db.get_test(test_id) if test['verification_status'] == const.TEST_VERIFIED: pecan.abort( 403, 'Can not update product_version_id for a ' 'verified test run.') if kw['product_version_id']: # Verify that the user is a member of the product's vendor. version = db.get_product_version(kw['product_version_id'], allowed_keys=['product_id']) is_vendor_admin = (api_utils.check_user_is_product_admin( version['product_id'])) else: # No product vendor to check membership for, so just set # is_vendor_admin to True. is_vendor_admin = True kw['product_version_id'] = None if not is_vendor_admin and not is_foundation_admin: pecan.abort(403, 'Forbidden.') test_info['product_version_id'] = kw['product_version_id'] if 'verification_status' in kw: if not is_foundation_admin: pecan.abort( 403, 'You do not have permission to change a ' 'verification status.') if kw['verification_status'] not in (0, 1): pecan.abort( 400, 'Invalid verification_status value: %d' % kw['verification_status']) # Check pre-conditions are met to mark a test verified. if (kw['verification_status'] == 1 and not (db.get_test_meta_key(test_id, 'target') and db.get_test_meta_key(test_id, 'guideline') and db.get_test_meta_key(test_id, const.SHARED_TEST_RUN))): pecan.abort( 403, 'In order to mark a test verified, the ' 'test must be shared and have been ' 'associated to a guideline and target ' 'program.') test_info['verification_status'] = kw['verification_status'] test = db.update_test(test_info) pecan.response.status = 201 return test
def delete(self, test_id, key): """Delete key from test run metadata.""" test = db.get_test(test_id) if test['verification_status'] == const.TEST_VERIFIED: pecan.abort( 403, 'Can not delete a metadata key for a ' 'verified test run.') db.delete_test_meta_item(test_id, key) pecan.response.status = 204
def post(self, test_id, key): """Save value for key in test run metadata.""" test = db.get_test(test_id) if test['verification_status'] == const.TEST_VERIFIED: pecan.abort( 403, 'Can not add/alter a new metadata key for a ' 'verified test run.') db.save_test_meta_item(test_id, key, pecan.request.body) pecan.response.status = 201
def get_item(self, item_id): """Handler for getting item""" test_info = db.get_test(item_id) if not test_info: pecan.abort(404) test_list = db.get_test_results(item_id) test_name_list = [test_dict[0] for test_dict in test_list] return {"cpid": test_info.cpid, "created_at": test_info.created_at, "duration_seconds": test_info.duration_seconds, "results": test_name_list}
def get_one(self, test_id): """Handler for getting item.""" if api_utils.get_user_role(test_id) == const.ROLE_OWNER: test_info = db.get_test( test_id, allowed_keys=['id', 'cpid', 'created_at', 'duration_seconds', 'meta'] ) else: test_info = db.get_test(test_id) test_list = db.get_test_results(test_id) test_name_list = [test_dict['name'] for test_dict in test_list] test_info.update({'results': test_name_list, 'user_role': api_utils.get_user_role(test_id)}) cloud_id = test_info['cpid'] cloud = db.get_cloud(cloud_id) if cloud: test_info.update({'cloud_name': cloud['name'], 'cloud_description': cloud['description'], 'cloud_shared': cloud['shared']}) return test_info
def get_item(self, item_id): """Handler for getting item""" test_info = db.get_test(item_id) if not test_info: pecan.abort(404) test_list = db.get_test_results(item_id) test_name_list = [test_dict[0] for test_dict in test_list] return { "cpid": test_info.cpid, "created_at": test_info.created_at, "duration_seconds": test_info.duration_seconds, "results": test_name_list }
def get(self, test_id): """Get test run metadata.""" test_info = db.get_test(test_id) role = api_utils.get_user_role(test_id) if role in (const.ROLE_FOUNDATION, const.ROLE_OWNER): return test_info['meta'] elif role in (const.ROLE_USER): return { k: v for k, v in test_info['meta'].items() if k in self.rw_access_keys } pecan.abort(403)
def check_owner(test_id): """Check that user has access to specified test run as owner.""" if not is_authenticated(): return False test = db.get_test(test_id) # If the test is owned by a product. if test.get('product_version_id'): version = db.get_product_version(test['product_version_id']) return check_user_is_product_admin(version['product_id']) # Otherwise, check the user ownership. else: user = db.get_test_meta_key(test_id, const.USER) return user and user == get_user_id()
def _check_owner(test_id): """Check that user has access to specified test run as owner.""" if not is_authenticated(): return False # Check that test has pubkey attached that equals to user key test_pubkey = db.get_test_meta_key(test_id, const.PUBLIC_KEY) pubkeys = [' '.join((pk['format'], pk['pubkey'])) for pk in get_user_public_keys()] if test_pubkey in pubkeys: return True # Check that test has link to cloud that belongs to user test = db.get_test(test_id) cloud = db.get_cloud(test['cpid']) if cloud and cloud['openid'] == get_user_id(): return True return False
def get(self, test_id): """Get test run metadata.""" test_info = db.get_test(test_id) return test_info['meta']
def test_get_test(self, mock_get_test): db.get_test(12345) mock_get_test.assert_called_once_with(12345)