From b7e8238eeb54637d5d277e44278cdada231d20b9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 18 Jan 2022 10:30:07 +0100
Subject: [PATCH 5/8] misc: split backoffice_pages tests

---
 tests/backoffice_pages/test_all.py     | 150 -------------
 tests/backoffice_pages/test_columns.py |  51 +----
 tests/backoffice_pages/test_sort.py    | 278 +++++++++++++++++++++++++
 3 files changed, 283 insertions(+), 196 deletions(-)
 create mode 100644 tests/backoffice_pages/test_sort.py

diff --git a/tests/backoffice_pages/test_all.py b/tests/backoffice_pages/test_all.py
index 073a88a3..fd83360b 100644
--- a/tests/backoffice_pages/test_all.py
+++ b/tests/backoffice_pages/test_all.py
@@ -490,63 +490,6 @@ def test_backoffice_listing_pagination(pub):
     resp = app.get('/backoffice/management/form-title/?limit=5&offset=toto', status=400)
 
 
-def test_backoffice_listing_order(pub):
-    if not pub.is_using_postgresql():
-        pytest.skip('this requires SQL')
-        return
-    create_superuser(pub)
-    create_environment(pub)
-    formdef = FormDef.get_by_urlname('form-title')
-    ids = []
-    for i, formdata in enumerate(formdef.data_class().select(order_by='id')):
-        if formdata.status in ('wf-new', 'wf-accepted'):
-            ids.append(formdata.id)
-            formdata.receipt_time = datetime.datetime(2015, 1, 1, 10, i).timetuple()
-            # ordered with odd-numbered ids then even-numbered ids
-            formdata.evolution[-1].time = datetime.datetime(2015, 2, 1, 10 + i % 2, i).timetuple()
-            formdata.store()
-
-    inversed_receipt_time_order = list(reversed([str(x) for x in sorted(ids)]))
-    last_update_time_order = [
-        str(x) for x in sorted(ids, key=lambda x: int(x) if int(x) % 2 else int(x) + 1000)
-    ]
-
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/management/form-title/')
-    assert resp.text.count('data-link') == 17
-    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
-    assert ids == inversed_receipt_time_order
-
-    resp = app.get('/backoffice/management/form-title/?order_by=receipt_time')
-    assert resp.text.count('data-link') == 17
-    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
-    assert ids == list(reversed(inversed_receipt_time_order))
-
-    resp = app.get('/backoffice/management/form-title/?order_by=last_update_time')
-    assert resp.text.count('data-link') == 17
-    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
-    assert ids == last_update_time_order
-
-    resp = app.get('/backoffice/management/form-title/?order_by=-last_update_time')
-    assert resp.text.count('data-link') == 17
-    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
-    assert ids == list(reversed(last_update_time_order))
-
-    if not pub.site_options.has_section('options'):
-        pub.site_options.add_section('options')
-        pub.site_options.set('options', 'default-sort-order', '-last_update_time')
-        with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
-            pub.site_options.write(fd)
-
-    resp = app.get('/backoffice/management/form-title/')
-    assert resp.text.count('data-link') == 17
-    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
-    assert ids == list(reversed(last_update_time_order))
-
-    # try invalid values
-    resp = app.get('/backoffice/management/form-title/?order_by=toto.plop', status=400)
-
-
 def test_backoffice_listing_anonymised(pub):
     if not pub.is_using_postgresql():
         pytest.skip('this requires SQL')
@@ -3354,99 +3297,6 @@ def test_backoffice_workflow_display_form_with_block_add(pub):
     }
 
 
