Skip to content

Commit

Permalink
test: Add test cases for logged out user
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuf-musleh committed Oct 16, 2023
1 parent d158767 commit ed0a7e2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
7 changes: 4 additions & 3 deletions openedx_tagging/core/tagging/rest_api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
46 changes: 46 additions & 0 deletions tests/openedx_tagging/core/tagging/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit ed0a7e2

Please sign in to comment.