Projet

Général

Profil

0001-misc-split-backoffice-pages-tests.patch

Lauréline Guérin, 23 octobre 2020 16:28

Télécharger (30,8 ko)

Voir les différences:

Subject: [PATCH 1/3] misc: split backoffice pages tests

 tests/backoffice_pages/test_all.py    | 309 ----------------------
 tests/backoffice_pages/test_export.py | 363 ++++++++++++++++++++++++++
 2 files changed, 363 insertions(+), 309 deletions(-)
 create mode 100644 tests/backoffice_pages/test_export.py
tests/backoffice_pages/test_all.py
5 5
import re
6 6
import time
7 7
import random
8
import xml.etree.ElementTree as ET
9 8
import zipfile
10 9

  
11 10
import mock
......
18 17

  
19 18
from django.utils.six import StringIO, BytesIO
20 19
from django.utils.six.moves.urllib import parse as urllib
21
from django.utils.six.moves.urllib import parse as urlparse
22 20

  
23 21
from webtest import Upload
24 22

  
25 23
from quixote import get_publisher
26 24
from quixote.http_request import Upload as QuixoteUpload
27
from wcs.qommon import ods
28 25
from wcs.api_utils import sign_url
29 26
from wcs.blocks import BlockDef
30 27
from wcs.qommon.form import PicklableUpload
......
1363 1360
    assert resp.text.count('>userB<') == 0
1364 1361

  
1365 1362

  
1366
def test_backoffice_csv(pub):
1367
    create_superuser(pub)
1368
    create_environment(pub)
1369
    app = login(get_app(pub))
1370
    resp = app.get('/backoffice/management/form-title/')
1371
    resp = resp.click('Export as CSV File')
1372
    assert resp.headers['content-type'].startswith('text/')
1373
    assert len(resp.text.splitlines()) == 18 # 17 + header line
1374
    assert len(resp.text.splitlines()[0].split(',')) == 7
1375

  
1376
    formdef = FormDef.get_by_urlname('form-title')
1377
    formdef.fields[-1].display_locations = ['validation', 'summary', 'listings']
1378
    formdef.store()
1379
    resp = app.get('/backoffice/management/form-title/')
1380
    resp = resp.click('Export as CSV File')
1381
    assert len(resp.text.splitlines()[0].split(',')) == 9
1382

  
1383
    # check item fields with datasources get two columns (id & text)
1384
    assert resp.text.splitlines()[0].split(',')[6] == '3rd field'
1385
    assert resp.text.splitlines()[0].split(',')[7] == '' # 3rd field, continue
1386
    assert resp.text.splitlines()[1].split(',')[6] == 'A'
1387
    assert resp.text.splitlines()[1].split(',')[7] == 'aa'
1388

  
1389
    resp = app.get('/backoffice/management/form-title/')
1390
    resp.forms['listing-settings']['filter'] = 'all'
1391
    resp = resp.forms['listing-settings'].submit()
1392
    resp_csv = resp.click('Export as CSV File')
1393
    assert len(resp_csv.text.splitlines()) == 51
1394

  
1395
    # test status filter
1396
    resp.forms['listing-settings']['filter'] = 'pending'
1397
    resp.forms['listing-settings']['filter-2'].checked = True
1398
    resp = resp.forms['listing-settings'].submit()
1399
    resp.forms['listing-settings']['filter-2-value'] = 'baz'
1400
    resp = resp.forms['listing-settings'].submit()
1401
    resp_csv = resp.click('Export as CSV File')
1402
    assert len(resp_csv.text.splitlines()) == 9
1403

  
1404
    # test criteria filters
1405
    resp.forms['listing-settings']['filter-start'].checked = True
1406
    resp = resp.forms['listing-settings'].submit()
1407
    resp.forms['listing-settings']['filter-start-value'] = datetime.datetime(2015, 2, 1).strftime('%Y-%m-%d')
1408
    resp = resp.forms['listing-settings'].submit()
1409
    resp_csv = resp.click('Export as CSV File')
1410
    assert len(resp_csv.text.splitlines()) == 1
1411

  
1412
    resp.forms['listing-settings']['filter-start-value'] = datetime.datetime(2014, 2, 1).strftime('%Y-%m-%d')
1413
    resp = resp.forms['listing-settings'].submit()
1414
    resp.forms['listing-settings']['filter-2-value'] = 'baz'
1415
    resp = resp.forms['listing-settings'].submit()
