From 76208ef6d2f34e0cf74a2ca01f02c9ed92e1ec6a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 3 Sep 2020 11:17:00 +0200
Subject: [PATCH 1/3] misc: split test module and simplify test data

---
 tests/test_admin_pages.py         |  92 ---------------------
 tests/test_wscalls_admin_pages.py | 129 ++++++++++++++++++++++++++++++
 2 files changed, 129 insertions(+), 92 deletions(-)
 create mode 100644 tests/test_wscalls_admin_pages.py

diff --git a/tests/test_admin_pages.py b/tests/test_admin_pages.py
index 43d41efe..43f37384 100644
--- a/tests/test_admin_pages.py
+++ b/tests/test_admin_pages.py
@@ -3047,98 +3047,6 @@ def test_settings_geolocation(pub):
     assert pub.cfg['misc']['default-position'] == '1.234;-1.234'
 
 
-def test_wscalls_new(pub):
-    create_superuser(pub)
-    NamedWsCall.wipe()
-    app = login(get_app(pub))
-
-    # go to the page and cancel
-    resp = app.get('/backoffice/settings/wscalls/')
-    resp = resp.click('New webservice call')
-    resp = resp.forms[0].submit('cancel')
-    assert resp.location == 'http://example.net/backoffice/settings/wscalls/'
-
-    # go to the page and add a webservice call
-    resp = app.get('/backoffice/settings/wscalls/')
-    resp = resp.click('New webservice call')
-    resp.form['name'] = 'a new webservice call'
-    resp.form['description'] = 'description'
-    resp.form['request$url'] = 'http://remote.example.net/json'
-    resp = resp.form.submit('submit')
-    assert resp.location == 'http://example.net/backoffice/settings/wscalls/'
-    resp = resp.follow()
-    assert 'a new webservice call' in resp.text
-    resp = resp.click('a new webservice call')
-    assert 'Webservice Call - a new webservice call' in resp.text
-    resp = resp.click('Edit')
-    assert 'Edit webservice call' in resp.text
-
-    assert NamedWsCall.get('a_new_webservice_call').name == 'a new webservice call'
-
-
-def test_wscalls_view(pub):
-    create_superuser(pub)
-    NamedWsCall.wipe()
-
-    wscall = NamedWsCall(name='xxx')
-    wscall.description = 'description'
-    wscall.request = {
-            'url': 'http://remote.example.net/json',
-            'request_signature_key': 'xxx',
-            'qs_data': {'a': 'b'},
-            'method': 'POST',
-            'post_data': {'c': 'd'},
-    }
-    wscall.store()
-
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/settings/wscalls/%s/' % wscall.id)
-    assert 'http://remote.example.net/json' in resp.text
-
-
-def test_wscalls_edit(pub):
-    test_wscalls_view(pub)
-
-    app = login(get_app(pub))
-
-    resp = app.get('/backoffice/settings/wscalls/xxx/')
-    resp = resp.click(href='edit')
-    assert resp.form['name'].value == 'xxx'
-    assert 'slug' in resp.form.fields
-    resp.form['description'] = 'bla bla bla'
-    resp = resp.form.submit('submit')
-    assert resp.location == 'http://example.net/backoffice/settings/wscalls/xxx/'
-    resp = resp.follow()
-
-    assert NamedWsCall.get('xxx').description == 'bla bla bla'
-
-    resp = app.get('/backoffice/settings/wscalls/xxx/')
-    resp = resp.click(href='edit')
-    assert resp.form['name'].value == 'xxx'
-    assert 'slug' in resp.form.fields
-    resp.form['slug'] = 'yyy'
-    resp = resp.form.submit('submit')
-    assert resp.location == 'http://example.net/backoffice/settings/wscalls/yyy/'
-
-
-def test_wscalls_delete(pub):
-    test_wscalls_view(pub)
-    assert NamedWsCall.count() == 1
-
-    app = login(get_app(pub))
-
-    resp = app.get('/backoffice/settings/wscalls/xxx/')
-    resp = resp.click(href='delete')
-    resp = resp.form.submit('cancel')
-    assert resp.location == 'http://example.net/backoffice/settings/wscalls/'
-
-    resp = app.get('/backoffice/settings/wscalls/xxx/')
-    resp = resp.click(href='delete')
-    resp = resp.form.submit('submit')
-    assert resp.location == 'http://example.net/backoffice/settings/wscalls/'
-    assert NamedWsCall.count() == 0
-
-
 def test_settings_permissions(pub):
     create_superuser(pub)
     role1 = create_role()
