zoo/tests/test_nanterre_synchronize_f...

63 lines
2.2 KiB
Python

# -*- coding: utf-8 -*-
import csv
from django.utils import six
from django.utils.encoding import force_bytes
from django.utils.six import StringIO
from webtest import Upload
def test_synchronize_federations(settings, app, nanterre_classic_family, admin):
settings.NANTERRE_SYNCHRONIZE_FEDERATIONS_DO_LATER = False
f = nanterre_classic_family
# ajout de clés de fédération technocarte
for entity in f.values():
entity.content.setdefault('cles_de_federation', {})['technocarte'] = str(entity.id + 1000)
entity.save()
response = app.get('/admin/').follow()
response.form.set('username', 'admin')
response.form.set('password', 'admin')
response = response.form.submit().follow()
response = response.click(u'Synchroniser les fédérations')
response = response.click(u'Nouvel import')
response.form.set('app_id', 'technocarte')
content = force_bytes('\n'.join(map(str, [f['kevin'].id + 1000, f['marie'].id + 1000, '99999'])))
response.form.set('csv_uploaded', Upload('federations.csv', content, 'application/octet-stream'))
response = response.form.submit().follow()
assert len(response.pyquery('table#result-list tbody tr')) == 1
response = response.click('Rapport')
def check_csv_response(csv_response):
if six.PY3:
reader = csv.DictReader(StringIO(csv_response.text))
reader.fieldnames = reader.reader.__next__()
else:
reader = csv.DictReader(StringIO(csv_response.content))
reader.fieldnames = reader.reader.next()
rows = list(reader)
def rows_by_action(action):
return [row for row in rows if row['action'] == action]
assert len(rows_by_action('KEEP')) == 2
assert len(rows_by_action('DELETE')) == 2
assert len(rows_by_action('UNKNOWN')) == 1
assert len(set(row['action'] for row in rows)) == 3
csv_response = response.click('CSV')
check_csv_response(csv_response)
response = response.click(u'Synchroniser les fédérations')
response = response.forms[0].submit().follow()
response = response.click(u'Rapport d\'exécution')
csv_response = response.click('CSV')
check_csv_response(csv_response)