fix error caused by assumption that json.loads() returns an object with a .keys() function

This commit is contained in:
mpdgraev 2020-10-09 16:15:32 +02:00
parent ca8472ac23
commit f1f31a1338
2 changed files with 30 additions and 0 deletions

View File

@ -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:

View File

@ -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)