From 45b341422620cdfa07b326932c960f75664e47f5 Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Mon, 14 Jun 2021 11:30:14 +0200 Subject: [PATCH] Reset chapter visibilities --- .../cypress/integration/e2e/bookmarks.spec.js | 1 - client/cypress/support/commands.js | 4 +- client/package.json | 12 ++-- server/books/models/snapshot.py | 3 + server/books/tests/queries.py | 2 + server/books/tests/test_snapshots.py | 69 ++++++++++++++----- 6 files changed, 67 insertions(+), 24 deletions(-) diff --git a/client/cypress/integration/e2e/bookmarks.spec.js b/client/cypress/integration/e2e/bookmarks.spec.js index 2d76b064..665e1598 100644 --- a/client/cypress/integration/e2e/bookmarks.spec.js +++ b/client/cypress/integration/e2e/bookmarks.spec.js @@ -4,7 +4,6 @@ describe('Bookmarks', () => { cy.exec('python ../server/manage.py prepare_bookmarks_for_cypress'); cy.viewport('macbook-15'); - cy.startGraphQLCapture(); cy.apolloLogin('rahel.cueni', 'test'); }); diff --git a/client/cypress/support/commands.js b/client/cypress/support/commands.js index 1f153f78..4c44718d 100644 --- a/client/cypress/support/commands.js +++ b/client/cypress/support/commands.js @@ -54,11 +54,11 @@ Cypress.Commands.add('login', (username, password, visitLogin = false) => { cy.visit('/beta-login'); } - if (username != '') { + if (username !== '') { cy.get('[data-cy=email-input]').type(username); } - if (password != '') { + if (password !== '') { cy.get('[data-cy=password-input]').type(password); } cy.get('[data-cy=login-button]').click(); diff --git a/client/package.json b/client/package.json index 781d261f..80b9fde6 100644 --- a/client/package.json +++ b/client/package.json @@ -10,10 +10,14 @@ "lint": "eslint --ext .js,.vue src", "fix-lint": "eslint --ext .js,.vue --fix src", "build": "node build/build.js", - "open:cypress:e2e": "cypress open --config-file cypress.e2e.json", - "open:cypress:frontend": "cypress open --config-file cypress.frontend.json", - "test:cypress:e2e": "cypress run --config-file cypress.e2e.json", - "test:cypress:frontend": "cypress run --config-file cypress.frontend.json", + "open:cypress:e2e": "npm run cypress:e2e:open", + "open:cypress:frontend": "npm run cypress:frontend:open", + "test:cypress:e2e": "npm run cypress:e2e:test", + "test:cypress:frontend": "npm run cypress:frontend:test", + "cypress:e2e:open": "cypress open --config-file cypress.e2e.json", + "cypress:frontend:open": "cypress open --config-file cypress.frontend.json", + "cypress:e2e:test": "cypress run --config-file cypress.e2e.json", + "cypress:frontend:test": "cypress run --config-file cypress.frontend.json", "install:cypress": "cypress install", "test:unit": "jest" }, diff --git a/server/books/models/snapshot.py b/server/books/models/snapshot.py index 3fa5276b..8f934032 100644 --- a/server/books/models/snapshot.py +++ b/server/books/models/snapshot.py @@ -127,6 +127,9 @@ class Snapshot(models.Model): without_owner = Q(owner__isnull=True) no_snapshot = Q(contentblocksnapshot__isnull=True) owner_user = Q(owner=user) + chapter.title_hidden_for.remove(selected_class) + chapter.description_hidden_for.remove(selected_class) + for content_block in qs.filter(without_owner & no_snapshot): content_block.hidden_for.remove(selected_class) for content_block in qs.filter(owner_user): diff --git a/server/books/tests/queries.py b/server/books/tests/queries.py index f5532264..69df53cc 100644 --- a/server/books/tests/queries.py +++ b/server/books/tests/queries.py @@ -17,6 +17,8 @@ query ModulesQuery($slug: String, $id: ID) { } chapters { id + titleHiddenFor { name } + descriptionHiddenFor { name } contentBlocks { id title diff --git a/server/books/tests/test_snapshots.py b/server/books/tests/test_snapshots.py index d8037240..19a93d9c 100644 --- a/server/books/tests/test_snapshots.py +++ b/server/books/tests/test_snapshots.py @@ -35,6 +35,9 @@ class CreateSnapshotTestCase(SkillboxTestCase): self.module = ModuleFactory(slug=self.slug) self.skillbox_class = SchoolClass.objects.get(name='skillbox') + self.teacher2 = User.objects.get(username='teacher2') + self.second_class_name = 'second_class' + self.second_class = SchoolClass.objects.get(name=self.second_class_name) # module M has a chapter self.chapter = ChapterFactory(parent=self.module, slug='some-chapter') @@ -62,7 +65,7 @@ class CreateSnapshotTestCase(SkillboxTestCase): 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 + # chapter title is hidden for school class X self.chapter.title_hidden_for.add(self.skillbox_class) # we make a snapshot S of the module M @@ -196,20 +199,14 @@ class CreateSnapshotTestCase(SkillboxTestCase): user=self.teacher) self.assertEqual(Snapshot.objects.count(), 1) self.assertEqual(self.snapshot.custom_objectives.count(), 2) - school_class_name = 'second_class' - second_class = SchoolClass.objects.get(name=school_class_name) - request = RequestFactory().get('/') - teacher2 = User.objects.get(username='teacher2') - request.user = teacher2 - client = Client(schema=schema, context_value=request) - result = client.execute(APPLY_SNAPSHOT_MUTATION, variables={ + result = self.get_client(self.teacher2).execute(APPLY_SNAPSHOT_MUTATION, variables={ 'input': { 'snapshot': to_global_id('SnapshotNode', self.snapshot.pk), - 'selectedClass': to_global_id('SchoolClassNode', second_class.pk), + 'selectedClass': to_global_id('SchoolClassNode', self.second_class.pk), } }) self.assertIsNone(result.get('errors')) - module = self._test_module_visibility(client, school_class_name) + module = self._test_module_visibility(self.get_client(self.teacher2), self.second_class_name) original_creator = module['chapters'][0]['contentBlocks'][2].get('originalCreator') self.assertIsNotNone(original_creator) self.assertEqual(original_creator.get('id'), to_global_id('PublicUserNode', self.teacher.pk)) @@ -249,15 +246,13 @@ class CreateSnapshotTestCase(SkillboxTestCase): self.assertEqual(changes['hiddenObjectives'], 1) def test_apply_initial_snapshot(self): - teacher2 = User.objects.get(username='teacher2') - teacher2_client = self.get_client(user=teacher2) third_class = SchoolClassFactory( - users=[teacher2], + users=[self.teacher2], name='third_class' ) # make a neutral snapshot, nothing new, nothing hidden - result = teacher2_client.execute(CREATE_SNAPSHOT_MUTATION, variables={ + result = self.get_client(user=self.teacher2).execute(CREATE_SNAPSHOT_MUTATION, variables={ 'input': { 'module': self.slug, 'selectedClass': to_global_id('SchoolClassNode', third_class.pk), @@ -305,12 +300,10 @@ class CreateSnapshotTestCase(SkillboxTestCase): teacher2 = User.objects.get(username='teacher2') teacher2_client = self.get_client(user=teacher2) - school_class_name = 'second_class' - second_class = SchoolClass.objects.get(name=school_class_name) result = teacher2_client.execute(APPLY_SNAPSHOT_MUTATION, variables={ 'input': { 'snapshot': snapshot_id, - 'selectedClass': to_global_id('SchoolClassNode', second_class.pk), + 'selectedClass': to_global_id('SchoolClassNode', self.second_class.pk), } }) self.assertIsNone(result.get('errors')) @@ -323,6 +316,48 @@ class CreateSnapshotTestCase(SkillboxTestCase): self.assertEqual(len(chapter['contentBlocks']), 4) + def test_snapshot_chapter_visibility_after_apply(self): + self.chapter.title_hidden_for.remove(self.skillbox_class) + self.chapter.description_hidden_for.remove(self.skillbox_class) + + self.chapter.title_hidden_for.add(self.second_class) + self.chapter.description_hidden_for.add(self.second_class) + + def assert_chapter_hidden(hidden): + result = self.get_client(self.teacher2).execute(MODULE_QUERY, variables={ + 'slug': self.module.slug + }) + self.assertIsNone(result.get('errors')) + chapter = result['data']['module']['chapters'][0] + self.assertEqual(self.second_class_name in map(lambda x: x['name'], chapter['titleHiddenFor']), hidden) + self.assertEqual(self.second_class_name in map(lambda x: x['name'], chapter['descriptionHiddenFor']), hidden) + + assert_chapter_hidden(True) + + result = self.graphene_client.execute(CREATE_SNAPSHOT_MUTATION, variables={ + 'input': { + 'module': self.slug, + 'selectedClass': to_global_id('SchoolClassNode', self.skillbox_class.pk), + } + }) + self.assertIsNone(result.get('errors')) + snapshot = result['data']['createSnapshot']['snapshot'] + snapshot_id = snapshot['id'] + chapter = snapshot['chapters'][0] + self.assertEqual(chapter['titleHidden'], False) + self.assertEqual(chapter['descriptionHidden'], False) + + result = self.get_client(self.teacher2).execute(APPLY_SNAPSHOT_MUTATION, variables={ + 'input': { + 'snapshot': snapshot_id, + 'selectedClass': to_global_id('SchoolClassNode', self.second_class.pk), + } + }) + self.assertIsNone(result.get('errors')) + + assert_chapter_hidden(False) + + class SnapshotTestCase(SkillboxTestCase): def setUp(self) -> None: self.createDefault()