diff --git a/tests/test_wscalls_admin_pages.py b/tests/test_wscalls_admin_pages.py
new file mode 100644
index 00000000..a389ff54
--- /dev/null
+++ b/tests/test_wscalls_admin_pages.py
@@ -0,0 +1,129 @@
+# -*- coding: utf-8 -*-
+
+import pytest
+
+from wcs.qommon.http_request import HTTPRequest
+from wcs.wscalls import NamedWsCall
+
+from utilities import get_app, login, create_temporary_pub, clean_temporary_pub
+from test_admin_pages 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()
+
+
+@pytest.fixture
+def wscall():
+    NamedWsCall.wipe()
+    wscall = NamedWsCall(name='xxx')
+    wscall.description = 'description'
+    wscall.request = {
+            'url': 'http://remote.example.net/json',
+            'request_signature_key': 'xxx',
+            'qs_data': {'a': 'b'},
+            'method': 'POST',
+            'post_data': {'c': 'd'},
+    }
+    wscall.store()
+    return wscall
+
+
+def test_wscalls_new(pub):
+    create_superuser(pub)
+    NamedWsCall.wipe()
+    app = login(get_app(pub))
+
+    # go to the page and cancel
+    resp = app.get('/backoffice/settings/wscalls/')
+    resp = resp.click('New webservice call')
+    resp = resp.forms[0].submit('cancel')
+    assert resp.location == 'http://example.net/backoffice/settings/wscalls/'
+
+    # go to the page and add a webservice call
+    resp = app.get('/backoffice/settings/wscalls/')
+    resp = resp.click('New webservice call')
+    resp.form['name'] = 'a new webservice call'
+    resp.form['description'] = 'description'
+    resp.form['request$url'] = 'http://remote.example.net/json'
+    resp = resp.form.submit('submit')
+    assert resp.location == 'http://example.net/backoffice/settings/wscalls/'
+    resp = resp.follow()
+    assert 'a new webservice call' in resp.text
+    resp = resp.click('a new webservice call')
+    assert 'Webservice Call - a new webservice call' in resp.text
+    resp = resp.click('Edit')
+    assert 'Edit webservice call' in resp.text
+
+    assert NamedWsCall.get('a_new_webservice_call').name == 'a new webservice call'
+
+
+def test_wscalls_view(pub, wscall):
+    create_superuser(pub)
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/settings/wscalls/%s/' % wscall.id)
+    assert 'http://remote.example.net/json' in resp.text
+
+
+def test_wscalls_edit(pub, wscall):
+    create_superuser(pub)
+
+    app = login(get_app(pub))
+
+    resp = app.get('/backoffice/settings/wscalls/xxx/')
+    resp = resp.click(href='edit')
+    assert resp.form['name'].value == 'xxx'
+    assert 'slug' in resp.form.fields
+    resp.form['description'] = 'bla bla bla'
+    resp = resp.form.submit('submit')
+    assert resp.location == 'http://example.net/backoffice/settings/wscalls/xxx/'
+    resp = resp.follow()
+
+    assert NamedWsCall.get('xxx').description == 'bla bla bla'
+
+    resp = app.get('/backoffice/settings/wscalls/xxx/')
+    resp = resp.click(href='edit')
+    assert resp.form['name'].value == 'xxx'
+    assert 'slug' in resp.form.fields
+    resp.form['slug'] = 'yyy'
+    resp = resp.form.submit('submit')
+    assert resp.location == 'http://example.net/backoffice/settings/wscalls/yyy/'
+
+
+def test_wscalls_delete(pub, wscall):
+    create_superuser(pub)
+
+    app = login(get_app(pub))
+
+    resp = app.get('/backoffice/settings/wscalls/xxx/')
+    resp = resp.click(href='delete')
+    resp = resp.form.submit('cancel')
+    assert resp.location == 'http://example.net/backoffice/settings/wscalls/'
+
+    resp = app.get('/backoffice/settings/wscalls/xxx/')
+    resp = resp.click(href='delete')
+    resp = resp.form.submit('submit')
+    assert resp.location == 'http://example.net/backoffice/settings/wscalls/'
+    assert NamedWsCall.count() == 0
-- 
2.28.0

