Commit 64a10d09 authored by Sascha Herzinger's avatar Sascha Herzinger
Browse files

All state controller unit tests are passing

parent 6155c19a
......@@ -57,15 +57,13 @@ def get_task_details(task_id: UUID) -> Tuple[Response, int]:
logger.warning(error)
return jsonify({'error': error}), 403
async_result = celery.AsyncResult(task_id)
state = async_result.state
state = 'NOTFOUND' if state == 'PENDING' else state
if wait and state == 'SUBMITTED':
if wait and async_result.state == 'SUBMITTED':
async_result.get(propagate=False)
result = async_result.result
if isinstance(result, Exception): # Exception -> str
result = "{}: {}".format(type(result).__name__, str(result))
logger.debug("Task found and has access. Sending response.")
return jsonify({'state': state, 'result': result}), 200
return jsonify({'state': async_result.state, 'result': result}), 200
@analytics_blueprint.route('/<uuid:task_id>', methods=['DELETE'])
......
......@@ -46,9 +46,7 @@ def get_data_state_for_task_id(task_id: str, wait: bool) -> dict:
:return: Data state that has been stored in Redis.
"""
async_result = celery.AsyncResult(task_id)
state = async_result.state
state = 'NOTFOUND' if state == 'PENDING' else state
if wait and state == 'SUBMITTED':
if wait and async_result.state == 'SUBMITTED':
logger.debug("'wait' was set. Waiting for tasks to finish ...")
async_result.get(propagate=False)
value = redis.get('data:{}'.format(task_id))
......@@ -63,7 +61,7 @@ def get_data_state_for_task_id(task_id: str, wait: bool) -> dict:
if isinstance(result, Exception): # Exception -> str
result = "{}: {}".format(type(result).__name__, str(result))
data_state['etl_message'] = result
data_state['etl_state'] = state
data_state['etl_state'] = async_result.state
return data_state
......
......@@ -113,10 +113,18 @@ def get_state_data(state_id: UUID) -> Tuple[Response, int]:
"""
logger.debug("Received GET request on /state/<uuid:state_id>.")
wait = request.args.get('wait') == '1'
state_id = str(state_id)
if state_id not in session['state_access']:
error = "Cannot get state. Make sure to submit a POST request " \
"to this very same URL containing credentials and server " \
"data to launch access verification. Only after that a GET " \
"request might or might not return you the saved state."
logger.error(error)
return jsonify({'error': error}), 404
for task_id in session['state_access'][state_id]:
data_state = get_data_state_for_task_id(task_id=task_id, wait=wait)
if data_state['etl_state'] == 'SUBMITTED':
return jsonify({'message': 'ETLs are still running.'}), 200
return jsonify({'message': 'ETLs are still running.'}), 202
elif data_state['etl_state'] == 'SUCCESS':
continue
else:
......
......@@ -103,48 +103,78 @@ class TestState:
def test_404_if_get_non_existing_state(self, test_client):
uuid = str(uuid4())
rv = test_client.post(
'/state/{}'.format(uuid), data=flask.json.dumps(
{'handler': '', 'server': '', 'auth': {'token': ''}}))
rv = test_client.get('/state/{}'.format(uuid))
body = flask.json.loads(rv.get_data())
assert 404 == rv.status_code
assert 'error' in body
assert 'Cannot get state.' in body['error']
def test_400_if_get_non_uuid_state(self, test_client):
rv = test_client.post('/state/123')
assert 400 == rv.status_code
def test_404_if_get_non_uuid_state(self, test_client):
rv = test_client.get('/state/123')
assert 404 == rv.status_code
def test_403_if_get_not_previously_self_requested_state(self, test_client):
def test_404_if_get_not_previously_self_requested_state(self, test_client):
uuid = str(uuid4())
redis.set('data:{}'.format(uuid), {'meta': {'descriptor': 'foo'}})
rv = test_client.post('/state', data=flask.json.dumps('test'))
redis.set(name='data:{}'.format(uuid),
value=json.dumps(
{'meta': {'descriptor': {'data_type': 'default'}}}))
rv = test_client.post('/state',
data=flask.json.dumps('${}$'.format(uuid)))
body = flask.json.loads(rv.get_data())
assert 201 == rv.status_code, body
state_id = body['state_id']
rv = test_client.post(
'/state/{}'.format(state_id), data=flask.json.dumps(
{'handler': '', 'server': '', 'auth': {'token': ''}}))
{'handler': 'test', 'server': 'foo', 'auth': {'token': ''}}))
body = flask.json.loads(rv.get_data())
assert 202 == rv.status_code, body
with test_client.session_transaction() as sess:
del sess['state_access'][state_id]
rv = test_client.get('/state/{}'.format(state_id))
body = flask.json.loads(rv.get_data())
assert 404 == rv.status_code, body
assert 'error' in body
assert 'Cannot get state.' in body['error']
def test_403_if_etl_fails(self, test_client):
redis.set(name='data:123',
value=json.dumps(
{'meta': {'descriptor': {'data_type': 'default'}}}))
rv = test_client.post('/state', data=flask.json.dumps('$123$'))
body = flask.json.loads(rv.get_data())
state_id = body['state_id']
test_client.post(
'/state/{}'.format(state_id),
data=flask.json.dumps(
{'handler': 'test',
'server': 'foo',
'auth': {'token': 'fail'}}))
rv = test_client.get('/state/{}?wait=1'.format(state_id))
body = flask.json.loads(rv.get_data())
assert 403 == rv.status_code, body
assert 'error' in body
assert 'ETLs failed' in body['error']
def test_return_state(self, test_client):
uuid = str(uuid4())
redis.set('data:{}'.format(uuid), {'meta': {'descriptor': 'foo'}})
rv = test_client.post('/state', data=flask.json.dumps('test'))
def test_202_then_200_for_running_etl(self, test_client):
redis.set(name='data:123',
value=json.dumps(
{'meta': {'descriptor': {'data_type': 'default'}}}))
rv = test_client.post('/state', data=flask.json.dumps('$123$'))
body = flask.json.loads(rv.get_data())
assert 201 == rv.status_code, body
state_id = body['state_id']
rv = test_client.post(
'/state/{}'.format(state_id), data=flask.json.dumps(
{'handler': '', 'server': '', 'auth': {'token': ''}}))
test_client.post(
'/state/{}'.format(state_id),
data=flask.json.dumps(
{'handler': 'test',
'server': 'foo',
'auth': {'token': ''}}))
rv = test_client.get('/state/{}'.format(state_id))
body = flask.json.loads(rv.get_data())
assert 202 == rv.status_code, body
rv = test_client.get('/state/{}'.format(state_id))
assert 'message' in body
assert 'ETLs are still running.' in body['message']
rv = test_client.get('/state/{}?wait=1'.format(state_id))
body = flask.json.loads(rv.get_data())
assert 200 == rv.status_code, body
assert 'state' in body
assert body['state'] == 'test'
assert '$123$' in body['state']
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment