Skip to content
Snippets Groups Projects
Commit de9086b9 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Added API test for getting archive data.

parent 16cbf234
Branches
Tags
No related merge requests found
from flask import Flask from flask import Flask, Response
from flask_restful import Resource, Api, abort from flask_restful import Resource, Api, abort
from datetime import datetime from datetime import datetime
from threading import Thread from threading import Thread
...@@ -69,25 +69,22 @@ class Upload(Resource): ...@@ -69,25 +69,22 @@ class Upload(Resource):
return Uploads._render(upload), 200 return Uploads._render(upload), 200
class Calc(Resource): @app.route('/archive/<string:upload_hash>/<string:calc_hash>', methods=['GET'])
def get(self, upload_hash, calc_hash): def get_calc(upload_hash, calc_hash):
archive_id = '%s/%s' % (upload_hash, calc_hash) archive_id = '%s/%s' % (upload_hash, calc_hash)
logger = get_logger(__name__, archive_id=archive_id) logger = get_logger(__name__, archive_id=archive_id)
try: try:
file = files.open_archive_json(archive_id) file = files.open_archive_json(archive_id)
return file, 200 return Response(file, mimetype='application/json', status=200)
except KeyError: except KeyError:
abort(404, message='Archive %s does not exist.' % archive_id) abort(404, message='Archive %s does not exist.' % archive_id)
except Exception as e: except Exception as e:
logger.error('Exception on reading archive', exc_info=e) logger.error('Exception on reading archive', exc_info=e)
abort(500, message='Could not read the archive.') abort(500, message='Could not read the archive.')
finally:
file.close()
api.add_resource(Uploads, '/uploads') api.add_resource(Uploads, '/uploads')
api.add_resource(Upload, '/uploads/<string:upload_id>') api.add_resource(Upload, '/uploads/<string:upload_id>')
api.add_resource(Calc, '/archive/<string:upload_hash>/<string:calc_hash>')
def start_upload_handler(quit=False): def start_upload_handler(quit=False):
......
...@@ -302,4 +302,7 @@ def open_archive_json(archive_id) -> IO: ...@@ -302,4 +302,7 @@ def open_archive_json(archive_id) -> IO:
""" Returns a file-like to read the archive json. """ """ Returns a file-like to read the archive json. """
# The result already is a file-like and due to the Content-Encoding metadata is # The result already is a file-like and due to the Content-Encoding metadata is
# will automatically be un-gzipped. # will automatically be un-gzipped.
return _client.get_object(config.files.archive_bucket, archive_id) try:
return _client.get_object(config.files.archive_bucket, archive_id)
except minio.error.NoSuchKey:
raise KeyError()
...@@ -12,7 +12,7 @@ from nomad import config, api, files ...@@ -12,7 +12,7 @@ from nomad import config, api, files
from tests.test_processing import example_files from tests.test_processing import example_files
from tests.test_files import assert_exists from tests.test_files import assert_exists
# import fixtures # import fixtures
from tests.test_files import clear_files # pylint: disable=unused-import from tests.test_files import clear_files, archive_id # pylint: disable=unused-import
@pytest.fixture @pytest.fixture
...@@ -136,3 +136,13 @@ def test_processing(client, file): ...@@ -136,3 +136,13 @@ def test_processing(client, file):
assert upload['processing']['status'] == 'SUCCESS' assert upload['processing']['status'] == 'SUCCESS'
assert_exists(config.files.uploads_bucket, upload['id']) assert_exists(config.files.uploads_bucket, upload['id'])
def test_get_archive(client, archive_id):
rv = client.get('/archive/%s' % archive_id)
assert rv.status_code == 200
def test_get_non_existing_archive(client):
rv = client.get('/archive/%s' % 'doesnt/exist')
assert rv.status_code == 404
...@@ -77,7 +77,11 @@ def upload_id(clear_files) -> Generator[str, None, None]: ...@@ -77,7 +77,11 @@ def upload_id(clear_files) -> Generator[str, None, None]:
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def archive_id(clear_files) -> Generator[str, None, None]: def archive_id(clear_files) -> Generator[str, None, None]:
example_archive_id = '__test_archive_id' example_archive_id = '__test_upload_hash/__test_calc_hash'
with files.write_archive_json(example_archive_id) as out:
json.dump({'test': 'value'}, out)
yield example_archive_id yield example_archive_id
...@@ -136,9 +140,6 @@ def test_hash(uploaded_id: str): ...@@ -136,9 +140,6 @@ def test_hash(uploaded_id: str):
def test_archive(archive_id: str): def test_archive(archive_id: str):
with files.write_archive_json(archive_id) as out:
json.dump({'test': 'value'}, out)
result = json.load(files.open_archive_json(archive_id)) result = json.load(files.open_archive_json(archive_id))
assert 'test' in result assert 'test' in result
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment