From b9797ab39c815da7d1bde54f0e2cb54c3b3fe2ba Mon Sep 17 00:00:00 2001 From: Jillian Vogel Date: Fri, 12 Jan 2024 06:00:22 +1030 Subject: [PATCH] fix: feat: check permissions only if ?include_perms fixes type/testing issues with previous commit --- openedx_tagging/core/tagging/rest_api/v1/serializers.py | 4 ++-- tests/openedx_tagging/core/tagging/test_views.py | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/openedx_tagging/core/tagging/rest_api/v1/serializers.py b/openedx_tagging/core/tagging/rest_api/v1/serializers.py index 6b6a514e..633868f4 100644 --- a/openedx_tagging/core/tagging/rest_api/v1/serializers.py +++ b/openedx_tagging/core/tagging/rest_api/v1/serializers.py @@ -116,8 +116,8 @@ def get_can_tag_object(self, instance) -> Optional[bool]: model = self._model app_label = model._meta.app_label perm_name = f'{app_label}.add_objecttag' - perm_object = ObjectTagPermissionItem(taxonomy=instance, object_id=None) - return request.user.has_perm(perm_name, perm_object) + perm_object = ObjectTagPermissionItem(taxonomy=instance, object_id="") + return request.user.has_perm(perm_name, perm_object) # type: ignore[arg-type] class ObjectTagListQueryParamsSerializer(serializers.Serializer): # pylint: disable=abstract-method diff --git a/tests/openedx_tagging/core/tagging/test_views.py b/tests/openedx_tagging/core/tagging/test_views.py index bb967272..56b1c645 100644 --- a/tests/openedx_tagging/core/tagging/test_views.py +++ b/tests/openedx_tagging/core/tagging/test_views.py @@ -1197,7 +1197,7 @@ def test_get_counts_invalid_spec(self): assert "Wildcard matches are only supported if the * is at the end." in str(result.content) -@ddt.data +@ddt.ddt class TestTaxonomyTagsView(TestTaxonomyViewMixin): """ Tests the list/create/update/delete tags of taxonomy view @@ -1442,7 +1442,7 @@ def test_small_query_count(self, include_perms): expected_perm = True self.client.force_authenticate(user=self.staff) - with self.assertNumQueries(1): + with self.assertNumQueries(6): response = self.client.get(url) assert response.status_code == status.HTTP_200_OK @@ -1491,7 +1491,7 @@ def test_large_taxonomy(self, include_perms): url += "&include_perms" expected_perm = True - with self.assertNumQueries(1): + with self.assertNumQueries(4): response = self.client.get(url) assert response.status_code == status.HTTP_200_OK @@ -1746,7 +1746,6 @@ def test_get_leaves_paginated(self): ] next_url = response.data.get("next") assert next_url is not None - response2 = self.client.get(next_url) results2 = response2.data["results"] assert pretty_format_tags(results2) == [