revise logic a bit to ensure user and contact do not overwrite each other

This commit is contained in:
Rachid Mrad 2024-04-30 13:28:03 -04:00
parent 8b36fef5c1
commit c6b6c17540
No known key found for this signature in database
3 changed files with 26 additions and 67 deletions

View file

@ -64,6 +64,7 @@ class OpenIdConnectBackendTestCase(TestCase):
# Remove given_name and family_name from the input, self.kwargs
self.kwargs.pop("given_name", None)
self.kwargs.pop("family_name", None)
self.kwargs.pop("phone", None)
# Ensure that the authenticate method updates the existing user
# and preserves existing first and last names
@ -78,53 +79,13 @@ class OpenIdConnectBackendTestCase(TestCase):
self.assertEqual(user.email, "john.doe@example.com")
self.assertEqual(user.phone, "9999999999")
def test_authenticate_with_existing_user_with_no_phone_will_update_phone(self):
"""Test that authenticate updates an existing user if it finds one.
For this test, the existing user has an empty string for phone
The existing user's phone number is overwritten"""
# Create an existing user with the same username and with first and last names
existing_user = User.objects.create_user(username="test_user", phone="")
# Ensure that the authenticate method updates the existing user
# and preserves existing first and last names
user = self.backend.authenticate(request=None, **self.kwargs)
self.assertIsNotNone(user)
self.assertIsInstance(user, User)
self.assertEqual(user, existing_user) # The same user instance should be returned
# Verify that user fields are correctly updated
self.assertEqual(user.first_name, "John")
self.assertEqual(user.last_name, "Doe")
self.assertEqual(user.email, "john.doe@example.com")
self.assertEqual(user.phone, "123456789")
def test_authenticate_with_existing_user_with_none_phone_will_update_phone(self):
"""Test that authenticate updates an existing user if it finds one.
For this test, the existing user has None for phone
The existing user's phone number is overwritten"""
# Create an existing user with the same username and with first and last names
existing_user = User.objects.create_user(username="test_user")
# Ensure that the authenticate method updates the existing user
# and preserves existing first and last names
user = self.backend.authenticate(request=None, **self.kwargs)
self.assertIsNotNone(user)
self.assertIsInstance(user, User)
self.assertEqual(user, existing_user) # The same user instance should be returned
# Verify that user fields are correctly updated
self.assertEqual(user.first_name, "John")
self.assertEqual(user.last_name, "Doe")
self.assertEqual(user.email, "john.doe@example.com")
self.assertEqual(user.phone, "123456789")
def test_authenticate_with_existing_user_different_name(self):
def test_authenticate_with_existing_user_different_name_phone(self):
"""Test that authenticate updates an existing user if it finds one.
For this test, given_name and family_name are supplied and overwrite"""
# Create an existing user with the same username and with first and last names
existing_user = User.objects.create_user(username="test_user", first_name="WillBe", last_name="Replaced")
existing_user = User.objects.create_user(
username="test_user", first_name="WillBe", last_name="Replaced", phone="987654321"
)
# Ensure that the authenticate method updates the existing user
# and preserves existing first and last names