1416
    resp_csv = resp.click('Export as CSV File')
1417
    assert len(resp_csv.text.splitlines()) == 9
1418
    assert 'Created' in resp_csv.text.splitlines()[0]
1419

  
1420
    # test column selection
1421
    resp.forms['listing-settings']['time'].checked = False
1422
    resp = resp.forms['listing-settings'].submit()
1423
    resp_csv = resp.click('Export as CSV File')
1424
    assert 'Created' not in resp_csv.text.splitlines()[0]
1425

  
1426

  
1427
def test_backoffice_export_long_listings(pub):
1428
    create_superuser(pub)
1429
    create_environment(pub)
1430
    formdef = FormDef.get_by_urlname('form-title')
1431
    for i in range(100):
1432
        formdata = formdef.data_class()()
1433
        formdata.just_created()
1434
        formdata.receipt_time = datetime.datetime(2015, 1, 1).timetuple()
1435
        formdata.data = {'1': 'BAZ BAZ %d' % i}
1436
        formdata.jump_status('new')
1437
        formdata.store()
1438

  
1439
    app = login(get_app(pub))
1440
    resp = app.get('/backoffice/management/form-title/')
1441
    resp = resp.click('Export as CSV File')
1442
    assert resp.location.startswith('http://example.net/backoffice/management/form-title/export?job=')
1443
    resp = resp.follow()
1444
    assert 'completed' in resp.text
1445
    resp = resp.click('Download Export')
1446
    resp_lines = resp.text.splitlines()
1447
    assert resp_lines[0] == 'Number,Created,Last Modified,User Label,1st field,2nd field,Status'
1448
    assert len(resp_lines) == 118
1449
    assert resp_lines[1].split(',')[1].startswith(
1450
            time.strftime('%Y-%m-%d', formdata.receipt_time))
1451
    assert resp_lines[1].split(',')[2].startswith(
1452
            time.strftime('%Y-%m-%d', formdata.last_update_time))
1453

  
1454
    resp = app.get('/backoffice/management/form-title/')
1455
    resp = resp.click('Export a Spreadsheet')
1456
    assert resp.location.startswith('http://example.net/backoffice/management/form-title/export?job=')
1457
    job_id = urlparse.parse_qs(urlparse.urlparse(resp.location).query)['job'][0]
1458
    resp = resp.follow()
1459
    assert 'completed' in resp.text
1460
    resp = resp.click('Download Export')
1461
    assert resp.content_type == 'application/vnd.oasis.opendocument.spreadsheet'
1462

  
1463
    # check afterjob ajax call
1464
    status_resp = app.get('/afterjobs/' + job_id)
1465
    assert status_resp.text == 'completed|completed'
1466

  
1467
    # check error handling
1468
    app.get('/afterjobs/whatever', status=404)
1469

  
1470

  
1471
def test_backoffice_csv_export_channel(pub):
1472
    if not pub.site_options.has_section('variables'):
1473
        pub.site_options.add_section('variables')
1474
    pub.site_options.set('variables', 'welco_url', 'xxx')
1475
    fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w')
1476
    pub.site_options.write(fd)
1477
    fd.close()
1478

  
1479
    create_superuser(pub)
1480
    create_environment(pub)
1481
    app = login(get_app(pub))
1482
    resp = app.get('/backoffice/management/form-title/')
1483
    resp_csv = resp.click('Export as CSV File')
1484
    assert 'Channel' not in resp_csv.text.splitlines()[0]
1485

  
1486
    # add submission channel column
1487
    resp.forms['listing-settings']['submission_channel'].checked = True
1488
    resp = resp.forms['listing-settings'].submit()
1489
    resp_csv = resp.click('Export as CSV File')
1490
    assert resp_csv.text.splitlines()[0].split(',')[-1] == 'Channel'
1491
    assert resp_csv.text.splitlines()[1].split(',')[-1] == 'Web'
1492

  
1493

  
1494
def test_backoffice_csv_export_anonymised(pub):
1495
    fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w')
1496
    pub.site_options.write(fd)
1497
    fd.close()
1498

  
1499
    create_superuser(pub)
1500
    create_environment(pub)
1501
    app = login(get_app(pub))
1502
    resp = app.get('/backoffice/management/form-title/')
1503
    resp_csv = resp.click('Export as CSV File')
1504
    assert resp_csv.text.splitlines()[0].split(',')[-1] != 'Anonymised'
