From f17f42e2d784cf236b6779a50124ae737718c1c2 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Thu, 12 Sep 2019 16:59:37 +0200
Subject: [PATCH] csv_import: import validated date field (#35800)

---
 src/authentic2/csv_import.py |  2 +-
 tests/test_user_manager.py   | 76 ++++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+), 1 deletion(-)

diff --git a/src/authentic2/csv_import.py b/src/authentic2/csv_import.py
index 4a768623..563b312f 100644
--- a/src/authentic2/csv_import.py
+++ b/src/authentic2/csv_import.py
@@ -442,7 +442,7 @@ class UserCsvImporter(object):
             CsvCell(
                 line=line,
                 header=header,
-                value=data.get(header.name),
+                value=form.cleaned_data.get(header.name),
                 missing=header.name not in data,
                 errors=get_form_errors(form, header.name))
             for header in self.headers]
diff --git a/tests/test_user_manager.py b/tests/test_user_manager.py
index 5a98b778..7a6a880e 100644
--- a/tests/test_user_manager.py
+++ b/tests/test_user_manager.py
@@ -15,6 +15,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 import csv
+import re
 import time
 
 import pytest
@@ -350,3 +351,78 @@ def test_su_superuser_dialog(app, app_factory, superuser, simple_user):
     new_app = app_factory()
     new_app.get(su_url).maybe_follow()
     assert new_app.session['_auth_user_id'] == str(simple_user.pk)
+
+@skipif_sqlite
+def test_user_import_attributes(transactional_db, app, admin, media):
+    Attribute.objects.create(name='more', kind='string', label='Signe particulier')
+    Attribute.objects.create(name='title', kind='title', label='Titre')
+    Attribute.objects.create(name='bike', kind='boolean', label='Vélo')
+    Attribute.objects.create(name='saintsday', kind='date', label='Fête')
+    Attribute.objects.create(name='birthdate', kind='birthdate', label='Date de naissance')
+    Attribute.objects.create(name='zip', kind='fr_postcode', label='Code postal (français)')
+    Attribute.objects.create(name='phone', kind='phone_number', label='Numéro de téléphone')
+    assert Attribute.objects.count() == 9
+    user_count = User.objects.count()
+    login(app, admin, '/manage/users/')
+
+    def import_csv(csv_content):
+        response = app.get('/manage/users/')
+        response = response.click('Import users')
+        index = [i for i in response.forms if 'import_file' in response.forms[i].fields][0]
+        response.forms[index].set(
+            'import_file',
+            Upload('users.csv', csv_content.encode('utf-8'), 'application/octet-stream'))
+        response.forms[index].set('encoding', 'utf-8')
+        response.forms[index].set('ou', str(get_default_ou().pk))
+        response = response.forms[index].submit().follow()
+        response = response.forms['action-form'].submit(name='execute').follow()
+
+        start = time.time()
+        response = response.click('Users Import')
+        while 'Running' in response.text:
+            response = response.click('Users Import')
+            assert time.time() - start < 2
+            time.sleep(.1)
+
+        # report
+        urls = re.findall('<a href="(/manage/users/import/[^/]+/[^/]+/)">', response.content)
+        response = app.get(urls[0])
+        return response
+
+    csv_lines = [
+        u"email key verified,first_name,last_name,more,title,bike,saintsday,birthdate,zip,phone",
+        u"elliot@universalpictures.com,Elliott,Thomas,petit,Mr,True,2019-7-20,1972-05-26,75014,1234",
+        u"et@universalpictures.com,ET,the Extra-Terrestrial,long,??,False,1/2/3/4,0002-2-22,42,home"]
+    response = import_csv('\n'.join(csv_lines))
+    urls = re.findall('<a href="(/manage/users/import/[^/]+/[^/]+/)">', response.content)
+    response = app.get(urls[0])
+    assert 'Select a valid choice. ?? is not one of the available choices.' in response.content
+    assert 'Enter a valid date.' in response.content
+    assert 'birthdate must be in the past and greater or equal than 1900-01-01.' in response.content
+    assert 'The value must be a valid french postcode' in response.content
+    assert 'Phone number can start with a + an must contain only digits' in response.content
+
+    assert User.objects.count() == user_count + 1
+    elliot = User.objects.filter(email='elliot@universalpictures.com')[0]
+    assert elliot.attributes.values['more'].content == 'petit'
+    assert elliot.attributes.values['title'].content == 'Mr'
+    assert elliot.attributes.values['bike'].content == '1'
+    assert elliot.attributes.values['saintsday'].content == '2019-07-20'
+    assert elliot.attributes.values['birthdate'].content == '1972-05-26'
+    assert elliot.attributes.values['zip'].content == '75014'
+    assert elliot.attributes.values['phone'].content == '1234'
+
+    csv_lines[2] = \
+        u"et@universalpictures.com,ET,the Extra-Terrestrial,,,,,,42000,+888 5678"
+    response = import_csv('\n'.join(csv_lines))
+    assert '0 rows have errors' in response.content
+
+    assert User.objects.count() == user_count + 2
+    et = User.objects.filter(email='et@universalpictures.com')[0]
+    assert et.attributes.values['more'].content == ''
+    assert et.attributes.values['title'].content == ''
+    assert et.attributes.values['bike'].content == '0'
+    assert 'saintsday' not in et.attributes.values
+    assert 'birthdate' not in et.attributes.values
+    assert et.attributes.values['zip'].content == '42000'
+    assert et.attributes.values['phone'].content == '+8885678'
-- 
2.23.0

