From c4d1fa50eaef04284711170ce906bd54c251cc6d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 10 Sep 2021 09:52:56 +0200
Subject: [PATCH 1/2] misc: split auth tests

---
 tests/form_pages/test_all.py  | 41 ---------------------
 tests/form_pages/test_auth.py | 67 +++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 41 deletions(-)
 create mode 100644 tests/form_pages/test_auth.py

diff --git a/tests/form_pages/test_all.py b/tests/form_pages/test_all.py
index 51f4fbf7..b9a2d72f 100644
--- a/tests/form_pages/test_all.py
+++ b/tests/form_pages/test_all.py
@@ -1597,47 +1597,6 @@ def test_form_visit_existing(pub):
     assert 'The form has been recorded on' in resp
 
 
-def test_form_auth(pub):
-    create_user(pub)
-    formdef = create_formdef()
-    formdef.data_class().wipe()
-    resp = get_app(pub).get('/test/auth')
-    assert resp.location == 'http://example.net/login/?ReturnUrl=http%3A//example.net/test/'
-
-    resp = login(get_app(pub), username='foo', password='foo').get('/test/auth')
-    assert resp.location == 'http://example.net/test/'
-
-
-def test_form_tryauth(pub):
-    create_user(pub)
-    formdef = create_formdef()
-    formdef.data_class().wipe()
-    resp = get_app(pub).get('/test/tryauth')
-    assert resp.location == 'http://example.net/test/'
-
-    app = login(get_app(pub), username='foo', password='foo')
-    pub.cfg['identification'] = {'methods': ['idp']}
-    pub.write_cfg()
-    # if the user is logged in, the form should be presented
-    resp = app.get('/test/tryauth')
-    assert resp.location == 'http://example.net/test/'
-
-    # if the user is unlogged, there should be a passive redirection to SSO
-    resp = get_app(pub).get('/test/tryauth')
-    assert 'IsPassive=true' in resp.location
-
-    pub.cfg['identification'] = {'methods': ['password']}
-    pub.write_cfg()
-
-
-def test_form_forceauth(pub):
-    create_user(pub)
-    formdef = create_formdef()
-    formdef.data_class().wipe()
-    resp = get_app(pub).get('/test/forceauth')
-    assert resp.location == 'http://example.net/login/?ReturnUrl=http%3A//example.net/test/&forceAuthn=true'
-
-
 def test_form_no_tracking_code(pub):
     formdef = create_formdef()
     formdef.data_class().wipe()
diff --git a/tests/form_pages/test_auth.py b/tests/form_pages/test_auth.py
new file mode 100644
index 00000000..d5dc0c17
--- /dev/null
+++ b/tests/form_pages/test_auth.py
@@ -0,0 +1,67 @@
+import pytest
+
+from wcs.formdef import FormDef
+
+from ..utilities import clean_temporary_pub, create_temporary_pub, get_app, login
+from .test_all import create_user
+
+
+@pytest.fixture
+def pub(request):
+    pub = create_temporary_pub()
+    pub.cfg['identification'] = {'methods': ['password']}
+    pub.cfg['language'] = {'language': 'en'}
+    pub.write_cfg()
+    return pub
+
+
+@pytest.fixture
+def formdef(pub):
+    FormDef.wipe()
+    formdef = FormDef()
+    formdef.name = 'test'
+    formdef.fields = []
+    formdef.store()
+    return formdef
+
+
+def teardown_module(module):
+    clean_temporary_pub()
+
+
+def test_form_auth(pub, formdef):
+    create_user(pub)
+
+    resp = get_app(pub).get('/test/auth')
+    assert resp.location == 'http://example.net/login/?ReturnUrl=http%3A//example.net/test/'
+
+    resp = login(get_app(pub), username='foo', password='foo').get('/test/auth')
+    assert resp.location == 'http://example.net/test/'
+
+
+def test_form_tryauth(pub, formdef):
+    create_user(pub)
+
+    resp = get_app(pub).get('/test/tryauth')
+    assert resp.location == 'http://example.net/test/'
+
+    app = login(get_app(pub), username='foo', password='foo')
+    pub.cfg['identification'] = {'methods': ['idp']}
+    pub.write_cfg()
+    # if the user is logged in, the form should be presented
+    resp = app.get('/test/tryauth')
+    assert resp.location == 'http://example.net/test/'
+
+    # if the user is unlogged, there should be a passive redirection to SSO
+    resp = get_app(pub).get('/test/tryauth')
+    assert 'IsPassive=true' in resp.location
+
+    pub.cfg['identification'] = {'methods': ['password']}
+    pub.write_cfg()
+
+
+def test_form_forceauth(pub, formdef):
+    create_user(pub)
+
+    resp = get_app(pub).get('/test/forceauth')
+    assert resp.location == 'http://example.net/login/?ReturnUrl=http%3A//example.net/test/&forceAuthn=true'
-- 
2.30.2

