Skip to content
Snippets Groups Projects
Commit 898db2e3 authored by Tasawer Nawaz's avatar Tasawer Nawaz
Browse files

update CourseGoalViewSet to return status=400 if no goal key is provided LEARNER-3342

parent fb5ca89f
No related merge requests found
......@@ -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
......@@ -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,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment