From d6fa31b7863168bdeb050e8074dc3734ee8ba35c Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Mon, 13 Jul 2020 23:09:12 +0200 Subject: [PATCH 2/4] csv_import: ignore empty values when checking uniqueness (#44805) --- src/authentic2/csv_import.py | 5 ++++ tests/test_csv_import.py | 45 ++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/authentic2/csv_import.py b/src/authentic2/csv_import.py index 7e682c1c..96558a36 100644 --- a/src/authentic2/csv_import.py +++ b/src/authentic2/csv_import.py @@ -576,6 +576,9 @@ class UserCsvImporter(object): if header.name == SOURCE_ID: unique_key = (SOURCE_ID, row[SOURCE_NAME].value, cell.value) elif header.key or header.globally_unique or header.unique: + if not cell.value: + # empty values are not checked + continue unique_key = (header.name, cell.value) else: continue @@ -595,6 +598,8 @@ class UserCsvImporter(object): for cell in row: if (not cell.header.globally_unique and not cell.header.unique) or (user and not cell.header.update): continue + if not cell.value: + continue qs = ou_users if cell.header.globally_unique: qs = users diff --git a/tests/test_csv_import.py b/tests/test_csv_import.py index 70cbc602..97fdf2ec 100644 --- a/tests/test_csv_import.py +++ b/tests/test_csv_import.py @@ -518,3 +518,48 @@ tnoel@entrouvert.com,Thomas,Noël,''' assert importer.run() assert importer.has_errors assert importer.rows[0].cells[-1].errors[0].code == 'data-error' + + +def test_check_empty_value_for_uniqueness(profile, user_csv_importer_factory): + ou = get_default_ou() + ou.username_is_unique = True + ou.email_is_unique = True + ou.save() + + Attribute.objects.update(required=False) + User.objects.create(username='john.doe', email='john.doe@gmail.com') + + content = '''_source_name,_source_id,username,email,first_name,last_name +remote,1,,john.doe1@gmail.com,John,Doe1 +remote,2,john.doe2,,John,Doe2 +remote,3,,,John,Doe3 +remote,4,,,John,Doe4 +remote,5,,john.doe1@gmail.com,, +remote,6,john.doe2,,, +remote,7,,john.doe@gmail.com,, +remote,8,john.doe,,, +''' + for i, line in enumerate(content.splitlines()): + print(i, line.count(',')) + importer = user_csv_importer_factory(content) + importer.run() + assert importer.has_errors + assert not importer.errors + assert importer.headers_by_name['username'].unique + assert importer.headers_by_name['username'].globally_unique + assert importer.headers_by_name['email'].unique + assert not importer.headers_by_name['email'].globally_unique + row_errors = {row.line: [error.description for error in row.errors] for row in importer.rows if row.errors} + assert row_errors == { + 6: ['Unique constraint on column "email" failed: value already appear on line 2', + 'Unique constraint on column "email" failed'], + 7: ['Unique constraint on column "username" failed: value already appear on line 3'], + 9: ['Unique constraint on column "username" failed'] + } + cell_errors = { + row.line: { + cell.header.name: [error for error in cell.errors] for cell in row.cells if cell.errors + } + for row in importer.rows if any(cell.errors for cell in row.cells) + } + assert cell_errors == {} -- 2.27.0