From f532dc7253530fd9dcb287646f9fad623e8dce74 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 2 Nov 2020 16:09:23 +0100
Subject: [PATCH 1/2] misc: split admin pages tests (category)

---
 tests/admin_pages/test_all.py      | 188 ------------------------
 tests/admin_pages/test_category.py | 223 +++++++++++++++++++++++++++++
 2 files changed, 223 insertions(+), 188 deletions(-)
 create mode 100644 tests/admin_pages/test_category.py

diff --git a/tests/admin_pages/test_all.py b/tests/admin_pages/test_all.py
index 5170aa63..fe60decc 100644
--- a/tests/admin_pages/test_all.py
+++ b/tests/admin_pages/test_all.py
@@ -601,194 +601,6 @@ def test_roles_delete(pub):
     assert Role.count() == 0
 
 
-def test_categories(pub):
-    create_superuser(pub)
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/categories/')
-
-
-def test_categories_legacy_urls(pub):
-    create_superuser(pub)
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/categories/')
-    assert resp.location.endswith('/backoffice/forms/categories/')
-    resp = app.get('/backoffice/categories/1')
-    assert resp.location.endswith('/backoffice/forms/categories/1')
-    resp = app.get('/backoffice/categories/1/')
-    assert resp.location.endswith('/backoffice/forms/categories/1/')
-
-
-def test_categories_new(pub):
-    create_superuser(pub)
-    Category.wipe()
-    app = login(get_app(pub))
-
-    # go to the page and cancel
-    resp = app.get('/backoffice/forms/categories/')
-    resp = resp.click('New Category')
-    resp = resp.forms[0].submit('cancel')
-    assert resp.location == 'http://example.net/backoffice/forms/categories/'
-
-    # go to the page and add a category
-    resp = app.get('/backoffice/forms/categories/')
-    resp = resp.click('New Category')
-    resp.forms[0]['name'] = 'a new category'
-    resp.forms[0]['description'] = 'description of the category'
-    resp = resp.forms[0].submit('submit')
-    assert resp.location == 'http://example.net/backoffice/forms/categories/'
-    resp = resp.follow()
-    assert 'a new category' in resp.text
-    resp = resp.click('a new category')
-    assert '<h2>a new category' in resp.text
-
-    assert Category.get(1).name == 'a new category'
-    assert Category.get(1).description == 'description of the category'
-
-
-def test_categories_edit(pub):
-    create_superuser(pub)
-    Category.wipe()
-    category = Category(name='foobar')
-    category.store()
-
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/categories/1/')
-    assert 'no form associated to this category' in resp.text
-
-    resp = resp.click(href='edit')
-    assert resp.forms[0]['name'].value == 'foobar'
-    resp.forms[0]['description'] = 'category description'
-    resp = resp.forms[0].submit('submit')
-    assert resp.location == 'http://example.net/backoffice/forms/categories/'
-    resp = resp.follow()
-    resp = resp.click('foobar')
-    assert '<h2>foobar' in resp.text
-
-    assert Category.get(1).description == 'category description'
-
-
-def test_categories_edit_duplicate_name(pub):
-    Category.wipe()
-    category = Category(name='foobar')
-    category.store()
-    category = Category(name='foobar2')
-    category.store()
-
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/categories/1/')
-
-    resp = resp.click(href='edit')
-    assert resp.forms[0]['name'].value == 'foobar'
-    resp.forms[0]['name'] = 'foobar2'
-    resp = resp.forms[0].submit('submit')
-    assert 'This name is already used' in resp.text
-
-    resp = resp.forms[0].submit('cancel')
-    assert resp.location == 'http://example.net/backoffice/forms/categories/'
-
-
-def test_categories_with_formdefs(pub):
-    Category.wipe()
-    category = Category(name='foobar')
-    category.store()
-
-    FormDef.wipe()
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/categories/1/')
-    assert 'form bar' not in resp.text
-
-    formdef = FormDef()
-    formdef.name = 'form bar'
-    formdef.fields = []
-    formdef.category_id = category.id
-    formdef.store()
-
-    resp = app.get('/backoffice/forms/categories/1/')
-    assert 'form bar' in resp.text
-    assert 'no form associated to this category' not in resp.text
-
-
-def test_categories_delete(pub):
-    Category.wipe()
-    category = Category(name='foobar')
-    category.store()
-
-    FormDef.wipe()
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/categories/1/')
-
-    resp = resp.click(href='delete')
-    resp = resp.forms[0].submit('cancel')
-    assert resp.location == 'http://example.net/backoffice/forms/categories/1/'
-    assert Category.count() == 1
-
-    resp = app.get('/backoffice/forms/categories/1/')
-    resp = resp.click(href='delete')
-    resp = resp.forms[0].submit()
-    assert resp.location == 'http://example.net/backoffice/forms/categories/'
-    resp = resp.follow()
-    assert Category.count() == 0
-
-
-def test_categories_edit_description(pub):
-    Category.wipe()
-    category = Category(name='foobar')
-    category.description = 'category description'
-    category.store()
-
-    app = login(get_app(pub))
-    # this URL is used for editing from the frontoffice, there's no link
-    # pointing to it in the admin.
-    resp = app.get('/backoffice/forms/categories/1/description')
-    assert resp.forms[0]['description'].value == 'category description'
-    resp.forms[0]['description'] = 'updated description'
-
-    # check cancel doesn't save the change
-    resp2 = resp.forms[0].submit('cancel')
-    assert resp2.location == 'http://example.net/backoffice/forms/categories/1/'
-    assert Category.get(1).description == 'category description'
-
-    # check submit does it properly
-    resp2 = resp.forms[0].submit('submit')
-    assert resp2.location == 'http://example.net/backoffice/forms/categories/1/'
-    resp2 = resp2.follow()
-    assert Category.get(1).description == 'updated description'
-
-
-def test_categories_new_duplicate_name(pub):
-    Category.wipe()
-    category = Category(name='foobar')
-    category.store()
-
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/categories/')
-    resp = resp.click('New Category')
-    resp.forms[0]['name'] = 'foobar'
-    resp = resp.forms[0].submit('submit')
-    assert 'This name is already used' in resp.text
-
-
-def test_categories_reorder(pub):
-    Category.wipe()
-    category = Category(name='foo')
-    category.store()
-    category = Category(name='bar')
-    category.store()
-    category = Category(name='baz')
-    category.store()
-
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/categories/update_order?order=1;2;3;')
-    categories = Category.select()
-    Category.sort_by_position(categories)
-    assert [x.id for x in categories] == ['1', '2', '3']
-
-    resp = app.get('/backoffice/forms/categories/update_order?order=3;1;2;')
-    categories = Category.select()
-    Category.sort_by_position(categories)
-    assert [x.id for x in categories] == ['3', '1', '2']
-
-
 def test_settings(pub):
     create_superuser(pub)
     app = login(get_app(pub))
