def get_user_from_token(token, url_name, max_age, validate=True): from emails.models import get_email_from_token email = get_email_from_token(token, url_name, max_age) user = User.objects.filter(email=email).first() if validate: validate_user(user) return user
def test_valid_token_user_not_found(client, valid_user_1, valid_email2): user = create_test_user_reset_password(valid_user_1) assert user.email != valid_email2 token = get_token_from_email(valid_email2, 'reset-password') email = get_email_from_token(token, 'reset-password', settings.RESET_PASSWORD_EXPIRATION_TIME) assert email == valid_email2 url = reverse('users:reset-password-token', kwargs={'token': token}) response = client.get(url) assert response.status_code == 302 assert response.url == reverse('users:login')
def perform_invalid_current_email_token(client, user_data, new_email, request): user = create_activated_test_user(user_data) client.force_login(user) new_email_token = get_token_from_email(new_email, 'change-email') email = get_email_from_token(new_email_token, 'change-email', settings.CHANGE_EMAIL_EXPIRATION_TIME) assert email == new_email args = {'current_email_token': 'token', 'new_email_token': new_email_token} url = reverse('users:change-email', kwargs=args) perform_failed_email_change(client, user, url, request)
def perform_new_email_already_taken(client, user_data_1, user_data_2, request): user = create_activated_test_user(user_data_1) client.force_login(user) create_test_user(user_data_2) current_email_token = get_token_from_email(user.email, 'change-email') email = get_email_from_token(current_email_token, 'change-email', settings.CHANGE_EMAIL_EXPIRATION_TIME) assert email == user.email new_email_token = get_token_from_email(user_data_2['email'], 'change-email') email = get_email_from_token(new_email_token, 'change-email', settings.CHANGE_EMAIL_EXPIRATION_TIME) assert email == user_data_2['email'] args = { 'current_email_token': current_email_token, 'new_email_token': new_email_token } url = reverse('users:change-email', kwargs=args) perform_failed_email_change(client, user, url, request)
def test_valid_token_user_not_found_logged_out_post(client, valid_user_1, valid_email2): user = create_account_delete_requested_user(valid_user_1) assert user.email != valid_email2 token = get_token_from_email(valid_email2, 'cancel-delete-account') email = get_email_from_token(token, 'cancel-delete-account', settings.ACCOUNT_DELETION_INTERVAL) assert email == valid_email2 url = reverse('users:cancel-delete-account-token', kwargs={'token': token}) response = client.post(url) assert response.status_code == 302 assert response.url == reverse('posts:home') user = UserModel.objects.filter(id=user.id).first() assert user.delete_requested_at
def perform_valid_token_user_not_found(user_data, valid_email, request): user = create_test_user(user_data) assert not user.activated_at assert user.email != valid_email token = get_token_from_email(valid_email, 'account-activation') email = get_email_from_token(token, 'account-activation', settings.ACCOUNT_ACTIVATION_EXPIRATION_TIME) assert email == valid_email url = reverse('users:activate-account', kwargs={'token': token}) response = request(url) assert response.status_code == 302 assert response.url == reverse('users:login') user = get_user_model().objects.first() assert not user.activated_at
def change_user_email(current_user, current_email_token, new_email_token): from emails.models import get_email_from_token user = get_user_from_token( current_email_token, 'change-email', settings.CHANGE_EMAIL_EXPIRATION_TIME) if user != current_user: raise ValueError('User not recognized.') new_email = get_email_from_token( new_email_token, 'change-email', settings.CHANGE_EMAIL_EXPIRATION_TIME) user = User.objects.filter(email=new_email).first() if user: raise ValueError('New email already taken.') current_user.email = new_email current_user.save()