1505

  
1506
    # add anonymised column
1507
    resp.forms['listing-settings']['anonymised'].checked = True
1508
    resp = resp.forms['listing-settings'].submit()
1509
    resp_csv = resp.click('Export as CSV File')
1510
    assert resp_csv.text.splitlines()[0].split(',')[-1] == 'Anonymised'
1511
    assert resp_csv.text.splitlines()[1].split(',')[-1] == 'No'
1512

  
1513

  
1514
def test_backoffice_csv_export_block(pub):
1515
    create_superuser(pub)
1516
    create_environment(pub)
1517

  
1518
    block = BlockDef()
1519
    block.name = 'foobar'
1520
    block.fields = [
1521
        fields.StringField(id='123', required=True, label='Test', type='string', varname='foo'),
1522
        fields.StringField(id='234', required=True, label='Test2', type='string', varname='bar'),
1523
    ]
1524
    block.digest_template = 'X{{foobar_var_foo}}Y'
1525
    block.store()
1526

  
1527
    formdef = FormDef.get_by_urlname('form-title')
1528
    formdef.fields = []
1529
    formdef.store()  # make sure sql columns are removed
1530

  
1531
    formdef.fields = [
1532
        fields.BlockField(id='1', label='test', type='block:foobar', max_items=3),
1533
    ]
1534
    formdef.store()
1535

  
1536
    formdef.data_class().wipe()
1537
    formdata = formdef.data_class()()
1538
    formdata.data = {}
1539
    formdata.data['1'] = {
1540
        'data': [
1541
            {'123': 'foo', '234': 'bar'},
1542
            {'123': 'foo2', '234': 'bar2'},
1543
        ],
1544
        'schema': {'123': 'string', '234': 'string'},
1545
    }
1546
    formdata.just_created()
1547
    formdata.jump_status('new')
1548
    formdata.store()
1549

  
1550
    app = login(get_app(pub))
1551
    resp = app.get('/backoffice/management/form-title/')
1552
    resp_csv = resp.click('Export as CSV File')
1553
    resp.forms['listing-settings']['1'].checked = True
1554
    resp = resp.forms['listing-settings'].submit()
1555
    resp_csv = resp.click('Export as CSV File')
1556
    assert resp_csv.text.splitlines()[0].split(',')[-3:] == ['test - 1', 'test - 2', 'test - 3']
1557
    assert resp_csv.text.splitlines()[1].split(',')[-3:] == ['XfooY', 'Xfoo2Y', '']
1558

  
1559

  
1560
def test_backoffice_ods(pub):
1561
    create_superuser(pub)
1562
    create_environment(pub)
1563
    app = login(get_app(pub))
1564
    resp = app.get('/backoffice/management/form-title/')
1565
    resp = resp.click('Export a Spreadsheet')
1566
    assert resp.headers['content-type'] == 'application/vnd.oasis.opendocument.spreadsheet'
1567
    assert 'filename=form-title.ods' in resp.headers['content-disposition']
1568
    assert resp.body[:2] == b'PK' # ods has a zip container
1569

  
1570
    formdef = FormDef.get_by_urlname('form-title')
1571
    formdef.fields.append(fields.FileField(id='4', label='file field', type='file',
1572
        display_locations=['validation', 'summary', 'listings']))
1573
    formdef.fields.append(fields.DateField(id='5', label='date field', type='date',
1574
        display_locations=['validation', 'summary', 'listings']))
1575
    formdef.fields.append(fields.StringField(id='6', label='number field', type='string',
1576
        display_locations=['validation', 'summary', 'listings']))
1577
    formdef.fields.append(fields.StringField(id='7', label='phone field', type='string',
1578
        display_locations=['validation', 'summary', 'listings']))
1579
    formdef.fields.append(fields.DateField(id='8', label='very old field', type='date',
1580
        display_locations=['validation', 'summary', 'listings']))
1581
    formdef.fields.append(fields.StringField(id='9', label='string field', type='string',
1582
        display_locations=['validation', 'summary', 'listings']))
1583
    formdef.fields.append(fields.StringField(id='10', label='number with comma field', type='string',
1584
        display_locations=['validation', 'summary', 'listings']))
1585
    formdef.store()
1586

  
1587
    formdata = formdef.data_class().select(lambda x: x.status == 'wf-new')[0]
1588
    formdata.data['4'] = PicklableUpload('/foo/bar', content_type='text/plain')
