If phone number is already filled in for the associated contact, do not overwrite

This commit is contained in:
Rachid Mrad 2024-04-29 19:41:51 -04:00
parent 781b9f7b81
commit 8b36fef5c1
No known key found for this signature in database
4 changed files with 34 additions and 7 deletions

View file

@ -8,6 +8,8 @@ from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
from django.utils import timezone
from registrar.models.contact import Contact
logger = logging.getLogger(__name__)
@ -80,6 +82,14 @@ class OpenIdConnectBackend(ModelBackend):
- 'first_name' and 'last_name' will be updated if the provided value is not empty.
"""
contacts = Contact.objects.filter(user=user)
if len(contacts) == 0: # no matching contact
logger.warning("Could not find a contact when one should've existed.")
if len(contacts) > 1: # multiple matches
logger.warning("There are multiple Contacts with the same email address.")
# Iterate over fields to update
for key, value in kwargs.items():
# Check if the field is not 'first_name', 'last_name', or 'phone',
@ -87,7 +97,7 @@ class OpenIdConnectBackend(ModelBackend):
# or if it's 'first_name' or 'last_name' and the provided value is not empty
if (
key not in ["first_name", "last_name", "phone"]
or (key == "phone" and (user.phone is None or user.phone == ""))
or (key == "phone" and not contacts[0].phone)
or (key in ["first_name", "last_name"] and value)
):
# Update the corresponding attribute of the user object

View file

@ -102,9 +102,10 @@ class Contact(TimeStampedModel):
super().save(*args, **kwargs)
# Update the related User object's first_name and last_name
if self.user and (not self.user.first_name or not self.user.last_name):
if self.user and (not self.user.first_name or not self.user.last_name or not self.user.phone):
self.user.first_name = self.first_name
self.user.last_name = self.last_name
self.user.phone = self.phone
self.user.save()
def __str__(self):

View file

@ -834,7 +834,7 @@ def completed_domain_request(
)
if not investigator:
investigator, _ = User.objects.get_or_create(
username="incrediblyfakeinvestigator",
username="incrediblyfakeinvestigator" + str(uuid.uuid4())[:8],
first_name="Joe",
last_name="Bob",
is_staff=True,

View file

@ -1152,12 +1152,18 @@ class TestContact(TestCase):
def setUp(self):
self.email_for_invalid = "intern@igorville.gov"
self.invalid_user, _ = User.objects.get_or_create(
username=self.email_for_invalid, email=self.email_for_invalid, first_name="", last_name=""
username=self.email_for_invalid,
email=self.email_for_invalid,
first_name="",
last_name="",
phone="",
)
self.invalid_contact, _ = Contact.objects.get_or_create(user=self.invalid_user)
self.email = "mayor@igorville.gov"
self.user, _ = User.objects.get_or_create(email=self.email, first_name="Jeff", last_name="Lebowski")
self.user, _ = User.objects.get_or_create(
email=self.email, first_name="Jeff", last_name="Lebowski", phone="123456789"
)
self.contact, _ = Contact.objects.get_or_create(user=self.user)
self.contact_as_ao, _ = Contact.objects.get_or_create(email="newguy@igorville.gov")
@ -1169,19 +1175,22 @@ class TestContact(TestCase):
Contact.objects.all().delete()
User.objects.all().delete()
def test_saving_contact_updates_user_first_last_names(self):
def test_saving_contact_updates_user_first_last_names_and_phone(self):
"""When a contact is updated, we propagate the changes to the linked user if it exists."""
# User and Contact are created and linked as expected.
# An empty User object should create an empty contact.
self.assertEqual(self.invalid_contact.first_name, "")
self.assertEqual(self.invalid_contact.last_name, "")
self.assertEqual(self.invalid_contact.phone, "")
self.assertEqual(self.invalid_user.first_name, "")
self.assertEqual(self.invalid_user.last_name, "")
self.assertEqual(self.invalid_user.phone, "")
# Manually update the contact - mimicking production (pre-existing data)
self.invalid_contact.first_name = "Joey"
self.invalid_contact.last_name = "Baloney"
self.invalid_contact.phone = "123456789"
self.invalid_contact.save()
# Refresh the user object to reflect the changes made in the database
@ -1190,20 +1199,25 @@ class TestContact(TestCase):
# Updating the contact's first and last names propagate to the user
self.assertEqual(self.invalid_contact.first_name, "Joey")
self.assertEqual(self.invalid_contact.last_name, "Baloney")
self.assertEqual(self.invalid_contact.phone, "123456789")
self.assertEqual(self.invalid_user.first_name, "Joey")
self.assertEqual(self.invalid_user.last_name, "Baloney")
self.assertEqual(self.invalid_user.phone, "123456789")
def test_saving_contact_does_not_update_user_first_last_names(self):
def test_saving_contact_does_not_update_user_first_last_names_and_phone(self):
"""When a contact is updated, we avoid propagating the changes to the linked user if it already has a value"""
# User and Contact are created and linked as expected
self.assertEqual(self.contact.first_name, "Jeff")
self.assertEqual(self.contact.last_name, "Lebowski")
self.assertEqual(self.contact.phone, "123456789")
self.assertEqual(self.user.first_name, "Jeff")
self.assertEqual(self.user.last_name, "Lebowski")
self.assertEqual(self.user.phone, "123456789")
self.contact.first_name = "Joey"
self.contact.last_name = "Baloney"
self.contact.phone = "987654321"
self.contact.save()
# Refresh the user object to reflect the changes made in the database
@ -1212,8 +1226,10 @@ class TestContact(TestCase):
# Updating the contact's first and last names propagate to the user
self.assertEqual(self.contact.first_name, "Joey")
self.assertEqual(self.contact.last_name, "Baloney")
self.assertEqual(self.contact.phone, "987654321")
self.assertEqual(self.user.first_name, "Jeff")
self.assertEqual(self.user.last_name, "Lebowski")
self.assertEqual(self.user.phone, "123456789")
def test_saving_contact_does_not_update_user_email(self):
"""When a contact's email is updated, the change is not propagated to the user."""