Add another unit test

This commit is contained in:
Ramon Wenger 2018-10-16 13:52:40 +02:00
parent 5218a3a867
commit a4479727ce
2 changed files with 20 additions and 20 deletions

View File

@ -1,6 +1,4 @@
from unittest import TestCase from django.test import RequestFactory, TestCase
from django.test import RequestFactory
from graphene.test import Client from graphene.test import Client
from graphql_relay import to_global_id from graphql_relay import to_global_id
@ -21,6 +19,7 @@ class AssignmentPermissionsTestCase(TestCase):
self.teacher2 = User.objects.get(username='teacher2') self.teacher2 = User.objects.get(username='teacher2')
self.student1 = User.objects.get(username='student1') self.student1 = User.objects.get(username='student1')
self.student2 = User.objects.get(username='student2') self.student2 = User.objects.get(username='student2')
self.student_second_class = User.objects.get(username='student_second_class')
self.assignment = AssignmentFactory( self.assignment = AssignmentFactory(
owner=self.teacher owner=self.teacher
) )
@ -28,24 +27,12 @@ class AssignmentPermissionsTestCase(TestCase):
self.assignment_id = to_global_id('AssignmentNode', self.assignment.pk) self.assignment_id = to_global_id('AssignmentNode', self.assignment.pk)
self.module_id = to_global_id('ModuleNode', self.assignment.module.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): def _create_client(self, user):
request = RequestFactory().get('/') request = RequestFactory().get('/')
request.user = user request.user = user
return Client(schema=schema, context_value=request) return Client(schema=schema, context_value=request)
def _submit_submission(self): def _submit_submission(self, user=None):
mutation = ''' mutation = '''
mutation UpdateAssignment($input: UpdateAssignmentInput!) { mutation UpdateAssignment($input: UpdateAssignmentInput!) {
updateAssignment(input: $input){ 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={ return client.execute(mutation, variables={
'input': { 'input': {
@ -92,7 +82,6 @@ class AssignmentPermissionsTestCase(TestCase):
self.assertEqual(StudentSubmission.objects.count(), 1) self.assertEqual(StudentSubmission.objects.count(), 1)
def _test_visibility(self, user, count): def _test_visibility(self, user, count):
self._submit_submission()
client = self._create_client(user) client = self._create_client(user)
query = ''' query = '''
query AssignmentWithSubmissions($id: ID!) { query AssignmentWithSubmissions($id: ID!) {
@ -117,15 +106,24 @@ class AssignmentPermissionsTestCase(TestCase):
self.assertIsNone(result.get('errors')) self.assertIsNone(result.get('errors'))
self.assertEqual(len(result.get('data').get('assignment').get('submissions')), count) self.assertEqual(len(result.get('data').get('assignment').get('submissions')), count)
def test_visible_for_teacher(self): def test_visible_for_teacher(self):
self._submit_submission()
self._test_visibility(self.teacher, 1) self._test_visibility(self.teacher, 1)
def test_visible_for_teacher2(self): def test_visible_for_teacher2(self):
self._submit_submission()
self._test_visibility(self.teacher2, 0) self._test_visibility(self.teacher2, 0)
def test_visible_for_student1(self): def test_visible_for_student1(self):
self._submit_submission()
self._test_visibility(self.student1, 0) self._test_visibility(self.student1, 0)
def test_visible_for_student2(self): def test_visible_for_student2(self):
self._submit_submission()
self._test_visibility(self.student2, 0) 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)

View File

@ -26,8 +26,10 @@ def create_users(data=None):
) )
teacher2 = UserFactory(username='teacher2') teacher2 = UserFactory(username='teacher2')
UserRole.objects.create(user=teacher2, role=teacher_role) 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( SchoolClassFactory(
users=[teacher2], users=[teacher2, student_second_class],
year='2018', year='2018',
name='second_class' name='second_class'
) )