From f1f31a1338a153bfa2ffef6e4ec1a77509aa043e Mon Sep 17 00:00:00 2001 From: mpdgraev Date: Fri, 9 Oct 2020 16:15:32 +0200 Subject: [PATCH] fix error caused by assumption that json.loads() returns an object with a .keys() function --- .../barcode/plugins/inventree_barcode.py | 2 ++ InvenTree/barcode/tests.py | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/InvenTree/barcode/plugins/inventree_barcode.py b/InvenTree/barcode/plugins/inventree_barcode.py index 6e4e6937a0..93fb58cbc0 100644 --- a/InvenTree/barcode/plugins/inventree_barcode.py +++ b/InvenTree/barcode/plugins/inventree_barcode.py @@ -42,6 +42,8 @@ class InvenTreeBarcodePlugin(BarcodePlugin): elif type(self.data) is str: try: self.data = json.loads(self.data) + if type(self.data) is not dict: + return False except json.JSONDecodeError: return False else: diff --git a/InvenTree/barcode/tests.py b/InvenTree/barcode/tests.py index 98e126eba1..4b7356aead 100644 --- a/InvenTree/barcode/tests.py +++ b/InvenTree/barcode/tests.py @@ -56,6 +56,34 @@ class BarcodeAPITest(APITestCase): self.assertIn('plugin', data) self.assertIsNone(data['plugin']) + def test_integer_barcode(self): + + response = self.postBarcode(self.scan_url, '123456789') + + self.assertEqual(response.status_code, status.HTTP_200_OK) + + data = response.data + self.assertIn('error', data) + + self.assertIn('barcode_data', data) + self.assertIn('hash', data) + self.assertIn('plugin', data) + self.assertIsNone(data['plugin']) + + def test_array_barcode(self): + + response = self.postBarcode(self.scan_url, "['foo', 'bar']") + + self.assertEqual(response.status_code, status.HTTP_200_OK) + + data = response.data + self.assertIn('error', data) + + self.assertIn('barcode_data', data) + self.assertIn('hash', data) + self.assertIn('plugin', data) + self.assertIsNone(data['plugin']) + def test_barcode_generation(self): item = StockItem.objects.get(pk=522)