Skip to content

Commit

Permalink
Ensure a response is returned in update_status #49 #285
Browse files Browse the repository at this point in the history
Signed-off-by: Jono Yang <[email protected]>
  • Loading branch information
JonoYang committed Feb 22, 2024
1 parent dc49887 commit 4217190
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
34 changes: 29 additions & 5 deletions minecode/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,29 @@ def update_status(self, request, *args, **kwargs):

if scan_status == 'in progress':
scan_project_url = request.data.get('scan_project_url')
scannable_uri.scan_project_url = scan_project_url
scannable_uri.scan_status = ScannableURI.SCAN_IN_PROGRESS
scannable_uri.save()
if scan_project_url:
scannable_uri.scan_project_url = scan_project_url
scannable_uri.scan_status = ScannableURI.SCAN_IN_PROGRESS
scannable_uri.save()
msg = {
'status': f'scan_status updated to {scan_status} for scannable_uri {scannable_uri_uuid}'
}
else:
msg = {
'status': f'missing scan_project_url when updating scannable_uri {scannable_uri_uuid} scan_status to {scan_status}'
}

if scan_status == 'failed':
elif scan_status == 'failed':
scan_log = request.data.get('scan_log')
scannable_uri.scan_error = scan_log
scannable_uri.scan_status = ScannableURI.SCAN_FAILED
scannable_uri.wip_date = None
scannable_uri.save()
msg = {
'status': f'updated scannable uri {scannable_uri_uuid} scan_status to {scan_status}'
}

if scan_status == 'scanned':
elif scan_status == 'scanned':
scan_file = request.data.get('scan_file')
scannable_uri.scan_status = ScannableURI.SCAN_COMPLETED
package = scannable_uri.package
Expand All @@ -153,7 +164,20 @@ def update_status(self, request, *args, **kwargs):
if indexing_errors:
scannable_uri.scan_status = ScannableURI.SCAN_INDEX_FAILED
scannable_uri.index_error = indexing_errors
msg = {
'status': f'scan index failed for scannable uri {scannable_uri_uuid}'
}
else:
scannable_uri.scan_status = ScannableURI.SCAN_INDEXED
msg = {
'status': f'scan indexed for scannable uri {scannable_uri_uuid}'
}
scannable_uri.wip_date = None
scannable_uri.save()

else:
msg = {
'status': f'invalid scan_status: {scan_status}'
}

return Response(msg)
25 changes: 25 additions & 0 deletions minecode/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,28 @@ def test_api_scannable_uri_get_next_download_url(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data.get('scannable_uri_uuid'), '')
self.assertEqual(response.data.get('download_url'), '')

def test_api_scannable_uri_update_status(self):
self.assertEqual(ScannableURI.SCAN_NEW, self.scannable_uri1.scan_status)

data = {
"scannable_uri_uuid": self.scannable_uri1.uuid,
"scan_status": 'in progress',
'scan_project_url': 'scan_project_url',
}
response = self.client.post('/api/scan_queue/update_status/', data=data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.scannable_uri1.refresh_from_db()
self.assertEqual(ScannableURI.SCAN_IN_PROGRESS, self.scannable_uri1.scan_status)
self.assertEqual('scan_project_url', self.scannable_uri1.scan_project_url)

data = {
"scannable_uri_uuid": self.scannable_uri1.uuid,
"scan_status": 'failed',
'scan_log': 'scan_log',
}
response = self.client.post('/api/scan_queue/update_status/', data=data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.scannable_uri1.refresh_from_db()
self.assertEqual(ScannableURI.SCAN_FAILED, self.scannable_uri1.scan_status)
self.assertEqual('scan_log', self.scannable_uri1.scan_error)

0 comments on commit 4217190

Please sign in to comment.