1589
    formdata.data['4'].receive([b'hello world'])
1590
    formdata.data['5'] = time.strptime('2015-05-12', '%Y-%m-%d')
1591
    formdata.data['6'] = '12345'
1592
    formdata.data['7'] = '0102030405'
1593
    formdata.data['8'] = time.strptime('1871-03-18', '%Y-%m-%d')
1594
    formdata.data['9'] = 'plop\npl\x1dop'  # with control characters
1595
    formdata.data['10'] = '123,45'
1596
    formdata.store()
1597

  
1598
    resp = app.get('/backoffice/management/form-title/')
1599
    resp = resp.click('Export a Spreadsheet')
1600
    assert resp.headers['content-type'] == 'application/vnd.oasis.opendocument.spreadsheet'
1601
    assert 'filename=form-title.ods' in resp.headers['content-disposition']
1602
    assert resp.body[:2] == b'PK' # ods has a zip container
1603

  
1604
    zipf = zipfile.ZipFile(BytesIO(resp.body))
1605
    ods_sheet = ET.parse(zipf.open('content.xml'))
1606
    # check the ods contains a link to the document
1607
    elem = ods_sheet.findall('.//{%s}a' % ods.NS['text'])[0]
1608
    assert elem.attrib['{%s}href' % ods.NS['xlink']] == 'http://example.net/backoffice/management/form-title/%s/files/4/bar' % formdata.id
1609
    resp = app.get(elem.attrib['{%s}href' % ods.NS['xlink']])
1610
    assert resp.text == 'hello world'
1611

  
1612
    all_texts = [x.text for x in ods_sheet.findall('.//{%s}table-row//{%s}p' % (ods.NS['table'], ods.NS['text']))]
1613
    created_column = all_texts.index('Created')
1614
    date_column = all_texts.index('date field')
1615
    number_column = all_texts.index('number field')
1616
    phone_column = all_texts.index('phone field')
1617
    old_column = all_texts.index('very old field')
1618
    string_column = all_texts.index('string field')
1619
    comma_number_column = all_texts.index('number with comma field')
1620

  
1621
    for row in ods_sheet.findall('.//{%s}table-row' % ods.NS['table']):
1622
        if row.findall('.//{%s}table-cell/{%s}p' % (
1623
                ods.NS['table'], ods.NS['text']))[0].text == formdata.get_display_id():
1624
            break
1625
    else:
1626
        assert False, 'failed to find data row'
1627

  
1628
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[created_column].attrib[
1629
            '{%s}value-type' % ods.NS['office']] == 'date'
1630
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[created_column].attrib[
1631
            '{%s}style-name' % ods.NS['table']] == 'DateTime'
1632
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[date_column].attrib[
1633
            '{%s}value-type' % ods.NS['office']] == 'date'
1634
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[date_column].attrib[
1635
            '{%s}style-name' % ods.NS['table']] == 'Date'
1636
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[number_column].attrib[
1637
            '{%s}value-type' % ods.NS['office']] == 'float'
1638
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[number_column].attrib[
1639
            '{%s}value' % ods.NS['office']] == '12345'
1640
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[phone_column].attrib[
1641
            '{%s}value-type' % ods.NS['office']] == 'string'
1642
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[old_column].attrib[
1643
            '{%s}value-type' % ods.NS['office']] == 'date'
1644
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[old_column].attrib[
1645
            '{%s}date-value' % ods.NS['office']] == '1871-03-18'
1646
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[
1647
            string_column].find('{%s}p' % ods.NS['text']).text == 'plop\nplop'
1648
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[comma_number_column].attrib[
1649
            '{%s}value' % ods.NS['office']] == '123.45'
1650

  
1651

  
1652
@pytest.mark.skipif('xlwt is None')
1653
def test_backoffice_xls(pub):
1654
    create_superuser(pub)
1655
    create_environment(pub)
1656
    app = login(get_app(pub))
1657
    resp = app.get('/backoffice/management/form-title/')
1658
    assert not 'Excel Export' in resp.text
1659

  
1660
    if not pub.site_options.has_section('options'):
1661
        pub.site_options.add_section('options')
1662
        pub.site_options.set('options', 'legacy-excel-export', 'true')
1663
        fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w')
1664
        pub.site_options.write(fd)
1665
        fd.close()
1666

  
1667
    resp = app.get('/backoffice/management/form-title/')
1668
    resp = resp.click('Excel Export')
