From b8ffd067c2283f359fa09d3cea3c99e63235f7a3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 23 Oct 2020 11:23:32 +0200
Subject: [PATCH 4/5] misc: split admin pages tests (block)

---
 tests/admin_pages/test_all.py   | 203 --------------------------
 tests/admin_pages/test_block.py | 245 ++++++++++++++++++++++++++++++++
 2 files changed, 245 insertions(+), 203 deletions(-)
 create mode 100644 tests/admin_pages/test_block.py

diff --git a/tests/admin_pages/test_all.py b/tests/admin_pages/test_all.py
index eb61a513..5170aa63 100644
--- a/tests/admin_pages/test_all.py
+++ b/tests/admin_pages/test_all.py
@@ -2,7 +2,6 @@
 
 import logging
 import os
-import re
 import zipfile
 
 try:
@@ -36,7 +35,6 @@ from wcs.wf.redirect_to_url import RedirectToUrlWorkflowStatusItem
 from wcs.wf.create_formdata import CreateFormdataWorkflowStatusItem, Mapping
 from wcs.formdef import FormDef
 from wcs.carddef import CardDef
-from wcs.blocks import BlockDef
 from wcs import fields
 
 from utilities import get_app, login, create_temporary_pub, clean_temporary_pub
@@ -1725,204 +1723,3 @@ def test_create_formdata(pub):
     wf2 = Workflow.get(wf2.id)
     assert wf2.possible_status[-1].items[-1].map_fields_by_varname is True
     assert wf2.possible_status[-1].items[-1].mappings is None
