From fe1799d811dfcdadfdded1495425c294eaeb235c Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Sat, 18 Oct 2025 18:42:37 +0200 Subject: [PATCH] add more INVE-I2 checks --- .../InvenTree/InvenTree/test_serializers.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/backend/InvenTree/InvenTree/test_serializers.py b/src/backend/InvenTree/InvenTree/test_serializers.py index 4f6d1fa0e2..863a988891 100644 --- a/src/backend/InvenTree/InvenTree/test_serializers.py +++ b/src/backend/InvenTree/InvenTree/test_serializers.py @@ -141,3 +141,57 @@ class FilteredSerializers(InvenTreeAPITestCase): BadSerializer.no_filters = True _ = BadSerializer() self.assertTrue(True) # Dummy assertion to ensure we reach here + + def test_failiure_OutputOptionsMixin(self): + """Test failure case for OutputOptionsMixin.""" + + class BadSerializer(InvenTree.serializers.InvenTreeModelSerializer): + """Sample serializer.""" + + class Meta: + """Meta options.""" + + model = User + fields = ['id'] + + field_a = SerializerMethodField(method_name='sample') + + # Bad implementation of OutputOptionsMixin + with self.assertRaises(Exception) as cm: + + class BadList(OutputOptionsMixin, ListCreateAPI): + """Bad list endpoint for testing OutputOptionsMixin.""" + + serializer_class = BadSerializer + queryset = User.objects.all() + permission_classes = [] + + self.assertTrue(True) + _ = BadList() # this should raise an exception + self.assertEqual( + str(cm.exception), + 'INVE-I2: `OutputOptionsMixin` can only be used with serializers that contain the `FilterableSerializerMixin` mixin', + ) + + # More creative bad implementation + with self.assertRaises(Exception) as cm: + + class BadList(OutputOptionsMixin, ListCreateAPI): + """Bad list endpoint for testing OutputOptionsMixin.""" + + queryset = User.objects.all() + permission_classes = [] + + def get_serializer(self, *args, **kwargs): + """Get serializer override.""" + self.serializer_class = BadSerializer + return super().get_serializer(*args, **kwargs) + + view = BadList() + self.assertTrue(True) + view.get_serializer() # this should raise an exception + + self.assertEqual( + str(cm.exception), + 'INVE-I2: `OutputOptionsMixin` can only be used with serializers that contain the `FilterableSerializerMixin` mixin', + )