-def test_backoffice_criticality_in_formdef_listing(pub):
-    if not pub.is_using_postgresql():
-        pytest.skip('this requires SQL')
-        return
-    create_user(pub)
-    create_environment(pub)
-
-    wf = Workflow.get_default_workflow()
-    wf.id = '2'
-    wf.criticality_levels = [
-        WorkflowCriticalityLevel(name='green'),
-        WorkflowCriticalityLevel(name='yellow'),
-        WorkflowCriticalityLevel(name='red'),
-    ]
-    wf.store()
-    formdef = FormDef.get_by_urlname('form-title')
-    formdef.workflow_id = wf.id
-    formdef.store()
-
-    formdata1, formdata2, formdata3, formdata4 = [
-        x for x in formdef.data_class().select() if x.status == 'wf-new'
-    ][:4]
-
-    formdata1.set_criticality_level(1)
-    formdata1.store()
-    formdata1_str = '>%s<' % formdata1.get_display_id()
-    formdata2.set_criticality_level(2)
-    formdata2.store()
-    formdata2_str = '>%s<' % formdata2.get_display_id()
-    formdata3.set_criticality_level(2)
-    formdata3.store()
-    formdata3_str = '>%s<' % formdata3.get_display_id()
-    formdata4_str = '>%s<' % formdata4.get_display_id()
-
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/management/form-title/?order_by=-criticality_level&limit=100')
-    assert resp.text.index(formdata1_str) > resp.text.index(formdata2_str)
-    assert resp.text.index(formdata1_str) > resp.text.index(formdata3_str)
-    assert resp.text.index(formdata1_str) < resp.text.index(formdata4_str)
-
-    resp = app.get('/backoffice/management/form-title/?order_by=criticality_level&limit=100')
-    assert resp.text.index(formdata1_str) < resp.text.index(formdata2_str)
-    assert resp.text.index(formdata1_str) < resp.text.index(formdata3_str)
-    assert resp.text.index(formdata1_str) > resp.text.index(formdata4_str)
-
-
-def test_backoffice_criticality_in_global_listing(pub):
-    if not pub.is_using_postgresql():
-        pytest.skip('this requires SQL')
-        return
-
-    create_user(pub)
-    create_environment(pub)
-
-    wf = Workflow.get_default_workflow()
-    wf.id = '2'
-    wf.criticality_levels = [
-        WorkflowCriticalityLevel(name='green'),
-        WorkflowCriticalityLevel(name='yellow'),
-        WorkflowCriticalityLevel(name='red'),
-    ]
-    wf.store()
-    formdef = FormDef.get_by_urlname('form-title')
-    formdef.workflow_id = wf.id
-    formdef.store()
-
-    formdata1, formdata2, formdata3, formdata4 = [
-        x for x in formdef.data_class().select() if x.status == 'wf-new'
-    ][:4]
-
-    formdata1.set_criticality_level(1)
-    formdata1.store()
-    formdata1_str = '>%s<' % formdata1.get_display_id()
-    formdata2.set_criticality_level(2)
-    formdata2.store()
-    formdata2_str = '>%s<' % formdata2.get_display_id()
-    formdata3.set_criticality_level(2)
-    formdata3.store()
-    formdata3_str = '>%s<' % formdata3.get_display_id()
-    formdata4_str = '>%s<' % formdata4.get_display_id()
-
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/management/listing?order_by=-criticality_level&limit=100')
-    assert resp.text.index(formdata1_str) > resp.text.index(formdata2_str)
-    assert resp.text.index(formdata1_str) > resp.text.index(formdata3_str)
-    assert resp.text.index(formdata1_str) < resp.text.index(formdata4_str)
-
-    resp = app.get('/backoffice/management/listing?order_by=criticality_level&limit=100')
-    assert resp.text.index(formdata1_str) < resp.text.index(formdata2_str)
-    assert resp.text.index(formdata1_str) < resp.text.index(formdata3_str)
-    assert resp.text.index(formdata1_str) > resp.text.index(formdata4_str)
-
-
 def test_backoffice_criticality_formdata_view(pub):
     create_user(pub)
     create_environment(pub)
diff --git a/tests/backoffice_pages/test_columns.py b/tests/backoffice_pages/test_columns.py
index c15adea5..bbd5604a 100644
--- a/tests/backoffice_pages/test_columns.py
+++ b/tests/backoffice_pages/test_columns.py
@@ -471,17 +471,17 @@ def test_backoffice_block_columns(pub):
     data_class = formdef.data_class()
     data_class.wipe()
 