1669
    assert resp.headers['content-type'].startswith('application/vnd.ms-excel')
1670

  
1671

  
1672 1363
def test_backoffice_statistics(pub):
1673 1364
    create_superuser(pub)
1674 1365
    create_environment(pub)
tests/backoffice_pages/test_export.py
1
# -*- coding: utf-8 -*-
2
import datetime
3
import os
4
import time
5
import xml.etree.ElementTree as ET
6
import zipfile
7

  
8
import pytest
9

  
10
try:
11
    import xlwt
12
except ImportError:
13
    xlwt = None
14

  
15
from django.utils.six import BytesIO
16
from django.utils.six.moves.urllib import parse as urlparse
17

  
18
from wcs.qommon import ods
19
from wcs.blocks import BlockDef
20
from wcs.qommon.form import PicklableUpload
21
from wcs.qommon.http_request import HTTPRequest
22
from wcs.formdef import FormDef
23
from wcs import fields
24

  
25
from utilities import get_app, login, create_temporary_pub, clean_temporary_pub
26
from test_all import create_superuser, create_environment
27

  
28

  
29
def pytest_generate_tests(metafunc):
30
    if 'pub' in metafunc.fixturenames:
31
        metafunc.parametrize('pub', ['pickle', 'sql', 'pickle-templates'], indirect=True)
32

  
33

  
34
@pytest.fixture
35
def pub(request, emails):
36
    pub = create_temporary_pub(
37
            sql_mode=bool('sql' in request.param),
38
            templates_mode=bool('templates' in request.param)
39
            )
40

  
41
    req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
42
    pub.set_app_dir(req)
43
    pub.cfg['identification'] = {'methods': ['password']}
44
    pub.cfg['language'] = {'language': 'en'}
45
    pub.write_cfg()
46
    fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w')
47
    fd.write('''
48
[api-secrets]
49
coucou = 1234
50
''')
51
    fd.close()
52

  
53
    return pub
54

  
55

  
56
def teardown_module(module):
57
    clean_temporary_pub()
58

  
59

  
60
def test_backoffice_csv(pub):
61
    create_superuser(pub)
62
    create_environment(pub)
63
    app = login(get_app(pub))
64
    resp = app.get('/backoffice/management/form-title/')
65
    resp = resp.click('Export as CSV File')
66
    assert resp.headers['content-type'].startswith('text/')
67
    assert len(resp.text.splitlines()) == 18 # 17 + header line
68
    assert len(resp.text.splitlines()[0].split(',')) == 7
69

  
70
    formdef = FormDef.get_by_urlname('form-title')
71
    formdef.fields[-1].display_locations = ['validation', 'summary', 'listings']
72
    formdef.store()
73
    resp = app.get('/backoffice/management/form-title/')
74
    resp = resp.click('Export as CSV File')
75
    assert len(resp.text.splitlines()[0].split(',')) == 9
76

  
77
    # check item fields with datasources get two columns (id & text)
78
    assert resp.text.splitlines()[0].split(',')[6] == '3rd field'
79
    assert resp.text.splitlines()[0].split(',')[7] == '' # 3rd field, continue
80
    assert resp.text.splitlines()[1].split(',')[6] == 'A'
81
    assert resp.text.splitlines()[1].split(',')[7] == 'aa'
82

  
83
    resp = app.get('/backoffice/management/form-title/')
84
    resp.forms['listing-settings']['filter'] = 'all'
85
    resp = resp.forms['listing-settings'].submit()
86
    resp_csv = resp.click('Export as CSV File')
87
    assert len(resp_csv.text.splitlines()) == 51
88

  
89
    # test status filter
90
    resp.forms['listing-settings']['filter'] = 'pending'
91
    resp.forms['listing-settings']['filter-2'].checked = True
92
    resp = resp.forms['listing-settings'].submit()
93
    resp.forms['listing-settings']['filter-2-value'] = 'baz'
94
    resp = resp.forms['listing-settings'].submit()
95
    resp_csv = resp.click('Export as CSV File')
96
    assert len(resp_csv.text.splitlines()) == 9
97

  
98
    # test criteria filters
99
    resp.forms['listing-settings']['filter-start'].checked = True
100
    resp = resp.forms['listing-settings'].submit()
101
    resp.forms['listing-settings']['filter-start-value'] = datetime.datetime(2015, 2, 1).strftime('%Y-%m-%d')
102
    resp = resp.forms['listing-settings'].submit()
