Change tests and fix contact

This commit is contained in:
zandercymatics 2023-12-20 11:31:05 -07:00
parent 59e7225dfd
commit 0ba0ce7c7c
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 62 additions and 21 deletions

View file

@ -64,7 +64,7 @@ class Contact(TimeStampedModel):
super().save(*args, **kwargs) super().save(*args, **kwargs)
# Update the related User object's first_name and last_name # 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):
self.user.first_name = self.first_name self.user.first_name = self.first_name
self.user.last_name = self.last_name self.user.last_name = self.last_name
self.user.save() self.user.save()

View file

@ -672,9 +672,14 @@ class TestUser(TestCase):
class TestContact(TestCase): class TestContact(TestCase):
def setUp(self): 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="")
self.invalid_contact, _ = Contact.objects.get_or_create(user=self.invalid_user)
self.email = "mayor@igorville.gov" self.email = "mayor@igorville.gov"
self.user, _ = User.objects.get_or_create(email=self.email, first_name="Jeff", last_name="Lebowski") self.existing_user, _ = User.objects.get_or_create(email=self.email, first_name="Jeff", last_name="Lebowski")
self.contact, _ = Contact.objects.get_or_create(user=self.user) self.existing_contact, _ = Contact.objects.get_or_create(user=self.existing_user)
def tearDown(self): def tearDown(self):
super().tearDown() super().tearDown()
@ -683,33 +688,69 @@ class TestContact(TestCase):
def test_saving_contact_updates_user_first_last_names(self): def test_saving_contact_updates_user_first_last_names(self):
"""When a contact is updated, we propagate the changes to the linked user if it exists.""" """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
self.assertEqual(self.contact.first_name, "Jeff")
self.assertEqual(self.contact.last_name, "Lebowski")
self.assertEqual(self.user.first_name, "Jeff")
self.assertEqual(self.user.last_name, "Lebowski")
self.contact.first_name = "Joey" # User and Contact are created and linked as expected
self.contact.last_name = "Baloney" self.assertEqual(self.invalid_contact.first_name, "")
self.contact.save() self.assertEqual(self.invalid_contact.last_name, "")
self.assertEqual(self.invalid_user.first_name, "")
self.assertEqual(self.invalid_user.last_name, "")
# Manually update the contact - mimicking production
self.invalid_contact.first_name = "Joey"
self.invalid_contact.last_name = "Baloney"
self.invalid_contact.save()
# Refresh the user object to reflect the changes made in the database # Refresh the user object to reflect the changes made in the database
self.user.refresh_from_db() self.invalid_user.refresh_from_db()
# Updating the contact's first and last names propagate to the user # Updating the contact's first and last names propagate to the user
self.assertEqual(self.contact.first_name, "Joey") self.assertEqual(self.invalid_contact.first_name, "Joey")
self.assertEqual(self.contact.last_name, "Baloney") self.assertEqual(self.invalid_contact.last_name, "Baloney")
self.assertEqual(self.user.first_name, "Joey") self.assertEqual(self.invalid_user.first_name, "Joey")
self.assertEqual(self.user.last_name, "Baloney") self.assertEqual(self.invalid_user.last_name, "Baloney")
def test_saving_contact_does_not_update_user_first_last_names(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.existing_contact.first_name, "Jeff")
self.assertEqual(self.existing_contact.last_name, "Lebowski")
self.assertEqual(self.existing_user.first_name, "Jeff")
self.assertEqual(self.existing_user.last_name, "Lebowski")
self.existing_contact.first_name = "Joey"
self.existing_contact.last_name = "Baloney"
self.existing_contact.save()
# Refresh the user object to reflect the changes made in the database
self.existing_user.refresh_from_db()
# Updating the contact's first and last names propagate to the user
self.assertEqual(self.existing_contact.first_name, "Joey")
self.assertEqual(self.existing_contact.last_name, "Baloney")
self.assertEqual(self.existing_user.first_name, "Jeff")
self.assertEqual(self.existing_user.last_name, "Lebowski")
def test_saving_contact_does_not_update_user_email(self): def test_saving_contact_does_not_update_user_email(self):
"""When a contact's email is updated, the change is not propagated to the lined user.""" """When a contact's email is updated, the change is not propagated to the lined user."""
self.contact.email = "joey.baloney@diaperville.com" self.existing_contact.email = "joey.baloney@diaperville.com"
self.contact.save() self.existing_contact.save()
# Refresh the user object to reflect the changes made in the database # Refresh the user object to reflect the changes made in the database
self.user.refresh_from_db() self.existing_user.refresh_from_db()
# Updating the contact's email does not propagate # Updating the contact's email does not propagate
self.assertEqual(self.contact.email, "joey.baloney@diaperville.com") self.assertEqual(self.existing_contact.email, "joey.baloney@diaperville.com")
self.assertEqual(self.user.email, "mayor@igorville.gov") self.assertEqual(self.existing_user.email, "mayor@igorville.gov")
def test_saving_contact_does_not_update_user_email_when_none(self):
"""When a contact's email is updated, the change is not propagated to the lined user."""
self.invalid_contact.email = "joey.baloney@diaperville.com"
self.invalid_contact.save()
# Refresh the user object to reflect the changes made in the database
self.invalid_user.refresh_from_db()
# Updating the contact's email does not propagate
self.assertEqual(self.invalid_contact.email, "joey.baloney@diaperville.com")
self.assertEqual(self.invalid_user.email, "mayor@igorville.gov")