Add unit test, fix multiple custom content block bug

This commit is contained in:
Ramon Wenger 2020-08-10 19:09:52 +02:00
parent bb50fc75a0
commit 05c43b80fc
2 changed files with 72 additions and 2 deletions

View File

@ -97,9 +97,9 @@ class ChapterNode(DjangoObjectType):
teacher_created_and_visible = Q(Q(user_created=True) & Q(visible_for__in=school_classes))
if user.has_perm('users.can_manage_school_class_content'): # teacher
return by_parent.filter(default_blocks | owned_by_user | teacher_created_and_visible)
return by_parent.filter(default_blocks | owned_by_user | teacher_created_and_visible).distinct()
else: # student
return by_parent.filter(default_blocks | teacher_created_and_visible)
return by_parent.filter(default_blocks | teacher_created_and_visible).distinct()
def resolve_bookmark(self, info, **kwags):
return ChapterBookmark.objects.filter(

View File

@ -0,0 +1,70 @@
from django.test import TestCase, RequestFactory
from graphene.test import Client
from graphql_relay import to_global_id
from api.schema import schema
from api.utils import get_graphql_mutation
from books.factories import ModuleFactory
from books.models import Chapter, ContentBlock
from users.models import User, SchoolClass
from users.services import create_users
class OwnContentTestCase(TestCase):
def setUp(self):
self.module = ModuleFactory()
self.chapter = Chapter(title='Hello')
self.module.add_child(instance=self.chapter)
create_users()
content_block = ContentBlock(title='bla', slug='bla')
self.chapter_id = to_global_id('ChapterNode', self.chapter.id)
self.chapter.specific.add_child(instance=content_block)
self.user = User.objects.get(username='teacher')
school_class2 = SchoolClass.objects.get(name='second_class')
school_class2.users.add(self.user)
school_class2.save()
request = RequestFactory().get('/')
request.user = self.user
self.client = Client(schema=schema, context_value=request)
def test_custom_content_blocks(self):
self.assertEqual(self.user.school_classes.count(), 2)
chapterQuery = """
query ChapterQuery($id: ID!) {
chapter(id: $id) {
id
title
contentBlocks {
edges {
node {
id
title
}
}
}
}
}
"""
result = self.client.execute(chapterQuery, variables={
"id": self.chapter_id
})
self.assertIsNone(result.get('errors'))
self.assertEqual(len(result.get('data').get('chapter').get('contentBlocks').get('edges')), 1)
custom_content_block = ContentBlock(title='own', slug='own', user_created=True, owner=self.user)
self.chapter.specific.add_child(instance=custom_content_block)
result = self.client.execute(chapterQuery, variables={
"id": self.chapter_id
})
self.assertEqual(len(result.get('data').get('chapter').get('contentBlocks').get('edges')), 2)
for school_class in self.user.school_classes.all():
custom_content_block.visible_for.add(school_class)
result = self.client.execute(chapterQuery, variables={
"id": self.chapter_id
})
self.assertEqual(len(result.get('data').get('chapter').get('contentBlocks').get('edges')), 2)