Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit 85e885f

Browse files
committed
fix ga4gh unit tests
1 parent 3245e6e commit 85e885f

3 files changed

Lines changed: 33 additions & 79 deletions

File tree

beacon_api/permissions/ga4gh.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ async def get_ga4gh_permissions(token):
161161
header, payload = await decode_passport(encoded_passport)
162162
# Sort passports that carry dataset permissions
163163
pass_type = payload.get('ga4gh_visa_v1', {}).get('type')
164-
if pass_type == 'ControlledAccessGrants':
164+
if pass_type == 'ControlledAccessGrants': # nosec
165165
dataset_passports.append((encoded_passport, header))
166166
# Sort passports that MAY carry bona fide status information
167167
if pass_type in ['AcceptedTermsAndPolicies', 'ResearcherStatus']:

beacon_api/utils/validate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ async def token_middleware(request, handler):
188188
# Retrieve GA4GH Passports from /userinfo and process them into dataset permissions and bona fide status
189189
bona_fide_status = False
190190
dataset_permissions = set()
191-
check_ga4gh_token(decoded_data, token, bona_fide_status, dataset_permissions)
191+
await check_ga4gh_token(decoded_data, token, bona_fide_status, dataset_permissions)
192192

193193
LOG.info(f'Bona fide after: {bona_fide_status}')
194194
LOG.info(f'Permissions after: {dataset_permissions}')

tests/test_basic.py

Lines changed: 31 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -290,42 +290,12 @@ def test_access_resolution_controlled_never_reached2(self):
290290
with self.assertRaises(aiohttp.web_exceptions.HTTPForbidden):
291291
access_resolution(request, token, host, [], [], [8])
292292

293-
# @asynctest.mock.patch('beacon_api.permissions.ga4gh.retrieve_user_data')
294-
# async def test_ga4gh_controlled(self, userinfo):
295-
# """Test ga4gh permissions claim parsing."""
296-
# userinfo.return_value = {
297-
# "ControlledAccessGrants": [
298-
# {
299-
# "value": "https://www.ebi.ac.uk/ega/EGAD000000000001",
300-
# "source": "https://ega-archive.org/dacs/EGAC00000000001",
301-
# "by": "dac",
302-
# "authoriser": "john.doe@dac.org",
303-
# "asserted": 1546300800,
304-
# "expires": 1577836800
305-
# },
306-
# {
307-
# "value": "https://www.ebi.ac.uk/ega/EGAD000000000002",
308-
# "source": "https://ega-archive.org/dacs/EGAC00000000001",
309-
# "by": "dac",
310-
# "authoriser": "john.doe@dac.org",
311-
# "asserted": 1546300800,
312-
# "expires": 1577836800
313-
# },
314-
# {
315-
# "value": "no-prefix-dataset",
316-
# "source": "https://ega-archive.org/dacs/EGAC00000000001",
317-
# "by": "dac",
318-
# "authoriser": "john.doe@dac.org",
319-
# "asserted": 1546300800,
320-
# "expires": 1577836800
321-
# }
322-
# ]
323-
# }
324-
# # Good test: claims OK, userinfo OK
325-
# token_claim = ["ga4gh.ControlledAccessGrants"]
326-
# token = 'this_is_a_jwt'
327-
# datasets = await get_ga4gh_controlled(token, token_claim)
328-
# self.assertEqual(datasets, {'EGAD000000000001', 'EGAD000000000002', 'no-prefix-dataset'}) # has permissions
293+
async def test_ga4gh_controlled(self):
294+
"""Test ga4gh permissions claim parsing."""
295+
# Good test: claims OK, userinfo OK
296+
token_claim = []
297+
datasets = await get_ga4gh_controlled(token_claim)
298+
self.assertEqual(datasets, set()) # has permissions
329299
# # Bad test: no claims, userinfo OK
330300
# token_claim = []
331301
# token = 'this_is_a_jwt'
@@ -343,47 +313,31 @@ def test_access_resolution_controlled_never_reached2(self):
343313
# datasets = await get_ga4gh_controlled(token, token_claim)
344314
# self.assertEqual(datasets, set()) # doesn't have permissions
345315

