diff --git a/lms/djangoapps/course_goals/tests/test_api.py b/lms/djangoapps/course_goals/tests/test_api.py index bb9635c6aa..417dfbb8e1 100644 --- a/lms/djangoapps/course_goals/tests/test_api.py +++ b/lms/djangoapps/course_goals/tests/test_api.py @@ -55,6 +55,17 @@ class TestCourseGoalsAPI(EventTrackingTestCase, SharedModuleStoreTestCase): self.assertEqual(response.status_code, 400) self.assertEqual(len(CourseGoal.objects.filter(user=self.user, course_key=self.course.id)), 0) + def test_add_without_goal_key(self): + """ Ensures if no goal key provided, post does not succeed. """ + + response = self.post_course_goal(goal_key=None) + self.assertEqual(len(CourseGoal.objects.filter(user=self.user, course_key=self.course.id)), 0) + self.assertContains( + response=response, + text='Please provide a valid goal key from following options.', + status_code=400 + ) + @mock.patch('lms.djangoapps.course_goals.views.update_google_analytics') @override_settings(LMS_SEGMENT_KEY="foobar") def test_update_goal(self, ga_call): @@ -74,12 +85,12 @@ class TestCourseGoalsAPI(EventTrackingTestCase, SharedModuleStoreTestCase): Sends a post request to set a course goal and returns the response. """ goal_key = goal_key if valid else 'invalid' - response = self.client.post( - self.apiUrl, - { - 'goal_key': goal_key, - 'course_key': self.course.id, - 'user': self.user.username, - }, - ) + post_data = { + 'course_key': self.course.id, + 'user': self.user.username, + } + if goal_key: + post_data['goal_key'] = goal_key + + response = self.client.post(self.apiUrl, post_data) return response diff --git a/lms/djangoapps/course_goals/views.py b/lms/djangoapps/course_goals/views.py index 35e22999ae..89628cca6f 100644 --- a/lms/djangoapps/course_goals/views.py +++ b/lms/djangoapps/course_goals/views.py @@ -61,8 +61,15 @@ class CourseGoalViewSet(viewsets.ModelViewSet): """ Create a new goal if one does not exist, otherwise update the existing goal. """ # Ensure goal_key is valid goal_options = get_course_goal_options() - goal_key = post_data.data['goal_key'] - if goal_key not in goal_options: + goal_key = post_data.data.get('goal_key') + if not goal_key: + return Response( + 'Please provide a valid goal key from following options. (options= {goal_options}).'.format( + goal_options=goal_options, + ), + status=status.HTTP_400_BAD_REQUEST, + ) + elif goal_key not in goal_options: return Response( 'Provided goal key, {goal_key}, is not a valid goal key (options= {goal_options}).'.format( goal_key=goal_key,