From 30650f9d3c4c76442156cf37b911a6b1474c47af Mon Sep 17 00:00:00 2001 From: Kelvin Muchiri Date: Mon, 20 May 2024 15:27:07 +0300 Subject: [PATCH] fix exception thrown when deleting export fix argument of type 'NoneType' is not iterable --- .../api/tests/viewsets/test_export_viewset.py | 26 ++++++++++++++++++- onadata/libs/filters.py | 4 ++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/onadata/apps/api/tests/viewsets/test_export_viewset.py b/onadata/apps/api/tests/viewsets/test_export_viewset.py index f9005f5fe6..6334c66e5b 100644 --- a/onadata/apps/api/tests/viewsets/test_export_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_export_viewset.py @@ -2,7 +2,6 @@ """ test_export_viewset module """ - import os from tempfile import NamedTemporaryFile @@ -293,6 +292,31 @@ def test_export_delete(self): response = view(request, pk=export.pk) self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) + def test_export_delete_null_body(self): + """Null request body is handled""" + markdown_xlsform = """ + | survey | + | | type | name | label | + | | select one fruits | fruit | Fruit | + + | choices | + | | list name | name | label | + | | fruits | orange | Orange | + | | fruits | mango | Mango | + """ + self._create_user_and_login() + xform = self._publish_markdown(markdown_xlsform, self.user) + bob = self.user + export = Export.objects.create(xform=xform) + export.save() + view = ExportViewSet.as_view({"delete": "destroy"}) + request = self.factory.delete( + "/export", data=None, content_type="application/json" + ) + force_authenticate(request, user=bob) + response = view(request, pk=export.pk) + self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) + def test_export_list_with_meta_perms(self): """ Test export list for forms with meta permissions. diff --git a/onadata/libs/filters.py b/onadata/libs/filters.py index f203601853..6eb34653c4 100644 --- a/onadata/libs/filters.py +++ b/onadata/libs/filters.py @@ -335,8 +335,10 @@ def _add_instance_prefix_to_dataview_filter_kwargs(self, filter_kwargs): def _xform_filter(self, request, view, keyword, queryset=None): """Use XForm permissions""" xform = request.query_params.get("xform") - if xform is None and "xform" in request.data: + + if xform is None and request.data is not None and "xform" in request.data: xform = request.data.get("xform") + dataview = request.query_params.get("dataview") merged_xform = request.query_params.get("merged_xform") filename = request.query_params.get("filename")