Skip to content

Commit 3e875c6

Browse files
authored
fix: check collection when querying single granule (#564)
* fix: check collection when querying granule * feat: refine response code * fix: wrong error code in error response
1 parent 10587a8 commit 3e875c6

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

cumulus_lambda_functions/uds_api/dapa/granules_dapa_query_es.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ def get_single_granule(self, granule_id):
172172
'size': 1,
173173
'sort': [{'id': {'order': 'asc'}}],
174174
'query': {'bool': {'must': [{
175-
'term': {'id': granule_id}
175+
'term': {'id': granule_id}}, {
176+
'term': {'collection': self.__collection_id},
176177
}]}}
177178
}
178179
LOGGER.debug(f'granules_query_dsl: {granules_query_dsl}')
@@ -182,7 +183,7 @@ def get_single_granule(self, granule_id):
182183
granules_query_dsl)
183184
LOGGER.debug(f'granules_query_result: {granules_query_result}')
184185
if len(granules_query_result['hits']['hits']) < 1:
185-
raise ValueError(f'cannot find granule for : {granule_id}')
186+
return None
186187

187188
each_granules_query_result_stripped = granules_query_result['hits']['hits'][0]['_source']
188189
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)

cumulus_lambda_functions/uds_api/granules_api.py

+2
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ async def get_single_granule_dapa(request: Request, collection_id: str, granule_
136136
except Exception as e:
137137
LOGGER.exception('failed during get_granules_dapa')
138138
raise HTTPException(status_code=500, detail=str(e))
139+
if granules_result is None:
140+
raise HTTPException(status_code=404, detail={'message': f'no granule with id: {granule_id} in collection: {collection_id}'})
139141
return granules_result
140142

141143
@router.delete("/{collection_id}/items/{granule_id}")

tests/integration_tests/test_uds_api.py

+20
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,26 @@ def test_single_granule_get(self):
233233

234234
return
235235

236+
237+
def test_single_granule_get_wrong_collection(self):
238+
# 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'
239+
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'
240+
headers = {
241+
'Authorization': f'Bearer {self.bearer_token}',
242+
}
243+
print(post_url)
244+
query_result = requests.get(url=post_url, headers=headers,)
245+
self.assertEqual(query_result.status_code, 404, f'wrong status code. {query_result.text}')
246+
response_json = json.loads(query_result.text)
247+
print(json.dumps(response_json))
248+
249+
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'
250+
query_result = requests.get(url=post_url, headers=headers,)
251+
self.assertEqual(query_result.status_code, 200, f'wrong status code. {query_result.text}')
252+
response_json = json.loads(query_result.text)
253+
print(json.dumps(response_json))
254+
return
255+
236256
def test_create_new_collection(self):
237257
post_url = f'{self.uds_url}collections/' # MCP Dev
238258
headers = {

0 commit comments

Comments
 (0)