From dd476ce796103f2a8fe84a0be240604249a060a8 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 18 May 2022 22:20:29 +1000 Subject: [PATCH] Add unit tests for the 'locate' plugin - Test various failure modes - Some of the failure modes didn't fail - this is also a failure - Fixing API code accordingly --- InvenTree/plugin/base/locate/api.py | 14 ++-- InvenTree/plugin/base/locate/test_locate.py | 89 +++++++++++++++++++++ 2 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 InvenTree/plugin/base/locate/test_locate.py diff --git a/InvenTree/plugin/base/locate/api.py b/InvenTree/plugin/base/locate/api.py index f617ba3577..a7effe91b3 100644 --- a/InvenTree/plugin/base/locate/api.py +++ b/InvenTree/plugin/base/locate/api.py @@ -40,9 +40,6 @@ class LocatePluginView(APIView): # StockLocation to identify location_pk = request.data.get('location', None) - if not item_pk and not location_pk: - raise ParseError("Must supply either 'item' or 'location' parameter") - data = { "success": "Identification plugin activated", "plugin": plugin, @@ -59,8 +56,8 @@ class LocatePluginView(APIView): return Response(data) - except StockItem.DoesNotExist: - raise NotFound("StockItem matching PK '{item}' not found") + except (ValueError, StockItem.DoesNotExist): + raise NotFound(f"StockItem matching PK '{item_pk}' not found") elif location_pk: try: @@ -72,8 +69,9 @@ class LocatePluginView(APIView): return Response(data) - except StockLocation.DoesNotExist: - raise NotFound("StockLocation matching PK {'location'} not found") + except (ValueError, StockLocation.DoesNotExist): + raise NotFound(f"StockLocation matching PK '{location_pk}' not found") else: - raise NotFound() + raise ParseError("Must supply either 'item' or 'location' parameter") + diff --git a/InvenTree/plugin/base/locate/test_locate.py b/InvenTree/plugin/base/locate/test_locate.py new file mode 100644 index 0000000000..26fafcca49 --- /dev/null +++ b/InvenTree/plugin/base/locate/test_locate.py @@ -0,0 +1,89 @@ +""" +Unit tests for the 'locate' plugin mixin class +""" + +from django.urls import reverse + +from InvenTree.api_tester import InvenTreeAPITestCase + +from plugin.registry import registry + + +class LocatePluginTests(InvenTreeAPITestCase): + + fixtures = [ + 'category', + 'part', + 'location', + 'stock', + ] + + def test_installed(self): + """Test that a locate plugin is actually installed""" + + plugins = registry.with_mixin('locate') + + self.assertTrue(len(plugins) > 0) + + self.assertTrue('samplelocate' in [p.slug for p in plugins]) + + def test_locate_fail(self): + """Test various API failure modes""" + + url = reverse('api-locate-plugin') + + # Post without a plugin + response = self.post( + url, + {}, + expected_code=400 + ) + + self.assertIn("'plugin' field must be supplied", str(response.data)) + + # Post with a plugin that does not exist, or is invalid + for slug in ['xyz', 'event', 'plugin']: + response = self.post( + url, + { + 'plugin': slug, + }, + expected_code=400, + ) + + self.assertIn(f"Plugin '{slug}' is not installed, or does not support the location mixin", str(response.data)) + + # Post with a valid plugin, but no other data + response = self.post( + url, + { + 'plugin': 'samplelocate', + }, + expected_code=400 + ) + + self.assertIn("Must supply either 'item' or 'location' parameter", str(response.data)) + + # Post with valid plugin, invalid item or location + for pk in ['qq', 99999, -42]: + response = self.post( + url, + { + 'plugin': 'samplelocate', + 'item': pk, + }, + expected_code=404 + ) + + self.assertIn(f"StockItem matching PK '{pk}' not found", str(response.data)) + + response = self.post( + url, + { + 'plugin': 'samplelocate', + 'location': pk, + }, + expected_code=404, + ) + + self.assertIn(f"StockLocation matching PK '{pk}' not found", str(response.data)) \ No newline at end of file