ECOM-763 handling InvalidKeyError exception.

This commit is contained in:
Awais
2015-01-29 17:19:52 +05:00
parent a260579af9
commit f3bf66c1c0
4 changed files with 45 additions and 1 deletions

View File

@@ -1533,3 +1533,17 @@ class UpdateEmailOptInTestCase(ApiTestCase):
user=self.user, org=self.course.id.org, key="email-optin"
)
self.assertEquals(preference.value, u"True")
def test_update_email_opt_with_invalid_course_key(self):
"""
Test that with invalid key it returns bad request
and not update their email optin preference.
"""
response = self.client.post(self.url, {
"course_id": 'invalid',
"email_opt_in": u"True"
})
self.assertHttpBadRequest(response)
with self.assertRaises(UserOrgTag.DoesNotExist):
UserOrgTag.objects.get(user=self.user, org=self.course.id.org, key="email-optin")

View File

@@ -1,5 +1,6 @@
"""HTTP end-points for the User API. """
import copy
from opaque_keys import InvalidKeyError
import third_party_auth
from django.conf import settings
@@ -862,7 +863,14 @@ class UpdateEmailOptInPreference(APIView):
"""
course_id = request.DATA['course_id']
org = locator.CourseLocator.from_string(course_id).org
try:
org = locator.CourseLocator.from_string(course_id).org
except InvalidKeyError:
return HttpResponse(
status=400,
content="No course '{course_id}' found".format(course_id=course_id),
content_type="text/plain"
)
# Only check for true. All other values are False.
email_opt_in = request.DATA['email_opt_in'].lower() == 'true'
profile_api.update_email_opt_in(request.user, org, email_opt_in)