Skip to content

Commit

Permalink
fix: validate allow_multiple tags
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Sep 21, 2023
1 parent 3ddfcda commit 432e4c2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
11 changes: 6 additions & 5 deletions openedx_tagging/core/tagging/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,25 +446,26 @@ def _find_object_tag_index(tag_ref, object_tags) -> int:
-1,
)

def _check_current_tag_count() -> None:
def _check_new_tag_count(new_tag_count: int) -> None:
"""
Checks if the current count of tags for the object is less than 100
Checks if the new count of tags for the object is equal or less than 100
"""
# Exclude self.id to avoid counting the tags that are going to be updated
current_count = ObjectTag.objects.filter(object_id=object_id).exclude(taxonomy_id=self.id).count()

if current_count >= 100:
if current_count + new_tag_count > 100:
raise ValueError(
_(f"Object ({object_id}) already have 100 or more tags.")
_(f"Cannot add more than 100 tags to ({object_id}).")
)

_check_current_tag_count()

if not isinstance(tags, list):
raise ValueError(_(f"Tags must be a list, not {type(tags).__name__}."))

tags = list(dict.fromkeys(tags)) # Remove duplicates preserving order

_check_new_tag_count(len(tags))

if not self.allow_multiple and len(tags) > 1:
raise ValueError(_(f"Taxonomy ({self.id}) only allows one tag per object."))

Expand Down
14 changes: 13 additions & 1 deletion tests/openedx_tagging/core/tagging/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,8 @@ def test_tag_object_limit(self) -> None:
["Eubacteria"],
"object_1",
)
assert "already have 100 or more tags" in str(exc.exception)
assert exc.exception
assert "Cannot add more than 100 tags to" in str(exc.exception)

# Updating existing tags should work
for taxonomy in self.dummy_taxonomies:
Expand All @@ -616,6 +617,17 @@ def test_tag_object_limit(self) -> None:
"object_1",
)

# Updating existing tags adding a new one should fail
for taxonomy in self.dummy_taxonomies:
with self.assertRaises(ValueError) as exc:
tagging_api.tag_object(
taxonomy,
["New Dummy Tag 1", "New Dummy Tag 2"],
"object_1",
)
assert exc.exception
assert "Cannot add more than 100 tags to" in str(exc.exception)

def test_get_object_tags(self) -> None:
# Alpha tag has no taxonomy
alpha = ObjectTag(object_id="abc")
Expand Down
6 changes: 5 additions & 1 deletion tests/openedx_tagging/core/tagging/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ def setUp(self):

self.dummy_taxonomies = []
for i in range(100):
taxonomy = Taxonomy.objects.create(name=f"ZZ Dummy Taxonomy {i:03}", allow_free_text=True)
taxonomy = Taxonomy.objects.create(
name=f"ZZ Dummy Taxonomy {i:03}",
allow_free_text=True,
allow_multiple=True
)
ObjectTag.objects.create(
object_id="limit_tag_count",
taxonomy=taxonomy,
Expand Down
14 changes: 12 additions & 2 deletions tests/openedx_tagging/core/tagging/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,11 @@ def _object_permission(_user, object_id: str) -> bool:

self.dummy_taxonomies = []
for i in range(100):
taxonomy = Taxonomy.objects.create(name=f"Dummy Taxonomy {i}", allow_free_text=True)
taxonomy = Taxonomy.objects.create(
name=f"Dummy Taxonomy {i}",
allow_free_text=True,
allow_multiple=True
)
ObjectTag.objects.create(
object_id="limit_tag_count",
taxonomy=taxonomy,
Expand Down Expand Up @@ -771,6 +775,12 @@ def test_tag_object_count_limit(self):
response = self.client.put(url, {"tags": ["New Tag"]}, format="json")
assert response.status_code == status.HTTP_200_OK

# Editing tags adding another one will fail
for taxonomy in self.dummy_taxonomies:
url = OBJECT_TAGS_UPDATE_URL.format(object_id=object_id, taxonomy_id=taxonomy.pk)
response = self.client.put(url, {"tags": ["New Tag 1", "New Tag 2"]}, format="json")
assert response.status_code == status.HTTP_400_BAD_REQUEST


class TestTaxonomyTagsView(TestTaxonomyViewMixin):
"""
Expand Down Expand Up @@ -1085,4 +1095,4 @@ def test_next_children(self):
)
assert data.get("count") == self.children_tags_count[0]
assert data.get("num_pages") == 2
assert data.get("current_page") == 2
assert data.get("current_page") == 2

0 comments on commit 432e4c2

Please sign in to comment.