wcs/tests/admin_pages/test_deprecations.py

251 lines
8.7 KiB
Python

import io
import os
import pytest
from quixote.http_request import Upload as QuixoteUpload
from wcs import fields
from wcs.backoffice.deprecations import DeprecationsScanAfterJob
from wcs.blocks import BlockDef
from wcs.carddef import CardDef
from wcs.data_sources import NamedDataSource
from wcs.formdef import FormDef
from wcs.qommon.form import UploadedFile
from wcs.qommon.http_request import HTTPRequest
from wcs.wf.create_formdata import Mapping
from wcs.wf.export_to_model import ExportToModel
from wcs.wf.external_workflow import ExternalWorkflowGlobalAction
from wcs.wf.form import WorkflowFormFieldsFormDef
from wcs.wf.geolocate import GeolocateWorkflowStatusItem
from wcs.wf.jump import JumpWorkflowStatusItem
from wcs.wf.notification import SendNotificationWorkflowStatusItem
from wcs.wf.redirect_to_url import RedirectToUrlWorkflowStatusItem
from wcs.workflows import Workflow
from wcs.wscalls import NamedWsCall
from ..utilities import clean_temporary_pub, create_temporary_pub, get_app, login
from .test_all import create_superuser
@pytest.fixture
def pub(request):
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()
if os.path.exists(os.path.join(pub.app_dir, 'deprecations.json')):
os.remove(os.path.join(pub.app_dir, 'deprecations.json'))
BlockDef.wipe()
CardDef.wipe()
FormDef.wipe()
NamedDataSource.wipe()
NamedWsCall.wipe()
Workflow.wipe()
return pub
def teardown_module(module):
clean_temporary_pub()
def test_no_deprecations(pub):
create_superuser(pub)
app = login(get_app(pub))
# first time, it's a redirect to the scanning job
resp = app.get('/backoffice/studio/deprecations/', status=302)
resp = resp.follow()
resp = resp.click('Go to deprecation report')
# second time, the page stays on
resp = app.get('/backoffice/studio/deprecations/', status=200)
assert 'No deprecated items were found on this site.' in resp.text
def test_deprecations(pub):
create_superuser(pub)
formdef = FormDef()
formdef.name = 'foobar'
formdef.fields = [
fields.PageField(id='1', label='page1', type='page', condition={'type': 'python', 'value': 'True'}),
fields.StringField(id='2', label='python_prefill', prefill={'type': 'formula', 'value': '1 + 2'}),
fields.StringField(
id='3', label='ezt_prefill', prefill={'type': 'string', 'value': '[form_var_test]'}
),
fields.StringField(id='4', label='jsonp_data', data_source={'type': 'jsonp', 'value': 'xxx'}),
fields.StringField(id='5', label='ezt_in_datasource', data_source={'type': 'json', 'value': '[xxx]'}),
fields.CommentField(id='6', label='[ezt] in label', type='comment'),
fields.CommentField(id='7', label='{{script.usage}} in template', type='comment'),
fields.PageField(
id='10',
label='page2',
type='page',
post_conditions=[
{'condition': {'type': 'python', 'value': 'False'}, 'error_message': 'You shall not pass.'}
],
),
fields.TableField(id='8', label='table field', type='table'),
]
formdef.store()
workflow = Workflow(name='test')
st0 = workflow.add_status('Status0', 'st0')
display = st0.add_action('displaymsg')
display.message = 'message with [ezt] info'
wscall = st0.add_action('webservice_call', id='_wscall')
wscall.varname = 'xxx'
wscall.url = 'http://remote.example.net/xml'
wscall.post_data = {'str': 'abcd', 'evalme': '=form_number'}
sendsms = st0.add_action('sendsms', id='_sendsms')
sendsms.to = 'xxx'
sendsms.condition = {'type': 'python', 'value': 'True'}
sendsms.parent = st0
st0.items.append(sendsms)
item = st0.add_action('set-backoffice-fields', id='_item')
item.fields = [{'field_id': 'bo1', 'value': '=form_var_foo'}]
create_formdata = st0.add_action('create_formdata', id='_create_formdata')
create_formdata.varname = 'resubmitted'
create_formdata.mappings = [
Mapping(field_id='0', expression='=form_var_toto_string'),
]
item = st0.add_action('update_user_profile', id='_item2')
item.fields = [{'field_id': '__email', 'value': '=form_var_foo'}]
sendmail = st0.add_action('sendmail', id='_sendmail')
sendmail.to = ['=plop']
sendmail = st0.add_action('sendmail', id='_sendmail2')
sendmail.attachments = ['python']
display_form = st0.add_action('form', id='_x')
display_form.formdef = WorkflowFormFieldsFormDef(item=display_form)
display_form.formdef.fields.append(
fields.StringField(id='0', label='Test', type='string', prefill={'type': 'formula', 'value': '1 + 2'})
)
export_to = st0.add_action('export_to_model', id='_export_to')
export_to.convert_to_pdf = False
export_to.label = 'create doc'
upload = QuixoteUpload('/foo/test.rtf', content_type='application/rtf')
upload.fp = io.BytesIO()
upload.fp.write(b'HELLO WORLD')
upload.fp.seek(0)
export_to.model_file = UploadedFile(pub.app_dir, None, upload)
export_to.by = ['_submitter']
for klass in (
ExportToModel,
ExternalWorkflowGlobalAction,
GeolocateWorkflowStatusItem,
JumpWorkflowStatusItem,
SendNotificationWorkflowStatusItem,
RedirectToUrlWorkflowStatusItem,
):
action = klass()
action.parent = st0
st0.items.append(action)
workflow.store()
data_source = NamedDataSource(name='ds_python')
data_source.data_source = {'type': 'formula', 'value': repr([('1', 'un'), ('2', 'deux')])}
data_source.store()
data_source = NamedDataSource(name='ds_jsonp')
data_source.data_source = {'type': 'jsonp', 'value': 'xxx'}
data_source.store()
NamedWsCall.wipe()
wscall = NamedWsCall()
wscall.name = 'Hello'
wscall.request = {'url': 'http://example.net', 'qs_data': {'a': '=1+2'}}
wscall.store()
app = login(get_app(pub))
resp = app.get('/backoffice/studio/deprecations/', status=302)
resp = resp.follow()
resp = resp.click('Go to deprecation report')
assert [x.text for x in resp.pyquery('.section--ezt li a')] == [
'foobar / Field "ezt_prefill"',
'foobar / Field "ezt_in_datasource"',
'foobar / Field "[ezt] in label"',
'test / Alert',
]
assert [x.text for x in resp.pyquery('.section--jsonp li a')] == [
'foobar / Field "jsonp_data"',
'Data source "ds_jsonp"',
]
assert [x.text for x in resp.pyquery('.section--python-data-source li a')] == ['Data source "ds_python"']
assert [x.text for x in resp.pyquery('.section--python-condition li a')] == [
'foobar / Field "page1"',
'foobar / Field "page2"',
'test / SMS',
]
assert [x.text for x in resp.pyquery('.section--python-prefill li a')] == [
'foobar / Field "python_prefill"',
'Form action in workflow "test" / Field "Test"',
]
assert [x.text for x in resp.pyquery('.section--python-expression li a')] == [
'test / Webservice',
'test / Backoffice Data',
'test / New Form Creation',
'test / User Profile Update',
'test / Email',
'test / Email',
'Webservice "Hello"',
]
assert [x.text for x in resp.pyquery('.section--script li a')] == [
'foobar / Field "{{script.usage}} in template"'
]
assert [x.text for x in resp.pyquery('.section--rtf li a')] == [
'test / Document Creation',
]
assert [x.text for x in resp.pyquery('.section--table-field li a')] == [
'foobar / Field "table field"',
]
# check all links are ok
for link in resp.pyquery('.section li a'):
resp.click(href=link.attrib['href'], index=0)
def test_deprecations_choice_label(pub):
# check choice labels are not considered as EZT
workflow = Workflow(name='test')
st0 = workflow.add_status('Status0', 'st0')
accept = st0.add_action('choice', id='_choice')
accept.label = '[test] action'
job = DeprecationsScanAfterJob()
job.execute()
assert not job.report_lines
def test_deprecations_skip_invalid_ezt(pub):
workflow = Workflow(name='test')
st0 = workflow.add_status('Status0', 'st0')
display = st0.add_action('displaymsg')
display.message = 'message with invalid [if-any] ezt'
job = DeprecationsScanAfterJob()
job.execute()
assert not job.report_lines
def test_deprecations_cronjob(pub):
assert not os.path.exists(os.path.join(pub.app_dir, 'deprecations.json'))
pub.update_deprecations_report()
assert os.path.exists(os.path.join(pub.app_dir, 'deprecations.json'))