15 |
15 |
# You should have received a copy of the GNU Affero General Public License
|
16 |
16 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 |
17 |
import csv
|
|
18 |
import re
|
18 |
19 |
import time
|
19 |
20 |
|
20 |
21 |
import pytest
|
... | ... | |
350 |
351 |
new_app = app_factory()
|
351 |
352 |
new_app.get(su_url).maybe_follow()
|
352 |
353 |
assert new_app.session['_auth_user_id'] == str(simple_user.pk)
|
|
354 |
|
|
355 |
@skipif_sqlite
|
|
356 |
def test_user_import_attributes(transactional_db, app, admin, media):
|
|
357 |
Attribute.objects.create(name='more', kind='string', label='Signe particulier')
|
|
358 |
Attribute.objects.create(name='title', kind='title', label='Titre')
|
|
359 |
Attribute.objects.create(name='bike', kind='boolean', label='Vélo')
|
|
360 |
Attribute.objects.create(name='saintsday', kind='date', label='Fête')
|
|
361 |
Attribute.objects.create(name='birthdate', kind='birthdate', label='Date de naissance')
|
|
362 |
Attribute.objects.create(name='zip', kind='fr_postcode', label='Code postal (français)')
|
|
363 |
Attribute.objects.create(name='phone', kind='phone_number', label='Numéro de téléphone')
|
|
364 |
assert Attribute.objects.count() == 9
|
|
365 |
user_count = User.objects.count()
|
|
366 |
login(app, admin, '/manage/users/')
|
|
367 |
|
|
368 |
def import_csv(csv_content):
|
|
369 |
response = app.get('/manage/users/')
|
|
370 |
response = response.click('Import users')
|
|
371 |
index = [i for i in response.forms if 'import_file' in response.forms[i].fields][0]
|
|
372 |
response.forms[index].set(
|
|
373 |
'import_file',
|
|
374 |
Upload('users.csv', csv_content.encode('utf-8'), 'application/octet-stream'))
|
|
375 |
response.forms[index].set('encoding', 'utf-8')
|
|
376 |
response.forms[index].set('ou', str(get_default_ou().pk))
|
|
377 |
response = response.forms[index].submit().follow()
|
|
378 |
response = response.forms['action-form'].submit(name='execute').follow()
|
|
379 |
|
|
380 |
start = time.time()
|
|
381 |
response = response.click('Users Import')
|
|
382 |
while 'Running' in response.text:
|
|
383 |
response = response.click('Users Import')
|
|
384 |
assert time.time() - start < 2
|
|
385 |
time.sleep(.1)
|
|
386 |
|
|
387 |
# report
|
|
388 |
urls = re.findall('<a href="(/manage/users/import/[^/]+/[^/]+/)">', response.content)
|
|
389 |
response = app.get(urls[0])
|
|
390 |
return response
|
|
391 |
|
|
392 |
csv_lines = [
|
|
393 |
u"email key verified,first_name,last_name,more,title,bike,saintsday,birthdate,zip,phone",
|
|
394 |
u"elliot@universalpictures.com,Elliott,Thomas,petit,Mr,True,2019-7-20,1972-05-26,75014,1234",
|
|
395 |
u"et@universalpictures.com,ET,the Extra-Terrestrial,long,??,False,1/2/3/4,0002-2-22,42,home"]
|
|
396 |
response = import_csv('\n'.join(csv_lines))
|
|
397 |
urls = re.findall('<a href="(/manage/users/import/[^/]+/[^/]+/)">', response.content)
|
|
398 |
response = app.get(urls[0])
|
|
399 |
assert 'Select a valid choice. ?? is not one of the available choices.' in response.content
|
|
400 |
assert 'Enter a valid date.' in response.content
|
|
401 |
assert 'birthdate must be in the past and greater or equal than 1900-01-01.' in response.content
|
|
402 |
assert 'The value must be a valid french postcode' in response.content
|
|
403 |
assert 'Phone number can start with a + an must contain only digits' in response.content
|
|
404 |
|
|
405 |
assert User.objects.count() == user_count + 1
|
|
406 |
elliot = User.objects.filter(email='elliot@universalpictures.com')[0]
|
|
407 |
assert elliot.attributes.values['more'].content == 'petit'
|
|
408 |
assert elliot.attributes.values['title'].content == 'Mr'
|
|
409 |
assert elliot.attributes.values['bike'].content == '1'
|
|
410 |
assert elliot.attributes.values['saintsday'].content == '2019-07-20'
|
|
411 |
assert elliot.attributes.values['birthdate'].content == '1972-05-26'
|
|
412 |
assert elliot.attributes.values['zip'].content == '75014'
|
|
413 |
assert elliot.attributes.values['phone'].content == '1234'
|
|
414 |
|
|
415 |
csv_lines[2] = \
|
|
416 |
u"et@universalpictures.com,ET,the Extra-Terrestrial,,,,,,42000,+888 5678"
|
|
417 |
response = import_csv('\n'.join(csv_lines))
|
|
418 |
assert '0 rows have errors' in response.content
|
|
419 |
|
|
420 |
assert User.objects.count() == user_count + 2
|
|
421 |
et = User.objects.filter(email='et@universalpictures.com')[0]
|
|
422 |
assert et.attributes.values['more'].content == ''
|
|
423 |
assert et.attributes.values['title'].content == ''
|
|
424 |
assert et.attributes.values['bike'].content == '0'
|
|
425 |
assert 'saintsday' not in et.attributes.values
|
|
426 |
assert 'birthdate' not in et.attributes.values
|
|
427 |
assert et.attributes.values['zip'].content == '42000'
|
|
428 |
assert et.attributes.values['phone'].content == '+8885678'
|
353 |
|
-
|