def test_email_change(self): response = self._change_email(self.NEW_EMAIL, self.OLD_PASSWORD) self.assertEquals(response.status_code, 200) # Verify that the email associated with the account remains unchanged 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/confirmation/([^ \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_name_change(self): # Verify that the name on the account is blank profile_info = profile_api.profile_info(self.USERNAME) self.assertEqual(profile_info['full_name'], '') response = self._change_name(self.FULL_NAME) self.assertEqual(response.status_code, 204) # Verify that the name on the account has been changed profile_info = profile_api.profile_info(self.USERNAME) self.assertEqual(profile_info['full_name'], self.FULL_NAME)
def test_email_change_confirmation_invalid_key(self): # Visit the confirmation page with an invalid key response = self.client.get(reverse('email_change_confirm', kwargs={'key': self.INVALID_KEY})) self.assertContains(response, "Something went wrong") # Verify that the email associated with the 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(self): # Get an email change activation key activation_key = account_api.request_email_change(self.USERNAME, self.NEW_EMAIL, self.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_create_profile(self): # Create a new account, which should have an empty profile by default. account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) # Retrieve the profile, expecting default values profile = profile_api.profile_info(username=self.USERNAME) self.assertEqual(profile, { 'username': self.USERNAME, 'email': self.EMAIL, 'full_name': u'', })
def test_change_email(self): response = self._change_email(self.NEW_EMAIL, self.PASSWORD) self.assertEquals(response.status_code, 204) # Verify that the email associated with the account remains unchanged 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_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.PASSWORD) # Create/activate a second user with the new email account_activation_key = account_api.create_account(self.ALTERNATE_USERNAME, self.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_create_profile(self): # Create a new account, which should have an empty profile by default. account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) # Retrieve the profile, expecting default values profile = profile_api.profile_info(username=self.USERNAME) self.assertEqual(profile, { 'username': self.USERNAME, 'email': self.EMAIL, 'full_name': u'', 'goals': None, 'level_of_education': None, 'mailing_address': None, 'year_of_birth': None, 'country': '', 'city': None, })
def test_create_profile(self): # Create a new account, which should have an empty profile by default. account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) # Retrieve the profile, expecting default values profile = profile_api.profile_info(username=self.USERNAME) self.assertEqual( profile, { 'username': self.USERNAME, 'email': self.EMAIL, 'full_name': u'', 'goals': None, 'level_of_education': None, 'mailing_address': None, 'year_of_birth': None, 'country': '', 'city': None, })
def email_change_request_handler(request): """Handle a request to change the user's email address. 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 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 HttpResponse: 500 if the user to which the email change will be applied does not exist 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.AccountUserNotFound: return HttpResponseServerError() except account_api.AccountEmailAlreadyExists: return HttpResponse(status=409) except account_api.AccountEmailInvalid: return HttpResponseBadRequest() 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]) # Send a 200 response code to the client to indicate that the email was sent successfully. return HttpResponse(status=200)
def test_update_full_name(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) profile_api.update_profile(self.USERNAME, full_name=u'ȻħȺɍłɇs') profile = profile_api.profile_info(self.USERNAME) self.assertEqual(profile['full_name'], u'ȻħȺɍłɇs')
def test_retrieve_profile_no_user(self): profile = profile_api.profile_info('does not exist') self.assertIs(profile, None)
def email_change_request_handler(request): """Handle a request to change the user's email address. Args: request (HttpRequest) Returns: HttpResponse: 204 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 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 HttpResponse: 500 if the user to which the email change will be applied does not exist Example usage: PUT /account/email_change_request """ put = QueryDict(request.body) user = request.user password = put.get('password') username = user.username old_email = profile_api.profile_info(username)['email'] new_email = put.get('new_email') if new_email is None: return HttpResponseBadRequest("Missing param 'new_email'") if password is None: return HttpResponseBadRequest("Missing param 'password'") try: key = account_api.request_email_change(username, new_email, password) except account_api.AccountUserNotFound: return HttpResponseServerError() except account_api.AccountEmailAlreadyExists: return HttpResponse(status=409) except account_api.AccountEmailInvalid: return HttpResponseBadRequest() 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 ) # Email new address send_mail(subject, message, from_address, [new_email]) # A 204 is intended to allow input for actions to take place # without causing a change to the user agent's active document view. return HttpResponse(status=204)
def email_change_request_handler(request): """Handle a request to change the user's email address. Args: request (HttpRequest) Returns: HttpResponse: 204 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 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 HttpResponse: 500 if the user to which the email change will be applied does not exist Example usage: PUT /account/email_change_request """ put = QueryDict(request.body) user = request.user password = put.get('password') username = user.username old_email = profile_api.profile_info(username)['email'] new_email = put.get('new_email') if new_email is None: return HttpResponseBadRequest("Missing param 'new_email'") if password is None: return HttpResponseBadRequest("Missing param 'password'") try: key = account_api.request_email_change(username, new_email, password) except account_api.AccountUserNotFound: return HttpResponseServerError() except account_api.AccountEmailAlreadyExists: return HttpResponse(status=409) except account_api.AccountEmailInvalid: return HttpResponseBadRequest() 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) # Email new address send_mail(subject, message, from_address, [new_email]) # A 204 is intended to allow input for actions to take place # without causing a change to the user agent's active document view. return HttpResponse(status=204)
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)