From a6703df3c7e74960249764dd3ce7525a346fef8e Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 10 May 2022 22:10:56 +1000 Subject: [PATCH] More fixes and unit tests --- InvenTree/InvenTree/api_version.py | 1 + .../barcodes/plugins/inventree_barcode.py | 6 ++-- InvenTree/barcodes/tests.py | 34 +++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/InvenTree/InvenTree/api_version.py b/InvenTree/InvenTree/api_version.py index 674cf7d78b..e391a00bd1 100644 --- a/InvenTree/InvenTree/api_version.py +++ b/InvenTree/InvenTree/api_version.py @@ -11,6 +11,7 @@ Increment this API version number whenever there is a significant change to the v47 -> 2022-05-10 : https://github.com/inventree/InvenTree/pull/2964 - Fixes barcode API error response when scanning a StockItem which does not exist + - Fixes barcode API error response when scanning a StockLocation which does not exist v46 -> 2022-05-09 - Fixes read permissions on settings API diff --git a/InvenTree/barcodes/plugins/inventree_barcode.py b/InvenTree/barcodes/plugins/inventree_barcode.py index 122de51260..604936c216 100644 --- a/InvenTree/barcodes/plugins/inventree_barcode.py +++ b/InvenTree/barcodes/plugins/inventree_barcode.py @@ -111,7 +111,7 @@ class InvenTreeBarcodePlugin(BarcodePlugin): loc = StockLocation.objects.get(pk=pk) return loc except (ValueError, StockLocation.DoesNotExist): # pragma: no cover - raise ValidationError({k, "Stock location does not exist"}) + raise ValidationError({k: "Stock location does not exist"}) return None @@ -132,12 +132,12 @@ class InvenTreeBarcodePlugin(BarcodePlugin): try: pk = self.data[k]['id'] except (AttributeError, KeyError): - raise ValidationError({k, 'id parameter not supplied'}) + raise ValidationError({k: 'id parameter not supplied'}) try: part = Part.objects.get(pk=pk) return part except (ValueError, Part.DoesNotExist): # pragma: no cover - raise ValidationError({k, 'Part does not exist'}) + raise ValidationError({k: 'Part does not exist'}) return None diff --git a/InvenTree/barcodes/tests.py b/InvenTree/barcodes/tests.py index 73bf066fb0..2834559fc8 100644 --- a/InvenTree/barcodes/tests.py +++ b/InvenTree/barcodes/tests.py @@ -86,6 +86,23 @@ class BarcodeAPITest(APITestCase): self.assertIn('barcode_data', response.data) self.assertEqual(response.data['part']['pk'], 1) + def test_invalid_part(self): + """Test response for invalid part""" + response = self.client.post( + self.scan_url, + { + 'barcode': { + 'part': 999999999, + } + }, + format='json' + ) + + self.assertEqual(response.status_code, 400) + + self.assertEqual(response.data['part'], 'Part does not exist') + + def test_find_stock_item(self): """ Test that we can lookup a stock item based on ID @@ -143,6 +160,23 @@ class BarcodeAPITest(APITestCase): self.assertIn('barcode_data', response.data) self.assertEqual(response.data['stocklocation']['pk'], 1) + def test_invalid_location(self): + """Test response for an invalid location""" + + response = self.client.post( + self.scan_url, + { + 'barcode': { + 'stocklocation': 999999999, + } + }, + format='json' + ) + + self.assertEqual(response.status_code, 400) + + self.assertEqual(response.data['stocklocation'], 'Stock location does not exist') + def test_integer_barcode(self): response = self.postBarcode(self.scan_url, '123456789')