Prevent assigning of empty barcode data (#3851)

* Prevent assigning of empty barcode data

* Make similar check for barcode scan endpoint

* Fixes for unit tests
This commit is contained in:
Oliver 2022-10-26 09:25:27 +11:00 committed by GitHub
parent 824a44a486
commit 13cfadaf29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 7 deletions

View File

@ -48,8 +48,10 @@ class BarcodeScan(APIView):
"""
data = request.data
if 'barcode' not in data:
raise ValidationError({'barcode': _('Must provide barcode_data parameter')})
barcode_data = data.get('barcode', None)
if not barcode_data:
raise ValidationError({'barcode': _('Missing barcode data')})
# Ensure that the default barcode handlers are run first
plugins = [
@ -57,7 +59,6 @@ class BarcodeScan(APIView):
InvenTreeExternalBarcodePlugin(),
] + registry.with_mixin('barcode')
barcode_data = data.get('barcode')
barcode_hash = hash_barcode(barcode_data)
# Look for a barcode plugin which knows how to deal with this barcode
@ -106,10 +107,10 @@ class BarcodeAssign(APIView):
data = request.data
if 'barcode' not in data:
raise ValidationError({'barcode': _('Must provide barcode_data parameter')})
barcode_data = data.get('barcode', None)
barcode_data = data['barcode']
if not barcode_data:
raise ValidationError({'barcode': _('Missing barcode data')})
# Here we only check against 'InvenTree' plugins
plugins = [

View File

@ -55,7 +55,8 @@ class BarcodeAPITest(InvenTreeAPITestCase):
self.assertEqual(response.status_code, 400)
data = response.data
self.assertIn('error', data)
self.assertIn('barcode', data)
self.assertIn('Missing barcode data', str(response.data['barcode']))
def test_find_part(self):
"""Test that we can lookup a part based on ID."""