import json from io import BytesIO import bulkperformancerights.config as config import application application.app.debug = True client = application.app.test_client() user_type = 'vendor' user_id = 1 user_prefix = '{}/{}'.format(user_type, user_id) fake_filename = 'foo.xlsx' expected_input_file_path = '{}{}{}/{}'.format(config.bucket_name, config.prefix, user_prefix, fake_filename) def test_integration_upload_post(monkeypatch, db_fixture): # monkey patching put_to_s3: let's not actually send anything, simply # adding in @mock_s3 here does not work as it does in the logic tests def put_to_s3(file='/', user_path="/vendor/1"): return True class async_task: def delay(job='/', s3_path='path', xlsx_path='path2', email='foo'): return True async_method = 'bulkperformancerights.logic.async.convert_validate_update' monkeypatch.setattr(async_method, async_task) monkeypatch.setattr('bulkperformancerights.logic.fileupload.put_to_s3', put_to_s3) upload_url = '/upload/' + user_prefix resp = client.post( upload_url, data={'file': (BytesIO(b'this is a test'), 'foo.xlsx'), 'email': 'foo@theorchard.com'}, headers={'WORKSTATION-TRUSTED': 'true'}) json_response = json.loads(resp.get_data(True)) assert json_response.get('input_file_path') == expected_input_file_path assert json_response.get('job_id') == '1' assert json_response.get('job_status') == 'uploaded' assert resp.status_code == 200 def test_integration_upload_get(): resp = client.get('/upload') assert resp.status_code == 404 def test_integration_upload_post_bad_file(): resp = client.post( '/upload', data={ 'file': (BytesIO(b'must be xlsx!'), 'foo.xls') }) assert resp.status_code == 404 def test_integration_get_status_for_one(db_fixture): client = application.app.test_client() status_url = '/status/{}/{}/{}'.format(user_type, user_id, 1) resp = client.get(status_url) json_response = json.loads(resp.get_data(True)) assert json_response.get('input_file_path') == expected_input_file_path assert json_response.get('job_id') == '1' def test_integration_get_status_list(db_fixture): client = application.app.test_client() status_url = '/status/{}/{}'.format(user_type, user_id) resp = client.get(status_url) json_response = json.loads(resp.get_data(True)) assert len(json_response) == 1 first_status = next(iter(json_response)) assert first_status.get('input_file_path') == expected_input_file_path assert first_status.get('job_id') == '1' def test_integration_export_tracks(monkeypatch, db_fixture): class export_tracks: def delay(export_status, email=None): return True monkey_method = 'bulkperformancerights.logic.async.export_tracks' monkeypatch.setattr(monkey_method, export_tracks) export_url = '/export/' + user_prefix resp = client.post(export_url, headers={'WORKSTATION-TRUSTED': 'true'}) json_response = json.loads(resp.get_data(True)) assert json_response.get('job_id') == '1' assert json_response.get('job_status') == 'querying' assert resp.status_code == 200 def test_integration_export_status_list(db_fixture): client = application.app.test_client() status_url = '/export/{}/{}'.format(user_type, user_id) resp = client.get(status_url) json_response = json.loads(resp.get_data(True)) assert len(json_response) == 1 first_status = next(iter(json_response)) assert first_status.get('job_id') == '1' def test_integration_get_hello(): resp = client.get('/hello') assert resp.status_code == 200 json_response = json.loads(resp.get_data(True)) assert json_response.get('hello') == 'hi'