def test_email_change_confirmation_internal_error(self): # Get an email change activation key activation_key = account_api.request_email_change(self.USERNAME, self.NEW_EMAIL, self.OLD_PASSWORD) # Patch account API to return an internal error with mock.patch('student_account.views.account_api.confirm_email_change') as mock_call: mock_call.side_effect = account_api.AccountInternalError response = self.client.get(reverse('email_change_confirm', kwargs={'key': activation_key})) self.assertContains(response, "Something went wrong")
def test_email_change_confirmation(self): # Get an email change activation key activation_key = account_api.request_email_change(self.USERNAME, self.NEW_EMAIL, self.OLD_PASSWORD) # Follow the link sent in the confirmation email response = self.client.get(reverse('email_change_confirm', kwargs={'key': activation_key})) self.assertContains(response, "Email change successful") # Verify that the email associated with the account has changed profile_info = profile_api.profile_info(self.USERNAME) self.assertEquals(profile_info['email'], self.NEW_EMAIL)
def test_email_change_confirmation(self): # Get an email change activation key activation_key = account_api.request_email_change( self.USERNAME, self.NEW_EMAIL, self.OLD_PASSWORD) # Follow the link sent in the confirmation email response = self.client.get( reverse('email_change_confirm', kwargs={'key': activation_key})) self.assertContains(response, "Email change successful") # Verify that the email associated with the account has changed profile_info = profile_api.profile_info(self.USERNAME) self.assertEquals(profile_info['email'], self.NEW_EMAIL)
def test_email_change_confirmation_email_already_exists(self): # Get an email change activation key email_activation_key = account_api.request_email_change(self.USERNAME, self.NEW_EMAIL, self.OLD_PASSWORD) # Create/activate a second user with the new email account_activation_key = account_api.create_account(self.ALTERNATE_USERNAME, self.OLD_PASSWORD, self.NEW_EMAIL) account_api.activate_account(account_activation_key) # Follow the link sent to the original user response = self.client.get(reverse('email_change_confirm', kwargs={'key': email_activation_key})) self.assertContains(response, "address you wanted to use is already used") # Verify that the email associated with the original account has not changed profile_info = profile_api.profile_info(self.USERNAME) self.assertEquals(profile_info['email'], self.OLD_EMAIL)
def test_email_change_confirmation_internal_error(self): # Get an email change activation key activation_key = account_api.request_email_change( self.USERNAME, self.NEW_EMAIL, self.OLD_PASSWORD) # Patch account API to return an internal error with mock.patch( 'student_account.views.account_api.confirm_email_change' ) as mock_call: mock_call.side_effect = account_api.AccountInternalError response = self.client.get( reverse('email_change_confirm', kwargs={'key': activation_key})) self.assertContains(response, "Something went wrong")
def test_email_change_confirmation_email_already_exists(self): # Get an email change activation key email_activation_key = account_api.request_email_change( self.USERNAME, self.NEW_EMAIL, self.OLD_PASSWORD) # Create/activate a second user with the new email account_activation_key = account_api.create_account( self.ALTERNATE_USERNAME, self.OLD_PASSWORD, self.NEW_EMAIL) account_api.activate_account(account_activation_key) # Follow the link sent to the original user response = self.client.get( reverse('email_change_confirm', kwargs={'key': email_activation_key})) self.assertContains(response, "address you wanted to use is already used") # Verify that the email associated with the original account has not changed profile_info = profile_api.profile_info(self.USERNAME) self.assertEquals(profile_info['email'], self.OLD_EMAIL)
def email_change_request_handler(request): """Handle a request to change the user's email address. Sends an email to the newly specified address containing a link to a confirmation page. Args: request (HttpRequest) Returns: HttpResponse: 200 if the confirmation email was sent successfully HttpResponse: 302 if not logged in (redirect to login page) HttpResponse: 400 if the format of the new email is incorrect, or if an email change is requested for a user which does not exist HttpResponse: 401 if the provided password (in the form) is incorrect HttpResponse: 405 if using an unsupported HTTP method HttpResponse: 409 if the provided email is already in use Example usage: POST /account/email """ username = request.user.username password = request.POST.get('password') new_email = request.POST.get('email') if new_email is None: return HttpResponseBadRequest("Missing param 'email'") if password is None: return HttpResponseBadRequest("Missing param 'password'") old_email = profile_api.profile_info(username)['email'] try: key = account_api.request_email_change(username, new_email, password) except (account_api.AccountEmailInvalid, account_api.AccountUserNotFound): return HttpResponseBadRequest() except account_api.AccountEmailAlreadyExists: return HttpResponse(status=409) except account_api.AccountNotAuthorized: return HttpResponse(status=401) context = { 'key': key, 'old_email': old_email, 'new_email': new_email, } subject = render_to_string('student_account/emails/email_change_request/subject_line.txt', context) subject = ''.join(subject.splitlines()) message = render_to_string('student_account/emails/email_change_request/message_body.txt', context) from_address = microsite.get_value( 'email_from_address', settings.DEFAULT_FROM_EMAIL ) # Send a confirmation email to the new address containing the activation key send_mail(subject, message, from_address, [new_email]) return HttpResponse(status=200)
def email_change_request_handler(request): """Handle a request to change the user's email address. Sends an email to the newly specified address containing a link to a confirmation page. Args: request (HttpRequest) Returns: HttpResponse: 200 if the confirmation email was sent successfully HttpResponse: 302 if not logged in (redirect to login page) HttpResponse: 400 if the format of the new email is incorrect, or if an email change is requested for a user which does not exist HttpResponse: 401 if the provided password (in the form) is incorrect HttpResponse: 405 if using an unsupported HTTP method HttpResponse: 409 if the provided email is already in use Example usage: POST /account/email """ username = request.user.username password = request.POST.get('password') new_email = request.POST.get('email') if new_email is None: return HttpResponseBadRequest("Missing param 'email'") if password is None: return HttpResponseBadRequest("Missing param 'password'") old_email = profile_api.profile_info(username)['email'] try: key = account_api.request_email_change(username, new_email, password) except (account_api.AccountEmailInvalid, account_api.AccountUserNotFound): return HttpResponseBadRequest() except account_api.AccountEmailAlreadyExists: return HttpResponse(status=409) except account_api.AccountNotAuthorized: return HttpResponse(status=401) context = { 'key': key, 'old_email': old_email, 'new_email': new_email, } subject = render_to_string('student_account/emails/email_change_request/subject_line.txt', context) subject = ''.join(subject.splitlines()) message = render_to_string('student_account/emails/email_change_request/message_body.txt', context) from_address = microsite.get_value( 'email_from_address', settings.DEFAULT_FROM_EMAIL ) # Send a confirmation email to the new address containing the activation key send_mail(subject, message, from_address, [new_email]) return HttpResponse(status=200)