103
    resp_csv = resp.click('Export as CSV File')
104
    assert len(resp_csv.text.splitlines()) == 1
105

  
106
    resp.forms['listing-settings']['filter-start-value'] = datetime.datetime(2014, 2, 1).strftime('%Y-%m-%d')
107
    resp = resp.forms['listing-settings'].submit()
108
    resp.forms['listing-settings']['filter-2-value'] = 'baz'
109
    resp = resp.forms['listing-settings'].submit()
110
    resp_csv = resp.click('Export as CSV File')
111
    assert len(resp_csv.text.splitlines()) == 9
112
    assert 'Created' in resp_csv.text.splitlines()[0]
113

  
114
    # test column selection
115
    resp.forms['listing-settings']['time'].checked = False
116
    resp = resp.forms['listing-settings'].submit()
117
    resp_csv = resp.click('Export as CSV File')
118
    assert 'Created' not in resp_csv.text.splitlines()[0]
119

  
120

  
121
def test_backoffice_export_long_listings(pub):
122
    create_superuser(pub)
123
    create_environment(pub)
124
    formdef = FormDef.get_by_urlname('form-title')
125
    for i in range(100):
126
        formdata = formdef.data_class()()
127
        formdata.just_created()
128
        formdata.receipt_time = datetime.datetime(2015, 1, 1).timetuple()
129
        formdata.data = {'1': 'BAZ BAZ %d' % i}
130
        formdata.jump_status('new')
131
        formdata.store()
132

  
133
    app = login(get_app(pub))
134
    resp = app.get('/backoffice/management/form-title/')
135
    resp = resp.click('Export as CSV File')
136
    assert resp.location.startswith('http://example.net/backoffice/management/form-title/export?job=')
137
    resp = resp.follow()
138
    assert 'completed' in resp.text
139
    resp = resp.click('Download Export')
140
    resp_lines = resp.text.splitlines()
141
    assert resp_lines[0] == 'Number,Created,Last Modified,User Label,1st field,2nd field,Status'
142
    assert len(resp_lines) == 118
143
    assert resp_lines[1].split(',')[1].startswith(
144
            time.strftime('%Y-%m-%d', formdata.receipt_time))
145
    assert resp_lines[1].split(',')[2].startswith(
146
            time.strftime('%Y-%m-%d', formdata.last_update_time))
147

  
148
    resp = app.get('/backoffice/management/form-title/')
149
    resp = resp.click('Export a Spreadsheet')
150
    assert resp.location.startswith('http://example.net/backoffice/management/form-title/export?job=')
151
    job_id = urlparse.parse_qs(urlparse.urlparse(resp.location).query)['job'][0]
152
    resp = resp.follow()
153
    assert 'completed' in resp.text
154
    resp = resp.click('Download Export')
155
    assert resp.content_type == 'application/vnd.oasis.opendocument.spreadsheet'
156

  
157
    # check afterjob ajax call
158
    status_resp = app.get('/afterjobs/' + job_id)
159
    assert status_resp.text == 'completed|completed'
160

  
161
    # check error handling
162
    app.get('/afterjobs/whatever', status=404)
163

  
164

  
165
def test_backoffice_csv_export_channel(pub):
166
    if not pub.site_options.has_section('variables'):
167
        pub.site_options.add_section('variables')
168
    pub.site_options.set('variables', 'welco_url', 'xxx')
169
    fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w')
170
    pub.site_options.write(fd)
171
    fd.close()
172

  
173
    create_superuser(pub)
174
    create_environment(pub)
175
    app = login(get_app(pub))
176
    resp = app.get('/backoffice/management/form-title/')
177
    resp_csv = resp.click('Export as CSV File')
178
    assert 'Channel' not in resp_csv.text.splitlines()[0]
179

  
180
    # add submission channel column
181
    resp.forms['listing-settings']['submission_channel'].checked = True
182
    resp = resp.forms['listing-settings'].submit()
183
    resp_csv = resp.click('Export as CSV File')
184
    assert resp_csv.text.splitlines()[0].split(',')[-1] == 'Channel'
185
    assert resp_csv.text.splitlines()[1].split(',')[-1] == 'Web'
186

  
187

  
188
def test_backoffice_csv_export_anonymised(pub):
189
    fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w')
190
    pub.site_options.write(fd)
191
    fd.close()
192

  
193
    create_superuser(pub)
194
    create_environment(pub)
