From 0f48ca60084ec74b94dcc9b9de244437a6f95161 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 7 Mar 2022 09:48:39 +1100 Subject: [PATCH 1/2] Ensure that the native barcode scan plugin is *always* in use --- InvenTree/barcodes/api.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/InvenTree/barcodes/api.py b/InvenTree/barcodes/api.py index 0d3656ce4c..047db357a4 100644 --- a/InvenTree/barcodes/api.py +++ b/InvenTree/barcodes/api.py @@ -12,6 +12,7 @@ from rest_framework.views import APIView from stock.models import StockItem from stock.serializers import StockItemSerializer +from barcodes.plugins.inventree_barcode import InvenTreeBarcodePlugin from barcodes.barcode import hash_barcode from plugin import registry @@ -57,6 +58,9 @@ class BarcodeScan(APIView): barcode_data = data.get('barcode') + # Ensure that the default barcode handler is installed + plugins.append(InvenTreeBarcodePlugin()) + # Look for a barcode plugin which knows how to deal with this barcode plugin = None From 9b14a41e831a98827a86220c3ad3cb3c2e85620a Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 7 Mar 2022 11:20:07 +1100 Subject: [PATCH 2/2] Add unit tests for barcode scanning API --- .../barcodes/plugins/inventree_barcode.py | 2 +- InvenTree/barcodes/tests.py | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/InvenTree/barcodes/plugins/inventree_barcode.py b/InvenTree/barcodes/plugins/inventree_barcode.py index 842f9029aa..1b451f0286 100644 --- a/InvenTree/barcodes/plugins/inventree_barcode.py +++ b/InvenTree/barcodes/plugins/inventree_barcode.py @@ -52,7 +52,7 @@ class InvenTreeBarcodePlugin(BarcodePlugin): # If any of the following keys are in the JSON data, # let's go ahead and assume that the code is a valid InvenTree one... - for key in ['tool', 'version', 'InvenTree', 'stockitem', 'location', 'part']: + for key in ['tool', 'version', 'InvenTree', 'stockitem', 'stocklocation', 'part']: if key in self.data.keys(): return True diff --git a/InvenTree/barcodes/tests.py b/InvenTree/barcodes/tests.py index a9795c3928..c9a063e8f0 100644 --- a/InvenTree/barcodes/tests.py +++ b/InvenTree/barcodes/tests.py @@ -56,6 +56,66 @@ class BarcodeAPITest(APITestCase): self.assertIn('plugin', data) self.assertIsNone(data['plugin']) + def test_find_part(self): + """ + Test that we can lookup a part based on ID + """ + + response = self.client.post( + self.scan_url, + { + 'barcode': { + 'part': 1, + }, + }, + format='json', + ) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertIn('part', response.data) + self.assertIn('barcode_data', response.data) + self.assertEqual(response.data['part']['pk'], 1) + + def test_find_stock_item(self): + """ + Test that we can lookup a stock item based on ID + """ + + response = self.client.post( + self.scan_url, + { + 'barcode': { + 'stockitem': 1, + } + }, + format='json', + ) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertIn('stockitem', response.data) + self.assertIn('barcode_data', response.data) + self.assertEqual(response.data['stockitem']['pk'], 1) + + def test_find_location(self): + """ + Test that we can lookup a stock location based on ID + """ + + response = self.client.post( + self.scan_url, + { + 'barcode': { + 'stocklocation': 1, + }, + }, + format='json' + ) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertIn('stocklocation', response.data) + self.assertIn('barcode_data', response.data) + self.assertEqual(response.data['stocklocation']['pk'], 1) + def test_integer_barcode(self): response = self.postBarcode(self.scan_url, '123456789')