diff --git a/.gitignore b/.gitignore index bb0c7d50..e42e3b07 100644 --- a/.gitignore +++ b/.gitignore @@ -70,4 +70,6 @@ data/dummy_prods** .venv js9.fits test-dispatcher-conf-with-gallery.yaml -tests/oda-ontology.ttl \ No newline at end of file +tests/oda-ontology.ttl +tests/request_files +tests/local_request_files \ No newline at end of file diff --git a/cdci_data_analysis/pytest_fixtures.py b/cdci_data_analysis/pytest_fixtures.py index 69a0ab7e..cbeeaaf2 100644 --- a/cdci_data_analysis/pytest_fixtures.py +++ b/cdci_data_analysis/pytest_fixtures.py @@ -1,4 +1,5 @@ # this could be a separate package or/and a pytest plugin +import pathlib from json import JSONDecodeError import sentry_sdk @@ -7,7 +8,7 @@ import cdci_data_analysis.flask_app.app from cdci_data_analysis.analysis.exceptions import BadRequest from cdci_data_analysis.flask_app.dispatcher_query import InstrumentQueryBackEnd -from cdci_data_analysis.analysis.hash import make_hash +from cdci_data_analysis.analysis.hash import make_hash, make_hash_file from cdci_data_analysis.configurer import ConfigEnv from cdci_data_analysis.analysis.email_helper import textify_email @@ -935,6 +936,38 @@ def empty_products_files_fixture(default_params_dict): yield scratch_params +@pytest.fixture +def request_files_fixture(default_params_dict): + DispatcherJobState.empty_request_files_folders() + request_file_info_obj = { + 'file_path': 'request_files/test.fits.gz', + 'content': os.urandom(20) + } + + with open(request_file_info_obj['file_path'], 'wb') as f: + f.write(request_file_info_obj['content']) + + request_file_info_obj['file_hash'] = make_hash_file(request_file_info_obj['file_path']) + + f_path_obj = pathlib.Path(request_file_info_obj['file_path']) + f_name = f_path_obj.name.split('.')[0] + f_ext = ''.join(f_path_obj.suffixes) + + new_file_name = f_name + '_' + request_file_info_obj['file_hash'] + f_ext + new_file_path = os.path.join('request_files', new_file_name) + os.rename(request_file_info_obj['file_path'], new_file_path) + request_file_info_obj['file_path'] = new_file_path + + ownership_file_path = 'request_files/.file_ownerships.json' + with open(ownership_file_path) as ownership_file: + ownerships = json.load(ownership_file) + ownerships[new_file_name] = ['public'] + with open(ownership_file_path, 'w') as ownership_file: + json.dump(ownerships, ownership_file) + + yield request_file_info_obj + + @pytest.fixture def empty_products_user_files_fixture(default_params_dict, default_token_payload): sub = default_token_payload['sub'] @@ -1441,6 +1474,16 @@ def remove_download_folders(id=None): for d in dir_list: shutil.rmtree(d) + @staticmethod + def empty_request_files_folders(): + dir_list = glob.glob('request_files/*') + for d in dir_list: + os.remove(d) + + ownership_file_path = 'request_files/.file_ownerships.json' + with open(ownership_file_path, 'w') as ownership_file: + json.dump({}, ownership_file) + @staticmethod def create_p_value_file(p_value): # generate ScWs list file diff --git a/tests/conftest.py b/tests/conftest.py index 280e3e6a..8ce5d577 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -42,6 +42,7 @@ cleanup, empty_products_files_fixture, empty_products_user_files_fixture, + request_files_fixture, default_params_dict, default_token_payload, dispatcher_live_fixture_empty_sentry, diff --git a/tests/test_server_basic.py b/tests/test_server_basic.py index 961f51e0..07a8c9c3 100644 --- a/tests/test_server_basic.py +++ b/tests/test_server_basic.py @@ -496,22 +496,18 @@ def test_download_products_outside_dir(dispatcher_long_living_fixture, @pytest.mark.fast @pytest.mark.parametrize('return_archive', [True, False]) @pytest.mark.parametrize('matching_file_name', [True, False]) -def test_download_file_public(dispatcher_long_living_fixture, empty_products_files_fixture, return_archive, matching_file_name): +def test_download_file_public(dispatcher_long_living_fixture, request_files_fixture, return_archive, matching_file_name): + server = dispatcher_long_living_fixture logger.info("constructed server: %s", server) - session_id = empty_products_files_fixture['session_id'] - job_id = empty_products_files_fixture['job_id'] - params = { # since we are passing a job_id 'query_status': 'ready', - 'file_list': 'test.fits.gz', + 'file_list': os.path.basename(request_files_fixture['file_path']), 'download_file_name': 'output_test', - 'session_id': session_id, 'return_archive': return_archive, - 'job_id': job_id } if matching_file_name: @@ -523,16 +519,16 @@ def test_download_file_public(dispatcher_long_living_fixture, empty_products_fil assert c.status_code == 200 # download the output, read it and then compare it - with open(f'scratch_sid_{session_id}_jid_{job_id}/output_test', 'wb') as fout: + with open('local_request_files/output_test', 'wb') as fout: fout.write(c.content) if return_archive: - with gzip.open(f'scratch_sid_{session_id}_jid_{job_id}/output_test', 'rb') as fout: + with gzip.open('local_request_files/output_test', 'rb') as fout: data_downloaded = fout.read() else: data_downloaded = c.content - assert data_downloaded == empty_products_files_fixture['content'] + assert data_downloaded == request_files_fixture['content'] def test_query_restricted_instrument(dispatcher_live_fixture): server = dispatcher_live_fixture