Skip to content

Commit

Permalink
Fixes to the testing and app to bring it in line with the OAuth system
Browse files Browse the repository at this point in the history
  • Loading branch information
markmcdowall committed Dec 20, 2017
1 parent 92aafec commit 62b9bb2
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 58 deletions.
120 changes: 72 additions & 48 deletions rest/app.py
Expand Up @@ -15,15 +15,21 @@
limitations under the License.
"""

from __future__ import print_function

import os
import sys

from flask import Flask, make_response, request
from flask_restful import Api, Resource

from dmp import dmp
from reader.hdf5_adjacency import adjacency

from mg_rest_util.mg_auth import authorized

APP = Flask(__name__)
#app.config['DEBUG'] = False
# APP.config['DEBUG'] = True

REST_API = Api(APP)

Expand All @@ -42,7 +48,6 @@ def output_tsv(data, code, headers=None):
resp.headers.extend(headers or {})
return resp


def help_usage(error_message, status_code,
parameters_required, parameters_provided):
"""
Expand Down Expand Up @@ -74,24 +79,23 @@ def help_usage(error_message, status_code,
JSON formated status message to display to the user
"""
parameters = {
'user_id' : ['User ID', 'str', 'REQUIRED'],
'file_id' : ['File ID', 'str', 'REQUIRED'],
'chrom' : ['Chromosome', 'str', 'REQUIRED'],
'start' : ['Start', 'int', 'REQUIRED'],
'end' : ['End', 'int', 'REQUIRED'],
'res' : ['Resolution', 'int', 'REQUIRED'],
'limit_chr' : [
'Limit interactions to interacting with a specific chromosome',
'str', 'OPTIONAL'],
'limit_start' : [
'Limits interactions based on a region within the chromosome defined by the limit_chr parameter. REQUIRES that limit_chr and limit_end are defined',
'int', 'OPTIONAL'],
'limit_end' : [
'Limits interactions based on a region within the chromosome defined by the limit_chr parameter. REQUIRES that limit_chr and limit_start are defined',
'int', 'OPTIONAL'],
'pos_x' : ['Position i', 'int', 'REQUIRED'],
'pos_y' : ['Position j', 'int', 'REQUIRED'],
'type' : ['add_meta|remove_meta', 'str', 'REQUIRED']
"file_id" : ["File ID", "str", "REQUIRED"],
"chrom" : ["Chromosome", "str", "REQUIRED"],
"start" : ["Start", "int", "REQUIRED"],
"end" : ["End", "int", "REQUIRED"],
"res" : ["Resolution", "int", "REQUIRED"],
"limit_chr" : [
"Limit interactions to interacting with a specific chromosome",
"str", "OPTIONAL"],
"limit_start" : [
"Limits interactions based on a region within the chromosome defined by the limit_chr parameter. REQUIRES that limit_chr and limit_end are defined",
"int", "OPTIONAL"],
"limit_end" : [
"Limits interactions based on a region within the chromosome defined by the limit_chr parameter. REQUIRES that limit_chr and limit_start are defined",
"int", "OPTIONAL"],
"pos_x" : ["Position i", "int", "REQUIRED"],
"pos_y" : ["Position j", "int", "REQUIRED"],
"type" : ["add_meta|remove_meta", "str", "REQUIRED"]
}

used_param = {k : parameters[k] for k in parameters_required if k in parameters}
Expand Down Expand Up @@ -181,30 +185,33 @@ def get(self, user_id):
.. code-block:: none
:linenos:
curl -X GET http://localhost:5001/mug/api/adjacency/details?user_id=test&file_id=test_file
curl -X GET
-H "Authorization: Bearer teststring"
http://localhost:5001/mug/api/adjacency/details?file_id=test_file
"""
if user_id is not None:
file_id = request.args.get('file_id')

params_required = ['user_id', 'file_id']
params_required = ['file_id']
params = [user_id, file_id]

# Display the parameters available
if sum([x is None for x in params]) == len(params):
return help_usage(None, 200, params_required, {})
return {"usage": "test"}
#return help_usage(None, 200, params_required, {})

# ERROR - one of the required parameters is NoneType
if sum([x is not None for x in params]) != len(params):
return help_usage(
'MissingParameters', 400, params_required,
{'user_id' : user_id, 'file_id' : file_id}
{'file_id' : file_id}
)

#request_path = request.path
#rp = request_path.split("/")

hdf5_handle = adjacency(user_id, file_id)
hdf5_handle = adjacency(user_id["user_id"], file_id)
h5_data = hdf5_handle.get_details()
hdf5_handle.close()

Expand All @@ -213,11 +220,13 @@ def get(self, user_id):
'_self': request.base_url,
'_parent': request.url_root + 'mug/api/adjacency'
},
'chromosomes' : [{'chromosome' : c[0], 'length' : c[1]} for c in h5_data["chromosomes"]],
'chromosomes' : [
{'chromosome' : c[0], 'length' : c[1]} for c in h5_data["chromosomes"]
],
'resolutions' : h5_data['resolutions']
}

