def store_item(self, test): """Handler for storing item. Should return new item id.""" test_ = test.copy() if pecan.request.headers.get('X-Public-Key'): key = pecan.request.headers.get('X-Public-Key').strip().split()[1] if 'meta' not in test_: test_['meta'] = {} pubkey = db.get_pubkey(key) if not pubkey: pecan.abort( 400, 'User with specified key not found. ' 'Please log into the RefStack server to ' 'upload your key.') test_['meta'][const.USER] = pubkey.openid if test.get('cpid'): version = db.get_product_version_by_cpid( test['cpid'], allowed_keys=['id', 'product_id']) # Only auto-associate if there is a single product version # with the given cpid. if len(version) == 1: is_foundation = api_utils.check_user_is_foundation_admin( pubkey.openid) is_product_admin = api_utils.check_user_is_product_admin( version[0]['product_id'], pubkey.openid) if is_foundation or is_product_admin: test_['product_version_id'] = version[0]['id'] test_id = db.store_results(test_) return { 'test_id': test_id, 'url': parse.urljoin(CONF.ui_url, CONF.api.test_results_url) % test_id }
def delete(self, id, version_id): """Delete a product version. Endpoint: /v1/products/<product_id>/versions/<version_id> """ if (not api_utils.check_user_is_product_admin(id) and not api_utils.check_user_is_foundation_admin()): pecan.abort(403, 'Forbidden.') try: version = db.get_product_version(version_id, allowed_keys=['version']) if not version['version']: pecan.abort( 400, 'Can not delete the empty version as it is ' 'used for basic product/test association. ' 'This version was implicitly created with ' 'the product, and so it cannot be deleted ' 'explicitly.') db.delete_product_version(version_id) except DBReferenceError: pecan.abort( 400, 'Unable to delete. There are still tests ' 'associated to this product version.') 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 store_item(self, version_info): """Add a new version for the product.""" if (not api_utils.check_user_is_product_admin(self.product_id) and not api_utils.check_user_is_foundation_admin()): pecan.abort(403, 'Forbidden.') creator = api_utils.get_user_id() pecan.response.status = 201 return db.add_product_version(self.product_id, version_info['version'], creator, version_info.get('cpid'))
def delete(self, id): """Delete product.""" if (not api_utils.check_user_is_foundation_admin() and not api_utils.check_user_is_product_admin(id)): pecan.abort(403, 'Forbidden.') try: db.delete_product(id) except DBReferenceError: pecan.abort(400, 'Unable to delete. There are still tests ' 'associated to versions of this product.') pecan.response.status = 204
def _auto_version_associate(self, test, test_, pubkey): if test.get('cpid'): version = db.get_product_version_by_cpid( test['cpid'], allowed_keys=['id', 'product_id']) # Only auto-associate if there is a single product version # with the given cpid. if len(version) == 1: is_foundation = api_utils.check_user_is_foundation_admin( pubkey.openid) is_product_admin = api_utils.check_user_is_product_admin( version[0]['product_id'], pubkey.openid) if is_foundation or is_product_admin: test_['product_version_id'] = version[0]['id'] return test_
def put(self, id, version_id, **kw): """Update details for a specific version. Endpoint: /v1/products/<product_id>/versions/<version_id> """ if (not api_utils.check_user_is_product_admin(id) and not api_utils.check_user_is_foundation_admin()): pecan.abort(403, 'Forbidden.') version_info = {'id': version_id} if 'cpid' in kw: version_info['cpid'] = kw['cpid'] version = db.update_product_version(version_info) pecan.response.status = 200 return version
def delete(self, id, version_id): """Delete a product version. Endpoint: /v1/products/<product_id>/versions/<version_id> """ if (not api_utils.check_user_is_product_admin(id) and not api_utils.check_user_is_foundation_admin()): pecan.abort(403, 'Forbidden.') try: db.delete_product_version(version_id) except DBReferenceError: pecan.abort( 400, 'Unable to delete. There are still tests ' 'associated to this product version.') pecan.response.status = 204