def forgot_username(request): """Forgot username form page. On POST, this view sends an email with the username. """ if request.method == "POST": form = ForgotUsernameForm(request.POST) was_valid = form.is_valid() if was_valid: try_send_email_with_form( form.save, form, 'email', use_https=request.is_secure()) # Form may now be invalid if email failed to send. # ForgotUsernameForm is invalid iff there is no user with the entered # email address. # The condition below ensures we don't leak existence of email address # _unless_ sending an email fails. if form.is_valid() or not was_valid: # Don't leak existence of email addresses. messages.add_message( request, messages.INFO, _(u"We've sent an email with the username to any account" " using {email}.").format(email=form.data['email'])) return HttpResponseRedirect(reverse('users.login')) else: form = ForgotUsernameForm() return jingo.render(request, 'users/forgot_username.html', {'form': form})
def forgot_username(request, template): """Forgot username form page. On POST, this view sends an email with the username. """ if request.method == "POST": form = ForgotUsernameForm(request.POST) was_valid = form.is_valid() if was_valid: try_send_email_with_form(form.save, form, 'email', use_https=request.is_secure()) # Form may now be invalid if email failed to send. # ForgotUsernameForm is invalid iff there is no user with the entered # email address. # The condition below ensures we don't leak existence of email address # _unless_ sending an email fails. if form.is_valid() or not was_valid: # Don't leak existence of email addresses. messages.add_message( request, messages.INFO, _(u"We've sent an email with the username to any account" " using {email}.").format(email=form.data['email'])) return HttpResponseRedirect(reverse('users.login')) else: form = ForgotUsernameForm() return render(request, template, {'form': form})
def test_valid_email(self): """"If an account with email exists, form is valid.""" u = user(save=True, email='*****@*****.**', is_active=True) form = ForgotUsernameForm({'email': u.email}) assert form.is_valid()
def test_email_doesnt_exist(self): """If no account with email exists, form isn't valid.""" form = ForgotUsernameForm({'email': '*****@*****.**'}) assert not form.is_valid()