if phone number is already filled in on user, do not overwrite

This commit is contained in:
Rachid Mrad 2024-04-29 18:39:00 -04:00
parent 3daa54ad43
commit 781b9f7b81
No known key found for this signature in database
2 changed files with 76 additions and 7 deletions

View file

@ -50,11 +50,16 @@ class OpenIdConnectBackendTestCase(TestCase):
self.assertEqual(user.email, "john.doe@example.com")
self.assertEqual(user.phone, "123456789")
def test_authenticate_with_existing_user_no_name(self):
def test_authenticate_with_existing_user_with_existing_first_last_phone(self):
"""Test that authenticate updates an existing user if it finds one.
For this test, given_name and family_name are not supplied"""
For this test, given_name and family_name are not supplied.
The existing user's first and last name are not overwritten.
The existing user's phone number is not overwritten"""
# 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="John", last_name="Doe")
existing_user = User.objects.create_user(
username="test_user", first_name="WillNotBe", last_name="Replaced", phone="9999999999"
)
# Remove given_name and family_name from the input, self.kwargs
self.kwargs.pop("given_name", None)
@ -67,6 +72,48 @@ class OpenIdConnectBackendTestCase(TestCase):
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, "WillNotBe")
self.assertEqual(user.last_name, "Replaced")
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")