Skip to content

Commit

Permalink
Do not allow updates of finished ScannableURIs #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 Mar 18, 2024
1 parent 1192879 commit c41dda0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
6 changes: 5 additions & 1 deletion minecode/management/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ def index_package(scannable_uri, package, scan_data, summary_data, project_extra
'copyright': copyright,
**checksums_and_size_by_field
}
package.update_fields(save=True, **values_by_updateable_fields)
_, updated_fields = package.update_fields(save=True, **values_by_updateable_fields)
updated_fields = ', '.join(updated_fields)
message = f'Updated fields for Package {package.purl}: {updated_fields}'
logger.info(message)
scannable_uri.scan_status = ScannableURI.SCAN_INDEXED
scannable_uri.save()
except Exception as e:
traceback_message = traceback.format_exc()
error_message = traceback_message + '\n'
Expand Down
6 changes: 6 additions & 0 deletions minecode/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,12 @@ class ScannableURI(BaseURI):

SCAN_STATUSES_BY_CODE = dict(SCAN_STATUS_CHOICES)

SCAN_STATUS_CODES_BY_SCAN_STATUS = {
status: code
for code, status
in SCAN_STATUS_CHOICES
}

scan_status = models.IntegerField(
default=SCAN_NEW,
choices=SCAN_STATUS_CHOICES,
Expand Down
41 changes: 40 additions & 1 deletion minecode/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ def setUp(self):
package=self.package2
)

self.package3 = Package.objects.create(
download_url='https://test-url.com/package3.tar.gz',
type='type3',
name='name3',
version='3.0',
)
self.scannable_uri3 = ScannableURI.objects.create(
uri='https://test-url.com/package3.tar.gz',
package=self.package3
)

self.client = APIClient()

def test_api_scannable_uri_list_endpoint(self):
Expand All @@ -65,7 +76,7 @@ def test_api_scannable_uri_list_endpoint(self):

response = self.csrf_client.get('/api/scan_queue/')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(2, response.data.get('count'))
self.assertEqual(3, response.data.get('count'))

def test_api_scannable_uri_get_next_download_url(self):
response = self.client.get('/api/scan_queue/get_next_download_url/')
Expand All @@ -81,6 +92,11 @@ def test_api_scannable_uri_get_next_download_url(self):
self.assertEqual(response.data.get('scannable_uri_uuid'), self.scannable_uri2.uuid)
self.assertEqual(response.data.get('download_url'), self.scannable_uri2.uri)

response = self.csrf_client.get('/api/scan_queue/get_next_download_url/')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data.get('scannable_uri_uuid'), self.scannable_uri3.uuid)
self.assertEqual(response.data.get('download_url'), self.scannable_uri3.uri)

response = self.csrf_client.get('/api/scan_queue/get_next_download_url/')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data.get('scannable_uri_uuid'), '')
Expand Down Expand Up @@ -166,3 +182,26 @@ def test_api_scannable_uri_update_status(self):
expected_response = {'error': 'invalid scannable_uri_uuid: asdf'}
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(expected_response, response.data)

def test_api_scannable_uri_update_status_update_finished_scannable_uri(self):
scannable_uri_uuid = self.scannable_uri3.uuid
for scan_status in [
ScannableURI.SCAN_INDEXED,
ScannableURI.SCAN_FAILED,
ScannableURI.SCAN_TIMEOUT,
ScannableURI.SCAN_INDEX_FAILED,
]:
self.scannable_uri3.scan_status = scan_status
self.scannable_uri3.save()
data = {
'scannable_uri_uuid': scannable_uri_uuid,
'scan_status': 'scanned'
}
response = self.csrf_client.post('/api/scan_queue/update_status/', data=data)
expected_response = {
'error': 'cannot update status for scannable_uri '
f'{self.scannable_uri3.uuid}: scannable_uri has finished '
f'with status "{ScannableURI.SCAN_STATUSES_BY_CODE[scan_status]}"'
}
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(expected_response, response.data)

0 comments on commit c41dda0

Please sign in to comment.