0001-misc-split-test-module.patch
tests/test_admin_pages.py | ||
---|---|---|
1 | 1 |
# -*- coding: utf-8 -*- |
2 | 2 | |
3 | 3 |
import datetime |
4 |
from email.mime.text import MIMEText |
|
5 |
import json |
|
6 | 4 |
import logging |
7 | 5 |
import os |
8 | 6 |
import re |
9 |
import shutil |
|
10 | 7 |
import tarfile |
11 | 8 |
import time |
12 | 9 |
import xml.etree.ElementTree as ET |
... | ... | |
21 | 18 |
from webtest import Upload |
22 | 19 |
import mock |
23 | 20 | |
24 |
from django.utils import six |
|
25 | 21 |
from django.utils.six import StringIO, BytesIO |
26 | 22 |
from django.utils.six.moves.urllib import parse as urlparse |
27 | 23 | |
28 |
from quixote import cleanup, get_publisher |
|
29 | 24 |
from quixote.http_request import Upload as QuixoteUpload |
30 | 25 | |
31 |
from wcs.qommon import errors, sessions |
|
32 | 26 |
from wcs.qommon.form import UploadedFile |
33 | 27 |
from wcs.qommon.ident.password_accounts import PasswordAccount |
34 | 28 |
from wcs.qommon.http_request import HTTPRequest |
... | ... | |
53 | 47 |
from wcs.blocks import BlockDef |
54 | 48 |
from wcs import fields |
55 | 49 | |
56 |
from utilities import get_app, login, create_temporary_pub, clean_temporary_pub, HttpRequestsMocking
|
|
50 |
from utilities import get_app, login, create_temporary_pub, clean_temporary_pub |
|
57 | 51 | |
58 | 52 | |
59 | 53 |
def pytest_generate_tests(metafunc): |
... | ... | |
5238 | 5232 |
assert pub.cfg['misc']['default-position'] == '1.234;-1.234' |
5239 | 5233 | |
5240 | 5234 | |
5241 |
def test_data_sources(pub): |
|
5242 |
create_superuser(pub) |
|
5243 |
app = login(get_app(pub)) |
|
5244 |
app.get('/backoffice/settings/data-sources/') |
|
5245 |
# also check it's accessible from forms and workflows sections |
|
5246 |
app.get('/backoffice/forms/data-sources/') |
|
5247 |
app.get('/backoffice/workflows/data-sources/') |
|
5248 | ||
5249 |
# unknown datasource |
|
5250 |
app.get('/backoffice/settings/data-sources/42/', status=404) |
|
5251 |
app.get('/backoffice/forms/data-sources/42/', status=404) |
|
5252 |
app.get('/backoffice/workflows/data-sources/42/', status=404) |
|
5253 | ||
5254 | ||
5255 |
def test_data_sources_new(pub): |
|
5256 |
create_superuser(pub) |
|
5257 |
NamedDataSource.wipe() |
|
5258 |
app = login(get_app(pub)) |
|
5259 | ||
5260 |
# go to the page and cancel |
|
5261 |
resp = app.get('/backoffice/settings/data-sources/') |
|
5262 |
resp = resp.click('New Data Source') |
|
5263 |
resp = resp.forms[0].submit('cancel') |
|
5264 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/' |
|
5265 | ||
5266 |
# go to the page and add a data source |
|
5267 |
resp = app.get('/backoffice/settings/data-sources/') |
|
5268 |
resp = resp.click('New Data Source') |
|
5269 |
resp.forms[0]['name'] = 'a new data source' |
|
5270 |
resp.forms[0]['description'] = 'description of the data source' |
|
5271 |
resp.forms[0]['data_source$type'] = 'python' |
|
5272 |
resp = resp.forms[0].submit('data_source$apply') |
|
5273 |
resp.forms[0]['data_source$value'] = repr( |
|
5274 |
[{'id': '1', 'text': 'un'}, {'id': '2', 'text': 'deux'}]) |
|
5275 |
resp = resp.forms[0].submit('submit') |
|
5276 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/' |
|
5277 |
resp = resp.follow() |
|
5278 |
assert 'a new data source' in resp.text |
|
5279 |
resp = resp.click('a new data source') |
|
5280 |
assert 'Data Source - a new data source' in resp.text |
|
5281 |
resp = resp.click('Edit') |
|
5282 |
assert 'Edit Data Source' in resp.text |
|
5283 | ||
5284 |
assert NamedDataSource.get(1).name == 'a new data source' |
|
5285 |
assert NamedDataSource.get(1).description == 'description of the data source' |
|
5286 | ||
5287 |
# add a second one |
|
5288 |
resp = app.get('/backoffice/settings/data-sources/') |
|
5289 |
resp = resp.click('New Data Source') |
|
5290 |
resp.forms[0]['name'] = 'an other data source' |
|
5291 |
resp.forms[0]['description'] = 'description of the data source' |
|
5292 |
resp.forms[0]['data_source$type'] = 'python' |
|
5293 |
resp = resp.forms[0].submit('data_source$apply') |
|
5294 |
resp.forms[0]['data_source$value'] = repr( |
|
5295 |
[{'id': '1', 'text': 'un'}, {'id': '2', 'text': 'deux'}]) |
|
5296 |
resp = resp.forms[0].submit('submit') |
|
5297 | ||
5298 |
assert NamedDataSource.count() == 2 |
|
5299 | ||
5300 | ||
5301 |
def test_data_sources_view(pub): |
|
5302 |
create_superuser(pub) |
|
5303 |
NamedDataSource.wipe() |
|
5304 | ||
5305 |
data_source = NamedDataSource(name='foobar') |
|
5306 |
app = login(get_app(pub)) |
|
5307 | ||
5308 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
5309 |
data_source.store() |
|
5310 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
5311 |
assert 'Type of source: Python Expression' in resp.text |
|
5312 |
assert 'Python Expression' in resp.text |
|
5313 |
assert not 'Preview' in resp.text |
|
5314 | ||
5315 |
data_source.data_source = {'type': 'formula', 'value': '["AAA", "BBB"]'} |
|
5316 |
data_source.store() |
|
5317 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
5318 |
assert 'Preview' in resp.text |
|
5319 |
assert resp.text.count('AAA') == 3 # expression + id + text |
|
5320 | ||
5321 |
# check unicode |
|
5322 |
data_source.data_source = {'type': 'formula', |
|
5323 |
'value': repr([('Y', 'Domicilié'), ('N', u'Pas domicilié')])} |
|
5324 |
data_source.store() |
|
5325 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
5326 |
assert 'Preview' in resp.text |
|
5327 |
assert 'Domicilié' in resp.text |
|
5328 | ||
5329 |
# check json |
|
5330 |
json_file_path = os.path.join(pub.app_dir, 'test.json') |
|
5331 |
json_file = open(json_file_path, 'w') |
|
5332 |
json.dump({'data': [{'id': '1', 'text': 'foo'}, {'id': '2', 'text': 'bar'}]}, json_file) |
|
5333 |
json_file.close() |
|
5334 | ||
5335 |
data_source.data_source = {'type': 'json', 'value': 'file://%s' % json_file_path} |
|
5336 |
data_source.store() |
|
5337 |
with HttpRequestsMocking() as http_requests: |
|
5338 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
5339 |
assert 'Preview' in resp.text |
|
5340 |
assert 'foo' in resp.text |
|
5341 | ||
5342 |
# variadic url |
|
5343 |
data_source.data_source = {'type': 'json', 'value': '{{ site_url }}/foo/bar'} |
|
5344 |
data_source.store() |
|
5345 |
with HttpRequestsMocking() as http_requests: |
|
5346 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
5347 |
assert '<a href="http://example.net/foo/bar"' in resp.text |
|
5348 | ||
5349 |
data_source.data_source = {'type': 'formula', 'value': '[str(x) for x in range(100)]'} |
|
5350 |
data_source.store() |
|
5351 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
5352 |
assert 'Preview' in resp.text |
|
5353 |
assert resp.text.count('<li>') < 100 |
|
5354 |
assert '<li>...</li>' in resp.text |
|
5355 | ||
5356 |
data_source.data_source = {'type': 'formula', 'value': repr([ |
|
5357 |
{'id': 'a', 'text': 'BBB', 'foo': 'bar1'}, |
|
5358 |
{'id': 'b', 'text': 'BBB', 'foo': 'bar2'}, |
|
5359 |
])} |
|
5360 |
data_source.store() |
|
5361 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
5362 |
assert 'Preview' in resp.text |
|
5363 |
assert 'Additional keys are available: foo' in resp.text |
|
5364 | ||
5365 |
# check formdef listing |
|
5366 |
FormDef.wipe() |
|
5367 |
formdef = FormDef() |
|
5368 |
formdef.name = 'test data source' |
|
5369 |
formdef.fields = [ |
|
5370 |
fields.ItemField(id='1', label='item', data_source={'type': data_source.slug}) |
|
5371 |
] |
|
5372 |
formdef.store() |
|
5373 | ||
5374 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
5375 |
assert 'Usage in forms' in resp.text |
|
5376 |
assert '/backoffice/forms/%s/' % formdef.id in resp.text |
|
5377 | ||
5378 |
# additional formdef types |
|
5379 |
user_formdef = UserFieldsFormDef(pub) |
|
5380 |
user_formdef.fields.append( |
|
5381 |
fields.ItemField(id='1', type='item', label='item', data_source={'type': data_source.slug})) |
|
5382 |
user_formdef.store() |
|
5383 | ||
5384 |
from wcs.workflows import WorkflowVariablesFieldsFormDef |
|
5385 |
Workflow.wipe() |
|
5386 |
workflow = Workflow(name='Workflow One') |
|
5387 |
workflow.variables_formdef = WorkflowVariablesFieldsFormDef(workflow=workflow) |
|
5388 |
workflow.variables_formdef.fields.append( |
|
5389 |
fields.ItemField(id='1', type='idem', label='item', data_source={'type': data_source.slug})) |
|
5390 |
workflow.backoffice_fields_formdef = WorkflowBackofficeFieldsFormDef(workflow) |
|
5391 |
workflow.backoffice_fields_formdef.fields.append( |
|
5392 |
fields.ItemField(id='1', type='item', label='item', data_source={'type': data_source.slug})) |
|
5393 | ||
5394 |
from wcs.wf.form import FormWorkflowStatusItem, WorkflowFormFieldsFormDef |
|
5395 |
baz_status = workflow.add_status(name='baz') |
|
5396 |
display_form = FormWorkflowStatusItem() |
|
5397 |
display_form.id = '_x' |
|
5398 |
display_form.formdef = WorkflowFormFieldsFormDef(item=display_form) |
|
5399 |
display_form.formdef.fields.append( |
|
5400 |
fields.ItemField(id='1', type='item', label='item', data_source={'type': data_source.slug})) |
|
5401 |
baz_status.items.append(display_form) |
|
5402 |
display_form.parent = baz_status |
|
5403 | ||
5404 |
workflow.store() |
|
5405 | ||
5406 |
carddef = CardDef() |
|
5407 |
carddef.name = 'Baz' |
|
5408 |
carddef.fields = [ |
|
5409 |
fields.ItemField(id='1', label='item', data_source={'type': data_source.slug}) |
|
5410 |
] |
|
5411 |
carddef.store() |
|
5412 | ||
5413 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
5414 |
assert 'Usage in forms' in resp.text |
|
5415 |
assert '/backoffice/forms/%s/' % formdef.id in resp.text |
|
5416 |
assert '/backoffice/workflows/%s/backoffice-fields/fields/' % workflow.id in resp.text |
|
5417 |
assert '/backoffice/workflows/%s/variables/fields/' % workflow.id in resp.text |
|
5418 |
assert '/backoffice/workflows/%s/status/1/items/_x/fields/' % workflow.id in resp.text |
|
5419 |
assert '/backoffice/settings/users/fields/' in resp.text |
|
5420 |
assert '/backoffice/cards/%s/' % carddef.id in resp.text |
|
5421 | ||
5422 |
# cleanup |
|
5423 |
user_formdef = UserFieldsFormDef(pub) |
|
5424 |
user_formdef.fields = [] |
|
5425 |
user_formdef.store() |
|
5426 |
Workflow.wipe() |
|
5427 |
CardDef.wipe() |
|
5428 | ||
5429 | ||
5430 |
def test_data_sources_edit(pub): |
|
5431 |
create_superuser(pub) |
|
5432 |
NamedDataSource.wipe() |
|
5433 |
data_source = NamedDataSource(name='foobar') |
|
5434 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
5435 |
data_source.store() |
|
5436 | ||
5437 |
FormDef.wipe() |
|
5438 |
app = login(get_app(pub)) |
|
5439 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
5440 | ||
5441 |
resp = resp.click(href='edit') |
|
5442 |
assert resp.forms[0]['name'].value == 'foobar' |
|
5443 |
resp.forms[0]['description'] = 'data source description' |
|
5444 |
resp = resp.forms[0].submit('submit') |
|
5445 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/1/' |
|
5446 |
resp = resp.follow() |
|
5447 | ||
5448 |
assert NamedDataSource.get(1).description == 'data source description' |
|
5449 | ||
5450 | ||
5451 |
def test_data_sources_edit_duplicate_name(pub): |
|
5452 |
create_superuser(pub) |
|
5453 |
NamedDataSource.wipe() |
|
5454 |
data_source = NamedDataSource(name='foobar') |
|
5455 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
5456 |
data_source.store() |
|
5457 |
data_source = NamedDataSource(name='foobar2') |
|
5458 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
5459 |
data_source.store() |
|
5460 | ||
5461 |
app = login(get_app(pub)) |
|
5462 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
5463 |
resp = resp.click(href='edit') |
|
5464 |
assert resp.forms[0]['name'].value == 'foobar' |
|
5465 |
resp.forms[0]['name'] = 'foobar2' |
|
5466 |
resp = resp.forms[0].submit('submit') |
|
5467 |
assert 'This name is already used' in resp.text |
|
5468 | ||
5469 |
resp = resp.forms[0].submit('cancel') |
|
5470 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/1/' |
|
5471 | ||
5472 | ||
5473 |
def test_data_sources_delete(pub): |
|
5474 |
create_superuser(pub) |
|
5475 |
NamedDataSource.wipe() |
|
5476 |
category = NamedDataSource(name='foobar') |
|
5477 |
category.store() |
|
5478 | ||
5479 |
FormDef.wipe() |
|
5480 |
app = login(get_app(pub)) |
|
5481 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
5482 | ||
5483 |
resp = resp.click(href='delete') |
|
5484 |
resp = resp.forms[0].submit('cancel') |
|
5485 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/' |
|
5486 |
assert NamedDataSource.count() == 1 |
|
5487 | ||
5488 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
5489 |
resp = resp.click(href='delete') |
|
5490 |
resp = resp.forms[0].submit() |
|
5491 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/' |
|
5492 |
resp = resp.follow() |
|
5493 |
assert NamedDataSource.count() == 0 |
|
5494 | ||
5495 | ||
5496 |
def test_data_sources_in_use_delete(pub): |
|
5497 |
create_superuser(pub) |
|
5498 |
NamedDataSource.wipe() |
|
5499 |
category = NamedDataSource(name='foobar') |
|
5500 |
category.store() |
|
5501 | ||
5502 |
FormDef.wipe() |
|
5503 |
formdef = FormDef() |
|
5504 |
formdef.name = 'form title' |
|
5505 |
formdef.fields = [ |
|
5506 |
fields.ItemField(id='0', label='string', type='item', |
|
5507 |
data_source={'type': 'foobar'}), |
|
5508 |
] |
|
5509 |
formdef.store() |
|
5510 | ||
5511 |
app = login(get_app(pub)) |
|
5512 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
5513 |
resp = resp.click(href='delete') |
|
5514 |
assert 'This datasource is still used, it cannot be deleted.' in resp.text |
|
5515 |
assert 'delete-button' not in resp.text |
|
5516 | ||
5517 |
formdef.fields = [] |
|
5518 |
formdef.store() |
|
5519 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
5520 |
resp = resp.click(href='delete') |
|
5521 |
assert 'delete-button' in resp.text |
|
5522 | ||
5523 | ||
5524 |
def test_data_sources_export(pub): |
|
5525 |
create_superuser(pub) |
|
5526 |
create_role() |
|
5527 | ||
5528 |
NamedDataSource.wipe() |
|
5529 |
data_source = NamedDataSource(name='foobar') |
|
5530 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
5531 |
data_source.store() |
|
5532 | ||
5533 |
app = login(get_app(pub)) |
|
5534 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
5535 | ||
5536 |
resp = resp.click(href='export') |
|
5537 |
xml_export = resp.text |
|
5538 | ||
5539 |
ds = StringIO(xml_export) |
|
5540 |
data_source2 = NamedDataSource.import_from_xml(ds) |
|
5541 |
assert data_source2.name == 'foobar' |
|
5542 | ||
5543 | ||
5544 |
def test_data_sources_import(pub): |
|
5545 |
create_superuser(pub) |
|
5546 |
create_role() |
|
5547 | ||
5548 |
NamedDataSource.wipe() |
|
5549 |
data_source = NamedDataSource(name='foobar') |
|
5550 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
5551 |
data_source.store() |
|
5552 |
data_source_xml = ET.tostring(data_source.export_to_xml(include_id=True)) |
|
5553 | ||
5554 |
NamedDataSource.wipe() |
|
5555 |
assert NamedDataSource.count() == 0 |
|
5556 | ||
5557 |
app = login(get_app(pub)) |
|
5558 |
resp = app.get('/backoffice/settings/data-sources/') |
|
5559 |
resp = resp.click(href='import') |
|
5560 |
resp.forms[0]['file'] = Upload('datasource.wcs', data_source_xml) |
|
5561 |
resp = resp.forms[0].submit() |
|
5562 |
assert NamedDataSource.count() == 1 |
|
5563 | ||
5564 |
# import the same datasource a second time, make sure slug is not reused |
|
5565 |
resp = app.get('/backoffice/settings/data-sources/') |
|
5566 |
resp = resp.click(href='import') |
|
5567 |
resp.forms[0]['file'] = Upload('datasource.wcs', data_source_xml) |
|
5568 |
resp = resp.forms[0].submit() |
|
5569 |
assert NamedDataSource.count() == 2 |
|
5570 |
assert NamedDataSource.get(1).slug == 'foobar' |
|
5571 |
assert NamedDataSource.get(2).slug == 'foobar-1' |
|
5572 | ||
5573 |
# import an invalid file |
|
5574 |
resp = app.get('/backoffice/settings/data-sources/') |
|
5575 |
resp = resp.click(href='import') |
|
5576 |
resp.form['file'] = Upload('datasource.wcs', b'garbage') |
|
5577 |
resp = resp.form.submit() |
|
5578 |
assert 'Invalid File' in resp.text |
|
5579 | ||
5580 | ||
5581 |
def test_data_sources_edit_slug(pub): |
|
5582 |
create_superuser(pub) |
|
5583 |
NamedDataSource.wipe() |
|
5584 |
data_source = NamedDataSource(name='foobar') |
|
5585 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
5586 |
data_source.store() |
|
5587 |
assert NamedDataSource.get(1).slug == 'foobar' |
|
5588 | ||
5589 |
FormDef.wipe() |
|
5590 |
app = login(get_app(pub)) |
|
5591 | ||
5592 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
5593 |
resp = resp.click(href='edit') |
|
5594 |
assert resp.forms[0]['name'].value == 'foobar' |
|
5595 |
resp.forms[0]['slug'] = 'foo_bar' |
|
5596 |
resp = resp.forms[0].submit('submit') |
|
5597 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/1/' |
|
5598 |
assert NamedDataSource.get(1).slug == 'foo_bar' |
|
5599 | ||
5600 |
data_source = NamedDataSource(name='barfoo') |
|
5601 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
5602 |
data_source.store() |
|
5603 | ||
5604 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
5605 |
resp = resp.click(href='edit') |
|
5606 |
assert resp.forms[0]['name'].value == 'foobar' |
|
5607 |
resp.forms[0]['slug'] = 'barfoo' |
|
5608 |
resp = resp.forms[0].submit('submit') |
|
5609 |
assert 'This value is already used' in resp.text |
|
5610 | ||
5611 |
resp.forms[0]['slug'] = 'foobar' |
|
5612 |
resp = resp.forms[0].submit('submit') |
|
5613 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/1/' |
|
5614 | ||
5615 | ||
5616 |
def test_data_sources_in_use_edit_slug(pub): |
|
5617 |
create_superuser(pub) |
|
5618 |
NamedDataSource.wipe() |
|
5619 |
data_source = NamedDataSource(name='foobar') |
|
5620 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
5621 |
data_source.store() |
|
5622 |
assert NamedDataSource.get(1).slug == 'foobar' |
|
5623 | ||
5624 |
FormDef.wipe() |
|
5625 |
formdef = FormDef() |
|
5626 |
formdef.name = 'form title' |
|
5627 |
formdef.fields = [ |
|
5628 |
fields.ItemField(id='0', label='string', type='item', |
|
5629 |
data_source={'type': 'foobar'}), |
|
5630 |
] |
|
5631 |
formdef.store() |
|
5632 | ||
5633 |
app = login(get_app(pub)) |
|
5634 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
5635 |
resp = resp.click(href='edit') |
|
5636 |
assert 'form_slug' not in resp.form.fields |
|
5637 |
resp = resp.form.submit('submit') |
|
5638 | ||
5639 |
formdef.fields = [] |
|
5640 |
formdef.store() |
|
5641 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
5642 |
resp = resp.click(href='edit') |
|
5643 |
assert 'form_slug' in resp.text |
|
5644 |
resp = resp.form.submit('submit') |
|
5645 | ||
5646 | ||
5647 | 5235 |
def test_wscalls_new(pub): |
5648 | 5236 |
create_superuser(pub) |
5649 | 5237 |
NamedWsCall.wipe() |
tests/test_datasources_admin_pages.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 | ||
3 |
import json |
|
4 |
import os |
|
5 |
import xml.etree.ElementTree as ET |
|
6 | ||
7 |
try: |
|
8 |
import lasso |
|
9 |
except ImportError: |
|
10 |
lasso = None |
|
11 | ||
12 |
from django.utils.six import StringIO |
|
13 | ||
14 |
import pytest |
|
15 |
from webtest import Upload |
|
16 | ||
17 |
from wcs.qommon.http_request import HTTPRequest |
|
18 |
from wcs.admin.settings import UserFieldsFormDef |
|
19 |
from wcs.data_sources import NamedDataSource |
|
20 |
from wcs.workflows import Workflow, WorkflowBackofficeFieldsFormDef |
|
21 |
from wcs.formdef import FormDef |
|
22 |
from wcs.carddef import CardDef |
|
23 |
from wcs import fields |
|
24 | ||
25 |
from utilities import get_app, login, create_temporary_pub, clean_temporary_pub, HttpRequestsMocking |
|
26 |
from test_admin_pages import create_superuser, create_role |
|
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): |
|
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 | ||
47 |
return pub |
|
48 | ||
49 | ||
50 |
def teardown_module(module): |
|
51 |
clean_temporary_pub() |
|
52 | ||
53 | ||
54 |
def test_data_sources(pub): |
|
55 |
create_superuser(pub) |
|
56 |
app = login(get_app(pub)) |
|
57 |
app.get('/backoffice/settings/data-sources/') |
|
58 |
# also check it's accessible from forms and workflows sections |
|
59 |
app.get('/backoffice/forms/data-sources/') |
|
60 |
app.get('/backoffice/workflows/data-sources/') |
|
61 | ||
62 |
# unknown datasource |
|
63 |
app.get('/backoffice/settings/data-sources/42/', status=404) |
|
64 |
app.get('/backoffice/forms/data-sources/42/', status=404) |
|
65 |
app.get('/backoffice/workflows/data-sources/42/', status=404) |
|
66 | ||
67 | ||
68 |
def test_data_sources_new(pub): |
|
69 |
create_superuser(pub) |
|
70 |
NamedDataSource.wipe() |
|
71 |
app = login(get_app(pub)) |
|
72 | ||
73 |
# go to the page and cancel |
|
74 |
resp = app.get('/backoffice/settings/data-sources/') |
|
75 |
resp = resp.click('New Data Source') |
|
76 |
resp = resp.forms[0].submit('cancel') |
|
77 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/' |
|
78 | ||
79 |
# go to the page and add a data source |
|
80 |
resp = app.get('/backoffice/settings/data-sources/') |
|
81 |
resp = resp.click('New Data Source') |
|
82 |
resp.forms[0]['name'] = 'a new data source' |
|
83 |
resp.forms[0]['description'] = 'description of the data source' |
|
84 |
resp.forms[0]['data_source$type'] = 'python' |
|
85 |
resp = resp.forms[0].submit('data_source$apply') |
|
86 |
resp.forms[0]['data_source$value'] = repr( |
|
87 |
[{'id': '1', 'text': 'un'}, {'id': '2', 'text': 'deux'}]) |
|
88 |
resp = resp.forms[0].submit('submit') |
|
89 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/' |
|
90 |
resp = resp.follow() |
|
91 |
assert 'a new data source' in resp.text |
|
92 |
resp = resp.click('a new data source') |
|
93 |
assert 'Data Source - a new data source' in resp.text |
|
94 |
resp = resp.click('Edit') |
|
95 |
assert 'Edit Data Source' in resp.text |
|
96 | ||
97 |
assert NamedDataSource.get(1).name == 'a new data source' |
|
98 |
assert NamedDataSource.get(1).description == 'description of the data source' |
|
99 | ||
100 |
# add a second one |
|
101 |
resp = app.get('/backoffice/settings/data-sources/') |
|
102 |
resp = resp.click('New Data Source') |
|
103 |
resp.forms[0]['name'] = 'an other data source' |
|
104 |
resp.forms[0]['description'] = 'description of the data source' |
|
105 |
resp.forms[0]['data_source$type'] = 'python' |
|
106 |
resp = resp.forms[0].submit('data_source$apply') |
|
107 |
resp.forms[0]['data_source$value'] = repr( |
|
108 |
[{'id': '1', 'text': 'un'}, {'id': '2', 'text': 'deux'}]) |
|
109 |
resp = resp.forms[0].submit('submit') |
|
110 | ||
111 |
assert NamedDataSource.count() == 2 |
|
112 | ||
113 | ||
114 |
def test_data_sources_view(pub): |
|
115 |
create_superuser(pub) |
|
116 |
NamedDataSource.wipe() |
|
117 | ||
118 |
data_source = NamedDataSource(name='foobar') |
|
119 |
app = login(get_app(pub)) |
|
120 | ||
121 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
122 |
data_source.store() |
|
123 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
124 |
assert 'Type of source: Python Expression' in resp.text |
|
125 |
assert 'Python Expression' in resp.text |
|
126 |
assert 'Preview' not in resp.text |
|
127 | ||
128 |
data_source.data_source = {'type': 'formula', 'value': '["AAA", "BBB"]'} |
|
129 |
data_source.store() |
|
130 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
131 |
assert 'Preview' in resp.text |
|
132 |
assert resp.text.count('AAA') == 3 # expression + id + text |
|
133 | ||
134 |
# check unicode |
|
135 |
data_source.data_source = { |
|
136 |
'type': 'formula', |
|
137 |
'value': repr([('Y', 'Domicilié'), ('N', u'Pas domicilié')])} |
|
138 |
data_source.store() |
|
139 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
140 |
assert 'Preview' in resp.text |
|
141 |
assert 'Domicilié' in resp.text |
|
142 | ||
143 |
# check json |
|
144 |
json_file_path = os.path.join(pub.app_dir, 'test.json') |
|
145 |
json_file = open(json_file_path, 'w') |
|
146 |
json.dump({'data': [{'id': '1', 'text': 'foo'}, {'id': '2', 'text': 'bar'}]}, json_file) |
|
147 |
json_file.close() |
|
148 | ||
149 |
data_source.data_source = {'type': 'json', 'value': 'file://%s' % json_file_path} |
|
150 |
data_source.store() |
|
151 |
with HttpRequestsMocking(): |
|
152 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
153 |
assert 'Preview' in resp.text |
|
154 |
assert 'foo' in resp.text |
|
155 | ||
156 |
# variadic url |
|
157 |
data_source.data_source = {'type': 'json', 'value': '{{ site_url }}/foo/bar'} |
|
158 |
data_source.store() |
|
159 |
with HttpRequestsMocking(): |
|
160 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
161 |
assert '<a href="http://example.net/foo/bar"' in resp.text |
|
162 | ||
163 |
data_source.data_source = {'type': 'formula', 'value': '[str(x) for x in range(100)]'} |
|
164 |
data_source.store() |
|
165 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
166 |
assert 'Preview' in resp.text |
|
167 |
assert resp.text.count('<li>') < 100 |
|
168 |
assert '<li>...</li>' in resp.text |
|
169 | ||
170 |
data_source.data_source = {'type': 'formula', 'value': repr([ |
|
171 |
{'id': 'a', 'text': 'BBB', 'foo': 'bar1'}, |
|
172 |
{'id': 'b', 'text': 'BBB', 'foo': 'bar2'}, |
|
173 |
])} |
|
174 |
data_source.store() |
|
175 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
176 |
assert 'Preview' in resp.text |
|
177 |
assert 'Additional keys are available: foo' in resp.text |
|
178 | ||
179 |
# check formdef listing |
|
180 |
FormDef.wipe() |
|
181 |
formdef = FormDef() |
|
182 |
formdef.name = 'test data source' |
|
183 |
formdef.fields = [ |
|
184 |
fields.ItemField(id='1', label='item', data_source={'type': data_source.slug}) |
|
185 |
] |
|
186 |
formdef.store() |
|
187 | ||
188 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
189 |
assert 'Usage in forms' in resp.text |
|
190 |
assert '/backoffice/forms/%s/' % formdef.id in resp.text |
|
191 | ||
192 |
# additional formdef types |
|
193 |
user_formdef = UserFieldsFormDef(pub) |
|
194 |
user_formdef.fields.append( |
|
195 |
fields.ItemField(id='1', type='item', label='item', data_source={'type': data_source.slug})) |
|
196 |
user_formdef.store() |
|
197 | ||
198 |
from wcs.workflows import WorkflowVariablesFieldsFormDef |
|
199 |
Workflow.wipe() |
|
200 |
workflow = Workflow(name='Workflow One') |
|
201 |
workflow.variables_formdef = WorkflowVariablesFieldsFormDef(workflow=workflow) |
|
202 |
workflow.variables_formdef.fields.append( |
|
203 |
fields.ItemField(id='1', type='idem', label='item', data_source={'type': data_source.slug})) |
|
204 |
workflow.backoffice_fields_formdef = WorkflowBackofficeFieldsFormDef(workflow) |
|
205 |
workflow.backoffice_fields_formdef.fields.append( |
|
206 |
fields.ItemField(id='1', type='item', label='item', data_source={'type': data_source.slug})) |
|
207 | ||
208 |
from wcs.wf.form import FormWorkflowStatusItem, WorkflowFormFieldsFormDef |
|
209 |
baz_status = workflow.add_status(name='baz') |
|
210 |
display_form = FormWorkflowStatusItem() |
|
211 |
display_form.id = '_x' |
|
212 |
display_form.formdef = WorkflowFormFieldsFormDef(item=display_form) |
|
213 |
display_form.formdef.fields.append( |
|
214 |
fields.ItemField(id='1', type='item', label='item', data_source={'type': data_source.slug})) |
|
215 |
baz_status.items.append(display_form) |
|
216 |
display_form.parent = baz_status |
|
217 | ||
218 |
workflow.store() |
|
219 | ||
220 |
carddef = CardDef() |
|
221 |
carddef.name = 'Baz' |
|
222 |
carddef.fields = [ |
|
223 |
fields.ItemField(id='1', label='item', data_source={'type': data_source.slug}) |
|
224 |
] |
|
225 |
carddef.store() |
|
226 | ||
227 |
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) |
|
228 |
assert 'Usage in forms' in resp.text |
|
229 |
assert '/backoffice/forms/%s/' % formdef.id in resp.text |
|
230 |
assert '/backoffice/workflows/%s/backoffice-fields/fields/' % workflow.id in resp.text |
|
231 |
assert '/backoffice/workflows/%s/variables/fields/' % workflow.id in resp.text |
|
232 |
assert '/backoffice/workflows/%s/status/1/items/_x/fields/' % workflow.id in resp.text |
|
233 |
assert '/backoffice/settings/users/fields/' in resp.text |
|
234 |
assert '/backoffice/cards/%s/' % carddef.id in resp.text |
|
235 | ||
236 |
# cleanup |
|
237 |
user_formdef = UserFieldsFormDef(pub) |
|
238 |
user_formdef.fields = [] |
|
239 |
user_formdef.store() |
|
240 |
Workflow.wipe() |
|
241 |
CardDef.wipe() |
|
242 | ||
243 | ||
244 |
def test_data_sources_edit(pub): |
|
245 |
create_superuser(pub) |
|
246 |
NamedDataSource.wipe() |
|
247 |
data_source = NamedDataSource(name='foobar') |
|
248 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
249 |
data_source.store() |
|
250 | ||
251 |
FormDef.wipe() |
|
252 |
app = login(get_app(pub)) |
|
253 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
254 | ||
255 |
resp = resp.click(href='edit') |
|
256 |
assert resp.forms[0]['name'].value == 'foobar' |
|
257 |
resp.forms[0]['description'] = 'data source description' |
|
258 |
resp = resp.forms[0].submit('submit') |
|
259 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/1/' |
|
260 |
resp = resp.follow() |
|
261 | ||
262 |
assert NamedDataSource.get(1).description == 'data source description' |
|
263 | ||
264 | ||
265 |
def test_data_sources_edit_duplicate_name(pub): |
|
266 |
create_superuser(pub) |
|
267 |
NamedDataSource.wipe() |
|
268 |
data_source = NamedDataSource(name='foobar') |
|
269 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
270 |
data_source.store() |
|
271 |
data_source = NamedDataSource(name='foobar2') |
|
272 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
273 |
data_source.store() |
|
274 | ||
275 |
app = login(get_app(pub)) |
|
276 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
277 |
resp = resp.click(href='edit') |
|
278 |
assert resp.forms[0]['name'].value == 'foobar' |
|
279 |
resp.forms[0]['name'] = 'foobar2' |
|
280 |
resp = resp.forms[0].submit('submit') |
|
281 |
assert 'This name is already used' in resp.text |
|
282 | ||
283 |
resp = resp.forms[0].submit('cancel') |
|
284 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/1/' |
|
285 | ||
286 | ||
287 |
def test_data_sources_delete(pub): |
|
288 |
create_superuser(pub) |
|
289 |
NamedDataSource.wipe() |
|
290 |
category = NamedDataSource(name='foobar') |
|
291 |
category.store() |
|
292 | ||
293 |
FormDef.wipe() |
|
294 |
app = login(get_app(pub)) |
|
295 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
296 | ||
297 |
resp = resp.click(href='delete') |
|
298 |
resp = resp.forms[0].submit('cancel') |
|
299 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/' |
|
300 |
assert NamedDataSource.count() == 1 |
|
301 | ||
302 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
303 |
resp = resp.click(href='delete') |
|
304 |
resp = resp.forms[0].submit() |
|
305 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/' |
|
306 |
resp = resp.follow() |
|
307 |
assert NamedDataSource.count() == 0 |
|
308 | ||
309 | ||
310 |
def test_data_sources_in_use_delete(pub): |
|
311 |
create_superuser(pub) |
|
312 |
NamedDataSource.wipe() |
|
313 |
category = NamedDataSource(name='foobar') |
|
314 |
category.store() |
|
315 | ||
316 |
FormDef.wipe() |
|
317 |
formdef = FormDef() |
|
318 |
formdef.name = 'form title' |
|
319 |
formdef.fields = [ |
|
320 |
fields.ItemField(id='0', label='string', type='item', data_source={'type': 'foobar'}), |
|
321 |
] |
|
322 |
formdef.store() |
|
323 | ||
324 |
app = login(get_app(pub)) |
|
325 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
326 |
resp = resp.click(href='delete') |
|
327 |
assert 'This datasource is still used, it cannot be deleted.' in resp.text |
|
328 |
assert 'delete-button' not in resp.text |
|
329 | ||
330 |
formdef.fields = [] |
|
331 |
formdef.store() |
|
332 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
333 |
resp = resp.click(href='delete') |
|
334 |
assert 'delete-button' in resp.text |
|
335 | ||
336 | ||
337 |
def test_data_sources_export(pub): |
|
338 |
create_superuser(pub) |
|
339 |
create_role() |
|
340 | ||
341 |
NamedDataSource.wipe() |
|
342 |
data_source = NamedDataSource(name='foobar') |
|
343 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
344 |
data_source.store() |
|
345 | ||
346 |
app = login(get_app(pub)) |
|
347 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
348 | ||
349 |
resp = resp.click(href='export') |
|
350 |
xml_export = resp.text |
|
351 | ||
352 |
ds = StringIO(xml_export) |
|
353 |
data_source2 = NamedDataSource.import_from_xml(ds) |
|
354 |
assert data_source2.name == 'foobar' |
|
355 | ||
356 | ||
357 |
def test_data_sources_import(pub): |
|
358 |
create_superuser(pub) |
|
359 |
create_role() |
|
360 | ||
361 |
NamedDataSource.wipe() |
|
362 |
data_source = NamedDataSource(name='foobar') |
|
363 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
364 |
data_source.store() |
|
365 |
data_source_xml = ET.tostring(data_source.export_to_xml(include_id=True)) |
|
366 | ||
367 |
NamedDataSource.wipe() |
|
368 |
assert NamedDataSource.count() == 0 |
|
369 | ||
370 |
app = login(get_app(pub)) |
|
371 |
resp = app.get('/backoffice/settings/data-sources/') |
|
372 |
resp = resp.click(href='import') |
|
373 |
resp.forms[0]['file'] = Upload('datasource.wcs', data_source_xml) |
|
374 |
resp = resp.forms[0].submit() |
|
375 |
assert NamedDataSource.count() == 1 |
|
376 | ||
377 |
# import the same datasource a second time, make sure slug is not reused |
|
378 |
resp = app.get('/backoffice/settings/data-sources/') |
|
379 |
resp = resp.click(href='import') |
|
380 |
resp.forms[0]['file'] = Upload('datasource.wcs', data_source_xml) |
|
381 |
resp = resp.forms[0].submit() |
|
382 |
assert NamedDataSource.count() == 2 |
|
383 |
assert NamedDataSource.get(1).slug == 'foobar' |
|
384 |
assert NamedDataSource.get(2).slug == 'foobar-1' |
|
385 | ||
386 |
# import an invalid file |
|
387 |
resp = app.get('/backoffice/settings/data-sources/') |
|
388 |
resp = resp.click(href='import') |
|
389 |
resp.form['file'] = Upload('datasource.wcs', b'garbage') |
|
390 |
resp = resp.form.submit() |
|
391 |
assert 'Invalid File' in resp.text |
|
392 | ||
393 | ||
394 |
def test_data_sources_edit_slug(pub): |
|
395 |
create_superuser(pub) |
|
396 |
NamedDataSource.wipe() |
|
397 |
data_source = NamedDataSource(name='foobar') |
|
398 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
399 |
data_source.store() |
|
400 |
assert NamedDataSource.get(1).slug == 'foobar' |
|
401 | ||
402 |
FormDef.wipe() |
|
403 |
app = login(get_app(pub)) |
|
404 | ||
405 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
406 |
resp = resp.click(href='edit') |
|
407 |
assert resp.forms[0]['name'].value == 'foobar' |
|
408 |
resp.forms[0]['slug'] = 'foo_bar' |
|
409 |
resp = resp.forms[0].submit('submit') |
|
410 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/1/' |
|
411 |
assert NamedDataSource.get(1).slug == 'foo_bar' |
|
412 | ||
413 |
data_source = NamedDataSource(name='barfoo') |
|
414 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
415 |
data_source.store() |
|
416 | ||
417 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
418 |
resp = resp.click(href='edit') |
|
419 |
assert resp.forms[0]['name'].value == 'foobar' |
|
420 |
resp.forms[0]['slug'] = 'barfoo' |
|
421 |
resp = resp.forms[0].submit('submit') |
|
422 |
assert 'This value is already used' in resp.text |
|
423 | ||
424 |
resp.forms[0]['slug'] = 'foobar' |
|
425 |
resp = resp.forms[0].submit('submit') |
|
426 |
assert resp.location == 'http://example.net/backoffice/settings/data-sources/1/' |
|
427 | ||
428 | ||
429 |
def test_data_sources_in_use_edit_slug(pub): |
|
430 |
create_superuser(pub) |
|
431 |
NamedDataSource.wipe() |
|
432 |
data_source = NamedDataSource(name='foobar') |
|
433 |
data_source.data_source = {'type': 'formula', 'value': '[]'} |
|
434 |
data_source.store() |
|
435 |
assert NamedDataSource.get(1).slug == 'foobar' |
|
436 | ||
437 |
FormDef.wipe() |
|
438 |
formdef = FormDef() |
|
439 |
formdef.name = 'form title' |
|
440 |
formdef.fields = [ |
|
441 |
fields.ItemField(id='0', label='string', type='item', data_source={'type': 'foobar'}), |
|
442 |
] |
|
443 |
formdef.store() |
|
444 | ||
445 |
app = login(get_app(pub)) |
|
446 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
447 |
resp = resp.click(href='edit') |
|
448 |
assert 'form_slug' not in resp.form.fields |
|
449 |
resp = resp.form.submit('submit') |
|
450 | ||
451 |
formdef.fields = [] |
|
452 |
formdef.store() |
|
453 |
resp = app.get('/backoffice/settings/data-sources/1/') |
|
454 |
resp = resp.click(href='edit') |
|
455 |
assert 'form_slug' in resp.text |
|
456 |
resp = resp.form.submit('submit') |
|
0 |
- |