From 3daac3ef3e63e271d9e755320e8aa39f1e5ba8c6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Mon, 11 Jul 2022 09:34:05 +0200
Subject: [PATCH 6/7] tests: update root directory tests to use fixtures

---
 tests/test_rootdirectory.py | 96 +++++++++++++++++--------------------
 1 file changed, 44 insertions(+), 52 deletions(-)

diff --git a/tests/test_rootdirectory.py b/tests/test_rootdirectory.py
index 601374ec7..5344a2fc3 100644
--- a/tests/test_rootdirectory.py
+++ b/tests/test_rootdirectory.py
@@ -1,7 +1,5 @@
-import shutil
-
 import pytest
-from quixote import cleanup
+from quixote import get_request
 
 import wcs.forms.root
 from wcs.categories import Category
@@ -9,16 +7,11 @@ from wcs.formdef import FormDef
 from wcs.qommon import sessions
 from wcs.qommon.http_request import HTTPRequest
 
-from .utilities import create_temporary_pub, get_app
-
-
-def setup_module(module):
-    cleanup()
+from .utilities import clean_temporary_pub, create_temporary_pub, get_app
 
-    global pub, req
-    global user1, user2
-    global category
 
+@pytest.fixture
+def pub():
     pub = create_temporary_pub()
 
     req = HTTPRequest(None, {'SCRIPT_NAME': '/'})
@@ -26,59 +19,74 @@ def setup_module(module):
     pub._set_request(req)
     req.session = sessions.Session(id=1)
 
+    FormDef.wipe()
+    Category.wipe()
+    pub.user_class.wipe()
+
+    yield pub
+    clean_temporary_pub()
+
+
+@pytest.fixture
+def user1(pub):
     user1 = pub.user_class(name='user-one-role')
     user1.id = 'user-one-role'
     user1.roles = ['role-1']
+    return user1
+
 
+@pytest.fixture
+def user2(pub):
     user2 = pub.user_class(name='user-other-role')
     user2.id = 'user-other-role'
     user2.roles = ['role-2']
+    return user2
+
 
+@pytest.fixture
+def category(pub):
     category = Category()
     category.name = 'category1'
     category.store()
+    return category
 
 
-def create_formdef():
-    global formdef1, formdef2
-
+@pytest.fixture
+def formdef1(pub, category):
     formdef1 = FormDef()
     formdef1.category_id = category.id
     formdef1.name = formdef1.url_name = 'test-formdef-1'
     formdef1.store()
+    return formdef1
+
 
+@pytest.fixture
+def formdef2(pub, category):
     formdef2 = FormDef()
     formdef2.category_id = category.id
     formdef2.name = formdef2.url_name = 'test-formdef-2'
     formdef2.store()
-
-
-def teardown_module(module):
-    shutil.rmtree(pub.APP_DIR)
+    return formdef2
 
 
 def indexhtml(user=None):
+    req = get_request()
     req._user = user
     req.session.user = user.id if user else None
     return str(wcs.forms.root.RootDirectory()._q_index())
 
 
-def test_empty_site():
-    FormDef.wipe()
+def test_empty_site(pub):
     assert indexhtml() == ''
 
 
-def test_public_site_anonymous_access():
-    FormDef.wipe()
-    create_formdef()
+def test_public_site_anonymous_access(pub, formdef1, formdef2):
     output = indexhtml()
     assert 'href="category1/test-formdef-1/"' in output
     assert 'href="category1/test-formdef-2/"' in output
 
 
-def test_private_site_anonymous_access():
-    FormDef.wipe()
-    create_formdef()
+def test_private_site_anonymous_access(pub, formdef1, formdef2):
     formdef1.roles = formdef2.roles = ['role-1']
     formdef1.store()
     formdef2.store()
@@ -86,9 +94,7 @@ def test_private_site_anonymous_access():
         indexhtml()
 
 
