From ed0a7e2b4040f1ba5089c92fc48c52a43e354491 Mon Sep 17 00:00:00 2001 From: Yusuf Musleh Date: Sun, 15 Oct 2023 16:32:16 +0300 Subject: [PATCH] test: Add test cases for logged out user --- .../core/tagging/rest_api/v1/views.py | 7 +-- .../core/tagging/test_views.py | 46 +++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/openedx_tagging/core/tagging/rest_api/v1/views.py b/openedx_tagging/core/tagging/rest_api/v1/views.py index d800c8a7..d9cb5b34 100644 --- a/openedx_tagging/core/tagging/rest_api/v1/views.py +++ b/openedx_tagging/core/tagging/rest_api/v1/views.py @@ -15,6 +15,7 @@ from openedx_tagging.core.tagging.models.base import Tag from ...api import ( + TagDoesNotExist, add_tag_to_taxonomy, create_taxonomy, delete_tags_from_taxonomy, @@ -377,7 +378,7 @@ def update(self, request, *args, **kwargs): tags = body.data.get("tags", []) try: tag_object(taxonomy, tags, object_id) - except Tag.DoesNotExist as e: + except TagDoesNotExist as e: raise ValidationError from e except ValueError as e: raise ValidationError from e @@ -653,7 +654,7 @@ def post(self, request, *args, **kwargs): new_tag = add_tag_to_taxonomy( taxonomy, tag, parent_tag_id, external_id ) - except Tag.DoesNotExist as e: + except TagDoesNotExist as e: raise Http404("Parent Tag not found") from e except ValueError as e: raise ValidationError(e) from e @@ -680,7 +681,7 @@ def update(self, request, *args, **kwargs): try: updated_tag = update_tag_in_taxonomy(taxonomy, tag, tag_value) - except Tag.DoesNotExist as e: + except TagDoesNotExist as e: raise Http404("Tag not found") from e except ValueError as e: raise ValidationError(e) from e diff --git a/tests/openedx_tagging/core/tagging/test_views.py b/tests/openedx_tagging/core/tagging/test_views.py index cd1ffd06..034047f8 100644 --- a/tests/openedx_tagging/core/tagging/test_views.py +++ b/tests/openedx_tagging/core/tagging/test_views.py @@ -1191,6 +1191,19 @@ def test_next_children(self): assert data.get("num_pages") == 2 assert data.get("current_page") == 2 + def test_create_tag_in_taxonomy_while_loggedout(self): + new_tag_value = "New Tag" + + create_data = { + "tag": new_tag_value + } + + response = self.client.post( + self.small_taxonomy_url, create_data, format="json" + ) + + assert response.status_code == status.HTTP_401_UNAUTHORIZED + def test_create_tag_in_taxonomy(self): self.client.force_authenticate(user=self.user) new_tag_value = "New Tag" @@ -1347,6 +1360,24 @@ def test_create_tag_in_taxonomy_with_already_existing_value(self): assert response.status_code == status.HTTP_400_BAD_REQUEST + def test_update_tag_in_taxonomy_while_loggedout(self): + updated_tag_value = "Updated Tag" + + # Existing Tag that will be updated + existing_tag = self.small_taxonomy.tag_set.filter(parent=None).first() + + update_data = { + "tag": existing_tag.id, + "tag_value": updated_tag_value + } + + # Test updating using the PUT method + response = self.client.put( + self.small_taxonomy_url, update_data, format="json" + ) + + assert response.status_code == status.HTTP_401_UNAUTHORIZED + def test_update_tag_in_taxonomy_with_different_methods(self): self.client.force_authenticate(user=self.user) updated_tag_value = "Updated Tag" @@ -1509,6 +1540,21 @@ def test_update_tag_in_invalid_taxonomy(self): assert response.status_code == status.HTTP_404_NOT_FOUND + def test_delete_single_tag_from_taxonomy_while_loggedout(self): + # Get Tag that will be deleted + existing_tag = self.small_taxonomy.tag_set.filter(parent=None).first() + + delete_data = { + "tag_ids": [existing_tag.id], + "with_subtags": True + } + + response = self.client.delete( + self.small_taxonomy_url, delete_data, format="json" + ) + + assert response.status_code == status.HTTP_401_UNAUTHORIZED + def test_delete_single_tag_from_taxonomy(self): self.client.force_authenticate(user=self.user)