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_result( test_id, allowed_keys=['id', 'cpid', 'created_at', 'duration_seconds', 'meta', 'product_version', 'verification_status'] ) else: test_info = db.get_test_result(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 delete(self, test_id, key): """Delete key from test run metadata.""" test = db.get_test_result(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_result_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_result(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_result_meta_item(test_id, key, pecan.request.body) pecan.response.status = 201
def delete(self, test_id): """Delete test run.""" test = db.get_test_result(test_id) if test['verification_status'] == const.TEST_VERIFIED: pecan.abort(403, 'Can not delete a verified test run.') db.delete_test_result(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_result(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_result_meta_key(test_id, 'target') and db.get_test_result_meta_key(test_id, 'guideline') and db.get_test_result_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_result(test_info) pecan.response.status = 201 return test
def get(self, test_id): """Get test run metadata.""" test_info = db.get_test_result(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_result(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_result_meta_key(test_id, const.USER) return user and user == get_user_id()
def test_get_test_result(self, mock_get_test_result): db.get_test_result(12345) mock_get_test_result.assert_called_once_with(12345, allowed_keys=None)