From 84463ef1a5c6c4134c6cbaaf1ef9ad4a6b42366d Mon Sep 17 00:00:00 2001 From: Will Daly Date: Thu, 25 Sep 2014 09:33:56 -0400 Subject: [PATCH] Added test for email activation and confirmation --- .../student_account/test/test_views.py | 42 ++++++++++++++++++- .../message_body.txt | 2 +- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/lms/djangoapps/student_account/test/test_views.py b/lms/djangoapps/student_account/test/test_views.py index 7d8e587a84..c3e522e18e 100644 --- a/lms/djangoapps/student_account/test/test_views.py +++ b/lms/djangoapps/student_account/test/test_views.py @@ -1,12 +1,14 @@ # -*- coding: utf-8 -*- """ Tests for student account views. """ +import re from urllib import urlencode from mock import patch import ddt from django.test import TestCase from django.conf import settings from django.core.urlresolvers import reverse +from django.core import mail from util.testing import UrlResetMixin from user_api.api import account as account_api @@ -76,7 +78,7 @@ class StudentAccountViewTest(UrlResetMixin, TestCase): response = self.client.get(reverse('account_index')) self.assertContains(response, "Student Account") - def test_email_change_request_handler(self): + def test_change_email(self): response = self._change_email(self.NEW_EMAIL, self.PASSWORD) self.assertEquals(response.status_code, 204) @@ -84,6 +86,38 @@ class StudentAccountViewTest(UrlResetMixin, TestCase): profile_info = profile_api.profile_info(self.USERNAME) self.assertEquals(profile_info['email'], self.OLD_EMAIL) + # Check that an email was sent with the activation key + self.assertEqual(len(mail.outbox), 1) + self._assert_email( + mail.outbox[0], + [self.NEW_EMAIL], + u'Email Change Request', + u'There was recently a request to change the email address' + ) + + # Retrieve the activation key from the email + email_body = mail.outbox[0].body + result = re.search('/email_change_confirm/([^ \n]+)', email_body) + self.assertIsNot(result, None) + activation_key = result.group(1) + + # Attempt to activate the email + response = self.client.get(reverse('email_change_confirm', kwargs={'key': activation_key})) + self.assertEqual(response.status_code, 200) + + # Verify that the email was changed + profile_info = profile_api.profile_info(self.USERNAME) + self.assertEquals(profile_info['email'], self.NEW_EMAIL) + + # Verify that notification emails were sent + self.assertEqual(len(mail.outbox), 2) + self._assert_email( + mail.outbox[1], + [self.OLD_EMAIL, self.NEW_EMAIL], + u'Email Change Successful', + u'You successfully changed the email address' + ) + def test_email_change_wrong_password(self): response = self._change_email(self.NEW_EMAIL, "wrong password") self.assertEqual(response.status_code, 401) @@ -202,3 +236,9 @@ class StudentAccountViewTest(UrlResetMixin, TestCase): for method in wrong_methods: response = getattr(self.client, method)(url) self.assertEqual(response.status_code, 405) + + def _assert_email(self, email, expected_to, expected_subject, expected_body): + """Check whether an email has the correct properties. """ + self.assertEqual(email.to, expected_to) + self.assertIn(expected_subject, email.subject) + self.assertIn(expected_body, email.body) diff --git a/lms/templates/student_account/emails/email_change_confirmation/message_body.txt b/lms/templates/student_account/emails/email_change_confirmation/message_body.txt index c54df4a369..9ae44be4ca 100644 --- a/lms/templates/student_account/emails/email_change_confirmation/message_body.txt +++ b/lms/templates/student_account/emails/email_change_confirmation/message_body.txt @@ -6,7 +6,7 @@ ${_("Hi there,")} ## Preamble -${_("You successfully changed the email address associated with your" +${_("You successfully changed the email address associated with your " "{platform_name} account from {old_email} to {new_email}.").format( platform_name=settings.PLATFORM_NAME, old_email=old_email,