195
    app = login(get_app(pub))
196
    resp = app.get('/backoffice/management/form-title/')
197
    resp_csv = resp.click('Export as CSV File')
198
    assert resp_csv.text.splitlines()[0].split(',')[-1] != 'Anonymised'
199

  
200
    # add anonymised column
201
    resp.forms['listing-settings']['anonymised'].checked = True
202
    resp = resp.forms['listing-settings'].submit()
203
    resp_csv = resp.click('Export as CSV File')
204
    assert resp_csv.text.splitlines()[0].split(',')[-1] == 'Anonymised'
205
    assert resp_csv.text.splitlines()[1].split(',')[-1] == 'No'
206

  
207

  
208
def test_backoffice_csv_export_block(pub):
209
    create_superuser(pub)
210
    create_environment(pub)
211

  
212
    block = BlockDef()
213
    block.name = 'foobar'
214
    block.fields = [
215
        fields.StringField(id='123', required=True, label='Test', type='string', varname='foo'),
216
        fields.StringField(id='234', required=True, label='Test2', type='string', varname='bar'),
217
    ]
218
    block.digest_template = 'X{{foobar_var_foo}}Y'
219
    block.store()
220

  
221
    formdef = FormDef.get_by_urlname('form-title')
222
    formdef.fields = []
223
    formdef.store()  # make sure sql columns are removed
224

  
225
    formdef.fields = [
226
        fields.BlockField(id='1', label='test', type='block:foobar', max_items=3),
227
    ]
228
    formdef.store()
229

  
230
    formdef.data_class().wipe()
231
    formdata = formdef.data_class()()
232
    formdata.data = {}
233
    formdata.data['1'] = {
234
        'data': [
235
            {'123': 'foo', '234': 'bar'},
236
            {'123': 'foo2', '234': 'bar2'},
237
        ],
238
        'schema': {'123': 'string', '234': 'string'},
239
    }
240
    formdata.just_created()
241
    formdata.jump_status('new')
242
    formdata.store()
243

  
244
    app = login(get_app(pub))
245
    resp = app.get('/backoffice/management/form-title/')
246
    resp_csv = resp.click('Export as CSV File')
247
    resp.forms['listing-settings']['1'].checked = True
248
    resp = resp.forms['listing-settings'].submit()
249
    resp_csv = resp.click('Export as CSV File')
250
    assert resp_csv.text.splitlines()[0].split(',')[-3:] == ['test - 1', 'test - 2', 'test - 3']
251
    assert resp_csv.text.splitlines()[1].split(',')[-3:] == ['XfooY', 'Xfoo2Y', '']
252

  
253

  
254
def test_backoffice_ods(pub):
255
    create_superuser(pub)
256
    create_environment(pub)
257
    app = login(get_app(pub))
258
    resp = app.get('/backoffice/management/form-title/')
259
    resp = resp.click('Export a Spreadsheet')
260
    assert resp.headers['content-type'] == 'application/vnd.oasis.opendocument.spreadsheet'
261
    assert 'filename=form-title.ods' in resp.headers['content-disposition']
262
    assert resp.body[:2] == b'PK' # ods has a zip container
263

  
264
    formdef = FormDef.get_by_urlname('form-title')
265
    formdef.fields.append(fields.FileField(id='4', label='file field', type='file',
266
        display_locations=['validation', 'summary', 'listings']))
267
    formdef.fields.append(fields.DateField(id='5', label='date field', type='date',
268
        display_locations=['validation', 'summary', 'listings']))
269
    formdef.fields.append(fields.StringField(id='6', label='number field', type='string',
270
        display_locations=['validation', 'summary', 'listings']))
271
    formdef.fields.append(fields.StringField(id='7', label='phone field', type='string',
272
        display_locations=['validation', 'summary', 'listings']))
273
    formdef.fields.append(fields.DateField(id='8', label='very old field', type='date',
274
        display_locations=['validation', 'summary', 'listings']))
275
    formdef.fields.append(fields.StringField(id='9', label='string field', type='string',
276
        display_locations=['validation', 'summary', 'listings']))
277
    formdef.fields.append(fields.StringField(id='10', label='number with comma field', type='string',
278
        display_locations=['validation', 'summary', 'listings']))
279
    formdef.store()
280

  
281
    formdata = formdef.data_class().select(lambda x: x.status == 'wf-new')[0]
282
    formdata.data['4'] = PicklableUpload('/foo/bar', content_type='text/plain')
