From 4217190b6d7cb025e257a63312fd6275be3184e6 Mon Sep 17 00:00:00 2001 From: Jono Yang Date: Fri, 16 Feb 2024 15:39:17 -0800 Subject: [PATCH] Ensure a response is returned in update_status #49 #285 Signed-off-by: Jono Yang --- minecode/api.py | 34 +++++++++++++++++++++++++++++----- minecode/tests/test_api.py | 25 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/minecode/api.py b/minecode/api.py index d8bb4c0c..68fd66ab 100644 --- a/minecode/api.py +++ b/minecode/api.py @@ -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 @@ -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) diff --git a/minecode/tests/test_api.py b/minecode/tests/test_api.py index aedacd5b..bc64ef76 100644 --- a/minecode/tests/test_api.py +++ b/minecode/tests/test_api.py @@ -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)