from django.test import TestCase, RequestFactory from graphene.test import Client from graphql_relay import to_global_id, from_global_id from api.schema import schema from books.factories import ModuleFactory, ChapterFactory, ContentBlockFactory from users.models import User, SchoolClass from users.services import create_users MODULE_QUERY = """ query ModulesQuery($slug: String!) { module(slug: $slug) { id title chapters { edges { node { id contentBlocks { edges { node { id visibleFor { edges { node { name } } } hiddenFor { edges { node { name } } } } } } } } } } } """ CREATE_SNAPSHOT_MUTATION = """ mutation CreateSnapshot($input: CreateSnapshotInput!) { createSnapshot(input: $input) { snapshot { id chapters { edges { node { id descriptionHidden titleHidden title description contentBlocks { edges { node { id title } } } } } } } success } } """ def edges_to_array(entity): return [edge['node'] for edge in entity.get('edges')] class CreateSnapshotTestCase(TestCase): def setUp(self): create_users() self.skillbox_class = SchoolClass.objects.get(name='skillbox') second_class = SchoolClass.objects.get(name='second_class') # teacher will create snapshot self.teacher = User.objects.get(username='teacher') self.module = ModuleFactory(slug='some-module') # module M has a chapter self.chapter = ChapterFactory(parent=self.module, slug='some-chapter') # chapter has some content blocks a, b, c self.title_visible = 'visible' self.title_hidden = 'hidden' self.title_custom = 'custom' self.visible_content_block = ContentBlockFactory(parent=self.chapter, module=self.module, title=self.title_visible, slug='cb-a') self.hidden_content_block = ContentBlockFactory(parent=self.chapter, module=self.module, title=self.title_hidden, slug='cb-b') # content block c is user created self.custom_content_block = ContentBlockFactory(parent=self.chapter, owner=self.teacher, user_created=True, module=self.module, title=self.title_custom, slug='cb-c') # content block a and c are visible to school class X self.hidden_content_block.hidden_for.add(self.skillbox_class) self.custom_content_block.visible_for.add(self.skillbox_class) # chapter description is hidden for school class X self.chapter.title_hidden_for.add(self.skillbox_class) request = RequestFactory().get('/') request.user = self.teacher self.client = Client(schema=schema, context_value=request) # we make a snapshot S of the module M # snapshot S looks like module M for school class X def test_setup(self): # make sure everything is setup correctly result = self.client.execute(MODULE_QUERY, variables={ 'slug': self.module.slug }) self.assertIsNone(result.get('errors')) module = result.get('data').get('module') chapter = edges_to_array(module.get('chapters'))[0] self.assertIsNotNone(chapter) content_blocks = edges_to_array(chapter.get('contentBlocks')) content_block_ids = [node['id'] for node in content_blocks] self.assertTrue(to_global_id('ContentBlockNode', self.visible_content_block.id) in content_block_ids) self.assertTrue(to_global_id('ContentBlockNode', self.hidden_content_block.id) in content_block_ids) self.assertTrue(to_global_id('ContentBlockNode', self.custom_content_block.id) in content_block_ids) b = [node for node in content_blocks if node['id'] == to_global_id('ContentBlockNode', self.hidden_content_block.id)][0] c = [node for node in content_blocks if node['id'] == to_global_id('ContentBlockNode', self.custom_content_block.id)][0] self.assertTrue('skillbox' in [school_class['name'] for school_class in edges_to_array(b.get('hiddenFor'))]) self.assertTrue('skillbox' in [school_class['name'] for school_class in edges_to_array(c.get('visibleFor'))]) def test_create_snapshot(self): result = self.client.execute(CREATE_SNAPSHOT_MUTATION, variables={ 'input': { 'module': to_global_id('ContentBlockNode', self.module.pk), 'selectedClass': to_global_id('SchoolClassNode', self.skillbox_class.pk), } }) self.assertIsNone(result.get('errors')) snapshot = result.get('data').get('createSnapshot').get('snapshot') chapter = snapshot.get('chapters').get('edges')[0]['node'] self.assertTrue(chapter['titleHidden']) self.assertFalse(chapter['descriptionHidden']) _, chapter_id = from_global_id(chapter['id']) self.assertEqual(int(chapter_id), self.chapter.id) content_blocks = [edge['node'] for edge in chapter['contentBlocks']['edges']] self.assertEqual(len(content_blocks), 2) self.assertEqual(content_blocks[0]['title'], self.title_visible) self.assertEqual(content_blocks[1]['title'], self.title_custom)