283
    formdata.data['4'].receive([b'hello world'])
284
    formdata.data['5'] = time.strptime('2015-05-12', '%Y-%m-%d')
285
    formdata.data['6'] = '12345'
286
    formdata.data['7'] = '0102030405'
287
    formdata.data['8'] = time.strptime('1871-03-18', '%Y-%m-%d')
288
    formdata.data['9'] = 'plop\npl\x1dop'  # with control characters
289
    formdata.data['10'] = '123,45'
290
    formdata.store()
291

  
292
    resp = app.get('/backoffice/management/form-title/')
293
    resp = resp.click('Export a Spreadsheet')
294
    assert resp.headers['content-type'] == 'application/vnd.oasis.opendocument.spreadsheet'
295
    assert 'filename=form-title.ods' in resp.headers['content-disposition']
296
    assert resp.body[:2] == b'PK' # ods has a zip container
297

  
298
    zipf = zipfile.ZipFile(BytesIO(resp.body))
299
    ods_sheet = ET.parse(zipf.open('content.xml'))
300
    # check the ods contains a link to the document
301
    elem = ods_sheet.findall('.//{%s}a' % ods.NS['text'])[0]
302
    assert elem.attrib['{%s}href' % ods.NS['xlink']] == 'http://example.net/backoffice/management/form-title/%s/files/4/bar' % formdata.id
303
    resp = app.get(elem.attrib['{%s}href' % ods.NS['xlink']])
304
    assert resp.text == 'hello world'
305

  
306
    all_texts = [x.text for x in ods_sheet.findall('.//{%s}table-row//{%s}p' % (ods.NS['table'], ods.NS['text']))]
307
    created_column = all_texts.index('Created')
308
    date_column = all_texts.index('date field')
309
    number_column = all_texts.index('number field')
310
    phone_column = all_texts.index('phone field')
311
    old_column = all_texts.index('very old field')
312
    string_column = all_texts.index('string field')
313
    comma_number_column = all_texts.index('number with comma field')
314

  
315
    for row in ods_sheet.findall('.//{%s}table-row' % ods.NS['table']):
316
        if row.findall('.//{%s}table-cell/{%s}p' % (
317
                ods.NS['table'], ods.NS['text']))[0].text == formdata.get_display_id():
318
            break
319
    else:
320
        assert False, 'failed to find data row'
321

  
322
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[created_column].attrib[
323
            '{%s}value-type' % ods.NS['office']] == 'date'
324
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[created_column].attrib[
325
            '{%s}style-name' % ods.NS['table']] == 'DateTime'
326
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[date_column].attrib[
327
            '{%s}value-type' % ods.NS['office']] == 'date'
328
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[date_column].attrib[
329
            '{%s}style-name' % ods.NS['table']] == 'Date'
330
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[number_column].attrib[
331
            '{%s}value-type' % ods.NS['office']] == 'float'
332
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[number_column].attrib[
333
            '{%s}value' % ods.NS['office']] == '12345'
334
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[phone_column].attrib[
335
            '{%s}value-type' % ods.NS['office']] == 'string'
336
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[old_column].attrib[
337
            '{%s}value-type' % ods.NS['office']] == 'date'
338
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[old_column].attrib[
339
            '{%s}date-value' % ods.NS['office']] == '1871-03-18'
340
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[
341
            string_column].find('{%s}p' % ods.NS['text']).text == 'plop\nplop'
342
    assert row.findall('.//{%s}table-cell' % ods.NS['table'])[comma_number_column].attrib[
343
            '{%s}value' % ods.NS['office']] == '123.45'
344

  
345

  
346
@pytest.mark.skipif('xlwt is None')
347
def test_backoffice_xls(pub):
348
    create_superuser(pub)
349
    create_environment(pub)
350
    app = login(get_app(pub))
351
    resp = app.get('/backoffice/management/form-title/')
352
    assert not 'Excel Export' in resp.text
353

  
354
    if not pub.site_options.has_section('options'):
355
        pub.site_options.add_section('options')
356
        pub.site_options.set('options', 'legacy-excel-export', 'true')
357
        fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w')
358
        pub.site_options.write(fd)
359
        fd.close()
360

  
361
    resp = app.get('/backoffice/management/form-title/')
362
    resp = resp.click('Excel Export')
363
    assert resp.headers['content-type'].startswith('application/vnd.ms-excel')
0
-