From a4479727ce578b008e11ed8d2274304c91e5091b Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Tue, 16 Oct 2018 13:52:40 +0200 Subject: [PATCH] Add another unit test --- .../tests/test_assignment_permissions.py | 36 +++++++++---------- server/users/services.py | 4 ++- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/server/assignments/tests/test_assignment_permissions.py b/server/assignments/tests/test_assignment_permissions.py index fcd3f74f..f45fff12 100644 --- a/server/assignments/tests/test_assignment_permissions.py +++ b/server/assignments/tests/test_assignment_permissions.py @@ -1,6 +1,4 @@ -from unittest import TestCase - -from django.test import RequestFactory +from django.test import RequestFactory, TestCase from graphene.test import Client from graphql_relay import to_global_id @@ -21,6 +19,7 @@ class AssignmentPermissionsTestCase(TestCase): self.teacher2 = User.objects.get(username='teacher2') self.student1 = User.objects.get(username='student1') self.student2 = User.objects.get(username='student2') + self.student_second_class = User.objects.get(username='student_second_class') self.assignment = AssignmentFactory( owner=self.teacher ) @@ -28,24 +27,12 @@ class AssignmentPermissionsTestCase(TestCase): self.assignment_id = to_global_id('AssignmentNode', self.assignment.pk) self.module_id = to_global_id('ModuleNode', self.assignment.module.pk) - """ - to test: - create assignment - should be visible to teachers - should be visible to students - student1 submits result - teacher1 should see result - student1 should see result - student2 should not see result - teacher2 should not see result - """ - def _create_client(self, user): request = RequestFactory().get('/') request.user = user return Client(schema=schema, context_value=request) - def _submit_submission(self): + def _submit_submission(self, user=None): mutation = ''' mutation UpdateAssignment($input: UpdateAssignmentInput!) { updateAssignment(input: $input){ @@ -65,7 +52,10 @@ class AssignmentPermissionsTestCase(TestCase): ''' - client = self._create_client(self.student1) + if user is None: + client = self._create_client(self.student1) + else: + client = self._create_client(user) return client.execute(mutation, variables={ 'input': { @@ -92,7 +82,6 @@ class AssignmentPermissionsTestCase(TestCase): self.assertEqual(StudentSubmission.objects.count(), 1) def _test_visibility(self, user, count): - self._submit_submission() client = self._create_client(user) query = ''' query AssignmentWithSubmissions($id: ID!) { @@ -117,15 +106,24 @@ class AssignmentPermissionsTestCase(TestCase): self.assertIsNone(result.get('errors')) self.assertEqual(len(result.get('data').get('assignment').get('submissions')), count) - def test_visible_for_teacher(self): + self._submit_submission() self._test_visibility(self.teacher, 1) def test_visible_for_teacher2(self): + self._submit_submission() self._test_visibility(self.teacher2, 0) def test_visible_for_student1(self): + self._submit_submission() self._test_visibility(self.student1, 0) def test_visible_for_student2(self): + self._submit_submission() self._test_visibility(self.student2, 0) + + def test_advanced_visibility(self): + self._submit_submission(self.student1) + self._submit_submission(self.student_second_class) + self._test_visibility(self.teacher, 1) + self._test_visibility(self.teacher2, 1) diff --git a/server/users/services.py b/server/users/services.py index 0c4d43c0..60b796fc 100644 --- a/server/users/services.py +++ b/server/users/services.py @@ -26,8 +26,10 @@ def create_users(data=None): ) teacher2 = UserFactory(username='teacher2') UserRole.objects.create(user=teacher2, role=teacher_role) + student_second_class = UserFactory(username='student_second_class') + UserRole.objects.create(user=student_second_class, role=student_role) SchoolClassFactory( - users=[teacher2], + users=[teacher2, student_second_class], year='2018', name='second_class' )