-
-
-def test_block_new(pub, blocks_feature):
-    create_superuser(pub)
-    create_role()
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/')
-    resp = resp.click('Fields blocks')
-    resp = resp.click('New field block')
-    resp.form['name'] = 'field block'
-    resp = resp.form.submit()
-    assert resp.location == 'http://example.net/backoffice/forms/blocks/1/'
-    resp = resp.follow()
-    assert '<h2>field block' in resp
-    assert 'There are not yet any fields' in resp
-
-    resp.form['label'] = 'foobar'
-    resp.form['type'] = 'string'
-    resp = resp.form.submit()
-    assert resp.location == 'http://example.net/backoffice/forms/blocks/1/'
-    resp = resp.follow()
-
-    resp.form['label'] = 'barfoo'
-    resp.form['type'] = 'string'
-    resp = resp.form.submit()
-    assert resp.location == 'http://example.net/backoffice/forms/blocks/1/'
-    resp = resp.follow()
-
-    assert len(BlockDef.get(1).fields) == 2
-    assert str(BlockDef.get(1).fields[0].id) != '1'  # don't use integers
-
-
-def test_block_options(pub, blocks_feature):
-    create_superuser(pub)
-    create_role()
-    BlockDef.wipe()
-    block = BlockDef()
-    block.name = 'foobar'
-    block.fields = [fields.StringField(id='123', required=True, label='Test', type='string')]
-    block.store()
-
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/blocks/%s/' % block.id)
-    resp = resp.click(href=re.compile('^settings$'))
-    assert 'readonly' not in resp.form['slug'].attrs
-    resp.form['name'] = 'foo bar'
-    resp = resp.form.submit('submit')
-    assert BlockDef.get(block.id).name == 'foo bar'
-
-    FormDef.wipe()
-    formdef = FormDef()
-    formdef.name = 'form title'
-    formdef.fields = [
-        fields.BlockField(id='0', label='test', type='block:%s' % block.slug),
-    ]
-    formdef.store()
-
-    resp = app.get('/backoffice/forms/blocks/%s/' % block.id)
-    resp = resp.click(href=re.compile('^settings$'))
-    assert 'readonly' in resp.form['slug'].attrs
-    resp = resp.form.submit('cancel')
-    resp = resp.follow()
-
-
-def test_block_export_import(pub, blocks_feature):
-    create_superuser(pub)
-    create_role()
-    BlockDef.wipe()
-    block = BlockDef()
-    block.name = 'foobar'
-    block.fields = [fields.StringField(id='123', required=True, label='Test', type='string')]
-    block.store()
-
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/blocks/%s/' % block.id)
-    resp = resp.click(href=re.compile('^export$'))
-    xml_export = resp.text
-
-    resp = app.get('/backoffice/forms/blocks/')
-    resp = resp.click(href='import')
-    resp = resp.form.submit('cancel')  # shouldn't block on missing file
-    resp = resp.follow()
-
-    resp = resp.click(href='import')
-    resp = resp.form.submit()
-    assert 'ere were errors processing your form.' in resp
-
-    resp.form['file'] = Upload('block', xml_export.encode('utf-8'))
-    resp = resp.form.submit()
-    resp = resp.follow()
-    assert BlockDef.count() == 2
-
-    new_blockdef = [x for x in BlockDef.select() if str(x.id) != str(block.id)][0]
-    assert new_blockdef.name == 'Copy of foobar'
-    assert new_blockdef.slug == 'foobar_1'
-    assert len(new_blockdef.fields) == 1
-    assert new_blockdef.fields[0].id == '123'
-
-    resp = app.get('/backoffice/forms/blocks/')
-    resp = resp.click(href='import')
-    resp.form['file'] = Upload('block', xml_export.encode('utf-8'))
-    resp = resp.form.submit()
-    assert 'Copy of foobar (2)' in [x.name for x in BlockDef.select()]
-
-    # import invalid content
-    resp = app.get('/backoffice/forms/blocks/')
-    resp = resp.click(href='import')
-    resp.form['file'] = Upload('block', b'whatever')
-    resp = resp.form.submit()
-    assert 'Invalid File' in resp
-
-
-def test_block_delete(pub, blocks_feature):
-    create_superuser(pub)
-    create_role()
-    BlockDef.wipe()
-    FormDef.wipe()
-    block = BlockDef()
-    block.name = 'foobar'
-    block.fields = [fields.StringField(id='123', required=True, label='Test', type='string')]
-    block.store()
-
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/blocks/%s/' % block.id)
-    resp = resp.click(href=re.compile('^delete$'))
-    assert 'You are about to irrevocably delete this block.' in resp
-    resp = resp.form.submit()
-    resp = resp.follow()
-    assert BlockDef.count() == 0
-
-    # in use
-    BlockDef.wipe()
-    block = BlockDef()
-    block.name = 'foobar'
-    block.fields = [fields.StringField(id='123', required=True, label='Test', type='string')]
-    block.store()
-
-    FormDef.wipe()
-    formdef = FormDef()
-    formdef.name = 'form title'
-    formdef.fields = [
-        fields.BlockField(id='0', label='test', type='block:%s' % block.slug),
-    ]
-    formdef.store()
-    resp = app.get('/backoffice/forms/blocks/%s/' % block.id)
-    resp = resp.click(href=re.compile('^delete$'))
-    assert 'This block is still used' in resp
-
-
-def test_block_edit_duplicate_delete_field(pub, blocks_feature):
-    create_superuser(pub)
-    create_role()
-    BlockDef.wipe()
-    block = BlockDef()
-    block.name = 'foobar'
-    block.fields = [fields.StringField(id='123', required=True, label='Test', type='string')]
-    block.store()
-
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/blocks/%s/' % block.id)
-    resp = resp.click(href=re.compile('123/$'))
-    resp.form['required'].checked = False
-    resp.form['varname'] = 'test'
-    resp = resp.form.submit('submit')
-    resp = resp.follow()
-    assert BlockDef.get(block.id).fields[0].required is False
-    assert BlockDef.get(block.id).fields[0].varname == 'test'
-
-    resp = resp.click(href=re.compile('123/duplicate$'))
-    resp = resp.follow()
-    assert len(BlockDef.get(block.id).fields) == 2
-
-    resp = resp.click(href='%s/delete' % BlockDef.get(block.id).fields[1].id)
-    resp = resp.form.submit('submit')
-    resp = resp.follow()
-    assert len(BlockDef.get(block.id).fields) == 1
-
-
-def test_block_use_in_formdef(pub, blocks_feature):
-    create_superuser(pub)
-    create_role()
-    FormDef.wipe()
-    BlockDef.wipe()
-    block = BlockDef()
-    block.name = 'foobar'
-    block.fields = [fields.StringField(id='123', required=True, label='Test', type='string')]
-    block.store()
-
-    formdef = FormDef()
-    formdef.name = 'form title'
-    formdef.fields = []
-    formdef.store()
-
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/1/fields/')
-    resp.forms[0]['label'] = 'a block field'
-    resp.forms[0]['type'] = 'block:foobar'
-    resp = resp.forms[0].submit().follow()
-    assert 'a block field' in resp.text
-    resp = resp.click('Edit', href='1/')
-    assert resp.form['max_items'].value == '1'
diff --git a/tests/admin_pages/test_block.py b/tests/admin_pages/test_block.py
new file mode 100644
index 00000000..08552f5f
--- /dev/null
+++ b/tests/admin_pages/test_block.py
@@ -0,0 +1,245 @@
+# -*- coding: utf-8 -*-
+
+import re
+
+try:
+    import lasso
+except ImportError:
+    lasso = None
+
+import pytest
+from webtest import Upload
+
+from wcs.qommon.http_request import HTTPRequest
+from wcs.formdef import FormDef
+from wcs.blocks import BlockDef
+from wcs import fields
+
+from utilities import get_app, login, create_temporary_pub, clean_temporary_pub
+from .test_all import create_superuser, create_role
+
+
+def pytest_generate_tests(metafunc):
+    if 'pub' in metafunc.fixturenames:
+        metafunc.parametrize('pub', ['pickle', 'sql', 'pickle-templates'], indirect=True)
+
+
+@pytest.fixture
+def pub(request):
+    pub = create_temporary_pub(
+            sql_mode=bool('sql' in request.param),
+            templates_mode=bool('templates' in request.param)
+            )
+
+    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()
+
+    return pub
+
+
+def teardown_module(module):
+    clean_temporary_pub()
+
+
+def test_block_new(pub, blocks_feature):
+    create_superuser(pub)
+    create_role()
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/forms/')
+    resp = resp.click('Fields blocks')
+    resp = resp.click('New field block')
+    resp.form['name'] = 'field block'
+    resp = resp.form.submit()
+    assert resp.location == 'http://example.net/backoffice/forms/blocks/1/'
+    resp = resp.follow()
+    assert '<h2>field block' in resp
+    assert 'There are not yet any fields' in resp
+
+    resp.form['label'] = 'foobar'
+    resp.form['type'] = 'string'
+    resp = resp.form.submit()
+    assert resp.location == 'http://example.net/backoffice/forms/blocks/1/'
+    resp = resp.follow()
+
+    resp.form['label'] = 'barfoo'
+    resp.form['type'] = 'string'
+    resp = resp.form.submit()
+    assert resp.location == 'http://example.net/backoffice/forms/blocks/1/'
+    resp = resp.follow()
+
+    assert len(BlockDef.get(1).fields) == 2
+    assert str(BlockDef.get(1).fields[0].id) != '1'  # don't use integers
+
+
+def test_block_options(pub, blocks_feature):
+    create_superuser(pub)
+    create_role()
+    BlockDef.wipe()
+    block = BlockDef()
+    block.name = 'foobar'
+    block.fields = [fields.StringField(id='123', required=True, label='Test', type='string')]
+    block.store()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/forms/blocks/%s/' % block.id)
+    resp = resp.click(href=re.compile('^settings$'))
+    assert 'readonly' not in resp.form['slug'].attrs
+    resp.form['name'] = 'foo bar'
+    resp = resp.form.submit('submit')
+    assert BlockDef.get(block.id).name == 'foo bar'
+
+    FormDef.wipe()
+    formdef = FormDef()
+    formdef.name = 'form title'
+    formdef.fields = [
+        fields.BlockField(id='0', label='test', type='block:%s' % block.slug),
+    ]
+    formdef.store()
+
+    resp = app.get('/backoffice/forms/blocks/%s/' % block.id)
+    resp = resp.click(href=re.compile('^settings$'))
+    assert 'readonly' in resp.form['slug'].attrs
+    resp = resp.form.submit('cancel')
+    resp = resp.follow()
+
+
+def test_block_export_import(pub, blocks_feature):
+    create_superuser(pub)
+    create_role()
+    BlockDef.wipe()
+    block = BlockDef()
+    block.name = 'foobar'
+    block.fields = [fields.StringField(id='123', required=True, label='Test', type='string')]
+    block.store()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/forms/blocks/%s/' % block.id)
+    resp = resp.click(href=re.compile('^export$'))
+    xml_export = resp.text
+
+    resp = app.get('/backoffice/forms/blocks/')
+    resp = resp.click(href='import')
+    resp = resp.form.submit('cancel')  # shouldn't block on missing file
+    resp = resp.follow()
+
+    resp = resp.click(href='import')
+    resp = resp.form.submit()
+    assert 'ere were errors processing your form.' in resp
+
+    resp.form['file'] = Upload('block', xml_export.encode('utf-8'))
+    resp = resp.form.submit()
+    resp = resp.follow()
+    assert BlockDef.count() == 2
+
+    new_blockdef = [x for x in BlockDef.select() if str(x.id) != str(block.id)][0]
+    assert new_blockdef.name == 'Copy of foobar'
+    assert new_blockdef.slug == 'foobar_1'
+    assert len(new_blockdef.fields) == 1
+    assert new_blockdef.fields[0].id == '123'
+
+    resp = app.get('/backoffice/forms/blocks/')
+    resp = resp.click(href='import')
+    resp.form['file'] = Upload('block', xml_export.encode('utf-8'))
+    resp = resp.form.submit()
+    assert 'Copy of foobar (2)' in [x.name for x in BlockDef.select()]
+
+    # import invalid content
+    resp = app.get('/backoffice/forms/blocks/')
+    resp = resp.click(href='import')
+    resp.form['file'] = Upload('block', b'whatever')
+    resp = resp.form.submit()
+    assert 'Invalid File' in resp
+
+
+def test_block_delete(pub, blocks_feature):
+    create_superuser(pub)
+    create_role()
+    BlockDef.wipe()
+    FormDef.wipe()
+    block = BlockDef()
+    block.name = 'foobar'
+    block.fields = [fields.StringField(id='123', required=True, label='Test', type='string')]
+    block.store()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/forms/blocks/%s/' % block.id)
+    resp = resp.click(href=re.compile('^delete$'))
+    assert 'You are about to irrevocably delete this block.' in resp
+    resp = resp.form.submit()
+    resp = resp.follow()
+    assert BlockDef.count() == 0
+
+    # in use
+    BlockDef.wipe()
+    block = BlockDef()
+    block.name = 'foobar'
+    block.fields = [fields.StringField(id='123', required=True, label='Test', type='string')]
+    block.store()
+
+    FormDef.wipe()
+    formdef = FormDef()
+    formdef.name = 'form title'
+    formdef.fields = [
+        fields.BlockField(id='0', label='test', type='block:%s' % block.slug),
+    ]
+    formdef.store()
+    resp = app.get('/backoffice/forms/blocks/%s/' % block.id)
+    resp = resp.click(href=re.compile('^delete$'))
+    assert 'This block is still used' in resp
+
+
+def test_block_edit_duplicate_delete_field(pub, blocks_feature):
+    create_superuser(pub)
+    create_role()
+    BlockDef.wipe()
+    block = BlockDef()
+    block.name = 'foobar'
+    block.fields = [fields.StringField(id='123', required=True, label='Test', type='string')]
+    block.store()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/forms/blocks/%s/' % block.id)
+    resp = resp.click(href=re.compile('123/$'))
+    resp.form['required'].checked = False
+    resp.form['varname'] = 'test'
+    resp = resp.form.submit('submit')
+    resp = resp.follow()
+    assert BlockDef.get(block.id).fields[0].required is False
+    assert BlockDef.get(block.id).fields[0].varname == 'test'
+
+    resp = resp.click(href=re.compile('123/duplicate$'))
+    resp = resp.follow()
+    assert len(BlockDef.get(block.id).fields) == 2
+
+    resp = resp.click(href='%s/delete' % BlockDef.get(block.id).fields[1].id)
+    resp = resp.form.submit('submit')
+    resp = resp.follow()
+    assert len(BlockDef.get(block.id).fields) == 1
+
+
+def test_block_use_in_formdef(pub, blocks_feature):
+    create_superuser(pub)
+    create_role()
+    FormDef.wipe()
+    BlockDef.wipe()
+    block = BlockDef()
+    block.name = 'foobar'
+    block.fields = [fields.StringField(id='123', required=True, label='Test', type='string')]
+    block.store()
+
+    formdef = FormDef()
+    formdef.name = 'form title'
+    formdef.fields = []
+    formdef.store()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/forms/1/fields/')
+    resp.forms[0]['label'] = 'a block field'
+    resp.forms[0]['type'] = 'block:foobar'
+    resp = resp.forms[0].submit().follow()
+    assert 'a block field' in resp.text
+    resp = resp.click('Edit', href='1/')
+    assert resp.form['max_items'].value == '1'
-- 
2.28.0