-def test_semi_private_site_anonymous_access():
-    FormDef.wipe()
-    create_formdef()
+def test_semi_private_site_anonymous_access(pub, formdef1, formdef2):
     formdef1.roles = ['role-1']
     formdef1.store()
     output = indexhtml()
@@ -96,9 +102,7 @@ def test_semi_private_site_anonymous_access():
     assert 'href="category1/test-formdef-2/"' in output
 
 
-def test_private_site_authorized_access():
-    FormDef.wipe()
-    create_formdef()
+def test_private_site_authorized_access(pub, formdef1, formdef2, user1):
     formdef1.roles = formdef2.roles = ['role-1']
     formdef1.store()
     formdef2.store()
@@ -107,9 +111,7 @@ def test_private_site_authorized_access():
     assert 'href="category1/test-formdef-2/"' in output
 
 
-def test_private_site_unauthorized_access():
-    FormDef.wipe()
-    create_formdef()
+def test_private_site_unauthorized_access(pub, formdef1, formdef2, user2):
     formdef1.roles = formdef2.roles = ['role-1']
     formdef1.store()
     formdef2.store()
@@ -117,9 +119,7 @@ def test_private_site_unauthorized_access():
         indexhtml(user2)
 
 
-def test_private_site_semi_authorized_access():
-    FormDef.wipe()
-    create_formdef()
+def test_private_site_semi_authorized_access(pub, formdef1, formdef2, user1):
     formdef1.roles = ['role-1']
     formdef2.roles = ['role-2']
     formdef1.store()
@@ -129,9 +129,7 @@ def test_private_site_semi_authorized_access():
     assert 'href="category1/test-formdef-2/"' not in output
 
 
-def test_advertized_site_anonymous_access():
-    FormDef.wipe()
-    create_formdef()
+def test_advertized_site_anonymous_access(pub, formdef1, formdef2):
     formdef1.roles = formdef2.roles = ['role-1']
     formdef1.always_advertise = True
     formdef1.store()
@@ -142,9 +140,7 @@ def test_advertized_site_anonymous_access():
     assert 'authentication required' in output  # locales ?
 
 
-def test_advertized_site_user_access():
-    FormDef.wipe()
-    create_formdef()
+def test_advertized_site_user_access(pub, formdef1, formdef2, user1):
     formdef1.roles = formdef2.roles = ['role-2']
     formdef1.always_advertise = True
     formdef1.store()
@@ -155,9 +151,7 @@ def test_advertized_site_user_access():
     assert 'authentication required' in output  # locales ?
 
 
-def test_categories_page():
-    FormDef.wipe()
-    create_formdef()
+def test_categories_page(pub, category, formdef1):
     resp = get_app(pub).get('/categories')
     assert 'href="category1/"' in resp
     FormDef.wipe()
@@ -165,7 +159,7 @@ def test_categories_page():
     assert 'href="category1/"' not in resp
 
 
-def test_static_directories():
+def test_static_directories(pub):
     assert get_app(pub).get('/static/images/feed-icon-10x10.png')
     assert get_app(pub).get('/static/css/gadjo.css')
     assert get_app(pub).get('/static/xstatic/jquery.js')
@@ -175,9 +169,7 @@ def test_static_directories():
     assert get_app(pub).get('/static/xxx', status=404)
 
 
-def test_jquery_debug_mode():
-    FormDef.wipe()
-    create_formdef()
+def test_jquery_debug_mode(pub, formdef1):
     resp = get_app(pub).get('/category1/test-formdef-1/')
     assert 'jquery.min.js' in resp.text
     pub.cfg['debug'] = {'debug_mode': True}
@@ -186,5 +178,5 @@ def test_jquery_debug_mode():
     assert 'jquery.js' in resp.text
 
 
-def test_i18n_js():
+def test_i18n_js(pub):
     get_app(pub).get('/i18n.js')
-- 
2.36.1

