Skip to content

fix: check collection when querying single granule #564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ def get_single_granule(self, granule_id):
'size': 1,
'sort': [{'id': {'order': 'asc'}}],
'query': {'bool': {'must': [{
'term': {'id': granule_id}
'term': {'id': granule_id}}, {
'term': {'collection': self.__collection_id},
}]}}
}
LOGGER.debug(f'granules_query_dsl: {granules_query_dsl}')
Expand All @@ -182,7 +183,7 @@ def get_single_granule(self, granule_id):
granules_query_dsl)
LOGGER.debug(f'granules_query_result: {granules_query_result}')
if len(granules_query_result['hits']['hits']) < 1:
raise ValueError(f'cannot find granule for : {granule_id}')
return None

each_granules_query_result_stripped = granules_query_result['hits']['hits'][0]['_source']
self_link = Link(rel='self', target=f'{self.__base_url}/{WebServiceConstants.COLLECTIONS}/{self.__collection_id}/items/{each_granules_query_result_stripped["id"]}', media_type='application/json', title=each_granules_query_result_stripped["id"]).to_dict(False)
Expand Down
2 changes: 2 additions & 0 deletions cumulus_lambda_functions/uds_api/granules_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ async def get_single_granule_dapa(request: Request, collection_id: str, granule_
except Exception as e:
LOGGER.exception('failed during get_granules_dapa')
raise HTTPException(status_code=500, detail=str(e))
if granules_result is None:
raise HTTPException(status_code=404, detail={'message': f'no granule with id: {granule_id} in collection: {collection_id}'})
return granules_result

@router.delete("/{collection_id}/items/{granule_id}")
Expand Down
20 changes: 20 additions & 0 deletions tests/integration_tests/test_uds_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,26 @@ def test_single_granule_get(self):

return


def test_single_granule_get_wrong_collection(self):
# post_url = f'{self.uds_url}collections/urn:nasa:unity:uds_local_test:DEV1:CHRP_16_DAY_REBIN___10/items/urn:nasa:unity:uds_local_test:DEV1:CHRP_16_DAY_REBIN___10:SNDR.SS1330.CHIRP.20230101T0000.m06.g001.L1_J1.std.v02_48.G.200101070318_REBIN'
post_url = f'{self.uds_url}collections/URN:NASA:UNITY:UDS_LOCAL_TEST_3:DEV:DDD-02___001/items/URN:NASA:UNITY:UDS_LOCAL_TEST_3:DEV:DDD-01___001:test_file10'
headers = {
'Authorization': f'Bearer {self.bearer_token}',
}
print(post_url)
query_result = requests.get(url=post_url, headers=headers,)
self.assertEqual(query_result.status_code, 404, f'wrong status code. {query_result.text}')
response_json = json.loads(query_result.text)
print(json.dumps(response_json))

post_url = f'{self.uds_url}collections/URN:NASA:UNITY:UDS_LOCAL_TEST_3:DEV:DDD-01___001/items/URN:NASA:UNITY:UDS_LOCAL_TEST_3:DEV:DDD-01___001:test_file10'
query_result = requests.get(url=post_url, headers=headers,)
self.assertEqual(query_result.status_code, 200, f'wrong status code. {query_result.text}')
response_json = json.loads(query_result.text)
print(json.dumps(response_json))
return

def test_create_new_collection(self):
post_url = f'{self.uds_url}collections/' # MCP Dev
headers = {
Expand Down
Loading