Stop errors when changing an email address to an invalid one

We use the `ChangeEmailForm` if you want to change your own email
address or someone else's email address. This has various validators
which get run. We check if the email address is valid (by using a
function from utils) and if the email address is already in use
(by calling API).

If the email address is not valid, we should not call API to see if it's
already in use because this will cause an exception in API leading to a
`500` in admin. We now only call API if there were no other errors with
the email address.

(The `test_should_redirect_after_name_change` test didn't need the
`mock_email_is_not_already_in_use` fixture, so this has been removed.)
This commit is contained in:
Katie Smith
2021-12-10 16:56:08 +00:00
parent 1da285cf52
commit d8ebcdce22
2 changed files with 10 additions and 1 deletions

View File

@@ -1535,6 +1535,12 @@ class ChangeEmailForm(StripWhitespaceForm):
email_address = email_address()
def validate_email_address(self, field):
# The validate_email_func can be used to call API to check if the email address is already in
# use. We don't want to run that check for invalid email addresses, since that will cause an error.
# If there are any other validation errors on the email_address, we should skip this check.
if self.email_address.errors:
return
is_valid = self.validate_email_func(field.data)
if is_valid:
raise ValidationError("The email address is already in use")