130 lines
4.2 KiB
Python
130 lines
4.2 KiB
Python
from django.test import RequestFactory, TestCase
|
|
from graphene.test import Client
|
|
from graphql_relay import to_global_id
|
|
|
|
from api.utils import get_graphql_mutation
|
|
from assignments.models import Assignment, StudentSubmission
|
|
from books.factories import ModuleFactory
|
|
from ..factories import AssignmentFactory
|
|
from users.models import User
|
|
from users.services import create_users
|
|
from api.schema import schema
|
|
|
|
|
|
class AssignmentPermissionsTestCase(TestCase):
|
|
def setUp(self):
|
|
create_users()
|
|
|
|
self.teacher = User.objects.get(username='teacher')
|
|
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
|
|
)
|
|
|
|
self.assignment_id = to_global_id('AssignmentNode', self.assignment.pk)
|
|
self.module_id = to_global_id('ModuleNode', self.assignment.module.pk)
|
|
|
|
def _create_client(self, user):
|
|
request = RequestFactory().get('/')
|
|
request.user = user
|
|
return Client(schema=schema, context_value=request)
|
|
|
|
def _submit_submission(self, user=None):
|
|
mutation = '''
|
|
mutation UpdateAssignment($input: UpdateAssignmentInput!) {
|
|
updateAssignment(input: $input){
|
|
updatedAssignment {
|
|
id
|
|
title
|
|
assignment
|
|
submission {
|
|
id
|
|
text
|
|
final
|
|
document
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
'''
|
|
|
|
if user is None:
|
|
client = self._create_client(self.student1)
|
|
else:
|
|
client = self._create_client(user)
|
|
|
|
return client.execute(mutation, variables={
|
|
'input': {
|
|
"assignment": {
|
|
"id": self.assignment_id,
|
|
"answer": 'Halo',
|
|
"final": True
|
|
}
|
|
}
|
|
})
|
|
|
|
def test_permissions(self):
|
|
self.assertTrue(self.teacher.has_perm('users.can_manage_school_class_content'))
|
|
self.assertTrue(self.teacher2.has_perm('users.can_manage_school_class_content'))
|
|
self.assertFalse(self.student1.has_perm('users.can_manage_school_class_content'))
|
|
self.assertFalse(self.student2.has_perm('users.can_manage_school_class_content'))
|
|
|
|
def test_count(self):
|
|
self.assertEqual(Assignment.objects.count(), 1)
|
|
|
|
def test_submit_submission(self):
|
|
result = self._submit_submission()
|
|
self.assertIsNone(result.get('errors'))
|
|
self.assertEqual(StudentSubmission.objects.count(), 1)
|
|
|
|
def _test_visibility(self, user, count):
|
|
client = self._create_client(user)
|
|
query = '''
|
|
query AssignmentWithSubmissions($id: ID!) {
|
|
assignment(id: $id) {
|
|
title
|
|
submissions {
|
|
id
|
|
text
|
|
document
|
|
student {
|
|
firstName
|
|
lastName
|
|
}
|
|
}
|
|
}
|
|
}
|
|
'''
|
|
result = client.execute(query, variables={
|
|
'id': self.assignment_id
|
|
})
|
|
|
|
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)
|