diff --git a/contentcuration/contentcuration/tests/views/test_views_internal.py b/contentcuration/contentcuration/tests/views/test_views_internal.py index 1beae83c40..b00e96229a 100644 --- a/contentcuration/contentcuration/tests/views/test_views_internal.py +++ b/contentcuration/contentcuration/tests/views/test_views_internal.py @@ -110,11 +110,6 @@ def setUp(self): invalid_copyright_holder["title"] = "invalid_copyright_holder" invalid_copyright_holder["copyright_holder"] = "" - # Node with tag greater than 30 characters - invalid_tag_length = self._make_node_data() - invalid_tag_length["title"] = "invalid_tag_length" - invalid_tag_length["tags"] = ["abcd abcd abcd abcd abcd abcd", "abcde abcd abcd abcd abcd abcd", "abcdef abcd abcd abcd abcd abcd"] - self.sample_data = { "root_id": self.root_node.id, "content_data": [ @@ -122,7 +117,6 @@ def setUp(self): invalid_title_node, invalid_license_description, invalid_copyright_holder, - invalid_tag_length, ], } self.resp = self.admin_client().post( @@ -184,10 +178,23 @@ def test_invalid_nodes_are_not_complete(self): self.assertFalse(node_3.complete) def test_tag_greater_than_30_chars_excluded(self): - node = ContentNode.objects.get(title="invalid_tag_length") - tags = node.tags.all() - for t in tags: - assert len(t.tag_name) <= 30 + # Node with tag greater than 30 characters + invalid_tag_length = self._make_node_data() + invalid_tag_length["title"] = "invalid_tag_length" + invalid_tag_length["tags"] = ["kolibri studio, kolibri studio!"] + + test_data = { + "root_id": self.root_node.id, + "content_data": [ + invalid_tag_length, + ], + } + + response = self.admin_client().post( + reverse_lazy("api_add_nodes_to_tree"), data=test_data, format="json" + ) + + self.assertEqual(response.status_code, 400, response.content) class ApiAddExerciseNodesToTreeTestCase(StudioTestCase): diff --git a/contentcuration/contentcuration/tests/viewsets/test_contentnode.py b/contentcuration/contentcuration/tests/viewsets/test_contentnode.py index 55872a58e4..ce05fd2aaa 100644 --- a/contentcuration/contentcuration/tests/viewsets/test_contentnode.py +++ b/contentcuration/contentcuration/tests/viewsets/test_contentnode.py @@ -1367,6 +1367,22 @@ def test_create_contentnode_tag(self): .exists() ) + def test_contentnode_tag_greater_than_30_chars(self): + user = testdata.user() + tag = "kolibri studio, kolibri studio!" + + self.client.force_authenticate(user=user) + contentnode = self.contentnode_metadata + contentnode["tags"] = { + tag: True, + } + + response = self.client.post( + reverse("contentnode-list"), contentnode, format="json", + ) + + self.assertEqual(response.status_code, 400, response.content) + def test_update_contentnode(self): user = testdata.user() contentnode = models.ContentNode.objects.create(**self.contentnode_db_metadata) diff --git a/contentcuration/contentcuration/viewsets/contentnode.py b/contentcuration/contentcuration/viewsets/contentnode.py index b77fafdc6a..cb9e4c9718 100644 --- a/contentcuration/contentcuration/viewsets/contentnode.py +++ b/contentcuration/contentcuration/viewsets/contentnode.py @@ -301,9 +301,11 @@ class Meta: nested_writes = True def validate(self, data): - for tag in data["tags"]: - if len(tag.tag_name) > 30: - raise ValidationError("tag is greater than 30 characters") + tags = data.get("tags") + if tags is not None: + for tag in tags: + if len(tag) > 30: + raise ValidationError("tag is greater than 30 characters") return data def create(self, validated_data):