Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions beacon/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,19 @@ async def post(self):
except Exception as e:# pragma: no cover
response_obj = build_beacon_error_response(self, ErrorClass.error_code, ErrorClass.error_message)
return web.Response(text=json_util.dumps(response_obj), status=ErrorClass.error_code, content_type='application/json')


@web.middleware
async def error_middleware(request, handler):
try:
response = await handler(request)
if response.status != 404:
return response
except web.HTTPException as ex:
if ex.status != 404:
raise
else:
response_obj = build_beacon_error_response(EndpointView(request), 404, "Not found")
return web.Response(text=json_util.dumps(response_obj), status=404, content_type='application/json')

async def initialize(app):# pragma: no cover
setattr(conf, 'update_datetime', datetime.now().isoformat())
Expand Down Expand Up @@ -270,7 +282,7 @@ async def create_api():# pragma: no cover
check_configuration()
app = web.Application(
middlewares=[
cors_middleware(origins=conf.cors_urls)
cors_middleware(origins=conf.cors_urls), error_middleware
]
)
app.on_startup.append(initialize)
Expand Down
2 changes: 1 addition & 1 deletion beacon/conf/api_version.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
api_version: v2.0-82d34a6
api_version: v2.0-478214b
1 change: 0 additions & 1 deletion beacon/connections/mongo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ def get_docs_by_response_type(self, include: str, query: dict, dataset: str, lim
queryid={}
queryid['datasetId']=dataset
query_count["$or"].append(queryid)
LOG.warning(query_count)
if query_count["$or"]!=[]:
dataset_count = get_count(self, mongo_collection, query_count)
if dataset_count == 0:
Expand Down
147 changes: 36 additions & 111 deletions beacon/tests/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from aiohttp.test_utils import TestClient, TestServer, loop_context
from aiohttp import web
from beacon.__main__ import Collection, Resultset, Info, ServiceInfo, Map, Configuration, FilteringTerms, EntryTypes
from beacon.__main__ import Collection, Resultset, Info, ServiceInfo, Map, Configuration, FilteringTerms, EntryTypes, error_middleware
import json
import unittest
import beacon.conf.conf as conf
Expand All @@ -12,11 +12,17 @@
from beacon.connections.mongo.filters import cross_query
from unittest.mock import MagicMock
from beacon.conf import analysis, biosample, cohort, dataset, genomicVariant, individual, run
from aiohttp_middlewares import cors_middleware



def create_app():
app = web.Application()
app = web.Application(
middlewares=[
cors_middleware(origins=conf.cors_urls), error_middleware
]
)
#app.on_startup.append(initialize)
app.add_routes([web.post(conf.uri_subpath+'', Info)])
app.add_routes([web.post(conf.uri_subpath+'/info', Info)])
Expand Down Expand Up @@ -1776,112 +1782,6 @@ async def test_check_runs_variants():
assert resp.status == 200
loop.run_until_complete(test_check_runs_variants())
loop.run_until_complete(client.close())
'''
def test_individuals_with_variant_filter(self):
with loop_context() as loop:
app = create_app()
client = TestClient(TestServer(app), loop=loop)
loop.run_until_complete(client.start_server())
async def test_check_individuals_variants():
resp = await client.post(conf.uri_subpath+"/"+individual.endpoint_name, json={
"meta": {
"apiVersion": "2.0"
},
"query": {
"filters": [
{"id":"ENSGLOSSARY:0000150", "scope":"genomicVariation"}],
"includeResultsetResponses": "HIT",
"pagination": {
"skip": 0,
"limit": 10
},
"testMode": True,
"requestedGranularity": "record"
}
}
)
assert resp.status == 200
loop.run_until_complete(test_check_individuals_variants())
loop.run_until_complete(client.close())
def test_biosamples_with_variant_filter(self):
with loop_context() as loop:
app = create_app()
client = TestClient(TestServer(app), loop=loop)
loop.run_until_complete(client.start_server())
async def test_check_biosamples_variants():
resp = await client.post(conf.uri_subpath+"/"+biosample.endpoint_name, json={
"meta": {
"apiVersion": "2.0"
},
"query": {
"filters": [
{"id":"ENSGLOSSARY:0000150", "scope":"genomicVariation"}],
"includeResultsetResponses": "HIT",
"pagination": {
"skip": 0,
"limit": 10
},
"testMode": True,
"requestedGranularity": "record"
}
}
)
assert resp.status == 200
loop.run_until_complete(test_check_biosamples_variants())
loop.run_until_complete(client.close())
def test_analyses_with_variant_filter(self):
with loop_context() as loop:
app = create_app()
client = TestClient(TestServer(app), loop=loop)
loop.run_until_complete(client.start_server())
async def test_check_analyses_variants():
resp = await client.post(conf.uri_subpath+"/"+analysis.endpoint_name+"", json={
"meta": {
"apiVersion": "2.0"
},
"query": {
"filters": [
{"id":"ENSGLOSSARY:0000150", "scope":"genomicVariation"}],
"includeResultsetResponses": "HIT",
"pagination": {
"skip": 0,
"limit": 10
},
"testMode": True,
"requestedGranularity": "record"
}
}
)
assert resp.status == 200
loop.run_until_complete(test_check_analyses_variants())
loop.run_until_complete(client.close())
def test_runs_with_variant_filter(self):
with loop_context() as loop:
app = create_app()
client = TestClient(TestServer(app), loop=loop)
loop.run_until_complete(client.start_server())
async def test_check_runs_variants():
resp = await client.post(conf.uri_subpath+"/"+run.endpoint_name, json={
"meta": {
"apiVersion": "2.0"
},
"query": {
"filters": [
{"id":"ENSGLOSSARY:0000150", "scope":"genomicVariation"}],
"includeResultsetResponses": "HIT",
"pagination": {
"skip": 0,
"limit": 10
},
"testMode": True,
"requestedGranularity": "record"
}
}
)
assert resp.status == 200
loop.run_until_complete(test_check_runs_variants())
loop.run_until_complete(client.close())
'''
def test_variants_with_run_filter(self):
with loop_context() as loop:
app = create_app()
Expand Down Expand Up @@ -2182,7 +2082,6 @@ async def test_check_runs_variants():
assert resp.status == 200
loop.run_until_complete(test_check_runs_variants())
loop.run_until_complete(client.close())
'''
def test_variants_with_request_parameters_and_filters(self):
with loop_context() as loop:
app = create_app()
Expand All @@ -2200,8 +2099,7 @@ async def test_check_runs_variants():
"start": [43045703],
"referenceName": "17",
"assemblyId": "GRCh38"
},
"filters": [{"id":"ENSGLOSSARY:0000150", "scope":"genomicVariation"}],
},
"includeResultsetResponses": "HIT",
"pagination": {
"skip": 0,
Expand All @@ -2215,7 +2113,6 @@ async def test_check_runs_variants():
assert resp.status == 200
loop.run_until_complete(test_check_runs_variants())
loop.run_until_complete(client.close())
'''
def test_main_check_g_variants_sequence_query_fails(self):
with loop_context() as loop:
app = create_app()
Expand Down Expand Up @@ -2546,6 +2443,34 @@ async def test_check_requestedSchemas_fails():
assert resp.status == 400
loop.run_until_complete(test_check_requestedSchemas_fails())
loop.run_until_complete(client.close())
def test_main_check_404_not_found_error(self):
with loop_context() as loop:
app = create_app()
client = TestClient(TestServer(app), loop=loop)
loop.run_until_complete(client.start_server())
async def test_check_404_not_found_error():
resp = await client.post(conf.uri_subpath+"/impossibleendpoint")
assert resp.status == 404
responsetext=await resp.text()
responsedict=json.loads(responsetext)
assert responsedict["error"]["errorMessage"] == "Not found"
assert responsedict["error"]["errorCode"] == "404"
loop.run_until_complete(test_check_404_not_found_error())
loop.run_until_complete(client.close())
def test_main_check_400_bad_request(self):
with loop_context() as loop:
app = create_app()
client = TestClient(TestServer(app), loop=loop)
loop.run_until_complete(client.start_server())
async def test_check_400_bad_request():
resp = await client.post(conf.uri_subpath+"/"+individual.endpoint_name+'?testMod=true')
LOG.warning(resp.status)
assert resp.status == 400
responsetext=await resp.text()
responsedict=json.loads(responsetext)
assert responsedict["error"]["errorCode"] == "400"
loop.run_until_complete(test_check_400_bad_request())
loop.run_until_complete(client.close())

if __name__ == '__main__':
unittest.main()