Skip to content

Fix user confirm email already exists #11161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion api/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from urllib.parse import urlencode

from django.apps import apps
from django.db import IntegrityError
from django.db.models import F
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_protect
Expand Down Expand Up @@ -1146,7 +1147,12 @@ def post(self, request, *args, **kwargs):
if not user.is_registered:
user.register(email)

user.emails.get_or_create(address=email.lower())
if not user.emails.filter(address=email.lower()).exists():
try:
user.emails.create(address=email.lower())
except IntegrityError:
raise ValidationError('Email address already exists.')

user.date_last_logged_in = timezone.now()

del user.email_verifications[token]
Expand Down
28 changes: 28 additions & 0 deletions api_tests/users/views/test_user_confirm.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,31 @@ def test_post_success_link_with_email_verification_none(

user.reload()
assert not user.external_identity

@mock.patch('website.mails.send_mail')
def test_post_success_link_with_email_already_exists(
self,
mock_send_mail,
app,
confirm_url,
user_with_email_verification
):
user, token, email = user_with_email_verification
AuthUserFactory(username=email) # User with already existing email
user.save()

res = app.post_json_api(
confirm_url,
{
'data': {
'attributes': {
'uid': user._id,
'token': token,
'destination': 'doesnotmatter'
}
}
},
expect_errors=True
)
assert res.status_code == 400
assert res.json['errors'][0] == {'detail': 'Email address already exists.'}
Loading