From 92f53236cf213f8bfb930283341f710f9b45486f Mon Sep 17 00:00:00 2001 From: Adam Palay Date: Mon, 30 Jun 2014 16:19:13 -0400 Subject: [PATCH] fix 400s from change_enrollment causing 404s (LMS-6625) --- common/djangoapps/student/tests/test_login.py | 24 ++++++++++++++++++- common/djangoapps/student/views.py | 2 -- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/common/djangoapps/student/tests/test_login.py b/common/djangoapps/student/tests/test_login.py index 7d3201ca8b..b3c7e7b655 100644 --- a/common/djangoapps/student/tests/test_login.py +++ b/common/djangoapps/student/tests/test_login.py @@ -11,6 +11,7 @@ from django.test.utils import override_settings from django.conf import settings from django.core.cache import cache from django.core.urlresolvers import reverse, NoReverseMatch +from django.http import HttpResponseBadRequest from student.tests.factories import UserFactory, RegistrationFactory, UserProfileFactory from student.views import _parse_course_id_from_string, _get_course_enrollment_domain @@ -206,9 +207,30 @@ class LoginTest(TestCase): # client1 will be logged out self.assertEqual(response.status_code, 302) - def _login_response(self, email, password, patched_audit_log='student.views.AUDIT_LOG'): + def test_change_enrollment_400(self): + """ + Tests that a 400 in change_enrollment doesn't lead to a 400 + and in fact just redirects the user to the dashboard + without incident. + """ + # add these post params to trigger a call to change_enrollment + extra_post_params = {"enrollment_action": "enroll"} + with patch('student.views.change_enrollment') as mock_change_enrollment: + mock_change_enrollment.return_value = HttpResponseBadRequest("I am a 400") + response, _ = self._login_response( + 'test@edx.org', + 'test_password', + extra_post_params=extra_post_params, + ) + response_content = json.loads(response.content) + self.assertIsNone(response_content["redirect_url"]) + self._assert_response(response, success=True) + + def _login_response(self, email, password, patched_audit_log='student.views.AUDIT_LOG', extra_post_params=None): ''' Post the login info ''' post_params = {'email': email, 'password': password} + if extra_post_params is not None: + post_params.update(extra_post_params) with patch(patched_audit_log) as mock_audit_log: result = self.client.post(self.url, post_params) return result, mock_audit_log diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index 941069c316..7f411ddf5e 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -559,8 +559,6 @@ def try_change_enrollment(request): enrollment_response.content ) ) - if enrollment_response.content != '': - return enrollment_response.content except Exception, e: log.exception("Exception automatically enrolling after login: {0}".format(str(e)))