Commit 4510a7ca by Muhammad Shoaib

updated the code and test cases

parent 37334953
...@@ -758,7 +758,7 @@ class TestStudentProctoredExamAttempt(LoggedInTestCase): ...@@ -758,7 +758,7 @@ class TestStudentProctoredExamAttempt(LoggedInTestCase):
attempt_id = response_data['exam_attempt_id'] attempt_id = response_data['exam_attempt_id']
self.assertGreater(attempt_id, 0) self.assertGreater(attempt_id, 0)
# now set the user is_staff t0 false # now set the user is_staff to False
# and also user is not a course staff # and also user is not a course staff
self.user.is_staff = False self.user.is_staff = False
self.user.save() self.user.save()
...@@ -770,7 +770,7 @@ class TestStudentProctoredExamAttempt(LoggedInTestCase): ...@@ -770,7 +770,7 @@ class TestStudentProctoredExamAttempt(LoggedInTestCase):
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
response_data = json.loads(response.content) response_data = json.loads(response.content)
self.assertEqual(response_data['detail'], 'Must be a instructor of the course to Perform this request.') self.assertEqual(response_data['detail'], 'Must be a Staff User to Perform this request.')
def test_read_others_attempt(self): def test_read_others_attempt(self):
""" """
...@@ -1788,7 +1788,7 @@ class TestExamAllowanceView(LoggedInTestCase): ...@@ -1788,7 +1788,7 @@ class TestExamAllowanceView(LoggedInTestCase):
self.user.save() self.user.save()
set_runtime_service('instructor', MockInstructorService(is_user_course_staff=False)) set_runtime_service('instructor', MockInstructorService(is_user_course_staff=False))
# Create an exam. # Create an exam.
proctored_exam = ProctoredExam.objects.create( timed_exam = ProctoredExam.objects.create(
course_id='a/b/c', course_id='a/b/c',
content_id='test_content', content_id='test_content',
exam_name='Test Exam', exam_name='Test Exam',
...@@ -1797,7 +1797,7 @@ class TestExamAllowanceView(LoggedInTestCase): ...@@ -1797,7 +1797,7 @@ class TestExamAllowanceView(LoggedInTestCase):
is_proctored=False is_proctored=False
) )
allowance_data = { allowance_data = {
'exam_id': proctored_exam.id, 'exam_id': timed_exam.id,
'user_info': self.student_taking_exam.username, 'user_info': self.student_taking_exam.username,
'key': 'a_key', 'key': 'a_key',
'value': '30' 'value': '30'
...@@ -1808,7 +1808,7 @@ class TestExamAllowanceView(LoggedInTestCase): ...@@ -1808,7 +1808,7 @@ class TestExamAllowanceView(LoggedInTestCase):
) )
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
response_data = json.loads(response.content) response_data = json.loads(response.content)
self.assertEqual(response_data['detail'], 'Must be a instructor of the course to Perform this request.') self.assertEqual(response_data['detail'], 'Must be a Staff User to Perform this request.')
def test_get_allowances_for_course(self): def test_get_allowances_for_course(self):
""" """
...@@ -1881,7 +1881,7 @@ class TestExamAllowanceView(LoggedInTestCase): ...@@ -1881,7 +1881,7 @@ class TestExamAllowanceView(LoggedInTestCase):
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
response_data = json.loads(response.content) response_data = json.loads(response.content)
self.assertEqual(response_data['detail'], 'Must be a instructor of the course to Perform this request.') self.assertEqual(response_data['detail'], 'Must be a Staff User to Perform this request.')
def test_get_timed_exam_allowances_for_course(self): # pylint: disable=invalid-name def test_get_timed_exam_allowances_for_course(self): # pylint: disable=invalid-name
""" """
......
...@@ -71,38 +71,30 @@ def require_course_or_global_staff(func): ...@@ -71,38 +71,30 @@ def require_course_or_global_staff(func):
course_id = kwargs['course_id'] if 'course_id' in kwargs else None course_id = kwargs['course_id'] if 'course_id' in kwargs else None
exam_id = request.DATA.get('exam_id', None) exam_id = request.DATA.get('exam_id', None)
attempt_id = kwargs['attempt_id'] if 'attempt_id' in kwargs else None attempt_id = kwargs['attempt_id'] if 'attempt_id' in kwargs else None
response_message = _("Must be a instructor of the course to Perform this request.")
if request.user.is_staff: if request.user.is_staff:
is_request_allowed = True return func(request, *args, **kwargs)
elif course_id is not None: else:
if course_id is None:
if exam_id is not None:
exam = ProctoredExam.get_exam_by_id(exam_id)
course_id = exam.course_id
elif attempt_id is not None:
exam_attempt = ProctoredExamStudentAttempt.objects.get_exam_attempt_by_id(attempt_id)
course_id = exam_attempt.proctored_exam.course_id
else:
response_message = _("could not determine the course_id")
return Response(
status=status.HTTP_403_FORBIDDEN,
data={"detail": response_message}
)
if instructor_service.is_course_staff(request.user, course_id): if instructor_service.is_course_staff(request.user, course_id):
is_request_allowed = True return func(request, *args, **kwargs)
else: else:
is_request_allowed = False return Response(
elif exam_id is not None: status=status.HTTP_403_FORBIDDEN,
# get the course_id from the exam data={"detail": _("Must be a Staff User to Perform this request.")}
exam = ProctoredExam.get_exam_by_id(exam_id) )
if instructor_service.is_course_staff(request.user, exam.course_id):
is_request_allowed = True
else:
is_request_allowed = False
elif attempt_id is not None:
exam_attempt = ProctoredExamStudentAttempt.objects.get_exam_attempt_by_id(attempt_id)
if instructor_service.is_course_staff(request.user, exam_attempt.proctored_exam.course_id):
is_request_allowed = True
else:
is_request_allowed = False
else:
is_request_allowed = False
response_message = _("Must be a Staff User to Perform this request.")
if is_request_allowed:
return func(request, *args, **kwargs)
else:
return Response(
status=status.HTTP_403_FORBIDDEN,
data={"detail": response_message}
)
return wrapped return wrapped
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment