Skip to content

Commit

Permalink
Merge pull request #708 from oda-hub/load-frontend-fits-file-uri
Browse files Browse the repository at this point in the history
load-frontend-fits-file-uri
  • Loading branch information
burnout87 authored Sep 4, 2024
2 parents 6e98f24 + 9faf4bd commit eb99daa
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
32 changes: 32 additions & 0 deletions cdci_data_analysis/flask_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import string
import random
import hashlib

import requests
import validators
import re
import logging
Expand Down Expand Up @@ -535,6 +537,36 @@ def resolve_job_url():
return redirect(location, 302)


@app.route('/load_frontend_fits_file_url')
def load_frontend_fits_file_url():
par_dic = request.values.to_dict()
sanitized_request_values = sanitize_dict_before_log(par_dic)
logger.info('\033[32m===========================> load_frontend_fits_file_url\033[0m')

logger.info('\033[33m raw request values: %s \033[0m', dict(sanitized_request_values))

token = par_dic.pop('token', None)
app_config = app.config.get('conf')
secret_key = app_config.secret_key
output, output_code = tokenHelper.validate_token_from_request(token=token, secret_key=secret_key,
# TODO do we actually need a special role for this?
required_roles=None,
action="loading a fits file from the frontend via a URL")

if output_code is not None:
return make_response(output, output_code)

fits_file_url = par_dic.get('fits_file_url', None)

if fits_file_url is not None:
logger.info(f"Loading fits file from URL: {fits_file_url}")
response = requests.get(fits_file_url)
return Response(response.content, status=response.status_code, mimetype='application/octet-stream')
else:
logging.warning(f'fits_file_url argument missing in request: {par_dic}')
return make_response("fits_file_url arg not provided", 400)


@app.route('/call_back', methods=['POST', 'GET'])
def dataserver_call_back():
sanitized_request_values = sanitize_dict_before_log(request.values)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_server_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,31 @@ def test_empty_request(dispatcher_live_fixture):
logger.info(jdata['config'])


@pytest.mark.parametrize('fits_file_url', [ 'valid', 'invalid', 'empty'])
def test_load_frontend_fits_file_url(dispatcher_live_fixture, fits_file_url):
server = dispatcher_live_fixture
print("constructed server:", server)

# let's generate a valid token
encoded_token = jwt.encode(default_token_payload, secret_key, algorithm='HS256')

if fits_file_url == 'valid':
fits_file_url = 'https://fits.gsfc.nasa.gov/samples/testkeys.fits'
output_status_code = 200
elif fits_file_url == 'invalid':
fits_file_url = 'https://fits.gsfc.nasa.gov/samples/aaaaaa.fits'
output_status_code = 404
else:
fits_file_url = None
output_status_code = 400

c=requests.get(os.path.join(server, 'load_frontend_fits_file_url'),
params={'fits_file_url': fits_file_url,
'token': encoded_token})

assert c.status_code == output_status_code


def test_no_debug_mode_empty_request(dispatcher_live_fixture_no_debug_mode):
server = dispatcher_live_fixture_no_debug_mode
print("constructed server:", server)
Expand Down

0 comments on commit eb99daa

Please sign in to comment.