From 8201ca5e777f6933a8329aab777b86e33b09f843 Mon Sep 17 00:00:00 2001 From: Sarina Canelake Date: Thu, 20 Jun 2013 18:27:45 -0400 Subject: [PATCH] Fix 500 error on reactivation email --- common/djangoapps/student/tests/test_email.py | 25 ++++++++++++++----- common/djangoapps/student/views.py | 18 +++++++------ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/common/djangoapps/student/tests/test_email.py b/common/djangoapps/student/tests/test_email.py index 3b31bb5c28..7e2d9ede00 100644 --- a/common/djangoapps/student/tests/test_email.py +++ b/common/djangoapps/student/tests/test_email.py @@ -55,11 +55,15 @@ class ReactivationEmailTests(EmailTestMixin, TestCase): def setUp(self): self.user = UserFactory.create() + self.unregisteredUser = UserFactory.create() self.registration = RegistrationFactory.create(user=self.user) - def reactivation_email(self): - """Send the reactivation email, and return the response as json data""" - return json.loads(reactivation_email_for_user(self.user).content) + def reactivation_email(self, user): + """ + Send the reactivation email to the specified user, + and return the response as json data. + """ + return json.loads(reactivation_email_for_user(user).content) def assertReactivateEmailSent(self, email_user): """Assert that the correct reactivation email has been sent""" @@ -78,13 +82,22 @@ class ReactivationEmailTests(EmailTestMixin, TestCase): def test_reactivation_email_failure(self, email_user): self.user.email_user.side_effect = Exception - response_data = self.reactivation_email() + response_data = self.reactivation_email(self.user) self.assertReactivateEmailSent(email_user) self.assertFalse(response_data['success']) + def test_reactivation_for_unregistered_user(self, email_user): + """ + Test that trying to send a reactivation email to an unregistered + user fails without throwing a 500 error. + """ + response_data = self.reactivation_email(self.unregisteredUser) + + self.assertFalse(response_data['success']) + def test_reactivation_email_success(self, email_user): - response_data = self.reactivation_email() + response_data = self.reactivation_email(self.user) self.assertReactivateEmailSent(email_user) self.assertTrue(response_data['success']) @@ -150,7 +163,7 @@ class EmailChangeRequestTests(TestCase): self.check_duplicate_email(self.new_email) def test_capitalized_duplicate_email(self): - raise SkipTest("We currently don't check for emails in a case insensitive way, but we should") + """Test that we check for email addresses in a case insensitive way""" UserFactory.create(email=self.new_email) self.check_duplicate_email(self.new_email.capitalize()) diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index 4da7b9d789..135ae59752 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -174,7 +174,7 @@ def _cert_info(user, course, cert_status): CertificateStatuses.downloadable: 'ready', CertificateStatuses.notpassing: 'notpassing', CertificateStatuses.restricted: 'restricted', - } + } status = template_state.get(cert_status['status'], default_status) @@ -183,10 +183,10 @@ def _cert_info(user, course, cert_status): 'show_disabled_download_button': status == 'generating', } if (status in ('generating', 'ready', 'notpassing', 'restricted') and - course.end_of_course_survey_url is not None): + course.end_of_course_survey_url is not None): d.update({ - 'show_survey_button': True, - 'survey_url': process_survey_link(course.end_of_course_survey_url, user)}) + 'show_survey_button': True, + 'survey_url': process_survey_link(course.end_of_course_survey_url, user)}) else: d['show_survey_button'] = False @@ -881,8 +881,8 @@ def get_random_post_override(): 'password': id_generator(), 'name': (id_generator(size=5, chars=string.ascii_lowercase) + " " + id_generator(size=7, chars=string.ascii_lowercase)), - 'honor_code': u'true', - 'terms_of_service': u'true', } + 'honor_code': u'true', + 'terms_of_service': u'true', } def create_random_account(create_account_function): @@ -967,7 +967,11 @@ def reactivation_email(request): def reactivation_email_for_user(user): - reg = Registration.objects.get(user=user) + try: + reg = Registration.objects.get(user=user) + except Registration.DoesNotExist: + return HttpResponse(json.dumps({'success': False, + 'error': 'No inactive user with this e-mail exists'})) d = {'name': user.profile.name, 'key': reg.activation_key}