def finalize_metadata(publisher, package): """ DPR metadata finalize operation. This API is responsible for getting data from S3 and push it to RDS. --- tags: - package parameters: - in: path name: publisher type: string required: true description: publisher name - in: path name: package type: string required: true description: package name responses: 200: description: Data transfer complete 400: description: JWT is invalid 401: description: Invalid Header for JWT 403: description: User name and publisher not matched 404: description: User not found 500: description: Internal Server Error """ try: user = _request_ctx_stack.top.current_user user_id = user['user'] user = User.query.filter_by(id=user_id).first() if user is not None: if user.name == publisher: bit_store = BitStore(publisher, package) body = bit_store.get_metadata_body() if body is not None: readme = bit_store.get_s3_object( bit_store.get_readme_object_key()) MetaDataDB.create_or_update(name=package, publisher_name=publisher, descriptor=body, readme=readme) return jsonify({"status": "OK"}), 200 raise Exception("Failed to get data from s3") return handle_error('NOT_PERMITTED', 'user name and publisher not matched', 403) return handle_error('USER_NOT_FOUND', 'user not found', 404) except Exception as e: app.logger.error(e) return handle_error('GENERIC_ERROR', e.message, 500)
def test_update_fields_if_instance_present(self): metadata = MetaDataDB.query.join(Publisher)\ .filter(Publisher.name == self.publisher_one, MetaDataDB.name == self.package_one).one() self.assertEqual(json.loads(metadata.descriptor)['name'], "test_one") MetaDataDB.create_or_update(self.package_one, self.publisher_one, descriptor=json.dumps(dict(name='sub')), private=True) metadata = MetaDataDB.query.join(Publisher) \ .filter(Publisher.name == self.publisher_one, MetaDataDB.name == self.package_one).one() self.assertEqual(json.loads(metadata.descriptor)['name'], "sub") self.assertEqual(metadata.private, True)
def test_insert_if_not_present(self): pub = self.publisher_two name = "custom_name" metadata = MetaDataDB.query.join(Publisher) \ .filter(Publisher.name == pub, MetaDataDB.name == name).all() self.assertEqual(len(metadata), 0) MetaDataDB.create_or_update(name, pub, descriptor=json.dumps(dict(name='sub')), private=True) metadata = MetaDataDB.query.join(Publisher) \ .filter(Publisher.name == pub, MetaDataDB.name == name).all() self.assertEqual(len(metadata), 1)