346-
# @asynctest.mock.patch('beacon_api.permissions.ga4gh.retrieve_user_data')
347-
# async def test_ga4gh_bona_fide(self, userinfo):
348-
# """Test ga4gh statuses claim parsing."""
349-
# userinfo.return_value = {
350-
# "AcceptedTermsAndPolicies": [
351-
# {
352-
# "value": "https://doi.org/10.1038/s41431-018-0219-y",
353-
# "source": "https://ga4gh.org/duri/no_org",
354-
# "by": "self",
355-
# "asserted": 1539069213,
356-
# "expires": 4694742813
357-
# }
358-
# ],
359-
# "ResearcherStatus": [
360-
# {
361-
# "value": "https://doi.org/10.1038/s41431-018-0219-y",
362-
# "source": "https://ga4gh.org/duri/no_org",
363-
# "by": "peer",
364-
# "asserted": 1539017776,
365-
# "expires": 1593165413
366-
# }
367-
# ]
368-
# }
369-
# # Good test: claims OK, userinfo OK
370-
# passports = ["ga4gh.AcceptedTermsAndPolicies", "ga4gh.ResearcherStatus"]
371-
# bona_fide_status = await get_ga4gh_bona_fide(passports)
372-
# self.assertEqual(bona_fide_status, True) # has bona fide
373-
# # Bad test: no claims, userinfo OK
374-
# passports = []
375-
# bona_fide_status = await get_ga4gh_bona_fide(passports)
376-
# self.assertEqual(bona_fide_status, False) # doesn't have bona fide
377-
# # Bad test: claims OK, no userinfo
378-
# userinfo.return_value = {}
379-
# passports = ["ga4gh.AcceptedTermsAndPolicies", "ga4gh.ResearcherStatus"]
380-
# bona_fide_status = await get_ga4gh_bona_fide(passports)
381-
# self.assertEqual(bona_fide_status, False) # doesn't have bona fide
382-
# # Bad test: no claims, no userinfo
383-
# userinfo.return_value = {}
384-
# passports = []
385-
# bona_fide_status = await get_ga4gh_bona_fide(passports)
386-
# self.assertEqual(bona_fide_status, False) # doesn't have bona fide
316+
@asynctest.mock.patch('beacon_api.permissions.ga4gh.retrieve_user_data')
317+
async def test_ga4gh_bona_fide(self, userinfo):
318+
"""Test ga4gh statuses claim parsing."""
319+
passports = [("enc", "header", {
320+
"ga4gh_visa_v1": {"type": "AcceptedTermsAndPolicies",
321+
"value": "https://doi.org/10.1038/s41431-018-0219-y",
322+
"source": "https://ga4gh.org/duri/no_org",
323+
"by": "self",
324+
"asserted": 1539069213,
325+
"expires": 4694742813}
326+
}),
327+
("enc", "header", {
328+
"ga4gh_visa_v1": {"type": "ResearcherStatus",
329+
"value": "https://doi.org/10.1038/s41431-018-0219-y",
330+
"source": "https://ga4gh.org/duri/no_org",
331+
"by": "peer",
332+
"asserted": 1539017776,
333+
"expires": 1593165413}})]
334+
# Good test: claims OK, userinfo OK
335+
bona_fide_status = await get_ga4gh_bona_fide(passports)
336+
self.assertEqual(bona_fide_status, True) # has bona fide
337+
# Bad test: no claims, userinfo OK
338+
passports_empty = []
339+
bona_fide_status = await get_ga4gh_bona_fide(passports_empty)
340+
self.assertEqual(bona_fide_status, False) # doesn't have bona fide
387341

388342

389343
if __name__ == '__main__':

0 commit comments

Comments
 (0)