From 7b17462d9093ed09c80cdc96ae7b408ac8b55d89 Mon Sep 17 00:00:00 2001
From: Paul Marillonnet <pmarillonnet@entrouvert.com>
Date: Mon, 19 Sep 2022 14:23:40 +0200
Subject: [PATCH 2/2] custom_user: perform implicit writes on redundant phone
 fields (#65173)

---
 src/authentic2/custom_user/models.py | 12 +++++--
 tests/test_user_model.py             | 53 ++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/src/authentic2/custom_user/models.py b/src/authentic2/custom_user/models.py
index a114cd539..ebc98c1d3 100644
--- a/src/authentic2/custom_user/models.py
+++ b/src/authentic2/custom_user/models.py
@@ -95,7 +95,7 @@ class Attributes:
                 )
 
             update_fields = ['modified']
-            if name in ['first_name', 'last_name']:
+            if name in ['first_name', 'last_name', 'phone']:
                 if getattr(self.owner, name) != value:
                     setattr(self.owner, name, value)
                     update_fields.append(name)
@@ -362,7 +362,7 @@ class User(AbstractBaseUser, PermissionMixin):
     def save(self, *args, **kwargs):
         update_fields = kwargs.get('update_fields')
         super().save(*args, **kwargs)
-        if not update_fields or not set(update_fields).isdisjoint({'first_name', 'last_name'}):
+        if not update_fields or not set(update_fields).isdisjoint({'first_name', 'last_name', 'phone'}):
             try:
                 self.attributes.first_name
             except AttributeError:
@@ -378,6 +378,14 @@ class User(AbstractBaseUser, PermissionMixin):
                 if self.attributes.last_name != self.last_name:
                     self.attributes.last_name = self.last_name
 
+            try:
+                self.attributes.phone
+            except AttributeError:
+                pass
+            else:
+                if self.attributes.phone != self.phone:
+                    self.attributes.phone = self.phone
+
     def can_change_password(self):
         return True
 
diff --git a/tests/test_user_model.py b/tests/test_user_model.py
index 36c7fe017..b288e7a4b 100644
--- a/tests/test_user_model.py
+++ b/tests/test_user_model.py
@@ -89,6 +89,19 @@ def test_sync_first_name(db, settings):
     assert user.attributes.first_name == 'John Paul'
 
 
+def test_sync_phone(db, settings):
+    Attribute.objects.get_or_create(name='phone', defaults={'label': 'Phone', 'kind': 'phone_number'})
+
+    user = User(username='john.doe', email='john.doe2@example.net')
+    user.save()
+    user.phone = '+33123456789'
+    user.save()
+    assert user.attributes.phone == '+33123456789'
+
+    user.attributes.phone = '+43876543210'
+    assert user.attributes.phone == '+43876543210'
+
+
 def test_save_does_not_reset_verified_attributes_first_name(db):
     user = User.objects.create()
     user.verified_attributes.first_name = 'John'
@@ -167,6 +180,46 @@ def test_save_does_not_reset_verified_attributes_last_name(db):
     assert user.attributes.last_name == 'John'
 
 
+def test_save_does_not_reset_verified_attributes_phone(db):
+    Attribute.objects.get_or_create(name='phone', defaults={'label': 'Phone', 'kind': 'phone_number'})
+    user = User.objects.create()
+    user.verified_attributes.phone = '+33123456789'
+
+    user = User.objects.get()
+    assert user.phone == '+33123456789'
+    assert user.is_verified.phone
+    assert user.verified_attributes.phone == '+33123456789'
+    assert user.attributes.phone == '+33123456789'
+    user.save()
+
+    user = User.objects.get()
+    assert user.phone == '+33123456789'
+    assert user.is_verified.phone
+    assert user.verified_attributes.phone == '+33123456789'
+    assert user.attributes.phone == '+33123456789'
+    user.phone = '+43876543210'
+    user.save()
+    assert not user.is_verified.phone
+    assert user.verified_attributes.phone is None
+    assert user.attributes.phone == '+43876543210'
+
+    user = User.objects.get()
+    assert user.phone == '+43876543210'
+    assert not user.is_verified.phone
+    assert user.verified_attributes.phone is None
+    assert user.attributes.phone == '+43876543210'
+    user.verified_attributes.phone = '+33123456789'
+    assert user.is_verified.phone
+    assert user.verified_attributes.phone == '+33123456789'
+    assert user.attributes.phone == '+33123456789'
+
+    user = User.objects.get()
+    assert user.phone == '+33123456789'
+    assert user.is_verified.phone
+    assert user.verified_attributes.phone == '+33123456789'
+    assert user.attributes.phone == '+33123456789'
+
+
 def test_fix_attributes(db):
     first_name_attribute = Attribute.objects.get(name='first_name')
     user = User.objects.create(first_name='john', last_name='Doe')
-- 
2.37.2