-    formdata1 = data_class()
-    formdata1.data = {
+    formdata = data_class()
+    formdata.data = {
         '8': {
             'data': [{'123': 'blah', '456': card.id, '456_display': card.default_digest}],
             'schema': {},  # not important here
         },
         '8_display': 'blah',
     }
-    formdata1.just_created()
-    formdata1.jump_status('new')
-    formdata1.store()
+    formdata.just_created()
+    formdata.jump_status('new')
+    formdata.store()
 
     app = login(get_app(pub))
     resp = app.get('/backoffice/management/form-title/')
@@ -508,37 +508,6 @@ def test_backoffice_block_columns(pub):
     formdef.fields[0].max_items = 1
     formdef.store()
 
-    card2 = carddef.data_class()()
-    card2.data = {
-        '1': 'Bar2',
-        '2': 'Foo2',
-    }
-    card2.store()
-
-    formdata2 = data_class()
-    formdata2.data = {
-        '8': {
-            'data': [{'123': 'blah', '456': card2.id, '456_display': card2.default_digest}],
-            'schema': {},  # not important here
-        },
-        '8_display': 'blah',
-    }
-    formdata2.just_created()
-    formdata2.jump_status('new')
-    formdata2.store()
-
-    formdata3 = data_class()
-    formdata3.data = {
-        '8': {
-            'data': [{'123': 'blah'}],
-            'schema': {},  # not important here
-        },
-        '8_display': 'blah',
-    }
-    formdata3.just_created()
-    formdata3.jump_status('new')
-    formdata3.store()
-
     resp = app.get('/backoffice/management/form-title/')
     assert [x.text_content() for x in resp.pyquery('#columns-filter label')] == [
         'Number',
@@ -556,13 +525,3 @@ def test_backoffice_block_columns(pub):
     resp = resp.forms['listing-settings'].submit()
     assert '<th data-field-sort-key="f8-123"><span>Block / Test</span></th>' in resp
     assert '<th data-field-sort-key="f8-456"><span>Block / card field</span></th>' in resp
-    assert resp.text.count('<tr') == 4
-
-    # XXX ordering is done on card id, to be fixed with #60742
-    resp = app.get('/backoffice/management/form-title/?order_by=f8-456')
-    ids = [int(x.strip('/')) for x in re.findall(r'data-link="(.*?)"', resp.text)]
-    assert ids == [formdata1.id, formdata2.id, formdata3.id]
-
-    resp = app.get('/backoffice/management/form-title/?order_by=-f8-456')
-    ids = [int(x.strip('/')) for x in re.findall(r'data-link="(.*?)"', resp.text)]
-    assert ids == [formdata3.id, formdata2.id, formdata1.id]
diff --git a/tests/backoffice_pages/test_sort.py b/tests/backoffice_pages/test_sort.py
new file mode 100644
index 00000000..b3a08e1c
--- /dev/null
+++ b/tests/backoffice_pages/test_sort.py
@@ -0,0 +1,278 @@
+import datetime
+import os
+import re
+
+import pytest
+
+from wcs import fields
+from wcs.blocks import BlockDef
+from wcs.carddef import CardDef
+from wcs.formdef import FormDef
+from wcs.qommon.http_request import HTTPRequest
+from wcs.workflows import Workflow, WorkflowCriticalityLevel
+
+from ..utilities import clean_temporary_pub, create_temporary_pub, get_app, login
+from .test_all import create_environment, create_superuser, create_user
+
+
+@pytest.fixture
+def pub(request, emails):
+    pub = create_temporary_pub(sql_mode=True)
+
+    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()
+    with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
+        fd.write(
+            '''
+    [api-secrets]
+    coucou = 1234
+    '''
+        )
+
+    return pub
+
+
+def teardown_module(module):
+    clean_temporary_pub()
+
+
+def test_backoffice_listing_order(pub):
+    create_superuser(pub)
+    create_environment(pub)
+    formdef = FormDef.get_by_urlname('form-title')
+    ids = []
+    for i, formdata in enumerate(formdef.data_class().select(order_by='id')):
+        if formdata.status in ('wf-new', 'wf-accepted'):
+            ids.append(formdata.id)
+            formdata.receipt_time = datetime.datetime(2015, 1, 1, 10, i).timetuple()
+            # ordered with odd-numbered ids then even-numbered ids
+            formdata.evolution[-1].time = datetime.datetime(2015, 2, 1, 10 + i % 2, i).timetuple()
+            formdata.store()
+
+    inversed_receipt_time_order = list(reversed([str(x) for x in sorted(ids)]))
+    last_update_time_order = [
+        str(x) for x in sorted(ids, key=lambda x: int(x) if int(x) % 2 else int(x) + 1000)
+    ]
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/management/form-title/')
+    assert resp.text.count('data-link') == 17
+    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
+    assert ids == inversed_receipt_time_order
+
+    resp = app.get('/backoffice/management/form-title/?order_by=receipt_time')
+    assert resp.text.count('data-link') == 17
+    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
+    assert ids == list(reversed(inversed_receipt_time_order))
+
+    resp = app.get('/backoffice/management/form-title/?order_by=last_update_time')
+    assert resp.text.count('data-link') == 17
+    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
+    assert ids == last_update_time_order
+
+    resp = app.get('/backoffice/management/form-title/?order_by=-last_update_time')
+    assert resp.text.count('data-link') == 17
+    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
+    assert ids == list(reversed(last_update_time_order))
+
+    if not pub.site_options.has_section('options'):
+        pub.site_options.add_section('options')
+        pub.site_options.set('options', 'default-sort-order', '-last_update_time')
+        with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
+            pub.site_options.write(fd)
+
+    resp = app.get('/backoffice/management/form-title/')
+    assert resp.text.count('data-link') == 17
+    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
+    assert ids == list(reversed(last_update_time_order))
+
+    # try invalid values
+    resp = app.get('/backoffice/management/form-title/?order_by=toto.plop', status=400)
+
+
+def test_backoffice_criticality_in_formdef_listing_order(pub):
+    create_user(pub)
+    create_environment(pub)
+
+    wf = Workflow.get_default_workflow()
+    wf.id = '2'
+    wf.criticality_levels = [
+        WorkflowCriticalityLevel(name='green'),
+        WorkflowCriticalityLevel(name='yellow'),
+        WorkflowCriticalityLevel(name='red'),
+    ]
+    wf.store()
+    formdef = FormDef.get_by_urlname('form-title')
+    formdef.workflow_id = wf.id
+    formdef.store()
+
+    formdata1, formdata2, formdata3, formdata4 = [
+        x for x in formdef.data_class().select() if x.status == 'wf-new'
+    ][:4]
+
+    formdata1.set_criticality_level(1)
+    formdata1.store()
+    formdata1_str = '>%s<' % formdata1.get_display_id()
+    formdata2.set_criticality_level(2)
+    formdata2.store()
+    formdata2_str = '>%s<' % formdata2.get_display_id()
+    formdata3.set_criticality_level(2)
+    formdata3.store()
+    formdata3_str = '>%s<' % formdata3.get_display_id()
+    formdata4_str = '>%s<' % formdata4.get_display_id()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/management/form-title/?order_by=-criticality_level&limit=100')
+    assert resp.text.index(formdata1_str) > resp.text.index(formdata2_str)
+    assert resp.text.index(formdata1_str) > resp.text.index(formdata3_str)
+    assert resp.text.index(formdata1_str) < resp.text.index(formdata4_str)
+
+    resp = app.get('/backoffice/management/form-title/?order_by=criticality_level&limit=100')
+    assert resp.text.index(formdata1_str) < resp.text.index(formdata2_str)
+    assert resp.text.index(formdata1_str) < resp.text.index(formdata3_str)
+    assert resp.text.index(formdata1_str) > resp.text.index(formdata4_str)
+
+
+def test_backoffice_criticality_in_global_listing_order(pub):
+
+    create_user(pub)
+    create_environment(pub)
+
+    wf = Workflow.get_default_workflow()
+    wf.id = '2'
+    wf.criticality_levels = [
+        WorkflowCriticalityLevel(name='green'),
+        WorkflowCriticalityLevel(name='yellow'),
+        WorkflowCriticalityLevel(name='red'),
+    ]
+    wf.store()
+    formdef = FormDef.get_by_urlname('form-title')
+    formdef.workflow_id = wf.id
+    formdef.store()
+
+    formdata1, formdata2, formdata3, formdata4 = [
+        x for x in formdef.data_class().select() if x.status == 'wf-new'
+    ][:4]
+
+    formdata1.set_criticality_level(1)
+    formdata1.store()
+    formdata1_str = '>%s<' % formdata1.get_display_id()
+    formdata2.set_criticality_level(2)
+    formdata2.store()
+    formdata2_str = '>%s<' % formdata2.get_display_id()
+    formdata3.set_criticality_level(2)
+    formdata3.store()
+    formdata3_str = '>%s<' % formdata3.get_display_id()
+    formdata4_str = '>%s<' % formdata4.get_display_id()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/management/listing?order_by=-criticality_level&limit=100')
+    assert resp.text.index(formdata1_str) > resp.text.index(formdata2_str)
+    assert resp.text.index(formdata1_str) > resp.text.index(formdata3_str)
+    assert resp.text.index(formdata1_str) < resp.text.index(formdata4_str)
+
+    resp = app.get('/backoffice/management/listing?order_by=criticality_level&limit=100')
+    assert resp.text.index(formdata1_str) < resp.text.index(formdata2_str)
+    assert resp.text.index(formdata1_str) < resp.text.index(formdata3_str)
+    assert resp.text.index(formdata1_str) > resp.text.index(formdata4_str)
+
+
+def test_backoffice_block_columns_order(pub):
+    pub.user_class.wipe()
+    create_superuser(pub)
+    pub.role_class.wipe()
+    role = pub.role_class(name='test')
+    role.store()
+
+    CardDef.wipe()
+    carddef = CardDef()
+    carddef.name = 'foo'
+    carddef.fields = [
+        fields.StringField(id='1', label='First Name', type='string', varname='first_name'),
+        fields.StringField(id='2', label='Last Name', type='string', varname='last_name'),
+    ]
+    carddef.digest_templates = {'default': '{{ form_var_first_name }} {{ form_var_last_name }}'}
+    carddef.store()
+    carddef.data_class().wipe()
+    card = carddef.data_class()()
+    card.data = {
+        '1': 'Foo',
+        '2': 'Bar',
+    }
+    card.store()
+    card2 = carddef.data_class()()
+    card2.data = {
+        '1': 'Bar2',
+        '2': 'Foo2',
+    }
+    card2.store()
+
+    BlockDef.wipe()
+    block = BlockDef()
+    block.name = 'foobar'
+    block.fields = [
+        fields.StringField(id='123', required=True, label='Test', type='string'),
+        fields.ItemField(id='456', label='card field', type='item', data_source={'type': 'carddef:foo'}),
+    ]
+    block.store()
+
+    FormDef.wipe()
+    formdef = FormDef()
+    formdef.name = 'form-title'
+    formdef.fields = [
+        fields.BlockField(id='8', label='Block', type='block:foobar', varname='data', max_items=3),
+    ]
+    formdef.workflow_roles = {'_receiver': role.id}
+    formdef.store()
+
+    data_class = formdef.data_class()
+    data_class.wipe()
+
+    formdata1 = data_class()
+    formdata1.data = {
+        '8': {
+            'data': [{'123': 'blah', '456': card.id, '456_display': card.default_digest}],
+            'schema': {},  # not important here
+        },
+        '8_display': 'blah',
+    }
+    formdata1.just_created()
+    formdata1.jump_status('new')
+    formdata1.store()
+
+    formdata2 = data_class()
+    formdata2.data = {
+        '8': {
+            'data': [{'123': 'blah', '456': card2.id, '456_display': card2.default_digest}],
+            'schema': {},  # not important here
+        },
+        '8_display': 'blah',
+    }
+    formdata2.just_created()
+    formdata2.jump_status('new')
+    formdata2.store()
+
+    formdata3 = data_class()
+    formdata3.data = {
+        '8': {
+            'data': [{'123': 'blah'}],
+            'schema': {},  # not important here
+        },
+        '8_display': 'blah',
+    }
+    formdata3.just_created()
+    formdata3.jump_status('new')
+    formdata3.store()
+
+    # XXX ordering is done on card id, to be fixed with #60742
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/management/form-title/?order_by=f8-456')
+    ids = [int(x.strip('/')) for x in re.findall(r'data-link="(.*?)"', resp.text)]
+    assert ids == [formdata1.id, formdata2.id, formdata3.id]
+
+    resp = app.get('/backoffice/management/form-title/?order_by=-f8-456')
+    ids = [int(x.strip('/')) for x in re.findall(r'data-link="(.*?)"', resp.text)]
+    assert ids == [formdata3.id, formdata2.id, formdata1.id]
-- 
2.34.1

