def test_email_change_request_email_taken_by_inactive_account(self): # Create a second user with the new email, but don't active them account_api.create_account(self.ALTERNATE_USERNAME, self.PASSWORD, self.NEW_EMAIL) # Request to change the original user's email to the email used by the inactive user response = self._change_email(self.NEW_EMAIL, self.PASSWORD) self.assertEquals(response.status_code, 204)
def test_update_and_retrieve_preference_info_unicode(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) profile_api.update_preferences(self.USERNAME, **{u'ⓟⓡⓔⓕⓔⓡⓔⓝⓒⓔ_ⓚⓔⓨ': u'ǝnןɐʌ_ǝɔuǝɹǝɟǝɹd'}) preferences = profile_api.preference_info(self.USERNAME) self.assertEqual(preferences[u'ⓟⓡⓔⓕⓔⓡⓔⓝⓒⓔ_ⓚⓔⓨ'], u'ǝnןɐʌ_ǝɔuǝɹǝɟǝɹd')
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_name_change_history(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) # Change the name once # Since the original name was an empty string, expect that the list # of old names is empty profile_api.update_profile(self.USERNAME, full_name='new name') meta = UserProfile.objects.get(user__username=self.USERNAME).get_meta() self.assertEqual(meta, {}) # Change the name again and expect the new name is stored in the history profile_api.update_profile(self.USERNAME, full_name='another new name') meta = UserProfile.objects.get(user__username=self.USERNAME).get_meta() self.assertEqual(len(meta['old_names']), 1) name, rationale, timestamp = meta['old_names'][0] self.assertEqual(name, 'new name') self.assertEqual(rationale, u'') self._assert_is_datetime(timestamp) # Change the name a third time and expect both names are stored in the history profile_api.update_profile(self.USERNAME, full_name='yet another new name') meta = UserProfile.objects.get(user__username=self.USERNAME).get_meta() self.assertEqual(len(meta['old_names']), 2) name, rationale, timestamp = meta['old_names'][1] self.assertEqual(name, 'another new name') self.assertEqual(rationale, u'') self._assert_is_datetime(timestamp)
def test_request_email_change_invalid_email(self, invalid_email): # Create an account with a valid email address account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) # Attempt to change the account to an invalid email with self.assertRaises(account_api.AccountEmailInvalid): account_api.request_email_change(self.USERNAME, invalid_email, self.PASSWORD)
def test_update_and_retrieve_preference_info(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) profile_api.update_preferences(self.USERNAME, preference_key='preference_value') preferences = profile_api.preference_info(self.USERNAME) self.assertEqual(preferences['preference_key'], 'preference_value')
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_update_and_retrieve_preference_info_unicode(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) profile_api.update_preferences( self.USERNAME, **{u'ⓟⓡⓔⓕⓔⓡⓔⓝⓒⓔ_ⓚⓔⓨ': u'ǝnןɐʌ_ǝɔuǝɹǝɟǝɹd'}) preferences = profile_api.preference_info(self.USERNAME) self.assertEqual(preferences[u'ⓟⓡⓔⓕⓔⓡⓔⓝⓒⓔ_ⓚⓔⓨ'], u'ǝnןɐʌ_ǝɔuǝɹǝɟǝɹd')
def test_request_email_change_wrong_password(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) # Use the wrong password with self.assertRaises(account_api.AccountNotAuthorized): account_api.request_email_change(self.USERNAME, u"*****@*****.**", u"wrong password")
def test_request_email_change_duplicates_unactivated_account(self): # Create two accounts, but the second account is inactive activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) account_api.activate_account(activation_key) account_api.create_account(u'another_user', u'password', u'*****@*****.**') # Try to change the first user's email to the same as the second user's # Since the second user has not yet activated, this should succeed. account_api.request_email_change(self.USERNAME, u'*****@*****.**', self.PASSWORD)
def test_request_password_change_invalid_user(self, create_inactive_account): if create_inactive_account: # Create an account, but do not activate it account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) account_api.request_password_change(self.EMAIL, self.ORIG_HOST, self.IS_SECURE) # Verify that no email messages have been sent self.assertEqual(len(mail.outbox), 0)
def test_request_email_change_duplicates_unactivated_account(self): # Create two accounts, but the second account is inactive activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) account_api.activate_account(activation_key) account_api.create_account(u"another_user", u"password", u"*****@*****.**") # Try to change the first user's email to the same as the second user's # Since the second user has not yet activated, this should succeed. account_api.request_email_change(self.USERNAME, u"*****@*****.**", self.PASSWORD)
def test_request_email_change_already_exists(self): # Create two accounts, both activated activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) account_api.activate_account(activation_key) activation_key = account_api.create_account(u"another_user", u"password", u"*****@*****.**") account_api.activate_account(activation_key) # Try to change the first user's email to the same as the second user's with self.assertRaises(account_api.AccountEmailAlreadyExists): account_api.request_email_change(self.USERNAME, u"*****@*****.**", self.PASSWORD)
def test_password_change_inactive_user(self): # Log out the user created during test setup self.client.logout() # Create a second user, but do not activate it account_api.create_account(self.ALTERNATE_USERNAME, self.OLD_PASSWORD, self.NEW_EMAIL) # Send the view the email address tied to the inactive user response = self._change_password(email=self.NEW_EMAIL) self.assertEqual(response.status_code, 400)
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_request_email_change_already_exists(self): # Create two accounts, both activated activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) account_api.activate_account(activation_key) activation_key = account_api.create_account(u'another_user', u'password', u'*****@*****.**') account_api.activate_account(activation_key) # Try to change the first user's email to the same as the second user's with self.assertRaises(account_api.AccountEmailAlreadyExists): account_api.request_email_change(self.USERNAME, u'*****@*****.**', self.PASSWORD)
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_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_update_email_optin_no_age_set(self): # Test that the API still works if no age is specified. # Create the course and account. course = CourseFactory.create() account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) user = User.objects.get(username=self.USERNAME) profile_api.update_email_opt_in(self.USERNAME, course.id.org, True) result_obj = UserOrgTag.objects.get(user=user, org=course.id.org, key='email-optin') self.assertEqual(result_obj.value, u"True")
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_update_email_optin(self, age, option, expected_result): # Create the course and account. course = CourseFactory.create() account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) # Set year of birth user = User.objects.get(username=self.USERNAME) profile = UserProfile.objects.get(user=user) year_of_birth = datetime.datetime.now().year - age # pylint: disable=maybe-no-member profile.year_of_birth = year_of_birth profile.save() profile_api.update_email_opt_in(self.USERNAME, course.id.org, option) result_obj = UserOrgTag.objects.get(user=user, org=course.id.org, key='email-optin') self.assertEqual(result_obj.value, expected_result)
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_request_email_change_same_address(self): # Create and activate the account activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) account_api.activate_account(activation_key) # Try to change the email address to the current address with self.assertRaises(account_api.AccountEmailAlreadyExists): account_api.request_email_change(self.USERNAME, self.EMAIL, self.PASSWORD)
def test_email_change_request_email_taken_by_active_account(self): # Create/activate a second user with the new email activation_key = account_api.create_account(self.ALTERNATE_USERNAME, self.PASSWORD, self.NEW_EMAIL) account_api.activate_account(activation_key) # Request to change the original user's email to the email now used by the second user response = self._change_email(self.NEW_EMAIL, self.PASSWORD) self.assertEquals(response.status_code, 409)
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_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_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 setUp(self): super(StudentAccountViewTest, self).setUp() # Create/activate a new account activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.OLD_EMAIL) account_api.activate_account(activation_key) # Login result = self.client.login(username=self.USERNAME, password=self.PASSWORD) self.assertTrue(result)
def test_login_and_registration_form_already_authenticated(self, url_name): # Create/activate a new account and log in activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) account_api.activate_account(activation_key) result = self.client.login(username=self.USERNAME, password=self.PASSWORD) self.assertTrue(result) # Verify that we're redirected to the dashboard response = self.client.get(reverse(url_name)) self.assertRedirects(response, reverse("dashboard"))
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_activate_account(self): # Create the account, which is initially inactive activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) account = account_api.account_info(self.USERNAME) self.assertEqual(account, { 'username': self.USERNAME, 'email': self.EMAIL, 'is_active': False }) # Activate the account and verify that it is now active account_api.activate_account(activation_key) account = account_api.account_info(self.USERNAME) self.assertTrue(account['is_active'])
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_request_password_change(self): # Create and activate an account activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) account_api.activate_account(activation_key) # Request a password change account_api.request_password_change(self.EMAIL, self.ORIG_HOST, self.IS_SECURE) # Verify that one email message has been sent self.assertEqual(len(mail.outbox), 1) # Verify that the body of the message contains something that looks # like an activation link email_body = mail.outbox[0].body result = re.search('(?P<url>https?://[^\s]+)', email_body) self.assertIsNot(result, None)
def test_create_account_username_password_equal(self): # Username and password cannot be the same account_api.create_account(self.USERNAME, self.USERNAME, self.EMAIL)
def test_create_account_invalid_username(self, invalid_username): account_api.create_account(invalid_username, self.PASSWORD, self.EMAIL)
def test_confirm_email_change_no_request_pending(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL)
def test_update_full_name_invalid(self, invalid_name): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) profile_api.update_profile(self.USERNAME, full_name=invalid_name)
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')