return help_usage('Forbidden', 403, ['file_id'], {})
return help_usage('Forbidden', 403, params_required, {})


class GetInteractions(Resource):
Expand Down Expand Up @@ -289,7 +298,9 @@ def get(self, user_id):
.. code-block:: none
:linenos:
curl -X GET http://localhost:5001/mug/api/adjacency/getInteractions?user_id=test&file_id=test_file&chr=<chr_id>&res=<res>
curl -X GET
-H "Authorization: Bearer teststring"
http://localhost:5001/mug/api/adjacency/getInteractions?file_id=test_file&chr=<chr_id>&res=<res>
Notes
-----
Expand All @@ -300,7 +311,10 @@ def get(self, user_id):
.. code-block:: none
:linenos:
curl -X GET --header "Accept: application/tsv" http://localhost:5001/mug/api/adjacency/getInteractions?user_id=test&file_id=test_file&chr=<chr_id>&res=<res>
curl -X GET
-H "Accept: application/tsv"
-H "Authorization: Bearer teststring"
http://localhost:5001/mug/api/adjacency/getInteractions?file_id=test_file&chr=<chr_id>&res=<res>
This will return the values from the JSON format in the following order
Expand Down Expand Up @@ -337,7 +351,7 @@ def get(self, user_id):
return help_usage(
'MissingParameters', 400, params_required,
{
'user_id' : user_id, 'file_id' : file_id,
'file_id' : file_id,
'chr' : chr_id, 'start' : start, 'end' : end,
'res' : resolution, 'limit_chr' : limit_chr,
'limit_start' : limit_start, 'limit_end' : limit_end
Expand All @@ -353,13 +367,13 @@ def get(self, user_id):
return help_usage(
'IncorrectParameterType', 400, params_required,
{
'user_id' : user_id, 'file_id' : file_id,
'file_id' : file_id,
'chr' : chr_id, 'start' : start, 'end' : end,
'res' : resolution, 'limit_chr' : limit_chr
}
)

hdf5_handle = adjacency(user_id, file_id, resolution)
hdf5_handle = adjacency(user_id["user_id"], file_id, resolution)
details = hdf5_handle.get_details()
#print("Details:", details)

Expand All @@ -368,7 +382,7 @@ def get(self, user_id):
return help_usage(
'Resolution Not Available', 400, params_required,
{
'user_id' : user_id, 'file_id' : file_id,
'file_id' : file_id,
'chr' : chr_id, 'start' : start, 'end' : end,
'res' : resolution, 'limit_chr' : limit_chr,
'limit_start' : limit_start, 'limit_end' : limit_end
Expand All @@ -380,7 +394,7 @@ def get(self, user_id):
return help_usage(
'MissingParameters', 400, params_required,
{
'user_id' : user_id, 'file_id' : file_id,
'file_id' : file_id,
'chr' : chr_id, 'start' : start, 'end' : end,
'res' : resolution, 'limit_chr' : limit_chr,
'limit_start' : limit_start, 'limit_end' : limit_end
Expand All @@ -394,7 +408,7 @@ def get(self, user_id):
return help_usage(
'IncorrectParameterType', 400, params_required,
{
'user_id' : user_id, 'file_id' : file_id,
'file_id' : file_id,
'chr' : chr_id, 'start' : start, 'end' : end,
'res' : resolution, 'limit_chr' : limit_chr,
'limit_start' : limit_start, 'limit_end' : limit_end
Expand Down Expand Up @@ -482,7 +496,9 @@ def get(self, user_id):
.. code-block:: none
:linenos:
curl -X GET http://localhost:5001/mug/api/adjacency/getValue?user_id=test&file_id=test_file&chr=<chr_id>&res=<res>
curl -X GET
-H "Authorization: Bearer teststring"
http://localhost:5001/mug/api/adjacency/getValue?file_id=test_file&chr=<chr_id>&res=<res>
"""
if user_id is not None:
file_id = request.args.get('file_id')
Expand All @@ -502,7 +518,7 @@ def get(self, user_id):
return help_usage(
'MissingParameters', 400, params_required,
{
'user_id' : user_id, 'file_id' : file_id,
'file_id' : file_id,
'resolution' : resolution, 'pos_x' : pos_x, 'pos_y' : pos_y
}
)
Expand All @@ -516,12 +532,12 @@ def get(self, user_id):
return help_usage(
'IncorrectParameterType', 400, params_required,
{
'user_id' : user_id, 'file_id' : file_id,
'file_id' : file_id,
'resolution' : resolution, 'pos_x' : pos_x, 'pos_y' : pos_y
}
)

h5_handle = adjacency(user_id, file_id, resolution)
h5_handle = adjacency(user_id["user_id"], file_id, resolution)
#meta_data = h5.get_details()
#print("chr_param:", meta_data["chr_param"])
value = h5_handle.get_value(pos_x, pos_y)
Expand All @@ -532,18 +548,18 @@ def get(self, user_id):
h5_handle.close()

return {
'_links': {
'_self': request.url_root + 'mug/api/adjacency/getValue?user_id=' + str(user_id) + "&file_id=" + str(file_id) + "&res=" + str(resolution) + "&pos_x=" + str(pos_x) + "&pos_y=" + str(pos_y)
"_links": {
"_self": request.url_root + "mug/api/adjacency/getValue?file_id=" + str(file_id) + "&res=" + str(resolution) + "&pos_x=" + str(pos_x) + "&pos_y=" + str(pos_y)
},
'chrA': chr_a_id,
'chrB': chr_b_id,
'resolution': resolution,
'pos_x': pos_x,
'pos_y': pos_y,
'value': int(value)
"chrA": chr_a_id,
"chrB": chr_b_id,
"resolution": resolution,
"pos_x": pos_x,
"pos_y": pos_y,
"value": int(value)
}

return help_usage('Forbidden', 403, ['file_id', 'res', 'pos_x', 'pos_y'], {})
return help_usage("Forbidden", 403, ["file_id", "res", "pos_x", "pos_y"], {})

class Ping(Resource):
"""
Expand Down Expand Up @@ -581,6 +597,14 @@ def get():
}
return res

#
# For the services where there needs to be an extra layer (adjacency lists),
# then there needs to be a way of forwarding for this. But the majority of
# things can be redirected to the raw files for use as a track.
#

sys._auth_meta_json = os.path.dirname(os.path.realpath(__file__)) + '/auth_meta.json'

# Define the URIs and their matching methods

# List the available end points for this service
Expand Down
46 changes: 36 additions & 10 deletions tests/test_rest.py
Expand Up @@ -63,17 +63,22 @@ def test_details(client):
"""
Test the details endpoint to ensure it returns the usage element
"""
rest_value = client.get('/mug/api/adjacency/details')
rest_value = client.get(
'/mug/api/adjacency/details',
headers=dict(Authorization='Authorization: Bearer teststring')
)
details = json.loads(rest_value.data)
print(details)
assert 'usage' in details

def test_details_00(client):
"""
Test that details endpoint includes a resolution value when a test user and
file are specified
"""
rest_value = client.get('/mug/api/adjacency/details?user_id=test&file_id=test')
rest_value = client.get(
'/mug/api/adjacency/details?file_id=test',
headers=dict(Authorization='Authorization: Bearer teststring')
)
details = json.loads(rest_value.data)
print(details)
assert 'resolutions' in details
Expand All @@ -83,7 +88,10 @@ def test_details_01(client):
Test that details endpoint includes 3 resolution values when a test user and
file are specified
"""
rest_value = client.get('/mug/api/adjacency/details?user_id=test&file_id=test')
rest_value = client.get(
'/mug/api/adjacency/details?file_id=test',
headers=dict(Authorization='Authorization: Bearer teststring')
)
details = json.loads(rest_value.data)
print(details)
assert len(details['resolutions']) == 3
Expand All @@ -92,7 +100,10 @@ def test_getinteractions(client):
"""
Test the interactions endpoint to ensure it returns the usage element
"""
rest_value = client.get('/mug/api/adjacency/getInteractions')
rest_value = client.get(
'/mug/api/adjacency/getInteractions',
headers=dict(Authorization='Authorization: Bearer teststring')
)
details = json.loads(rest_value.data)
print(details)
assert 'usage' in details
Expand All @@ -102,7 +113,10 @@ def test_getinteractions_00(client):
Test that interactions returns a values block when test parameters are
provided
"""
rest_value = client.get('/mug/api/adjacency/getInteractions?user_id=test&file_id=test&chr=chr1&res=10000&start=100000&end=200000')
rest_value = client.get(
'/mug/api/adjacency/getInteractions?file_id=test&chr=chr1&res=10000&start=100000&end=200000',
headers=dict(Authorization='Authorization: Bearer teststring')
)
details = json.loads(rest_value.data)
print('TEST - getinteractions_00:', details.keys())
assert 'values' in details
Expand All @@ -112,7 +126,10 @@ def test_getinteractions_01(client):
Test that interactions returns a known number of values when test parameters
are provided
"""
rest_value = client.get('/mug/api/adjacency/getInteractions?user_id=test&file_id=test&chr=chr1&res=10000&start=100000&end=200000')
rest_value = client.get(
'/mug/api/adjacency/getInteractions?file_id=test&chr=chr1&res=10000&start=100000&end=200000',
headers=dict(Authorization='Authorization: Bearer teststring')
)
details = json.loads(rest_value.data)
print('TEST - getinteractions_01:', details.keys())
value_count = len(details['values'])
Expand All @@ -122,7 +139,10 @@ def test_getvalue(client):
"""
Test the values endpoint to ensure it returns the usage element
"""
rest_value = client.get('/mug/api/adjacency/getValue')
rest_value = client.get(
'/mug/api/adjacency/getValue',
headers=dict(Authorization='Authorization: Bearer teststring')
)
details = json.loads(rest_value.data)
print(details)
assert 'usage' in details
Expand All @@ -132,13 +152,19 @@ def test_getvalue_00(client):
Test that values returns with a value element when test parameters are
provided
"""
rest_interactions = client.get('/mug/api/adjacency/getInteractions?user_id=test&file_id=test&chr=chr1&res=10000&start=100000&end=200000')
rest_interactions = client.get(
'/mug/api/adjacency/getInteractions?file_id=test&chr=chr1&res=10000&start=100000&end=200000',
headers=dict(Authorization='Authorization: Bearer teststring')
)
interaction_details = json.loads(rest_interactions.data)
print(interaction_details['values'][0])
pos_x = interaction_details['values'][0]['pos_x']
pos_y = interaction_details['values'][0]['pos_y']

rest_value = client.get('/mug/api/adjacency/getValue?user_id=test&file_id=test&res=10000&pos_x=' + str(pos_x) + '&pos_y=' + str(pos_y))
rest_value = client.get(
'/mug/api/adjacency/getValue?file_id=test&res=10000&pos_x=' + str(pos_x) + '&pos_y=' + str(pos_y),
headers=dict(Authorization='Authorization: Bearer teststring')
)
value_details = json.loads(rest_value.data)
print(value_details)
assert 'value' in value_details
Expand Down

0 comments on commit 62b9bb2

Please sign in to comment.