315 lines
10 KiB
Python
315 lines
10 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import io
|
|
import os
|
|
import xml.etree.ElementTree as ET
|
|
import zipfile
|
|
|
|
import pytest
|
|
from quixote import get_publisher
|
|
from utilities import clean_temporary_pub, create_temporary_pub, get_app
|
|
|
|
from wcs import fields
|
|
from wcs.carddef import CardDef
|
|
from wcs.formdef import FormDef
|
|
from wcs.qommon import ods
|
|
from wcs.qommon.http_request import HTTPRequest
|
|
|
|
from .utils import sign_uri
|
|
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
if 'pub' in metafunc.fixturenames:
|
|
metafunc.parametrize('pub', ['pickle', 'sql'], indirect=True)
|
|
|
|
|
|
@pytest.fixture
|
|
def pub(request, emails):
|
|
pub = create_temporary_pub(sql_mode=(request.param == 'sql'))
|
|
|
|
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()
|
|
|
|
open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w').write(
|
|
'''\
|
|
[api-secrets]
|
|
coucou = 1234
|
|
'''
|
|
)
|
|
|
|
return pub
|
|
|
|
|
|
def teardown_module(module):
|
|
clean_temporary_pub()
|
|
|
|
|
|
@pytest.fixture
|
|
def local_user():
|
|
get_publisher().user_class.wipe()
|
|
user = get_publisher().user_class()
|
|
user.name = 'Jean Darmette'
|
|
user.email = 'jean.darmette@triffouilis.fr'
|
|
user.name_identifiers = ['0123456789']
|
|
user.store()
|
|
return user
|
|
|
|
|
|
@pytest.fixture
|
|
def test_api_custom_view_access(pub, local_user):
|
|
pub.role_class.wipe()
|
|
role = pub.role_class(name='test')
|
|
role.store()
|
|
local_user.roles = [role.id]
|
|
local_user.store()
|
|
|
|
FormDef.wipe()
|
|
formdef = FormDef()
|
|
formdef.name = 'test'
|
|
formdef.workflow_roles = {'_receiver': role.id}
|
|
formdef.fields = [fields.StringField(id='0', label='foobar', varname='foobar')]
|
|
formdef.geolocations = {'base': 'Location'}
|
|
formdef.store()
|
|
|
|
carddef = CardDef()
|
|
carddef.name = 'test'
|
|
carddef.fields = [fields.StringField(id='0', label='foobar', varname='foo')]
|
|
carddef.workflow_roles = {'_viewer': role.id}
|
|
carddef.digest_template = 'bla {{ form_var_foo }} xxx'
|
|
carddef.geolocations = {'base': 'Location'}
|
|
carddef.store()
|
|
|
|
pub.custom_view_class.wipe()
|
|
custom_view = pub.custom_view_class()
|
|
custom_view.title = 'shared formdef custom view'
|
|
custom_view.formdef = formdef
|
|
custom_view.columns = {'list': [{'id': '0'}]}
|
|
custom_view.filters = {}
|
|
custom_view.visibility = 'any'
|
|
custom_view.store()
|
|
|
|
custom_view = pub.custom_view_class()
|
|
custom_view.title = 'private formdef custom view'
|
|
custom_view.formdef = formdef
|
|
custom_view.columns = {'list': [{'id': '0'}]}
|
|
custom_view.filters = {}
|
|
custom_view.visibility = 'owner'
|
|
custom_view.user = local_user
|
|
custom_view.store()
|
|
|
|
custom_view = pub.custom_view_class()
|
|
custom_view.title = 'shared carddef custom view'
|
|
custom_view.formdef = carddef
|
|
custom_view.columns = {'list': [{'id': '0'}]}
|
|
custom_view.filters = {}
|
|
custom_view.visibility = 'any'
|
|
custom_view.store()
|
|
|
|
custom_view = pub.custom_view_class()
|
|
custom_view.title = 'private carddef custom view'
|
|
custom_view.formdef = carddef
|
|
custom_view.columns = {'list': [{'id': '0'}]}
|
|
custom_view.filters = {}
|
|
custom_view.visibility = 'owner'
|
|
custom_view.user = local_user
|
|
custom_view.store()
|
|
|
|
custom_view = pub.custom_view_class()
|
|
custom_view.title = 'datasource carddef custom view'
|
|
custom_view.formdef = carddef
|
|
custom_view.columns = {'list': [{'id': '0'}]}
|
|
custom_view.filters = {}
|
|
custom_view.visibility = 'datasource'
|
|
custom_view.store()
|
|
|
|
get_app(pub).get(sign_uri('/api/forms/test/list/shared-formdef-custom-view', user=local_user), status=200)
|
|
get_app(pub).get(sign_uri('/api/forms/test/ods/shared-formdef-custom-view', user=local_user), status=200)
|
|
get_app(pub).get(
|
|
sign_uri('/api/forms/test/geojson/shared-formdef-custom-view', user=local_user), status=200
|
|
)
|
|
get_app(pub).get(
|
|
sign_uri('/api/forms/test/list/private-formdef-custom-view', user=local_user), status=404
|
|
)
|
|
get_app(pub).get(sign_uri('/api/forms/test/ods/private-formdef-custom-view', user=local_user), status=404)
|
|
get_app(pub).get(
|
|
sign_uri('/api/forms/test/geojson/private-formdef-custom-view', user=local_user), status=404
|
|
)
|
|
|
|
get_app(pub).get(sign_uri('/api/cards/test/list/shared-carddef-custom-view', user=local_user), status=200)
|
|
get_app(pub).get(sign_uri('/api/cards/test/ods/shared-carddef-custom-view', user=local_user), status=200)
|
|
get_app(pub).get(
|
|
sign_uri('/api/cards/test/geojson/shared-carddef-custom-view', user=local_user), status=200
|
|
)
|
|
get_app(pub).get(
|
|
sign_uri('/api/cards/test/list/private-carddef-custom-view', user=local_user), status=404
|
|
)
|
|
get_app(pub).get(sign_uri('/api/cards/test/ods/private-carddef-custom-view', user=local_user), status=404)
|
|
get_app(pub).get(
|
|
sign_uri('/api/cards/test/geojson/private-carddef-custom-view', user=local_user), status=404
|
|
)
|
|
get_app(pub).get(
|
|
sign_uri('/api/cards/test/list/datasource-carddef-custom-view', user=local_user), status=200
|
|
)
|
|
get_app(pub).get(
|
|
sign_uri('/api/cards/test/ods/datasource-carddef-custom-view', user=local_user), status=200
|
|
)
|
|
get_app(pub).get(
|
|
sign_uri('/api/cards/test/geojson/datasource-carddef-custom-view', user=local_user), status=200
|
|
)
|
|
|
|
|
|
def test_api_list_formdata_custom_view(pub, local_user):
|
|
pub.role_class.wipe()
|
|
role = pub.role_class(name='test')
|
|
role.store()
|
|
|
|
FormDef.wipe()
|
|
formdef = FormDef()
|
|
formdef.name = 'test'
|
|
formdef.workflow_roles = {'_receiver': role.id}
|
|
formdef.fields = [
|
|
fields.StringField(id='0', label='foobar', varname='foobar'),
|
|
]
|
|
formdef.store()
|
|
|
|
data_class = formdef.data_class()
|
|
data_class.wipe()
|
|
|
|
for i in range(30):
|
|
formdata = data_class()
|
|
formdata.data = {'0': 'FOO BAR %d' % i}
|
|
formdata.user_id = local_user.id
|
|
formdata.just_created()
|
|
if i % 3 == 0:
|
|
formdata.jump_status('new')
|
|
else:
|
|
formdata.jump_status('finished')
|
|
formdata.store()
|
|
|
|
# add proper role to user
|
|
local_user.roles = [role.id]
|
|
local_user.store()
|
|
|
|
# check it now gets the data
|
|
resp = get_app(pub).get(sign_uri('/api/forms/test/list', user=local_user))
|
|
assert len(resp.json) == 30
|
|
|
|
pub.custom_view_class.wipe()
|
|
custom_view = pub.custom_view_class()
|
|
custom_view.title = 'custom view'
|
|
custom_view.formdef = formdef
|
|
custom_view.columns = {'list': [{'id': '0'}]}
|
|
custom_view.filters = {"filter": "done", "filter-status": "on"}
|
|
custom_view.visibility = 'any'
|
|
custom_view.store()
|
|
|
|
resp = get_app(pub).get(sign_uri('/api/forms/test/list/custom-view', user=local_user))
|
|
assert len(resp.json['data']) == 20
|
|
|
|
|
|
def test_api_ods_formdata_custom_view(pub, local_user):
|
|
pub.role_class.wipe()
|
|
role = pub.role_class(name='test')
|
|
role.store()
|
|
|
|
FormDef.wipe()
|
|
formdef = FormDef()
|
|
formdef.name = 'test'
|
|
formdef.workflow_roles = {'_receiver': role.id}
|
|
formdef.fields = [
|
|
fields.StringField(id='0', label='foobar', varname='foobar'),
|
|
]
|
|
formdef.store()
|
|
|
|
data_class = formdef.data_class()
|
|
data_class.wipe()
|
|
|
|
for i in range(30):
|
|
formdata = data_class()
|
|
formdata.data = {'0': 'FOO BAR %d' % i}
|
|
formdata.user_id = local_user.id
|
|
formdata.just_created()
|
|
if i % 3 == 0:
|
|
formdata.jump_status('new')
|
|
else:
|
|
formdata.jump_status('finished')
|
|
formdata.store()
|
|
|
|
# add proper role to user
|
|
local_user.roles = [role.id]
|
|
local_user.store()
|
|
|
|
# check it now gets the data
|
|
resp = get_app(pub).get(sign_uri('/api/forms/test/ods', user=local_user))
|
|
zipf = zipfile.ZipFile(io.BytesIO(resp.body))
|
|
ods_sheet = ET.parse(zipf.open('content.xml'))
|
|
assert len(ods_sheet.findall('.//{%s}table-row' % ods.NS['table'])) == 11
|
|
|
|
pub.custom_view_class.wipe()
|
|
custom_view = pub.custom_view_class()
|
|
custom_view.title = 'custom view'
|
|
custom_view.formdef = formdef
|
|
custom_view.columns = {'list': [{'id': '0'}]}
|
|
custom_view.filters = {"filter": "done", "filter-status": "on"}
|
|
custom_view.visibility = 'any'
|
|
custom_view.store()
|
|
|
|
resp = get_app(pub).get(sign_uri('/api/forms/test/ods/custom-view', user=local_user))
|
|
zipf = zipfile.ZipFile(io.BytesIO(resp.body))
|
|
ods_sheet = ET.parse(zipf.open('content.xml'))
|
|
assert len(ods_sheet.findall('.//{%s}table-row' % ods.NS['table'])) == 21
|
|
|
|
|
|
def test_api_geojson_formdata_custom_view(pub, local_user):
|
|
pub.role_class.wipe()
|
|
role = pub.role_class(name='test')
|
|
role.store()
|
|
|
|
FormDef.wipe()
|
|
formdef = FormDef()
|
|
formdef.name = 'test'
|
|
formdef.workflow_roles = {'_receiver': role.id}
|
|
formdef.fields = [
|
|
fields.StringField(id='0', label='foobar', varname='foobar'),
|
|
]
|
|
formdef.geolocations = {'base': 'Location'}
|
|
formdef.store()
|
|
|
|
data_class = formdef.data_class()
|
|
data_class.wipe()
|
|
|
|
for i in range(30):
|
|
formdata = data_class()
|
|
formdata.data = {'0': 'FOO BAR %d' % i}
|
|
formdata.geolocations = {'base': {'lat': 48, 'lon': 2}}
|
|
formdata.user_id = local_user.id
|
|
formdata.just_created()
|
|
if i % 3 == 0:
|
|
formdata.jump_status('new')
|
|
else:
|
|
formdata.jump_status('finished')
|
|
formdata.store()
|
|
|
|
# add proper role to user
|
|
local_user.roles = [role.id]
|
|
local_user.store()
|
|
|
|
# check it now gets the data
|
|
resp = get_app(pub).get(sign_uri('/api/forms/test/geojson', user=local_user))
|
|
assert len(resp.json['features']) == 10
|
|
|
|
pub.custom_view_class.wipe()
|
|
custom_view = pub.custom_view_class()
|
|
custom_view.title = 'custom view'
|
|
custom_view.formdef = formdef
|
|
custom_view.columns = {'list': [{'id': '0'}]}
|
|
custom_view.filters = {"filter": "done", "filter-status": "on"}
|
|
custom_view.visibility = 'any'
|
|
custom_view.store()
|
|
|
|
resp = get_app(pub).get(sign_uri('/api/forms/test/geojson/custom-view', user=local_user))
|
|
assert len(resp.json['features']) == 20
|