diff --git a/tests/admin_pages/test_category.py b/tests/admin_pages/test_category.py
new file mode 100644
index 00000000..144b3fa1
--- /dev/null
+++ b/tests/admin_pages/test_category.py
@@ -0,0 +1,223 @@
+# -*- coding: utf-8 -*-
+
+import pytest
+
+from wcs.qommon.http_request import HTTPRequest
+from wcs.categories import Category
+from wcs.formdef import FormDef
+
+from utilities import get_app, login, create_temporary_pub, clean_temporary_pub
+from .test_all import create_superuser
+
+
+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_categories(pub):
+    create_superuser(pub)
+    app = login(get_app(pub))
+    app.get('/backoffice/forms/categories/')
+
+
+def test_categories_legacy_urls(pub):
+    create_superuser(pub)
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/categories/')
+    assert resp.location.endswith('/backoffice/forms/categories/')
+    resp = app.get('/backoffice/categories/1')
+    assert resp.location.endswith('/backoffice/forms/categories/1')
+    resp = app.get('/backoffice/categories/1/')
+    assert resp.location.endswith('/backoffice/forms/categories/1/')
+
+
+def test_categories_new(pub):
+    create_superuser(pub)
+    Category.wipe()
+    app = login(get_app(pub))
+
+    # go to the page and cancel
+    resp = app.get('/backoffice/forms/categories/')
+    resp = resp.click('New Category')
+    resp = resp.forms[0].submit('cancel')
+    assert resp.location == 'http://example.net/backoffice/forms/categories/'
+
+    # go to the page and add a category
+    resp = app.get('/backoffice/forms/categories/')
+    resp = resp.click('New Category')
+    resp.forms[0]['name'] = 'a new category'
+    resp.forms[0]['description'] = 'description of the category'
+    resp = resp.forms[0].submit('submit')
+    assert resp.location == 'http://example.net/backoffice/forms/categories/'
+    resp = resp.follow()
+    assert 'a new category' in resp.text
+    resp = resp.click('a new category')
+    assert '<h2>a new category' in resp.text
+
+    assert Category.get(1).name == 'a new category'
+    assert Category.get(1).description == 'description of the category'
+
+
+def test_categories_edit(pub):
+    create_superuser(pub)
+    Category.wipe()
+    category = Category(name='foobar')
+    category.store()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/forms/categories/1/')
+    assert 'no form associated to this category' in resp.text
+
+    resp = resp.click(href='edit')
+    assert resp.forms[0]['name'].value == 'foobar'
+    resp.forms[0]['description'] = 'category description'
+    resp = resp.forms[0].submit('submit')
+    assert resp.location == 'http://example.net/backoffice/forms/categories/'
+    resp = resp.follow()
+    resp = resp.click('foobar')
+    assert '<h2>foobar' in resp.text
+
+    assert Category.get(1).description == 'category description'
+
+
+def test_categories_edit_duplicate_name(pub):
+    Category.wipe()
+    category = Category(name='foobar')
+    category.store()
+    category = Category(name='foobar2')
+    category.store()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/forms/categories/1/')
+
+    resp = resp.click(href='edit')
+    assert resp.forms[0]['name'].value == 'foobar'
+    resp.forms[0]['name'] = 'foobar2'
+    resp = resp.forms[0].submit('submit')
+    assert 'This name is already used' in resp.text
+
+    resp = resp.forms[0].submit('cancel')
+    assert resp.location == 'http://example.net/backoffice/forms/categories/'
+
+
+def test_categories_with_formdefs(pub):
+    Category.wipe()
+    category = Category(name='foobar')
+    category.store()
+
+    FormDef.wipe()
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/forms/categories/1/')
+    assert 'form bar' not in resp.text
+
+    formdef = FormDef()
+    formdef.name = 'form bar'
+    formdef.fields = []
+    formdef.category_id = category.id
+    formdef.store()
+
+    resp = app.get('/backoffice/forms/categories/1/')
+    assert 'form bar' in resp.text
+    assert 'no form associated to this category' not in resp.text
+
+
+def test_categories_delete(pub):
+    Category.wipe()
+    category = Category(name='foobar')
+    category.store()
+
+    FormDef.wipe()
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/forms/categories/1/')
+
+    resp = resp.click(href='delete')
+    resp = resp.forms[0].submit('cancel')
+    assert resp.location == 'http://example.net/backoffice/forms/categories/1/'
+    assert Category.count() == 1
+
+    resp = app.get('/backoffice/forms/categories/1/')
+    resp = resp.click(href='delete')
+    resp = resp.forms[0].submit()
+    assert resp.location == 'http://example.net/backoffice/forms/categories/'
+    resp = resp.follow()
+    assert Category.count() == 0
+
+
+def test_categories_edit_description(pub):
+    Category.wipe()
+    category = Category(name='foobar')
+    category.description = 'category description'
+    category.store()
+
+    app = login(get_app(pub))
+    # this URL is used for editing from the frontoffice, there's no link
+    # pointing to it in the admin.
+    resp = app.get('/backoffice/forms/categories/1/description')
+    assert resp.forms[0]['description'].value == 'category description'
+    resp.forms[0]['description'] = 'updated description'
+
+    # check cancel doesn't save the change
+    resp2 = resp.forms[0].submit('cancel')
+    assert resp2.location == 'http://example.net/backoffice/forms/categories/1/'
+    assert Category.get(1).description == 'category description'
+
+    # check submit does it properly
+    resp2 = resp.forms[0].submit('submit')
+    assert resp2.location == 'http://example.net/backoffice/forms/categories/1/'
+    resp2 = resp2.follow()
+    assert Category.get(1).description == 'updated description'
+
+
+def test_categories_new_duplicate_name(pub):
+    Category.wipe()
+    category = Category(name='foobar')
+    category.store()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/forms/categories/')
+    resp = resp.click('New Category')
+    resp.forms[0]['name'] = 'foobar'
+    resp = resp.forms[0].submit('submit')
+    assert 'This name is already used' in resp.text
+
+
+def test_categories_reorder(pub):
+    Category.wipe()
+    category = Category(name='foo')
+    category.store()
+    category = Category(name='bar')
+    category.store()
+    category = Category(name='baz')
+    category.store()
+
+    app = login(get_app(pub))
+    app.get('/backoffice/forms/categories/update_order?order=1;2;3;')
+    categories = Category.select()
+    Category.sort_by_position(categories)
+    assert [x.id for x in categories] == ['1', '2', '3']
+
+    app.get('/backoffice/forms/categories/update_order?order=3;1;2;')
+    categories = Category.select()
+    Category.sort_by_position(categories)
+    assert [x.id for x in categories] == ['3', '1', '2']
-- 
2.28.0

