226 lines
7.0 KiB
Python
226 lines
7.0 KiB
Python
import datetime
|
|
import os
|
|
import re
|
|
import shutil
|
|
import StringIO
|
|
|
|
import pytest
|
|
|
|
from quixote import cleanup, get_publisher
|
|
from wcs.qommon import errors, sessions
|
|
from qommon.ident.password_accounts import PasswordAccount
|
|
from wcs.qommon.http_request import HTTPRequest
|
|
from wcs.roles import Role
|
|
from wcs.workflows import Workflow
|
|
from wcs.formdef import FormDef
|
|
from wcs import fields
|
|
|
|
from utilities import get_app, login, create_temporary_pub
|
|
|
|
def setup_module(module):
|
|
cleanup()
|
|
|
|
global pub
|
|
|
|
pub = create_temporary_pub()
|
|
|
|
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
|
|
pub.set_app_dir(req)
|
|
pub.cfg['identification'] = {'methods': ['password']}
|
|
pub.cfg['language'] = {'language': 'en'}
|
|
pub.write_cfg()
|
|
|
|
def create_superuser():
|
|
global user1
|
|
if pub.user_class.has_key('admin'):
|
|
return
|
|
user1 = pub.user_class(name='admin')
|
|
user1.id = 'admin'
|
|
user1.is_admin = True
|
|
user1.store()
|
|
|
|
account1 = PasswordAccount(id='admin')
|
|
account1.set_password('admin')
|
|
account1.user_id = user1.id
|
|
account1.store()
|
|
|
|
Role.wipe()
|
|
role = Role(name='foobar')
|
|
role.store()
|
|
|
|
user1.roles = [str(role.id)]
|
|
user1.store()
|
|
|
|
def create_environment(set_receiver=True):
|
|
FormDef.wipe()
|
|
formdef = FormDef()
|
|
formdef.name = 'form title'
|
|
if set_receiver:
|
|
formdef.workflow_roles = {'_receiver': '1'}
|
|
formdef.fields = [fields.StringField(id='1', label='1st field', type='string'),
|
|
fields.ItemField(id='2', label='2nd field', type='item',
|
|
items=['foo', 'bar', 'baz']),
|
|
]
|
|
formdef.store()
|
|
formdef.data_class().wipe()
|
|
for i in range(50):
|
|
formdata = formdef.data_class()()
|
|
formdata.just_created()
|
|
formdata.receipt_time = datetime.datetime(2015, 1, 1).timetuple()
|
|
formdata.data = {'1': 'FOO BAR %d' % i}
|
|
if i%4 == 0:
|
|
formdata.data['2'] = 'foo'
|
|
elif i%4 == 1:
|
|
formdata.data['2'] = 'bar'
|
|
else:
|
|
formdata.data['2'] = 'baz'
|
|
if i%3 == 0:
|
|
formdata.jump_status('new')
|
|
else:
|
|
formdata.jump_status('finished')
|
|
formdata.store()
|
|
|
|
formdef = FormDef()
|
|
formdef.name = 'other form'
|
|
formdef.store()
|
|
formdef.data_class().wipe()
|
|
for i in range(20):
|
|
formdata = formdef.data_class()()
|
|
formdata.receipt_time = datetime.datetime(2014, 1, 1).timetuple()
|
|
formdata.store()
|
|
|
|
def teardown_module(module):
|
|
shutil.rmtree(pub.APP_DIR)
|
|
|
|
def test_backoffice_unlogged():
|
|
create_superuser()
|
|
resp = get_app(pub).get('/backoffice/', status=302)
|
|
resp = resp.follow()
|
|
assert resp.location == 'http://example.net/login/'
|
|
|
|
def test_backoffice_home():
|
|
create_superuser()
|
|
app = login(get_app(pub))
|
|
resp = app.get('/backoffice/')
|
|
assert 'Management' in resp.body
|
|
|
|
def test_backoffice_forms():
|
|
create_superuser()
|
|
create_environment(set_receiver=False)
|
|
|
|
# 1st time with user not handling those forms
|
|
app = login(get_app(pub))
|
|
resp = app.get('/backoffice/')
|
|
resp = resp.click('Management', index=0)
|
|
assert not 'Forms in your care' in resp.body
|
|
assert re.findall('Other Forms.*form-title', resp.body)
|
|
|
|
# 2nd time with user set as receiver of the forms
|
|
create_environment(set_receiver=True)
|
|
app = login(get_app(pub))
|
|
resp = app.get('/backoffice/')
|
|
resp = resp.click('Management', index=0)
|
|
assert 'Forms in your care' in resp.body
|
|
assert '17 open on 50' in resp.body
|
|
|
|
def test_backoffice_listing():
|
|
create_superuser()
|
|
create_environment()
|
|
app = login(get_app(pub))
|
|
resp = app.get('/backoffice/form-title/')
|
|
assert resp.body.count('data-link') == 17
|
|
|
|
# check status filter <select>
|
|
resp = app.get('/backoffice/form-title/')
|
|
resp.forms[0]['filter'] = 'all'
|
|
resp = resp.forms[0].submit()
|
|
assert resp.body.count('data-link') == 50 # not using sql -> no pagination
|
|
|
|
# check status filter <select>
|
|
resp = app.get('/backoffice/form-title/')
|
|
resp.forms[0]['filter'] = 'done'
|
|
resp = resp.forms[0].submit()
|
|
assert resp.body.count('data-link') == 33
|
|
|
|
def test_backoffice_columns():
|
|
create_superuser()
|
|
create_environment()
|
|
app = login(get_app(pub))
|
|
resp = app.get('/backoffice/form-title/')
|
|
assert resp.body.count('</th>') == 6 # five columns
|
|
resp.forms[0]['1'].checked = False
|
|
resp = resp.forms[0].submit()
|
|
assert resp.body.count('</th>') == 5 # four columns
|
|
assert resp.body.count('data-link') == 17 # 17 rows
|
|
assert resp.body.count('FOO BAR') == 0 # no field 1 column
|
|
|
|
def test_backoffice_csv():
|
|
create_superuser()
|
|
create_environment()
|
|
app = login(get_app(pub))
|
|
resp = app.get('/backoffice/form-title/')
|
|
resp = resp.click('CSV Export')
|
|
assert resp.headers['content-type'].startswith('text/')
|
|
assert len(resp.body.splitlines()) == 18 # 17 + header line
|
|
|
|
resp = app.get('/backoffice/form-title/')
|
|
resp.forms[0]['filter'] = 'all'
|
|
resp = resp.forms[0].submit()
|
|
resp = resp.click('CSV Export')
|
|
assert len(resp.body.splitlines()) == 51
|
|
|
|
def test_backoffice_ods():
|
|
create_superuser()
|
|
create_environment()
|
|
app = login(get_app(pub))
|
|
resp = app.get('/backoffice/form-title/')
|
|
resp = resp.click('Open Document Format Export')
|
|
assert resp.headers['content-type'] == 'application/vnd.oasis.opendocument.spreadsheet'
|
|
assert 'filename=form-title.ods' in resp.headers['content-disposition']
|
|
assert resp.body[:2] == 'PK' # ods has a zip container
|
|
|
|
def test_backoffice_statistics():
|
|
create_superuser()
|
|
create_environment()
|
|
app = login(get_app(pub))
|
|
resp = app.get('/backoffice/form-title/')
|
|
resp = resp.click('Statistics')
|
|
assert 'Total number of records: 50' in resp.body
|
|
assert 'New: 17' in resp.body
|
|
assert 'Finished: 33' in resp.body
|
|
assert re.findall('foo.*26.*bar.*26.*bar.*48', resp.body) # percentages
|
|
assert 'Resolution time' in resp.body
|
|
assert 'To Status "New"' in resp.body
|
|
assert 'To Status "Finished"' in resp.body
|
|
|
|
resp.forms[0]['end'] = '2013-01-01'
|
|
resp = resp.forms[0].submit()
|
|
assert 'Total number of records: 0' in resp.body
|
|
|
|
def test_backoffice_handling():
|
|
create_superuser()
|
|
create_environment()
|
|
form_class = FormDef.get_by_urlname('form-title').data_class()
|
|
app = login(get_app(pub))
|
|
resp = app.get('/backoffice/form-title/')
|
|
resp = resp.click(href='31/')
|
|
assert ' with the number 31.' in resp.body
|
|
resp.forms[0]['comment'] = 'HELLO WORLD'
|
|
resp = resp.forms[0].submit('button_accept')
|
|
resp = resp.follow()
|
|
assert form_class.get(31).status == 'wf-accepted'
|
|
assert 'HELLO WORLD' in resp.body
|
|
|
|
def test_global_statistics():
|
|
create_superuser()
|
|
create_environment()
|
|
app = login(get_app(pub))
|
|
resp = app.get('/backoffice/')
|
|
resp = resp.click('Global statistics')
|
|
assert 'Total count: 70' in resp.body
|
|
|
|
resp.forms[0]['start'] = '2014-01-01'
|
|
resp.forms[0]['end'] = '2014-12-31'
|
|
resp = resp.forms[0].submit()
|
|
assert 'Total count: 20' in resp.body
|