diff --git a/edx_exams/apps/api/v1/tests/test_views.py b/edx_exams/apps/api/v1/tests/test_views.py index 6be0f561..462dceb2 100644 --- a/edx_exams/apps/api/v1/tests/test_views.py +++ b/edx_exams/apps/api/v1/tests/test_views.py @@ -1961,7 +1961,7 @@ def test_post_invalid_field_value(self): response = self.request_api('post', self.user, self.exam.course_id, data=request_data) self.assertEqual(response.status_code, 400) - def test_post_invalid_missing_user(self): + def test_post_missing_user(self): """ Test that 400 response is returned if serializer is invalid due to missing required field """ @@ -1971,6 +1971,16 @@ def test_post_invalid_missing_user(self): response = self.request_api('post', self.user, self.exam.course_id, data=request_data) self.assertEqual(response.status_code, 400) + def test_post_invalid_user(self): + """ + Test that 400 response is returned if a username/email does not exist + """ + request_data = [ + {'exam_id': self.exam.id, 'username': 'junk', 'extra_time_mins': 45}, + ] + response = self.request_api('post', self.user, self.exam.course_id, data=request_data) + self.assertEqual(response.status_code, 400) + def test_post_unauthorized_course(self): """ Test that an error is returned when atttempting to create an allowance for diff --git a/edx_exams/apps/api/v1/views.py b/edx_exams/apps/api/v1/views.py index 346c316c..9a07dff8 100644 --- a/edx_exams/apps/api/v1/views.py +++ b/edx_exams/apps/api/v1/views.py @@ -872,6 +872,11 @@ def post(self, request, course_id): status=status.HTTP_400_BAD_REQUEST, data={'detail': 'Exam does not exist'} ) + except User.DoesNotExist: + return Response( + status=status.HTTP_400_BAD_REQUEST, + data={'detail': 'Learner with username/email not found'} + ) StudentAllowance.bulk_create_or_update(allowance_objects)