"""Adjustment file upload endpoints.""" import os import boto3 from botocore.exceptions import ClientError from abacus_file_upload.tests.integration.conftest import ( has_records_in_table, ows_royalties_api_client, ) def test_file_upload_cancel( clear_db_file_upload, basic_headers, insert_file_upload_fixture ): """POST /file-upload/{file_key}/cancel .""" file_key = 'a58863c8-9f99-407c-85ab-75bd88201e9e' ows_royalties_client = ows_royalties_api_client(basic_headers) # post new config post_body = { 's3_key_template': 'test_fixtures/ows-royalties-integration-tests/{file_key}.{ext}', 'upload_type': 'adjustments', 'description': 'integration tests configuration', } res_post = ows_royalties_client.post_file_upload_config(post_body) assert res_post.status_code == 201 file_upload_config_id = res_post.json()['file_upload_config_id'] insert_file_upload_fixture(file_upload_config_id) res_post = ows_royalties_client.post_file_upload_cancel(file_key) assert res_post.status_code == 200 assert res_post.json()['upload_status'] == 'cancelled' assert res_post.json()['file_key'] == file_key assert file_key in res_post.json()['original_file_name'] assert file_key in res_post.json()['s3_key'] assert res_post.json()['completed_at'] is None def test_file_upload_complete( clear_db_file_upload, basic_headers, insert_file_upload_fixture, upload_xlsx_to_s3 ): """POST /file-upload/{file_key}/complete .""" assert has_records_in_table('abacus_outbox') is False file_key = 'a58863c8-9f99-407c-85ab-75bd88201e9e' ows_royalties_client = ows_royalties_api_client(basic_headers) upload_xlsx_to_s3('{}.xlsx'.format(file_key)) # post new config post_body = { 's3_key_template': 'test_fixtures/ows-royalties-integration-tests/{file_key}.{ext}', 'upload_type': 'adjustments', 'description': 'integration tests configuration', } res_post = ows_royalties_client.post_file_upload_config(post_body) assert res_post.status_code == 201 file_upload_config_id = res_post.json()['file_upload_config_id'] insert_file_upload_fixture(file_upload_config_id) res_post = ows_royalties_client.post_file_upload_complete(file_key) assert res_post.status_code == 200 assert res_post.json()['upload_status'] == 'complete' assert res_post.json()['file_key'] == file_key assert file_key in res_post.json()['original_file_name'] assert file_key in res_post.json()['s3_key'] assert res_post.json()['completed_at'] is not None assert has_records_in_table('abacus_outbox') is True def test_file_upload_download( clear_db_file_upload, basic_headers, insert_file_upload_fixture, upload_xlsx_to_s3 ): """GET /file-upload/{file_key}/download .""" file_key = 'a58863c8-9f99-407c-85ab-75bd88201e9e' ows_royalties_client = ows_royalties_api_client(basic_headers) upload_xlsx_to_s3('{}.xlsx'.format(file_key)) # post new config post_body = { 's3_key_template': 'test_fixtures/ows-royalties-integration-tests/{file_key}.{ext}', 'upload_type': 'adjustments', 'description': 'integration tests configuration', } res_post = ows_royalties_client.post_file_upload_config(post_body) assert res_post.status_code == 201 file_upload_config_id = res_post.json()['file_upload_config_id'] insert_file_upload_fixture(file_upload_config_id) # complete the upload before downloading res_post = ows_royalties_client.post_file_upload_complete(file_key) assert res_post.status_code == 200 # download the file res_get = ows_royalties_client.get_file_upload_download(file_key) assert res_get.status_code == 200 assert file_key in res_get.json()['file_name'] assert file_key in res_get.json()['download_url'] assert ( 'test_fixtures/ows-royalties-integration-tests' in res_get.json()['download_url'] ) def test_file_upload_quarantine( clear_db_file_upload, delete_quarantine_file, basic_headers, insert_file_upload_fixture, upload_xlsx_to_s3, ): """POST /file-upload/{file_key}/quarantine .""" file_key = 'a58863c8-9f99-407c-85ab-75bd88201e9e' delete_quarantine_file(file_key) ows_royalties_client = ows_royalties_api_client(basic_headers) upload_xlsx_to_s3('{}.xlsx'.format(file_key)) # post new config post_body = { 's3_key_template': 'test_fixtures/ows-royalties-integration-tests/{file_key}.{ext}', 'upload_type': 'adjustments', 'description': 'integration tests configuration', } res_post = ows_royalties_client.post_file_upload_config(post_body) assert res_post.status_code == 201 file_upload_config_id = res_post.json()['file_upload_config_id'] insert_file_upload_fixture(file_upload_config_id) res_get = ows_royalties_client.post_file_upload_quarantine(file_key) assert res_get.status_code == 200 # Check that the file is present in the quarantine S3 bucket s3 = boto3.client('s3') bucket = 'qa-abacus-quarantine' key_prefix = f'test_fixtures/ows-royalties-integration-tests/{file_key}' expected_bucket_owner = os.environ.get('AWS_BUCKET_OWNER') try: s3.head_object( Bucket=bucket, Key=f'{key_prefix}.xlsx', ExpectedBucketOwner=expected_bucket_owner, ) exists = True except ClientError as e: if e.response.get('Error', {}).get('Code') == '404': exists = False else: raise assert exists