def test_updates_status_pending(self): existing_asset_row = AssetRow( id=None, uploaded_status=UploadedStatus.COMPLETE.value, bucket=DEFAULT_BUCKET, object_key='abc', create_date=datetime.now()) with self.connection.cursor() as cur: existing_asset_row = AssetDao.insert_one(existing_asset_row, cur) with run_server(): response = self.request('put', '/status', data=json.dumps({ 'asset_id': existing_asset_row.id, 'uploaded_status': UploadedStatus.PENDING.value })) self.assertEqual(response.status_code, 200) response_body = json.loads(response.content) with self.connection.cursor() as cur: updated_asset_row = AssetDao.get_by_id(existing_asset_row.id, cur) self.assertEqual(updated_asset_row.uploaded_status, UploadedStatus.PENDING.value) self.assertEqual(response_body, { 'success': True, 'uploaded_status': UploadedStatus.PENDING.value, })
def test_not_found(self): """ Given: The request asset_id does not exist in the database Then: `None` is returned """ self.mock_cursor.fetchone.return_value = None result = AssetDao.get_by_id(1, self.mock_cursor) self.mock_cursor.execute.assert_called_once_with( 'select id,uploaded_status,bucket,object_key,create_date ' 'from asset where id = %s', (1,) ) self.assertIsNone(result)
def test_happy_path(self): """ Given: The `asset_id` exists in the database Then: A dataclass with its row's data is returned """ self.mock_cursor.fetchone.return_value = self.sample_asset_row_tuple result = AssetDao.get_by_id(1, self.mock_cursor) self.mock_cursor.execute.assert_called_once_with( 'select id,uploaded_status,bucket,object_key,create_date ' 'from asset where id = %s', (1,) ) self.assertEqual(result, self.sample_asset_row_dataclass)
def change_asset_upload_status(request, cursor): """ Updates a requested asset to have a new status. :param request: a dict with keys `asset_id` and `uploaded_status`, denoting the asset's id and the new upload status. """ _check_valid_change_upload_status_request(request) asset = AssetDao.get_by_id(request['asset_id'], cursor) if not asset: raise AssetNotFoundException( f'Asset with id {request["asset_id"]} not found') AssetDao.update_uploaded_status(request['asset_id'], request['uploaded_status'], cursor) return { 'success': True, 'uploaded_status': request['uploaded_status'], }
def initiate_access(access_request, cursor): """ Creates a signed URL for a get_object operation. The signed URL can only be created for the asset if its uploaded_status is `completed`. :param access_request: a dict with keys `asset_id` and `expires_in`, denoting the asset's name and the amount of time, in seconds, that the signed URL should last. """ _check_valid_access_request(access_request) asset_id = int(access_request['asset_id']) expiration = access_request.get('expires_in') asset = AssetDao.get_by_id(asset_id, cursor) if not asset: raise AssetNotFoundException(f'Asset with id {asset_id} not found') if asset.uploaded_status != UploadedStatus.COMPLETE.value: raise AccessInvalidArgsException('Asset upload is not yet completed.') if expiration: return { 'url': S3Service.create_signed_url( S3ClientMethod.GET_OBJECT, asset.object_key, bucket_name=asset.bucket, expiration=int(expiration), ), 'asset_id': asset.id, } return { 'url': S3Service.create_signed_url( S3ClientMethod.GET_OBJECT, asset.object_key, bucket_name=asset.bucket, ), 'asset_id': asset.id, }