From 015a86841b83897dfcc3fae6de06a5586d64bd38 Mon Sep 17 00:00:00 2001
From: Paul Marillonnet <pmarillonnet@entrouvert.com>
Date: Wed, 8 Jun 2022 10:29:21 +0200
Subject: [PATCH 3/3] api_views: set api as verification source for custom
 attributes (#65612)

---
 src/authentic2/api_views.py | 27 +++++++++++++++------------
 tests/api/test_all.py       | 12 ++++++++++++
 2 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/src/authentic2/api_views.py b/src/authentic2/api_views.py
index e126614d1..196aa2809 100644
--- a/src/authentic2/api_views.py
+++ b/src/authentic2/api_views.py
@@ -411,14 +411,16 @@ class BaseUserSerializer(serializers.ModelSerializer):
         if not getattr(instance, '_a2_created', True):
             return instance
         for key, value in attributes.items():
-            if is_verified.get(key):
-                setattr(instance.verified_attributes, key, value)
-            else:
-                setattr(instance.attributes, key, value)
+            verified = True if is_verified.get(key) else False
+            accessor = instance.verified_attributes if verified else instance.attributes
+            accessor._set_sourced_attr(key, value, 'api')
+        instance.refresh_from_db()
         if is_verified.get('first_name'):
             instance.verified_attributes.first_name = instance.first_name
+            Attribute.add_verification_source(instance, 'first_name', instance.first_name, 'api')
         if is_verified.get('last_name'):
             instance.verified_attributes.last_name = instance.last_name
+            Attribute.add_verification_source(instance, 'last_name', instance.last_name, 'api')
         if password is not None:
             instance.set_password(password)
         else:
@@ -466,20 +468,21 @@ class BaseUserSerializer(serializers.ModelSerializer):
             instance.set_email_verified(False)
         super().update(instance, validated_data)
         for key, value in attributes.items():
-            if is_verified.get(key):
-                setattr(instance.verified_attributes, key, value)
-            else:
-                setattr(instance.attributes, key, value)
+            verified = True if is_verified.get(key) else False
+            accessor = instance.verified_attributes if verified else instance.attributes
+            accessor._set_sourced_attr(key, value, 'api')
         for key in is_verified:
             if key not in attributes:
-                if is_verified.get(key):
-                    setattr(instance.verified_attributes, key, getattr(instance.attributes, key))
-                else:
-                    setattr(instance.attributes, key, getattr(instance.attributes, key))
+                verified = True if is_verified.get(key) else False
+                accessor = instance.verified_attributes if verified else instance.attributes
+                accessor._set_sourced_attr(key, getattr(instance.attributes, key), 'api')
+        instance.refresh_from_db()
         if is_verified.get('first_name'):
             instance.verified_attributes.first_name = instance.first_name
+            Attribute.add_verification_source(instance, 'first_name', instance.first_name, 'api')
         if is_verified.get('last_name'):
             instance.verified_attributes.last_name = instance.last_name
+            Attribute.add_verification_source(instance, 'last_name', instance.last_name, 'api')
         if password is not None:
             instance.set_password(password)
             instance.save()
diff --git a/tests/api/test_all.py b/tests/api/test_all.py
index 6cf4b0392..0dd05e1f7 100644
--- a/tests/api/test_all.py
+++ b/tests/api/test_all.py
@@ -569,6 +569,18 @@ def test_api_users_create(settings, app, api_user):
         assert user.verified_attributes.first_name == 'John'
         assert user.verified_attributes.last_name == 'Doe'
         assert user.verified_attributes.title == 'Mr'
+        first_name = Attribute.objects.get(name='first_name')
+        last_name = Attribute.objects.get(name='last_name')
+        title = Attribute.objects.get(name='title')
+        first_name_value = AttributeValue.objects.with_owner(user).get(attribute=first_name)
+        last_name_value = AttributeValue.objects.with_owner(user).get(attribute=last_name)
+        title_value = AttributeValue.objects.with_owner(user).get(attribute=title)
+        assert first_name_value.last_verified_on
+        assert last_name_value.last_verified_on
+        assert title_value.last_verified_on
+        assert first_name_value.verification_sources == ['api']
+        assert last_name_value.verification_sources == ['api']
+        assert title_value.verification_sources == ['api']
         assert resp.json['first_name_verified']
         assert resp.json['last_name_verified']
         assert resp.json['title_verified']
-- 
2.36.1

