Projet

Général

Profil

0002-csv-set-verification-source-for-user-attributes-6605.patch

Paul Marillonnet, 06 décembre 2022 11:57

Télécharger (4,33 ko)

Voir les différences:

Subject: [PATCH 2/2] csv: set verification source for user attributes (#66053)

 src/authentic2/csv_import.py | 31 +++++++++++++++++++++++++------
 tests/test_csv_import.py     | 34 +++++++++++++++++++++++++++++++++-
 2 files changed, 58 insertions(+), 7 deletions(-)
src/authentic2/csv_import.py
739 739
                row.action == 'update' and cell.header.update
740 740
            ):
741 741
                attributes = user.attributes
742
                if cell.header.verified:
743
                    attributes = user.verified_attributes
744
                if getattr(attributes, cell.header.name) != cell.value:
745
                    setattr(attributes, cell.header.name, cell.value)
746
                    cell.action = 'updated'
747
                    continue
742
                dummy = AttributeValue.objects.with_owner(user).select_for_update()
743
                updated = False
744

  
745
                with atomic():
746
                    if cell.header.verified:
747
                        attributes = user.verified_attributes
748
                        is_known_unverified_value = False
749
                        unverified_value = cell.header.attribute.get_value(user, verified=False)
750
                        if unverified_value:
751
                            is_known_unverified_value = (
752
                                cell.value in unverified_value
753
                                if cell.header.attribute.multiple
754
                                else cell.value == unverified_value
755
                            )
756
                        if getattr(attributes, cell.header.name) != cell.value or is_known_unverified_value:
757
                            attributes._set_sourced_attr(cell.header.name, cell.value, 'csv')
758
                        updated = True
759
                    else:
760
                        # TODO remove only 'csv' among verification sources
761
                        setattr(attributes, cell.header.name, cell.value)
762
                        updated = True
763

  
764
                    if updated:
765
                        cell.action = 'updated'
766
                        continue
748 767
            cell.action = 'nothing'
749 768

  
750 769
        for cell in row.cells:
tests/test_csv_import.py
27 27
from authentic2.a2_rbac.utils import get_default_ou
28 28
from authentic2.csv_import import CsvHeader, CsvImporter, Error, LineError, UserCsvImporter
29 29
from authentic2.custom_user.models import User
30
from authentic2.models import Attribute, PasswordReset
30
from authentic2.models import Attribute, AttributeValue, PasswordReset
31 31

  
32 32
ENCODINGS = [
33 33
    'iso-8859-1',
......
701 701
    user2.refresh_from_db()
702 702
    assert user1.last_name == 'Doe'
703 703
    assert user2.last_name == 'Doe2'
704

  
705

  
706
def test_user_attributes_verified(db, profile, user_csv_importer_factory):
707
    content = '''email key,first_name,last_name,phone verified
708
jdoe@nowhere.null,John,Doe,1234
709
jsmith@nowhere.null,Jimmy,Smith,5678'''
710
    importer = user_csv_importer_factory(content)
711
    phone = Attribute.objects.get(name='phone')
712

  
713
    assert importer.run(), importer.errors
714
    assert importer.headers == [
715
        CsvHeader(1, 'email', field=True, key=True, verified=True),
716
        CsvHeader(2, 'first_name', field=True),
717
        CsvHeader(3, 'last_name', field=True),
718
        CsvHeader(4, 'phone', attribute=phone, verified=True),
719
    ]
720

  
721
    jdoe = User.objects.get(email='jdoe@nowhere.null')
722
    assert jdoe.verified_attributes.phone == '1234'
723

  
724
    jsmith = User.objects.get(email='jsmith@nowhere.null')
725
    assert jsmith.verified_attributes.phone == '5678'
726

  
727
    for user in (
728
        jdoe,
729
        jsmith,
730
    ):
731
        attribute = Attribute.objects.get(name='phone')
732
        av = AttributeValue.objects.with_owner(user).get(attribute=attribute)
733
        assert av.verified
734
        assert av.verification_sources == ['csv']
735
        assert av.last_verified_on
704
-