Skip to content

Commit 3097074

Browse files
fix: Send create user email for password resets where we have an email and person, but no user. (ietf-tools#7729)
* fix: Send create user email for password resets where we have an email and person, but no user account This fixes ietf-tools#6458 * fix: create User straight away and use nomral password reset --------- Co-authored-by: Robert Sparks <rjsparks@nostrum.com>
1 parent 0c8db80 commit 3097074

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

ietf/ietfauth/tests.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,24 @@ def test_reset_password_without_username(self):
527527
self.assertIn(secondary_address, to)
528528
self.assertNotIn(inactive_secondary_address, to)
529529

530+
def test_reset_password_without_user(self):
531+
"""Reset password using email address for person without a user account"""
532+
url = urlreverse('ietf.ietfauth.views.password_reset')
533+
email = EmailFactory()
534+
person = email.person
535+
# Remove the user object from the person to get a Email/Person without User:
536+
person.user = None
537+
person.save()
538+
# Remove the remaining User record, since reset_password looks for that by username:
539+
User.objects.filter(username__iexact=email.address).delete()
540+
empty_outbox()
541+
r = self.client.post(url, { 'username': email.address })
542+
self.assertEqual(len(outbox), 1)
543+
lastReceivedEmail = outbox[-1]
544+
self.assertIn(email.address, lastReceivedEmail.get('To'))
545+
self.assertTrue(lastReceivedEmail.get('Subject').startswith("Confirm password reset"))
546+
self.assertContains(r, "Your password reset request has been successfully received", status_code=200)
547+
530548
def test_review_overview(self):
531549
review_req = ReviewRequestFactory()
532550
assignment = ReviewAssignmentFactory(review_request=review_req,reviewer=EmailFactory(person__user__username='reviewer'))

ietf/ietfauth/views.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,19 @@ def password_reset(request):
491491
if not user:
492492
# try to find user ID from the email address
493493
email = Email.objects.filter(address=submitted_username).first()
494-
if email and email.person and email.person.user:
495-
user = email.person.user
496-
494+
if email and email.person:
495+
if email.person.user:
496+
user = email.person.user
497+
else:
498+
# Create a User record with this (conditioned by way of Email) username
499+
# Don't bother setting the name or email fields on User - rely on the
500+
# Person pointer.
501+
user = User.objects.create(
502+
username=email.address.lower(),
503+
is_active=True,
504+
)
505+
email.person.user = user
506+
email.person.save()
497507
if user and user.person.email_set.filter(active=True).exists():
498508
data = {
499509
'username': user.username,

0 commit comments

Comments
 (0)