Skip to content

Commit

Permalink
fix: tests for validations
Browse files Browse the repository at this point in the history
  • Loading branch information
vkWeb committed Mar 3, 2022
1 parent b9a6762 commit 2cb48b0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
27 changes: 17 additions & 10 deletions contentcuration/contentcuration/tests/views/test_views_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,13 @@ 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": [
valid_node,
invalid_title_node,
invalid_license_description,
invalid_copyright_holder,
invalid_tag_length,
],
}
self.resp = self.admin_client().post(
Expand Down Expand Up @@ -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):
Expand Down
16 changes: 16 additions & 0 deletions contentcuration/contentcuration/tests/viewsets/test_contentnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions contentcuration/contentcuration/viewsets/contentnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 2cb48b0

Please sign in to comment.