def test_record_email_change_history(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) # Change the email once activation_key = account_api.request_email_change( self.USERNAME, u"*****@*****.**", self.PASSWORD ) account_api.confirm_email_change(activation_key) # Verify that the old email appears in the history meta = UserProfile.objects.get(user__username=self.USERNAME).get_meta() self.assertEqual(len(meta['old_emails']), 1) email, timestamp = meta['old_emails'][0] self.assertEqual(email, self.EMAIL) self._assert_is_datetime(timestamp) # Change the email again activation_key = account_api.request_email_change( self.USERNAME, u"*****@*****.**", self.PASSWORD ) account_api.confirm_email_change(activation_key) # Verify that both emails appear in the history meta = UserProfile.objects.get(user__username=self.USERNAME).get_meta() self.assertEqual(len(meta['old_emails']), 2) email, timestamp = meta['old_emails'][1] self.assertEqual(email, "*****@*****.**") self._assert_is_datetime(timestamp)
def test_record_email_change_history(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) # Change the email once activation_key = account_api.request_email_change( self.USERNAME, u'*****@*****.**', self.PASSWORD ) account_api.confirm_email_change(activation_key) # Verify that the old email appears in the history meta = UserProfile.objects.get(user__username=self.USERNAME).get_meta() self.assertEqual(len(meta['old_emails']), 1) email, timestamp = meta['old_emails'][0] self.assertEqual(email, self.EMAIL) self._assert_is_datetime(timestamp) # Change the email again activation_key = account_api.request_email_change( self.USERNAME, u'*****@*****.**', self.PASSWORD ) account_api.confirm_email_change(activation_key) # Verify that both emails appear in the history meta = UserProfile.objects.get(user__username=self.USERNAME).get_meta() self.assertEqual(len(meta['old_emails']), 2) email, timestamp = meta['old_emails'][1] self.assertEqual(email, '*****@*****.**') self._assert_is_datetime(timestamp)
def test_confirm_email_change_invalid_activation_key(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) account_api.request_email_change(self.USERNAME, u"*****@*****.**", self.PASSWORD) with self.assertRaises(account_api.AccountNotAuthorized): account_api.confirm_email_change(u"invalid")
def test_confirm_email_no_user_profile(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) activation_key = account_api.request_email_change( self.USERNAME, u"*****@*****.**", self.PASSWORD) # This should never happen, but just in case... UserProfile.objects.get(user__username=self.USERNAME).delete() with self.assertRaises(account_api.AccountInternalError): account_api.confirm_email_change(activation_key)
def test_confirm_email_no_user_profile(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) activation_key = account_api.request_email_change( self.USERNAME, u"*****@*****.**", self.PASSWORD ) # This should never happen, but just in case... UserProfile.objects.get(user__username=self.USERNAME).delete() with self.assertRaises(account_api.AccountInternalError): account_api.confirm_email_change(activation_key)
def test_confirm_email_change_repeat(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) activation_key = account_api.request_email_change( self.USERNAME, u'*****@*****.**', self.PASSWORD ) # Confirm the change once account_api.confirm_email_change(activation_key) # Confirm the change again. The activation code should be # single-use, so this should raise an error. with self.assertRaises(account_api.AccountNotAuthorized): account_api.confirm_email_change(activation_key)
def test_confirm_email_already_exists(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) # Request a change activation_key = account_api.request_email_change( self.USERNAME, u"*****@*****.**", self.PASSWORD ) # Another use takes the email before we confirm the change account_api.create_account(u"other_user", u"password", u"*****@*****.**") # When we try to confirm our change, we get an error because the email is taken with self.assertRaises(account_api.AccountEmailAlreadyExists): account_api.confirm_email_change(activation_key) # Verify that the email was NOT changed self.assertEqual(account_api.account_info(self.USERNAME)['email'], self.EMAIL)
def test_confirm_email_already_exists(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) # Request a change activation_key = account_api.request_email_change( self.USERNAME, u'*****@*****.**', self.PASSWORD ) # Another use takes the email before we confirm the change account_api.create_account(u'other_user', u'password', u'*****@*****.**') # When we try to confirm our change, we get an error because the email is taken with self.assertRaises(account_api.AccountEmailAlreadyExists): account_api.confirm_email_change(activation_key) # Verify that the email was NOT changed self.assertEqual(account_api.account_info(self.USERNAME)['email'], self.EMAIL)
def test_change_email(self): # Request an email change account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) activation_key = account_api.request_email_change( self.USERNAME, u"*****@*****.**", self.PASSWORD) # Verify that the email has not yet changed account = account_api.account_info(self.USERNAME) self.assertEqual(account['email'], self.EMAIL) # Confirm the change, using the activation code old_email, new_email = account_api.confirm_email_change(activation_key) self.assertEqual(old_email, self.EMAIL) self.assertEqual(new_email, u"*****@*****.**") # Verify that the email is changed account = account_api.account_info(self.USERNAME) self.assertEqual(account['email'], u"*****@*****.**")
def test_change_email(self): # Request an email change account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) activation_key = account_api.request_email_change( self.USERNAME, u"*****@*****.**", self.PASSWORD ) # Verify that the email has not yet changed account = account_api.account_info(self.USERNAME) self.assertEqual(account['email'], self.EMAIL) # Confirm the change, using the activation code old_email, new_email = account_api.confirm_email_change(activation_key) self.assertEqual(old_email, self.EMAIL) self.assertEqual(new_email, u"*****@*****.**") # Verify that the email is changed account = account_api.account_info(self.USERNAME) self.assertEqual(account['email'], u"*****@*****.**")
def email_change_confirmation_handler(request, key): """Complete a change of the user's email address. This is called when the activation link included in the confirmation email is clicked. Args: request (HttpRequest) Returns: HttpResponse: 200 if the email change is successful, the activation key is invalid, the new email is already in use, or the user to which the email change will be applied does not exist HttpResponse: 302 if not logged in (redirect to login page) HttpResponse: 405 if using an unsupported HTTP method Example usage: GET /account/email_change_confirm/{key} """ try: old_email, new_email = account_api.confirm_email_change(key) except account_api.AccountNotAuthorized: return render_to_response('student_account/email_change_failed.html', { 'disable_courseware_js': True, 'error': 'key_invalid', }) except account_api.AccountEmailAlreadyExists: return render_to_response('student_account/email_change_failed.html', { 'disable_courseware_js': True, 'error': 'email_used', }) except account_api.AccountInternalError: return render_to_response('student_account/email_change_failed.html', { 'disable_courseware_js': True, 'error': 'internal', }) context = { 'old_email': old_email, 'new_email': new_email, } subject = render_to_string( 'student_account/emails/email_change_confirmation/subject_line.txt', context) subject = ''.join(subject.splitlines()) message = render_to_string( 'student_account/emails/email_change_confirmation/message_body.txt', context) from_address = microsite.get_value('email_from_address', settings.DEFAULT_FROM_EMAIL) # Notify both old and new emails of the change send_mail(subject, message, from_address, [old_email, new_email]) return render_to_response('student_account/email_change_successful.html', { 'disable_courseware_js': True, })
def email_change_confirmation_handler(request, key): """Complete a change of the user's email address. This is called when the activation link included in the confirmation email is clicked. Args: request (HttpRequest) Returns: HttpResponse: 200 if the email change is successful, the activation key is invalid, the new email is already in use, or the user to which the email change will be applied does not exist HttpResponse: 302 if not logged in (redirect to login page) HttpResponse: 405 if using an unsupported HTTP method Example usage: GET /account/email_change_confirm/{key} """ try: old_email, new_email = account_api.confirm_email_change(key) except account_api.AccountNotAuthorized: return render_to_response( 'student_account/email_change_failed.html', { 'disable_courseware_js': True, 'error': 'key_invalid', } ) except account_api.AccountEmailAlreadyExists: return render_to_response( 'student_account/email_change_failed.html', { 'disable_courseware_js': True, 'error': 'email_used', } ) except account_api.AccountInternalError: return render_to_response( 'student_account/email_change_failed.html', { 'disable_courseware_js': True, 'error': 'internal', } ) context = { 'old_email': old_email, 'new_email': new_email, } subject = render_to_string('student_account/emails/email_change_confirmation/subject_line.txt', context) subject = ''.join(subject.splitlines()) message = render_to_string('student_account/emails/email_change_confirmation/message_body.txt', context) from_address = microsite.get_value( 'email_from_address', settings.DEFAULT_FROM_EMAIL ) # Notify both old and new emails of the change send_mail(subject, message, from_address, [old_email, new_email]) return render_to_response( 'student_account/email_change_successful.html', { 'disable_courseware_js': True, } )