Skip to content
Snippets Groups Projects
Commit a77e588f authored by kaiyou's avatar kaiyou
Browse files

Slighlty refactor tests

parent b8a4c4a6
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,6 @@ import time
import requests
# Generic usefule fixtures
@pytest.fixture
def temp():
"""Write data to a temporary file and return the handle
......@@ -27,7 +26,6 @@ def temp():
del handles # Useless but explicit
# Test application instance
@pytest.fixture(scope="module")
def app(username, password):
"""Run an isolated application instance and return the URL"""
......@@ -72,82 +70,3 @@ def service_name():
"""A randomly generated service name"""
return "Test service " + random.randbytes(3).hex()
# Test webserver
@pytest.fixture
def httpd(temp):
"""Test httpd server"""
proc = []
def start(config):
proc.append(subprocess.Popen(["httpd", "-DFOREGROUND", "-f", temp(config)]))
time.sleep(1) # Sleep so apache can start
yield start
for pid in proc:
pid.terminate()
pid.wait()
@pytest.fixture
def httpd_minimal():
"""Minimal httpd config"""
rootDirectory = tempfile.TemporaryDirectory()
with open(os.path.join(rootDirectory.name, "index.html"), "w") as handle:
handle.write("Hello world!")
yield f"""
ServerName localhost
ServerRoot /usr/lib/httpd
PidFile /tmp/apache.pid
LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule env_module modules/mod_env.so
LoadModule dir_module modules/mod_dir.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule authz_user_module modules/mod_authz_user.so
LogLevel debug
ErrorLog /dev/stderr
TransferLog /dev/stdout
Listen 127.0.0.1:8123
DocumentRoot {rootDirectory.name}
DirectoryIndex index.html
"""
del rootDirectory
@pytest.fixture
def httpd_saml(httpd_minimal):
return (httpd_minimal + """
LoadModule auth_mellon_module modules/mod_auth_mellon.so
<Location />
Require valid-user
AuthType "Mellon"
MellonEnable "auth"
MellonEndpointPath "/mellon"
MellonSPPrivateKeyFile {key}
MellonSPCertFile {cert}
MellonIdPMetadataFile {metadata}
SetEnv MELLON_DISABLE_SAMESITE 1
</Location>
""")
@pytest.fixture
def httpd_oidc(httpd_minimal):
return (httpd_minimal + """
LoadModule auth_openidc_module modules/mod_auth_openidc.so
OIDCProviderMetadataURL {metadata}
OIDCClientID {client_id}
OIDCClientSecret {client_secret}
OIDCRedirectURI http://localhost:8123/redirect_uri
OIDCCryptoPassphrase changeme
OIDCScope "openid email profile"
<Location />
Require valid-user
AuthType "openid-connect"
</Location>
""")
from playwright import sync_api as pw
import requests
import pytest
import subprocess
import tempfile
import os
import time
HTTPD_CONFIG = """
ServerName localhost
ServerRoot /usr/lib/httpd
PidFile /tmp/apache.pid
LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule env_module modules/mod_env.so
LoadModule dir_module modules/mod_dir.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule authz_user_module modules/mod_authz_user.so
LogLevel debug
ErrorLog /dev/stderr
TransferLog /dev/stdout
Listen 127.0.0.1:8123
DocumentRoot {root}
DirectoryIndex index.html
"""
SAML_CONFIG = """"
LoadModule auth_mellon_module modules/mod_auth_mellon.so
<Location />
Require valid-user
AuthType "Mellon"
MellonEnable "auth"
MellonEndpointPath "/mellon"
MellonSPPrivateKeyFile {key}
MellonSPCertFile {cert}
MellonIdPMetadataFile {metadata}
SetEnv MELLON_DISABLE_SAMESITE 1
</Location>
"""
OIDC_CONFIG = """"
LoadModule auth_openidc_module modules/mod_auth_openidc.so
OIDCProviderMetadataURL {metadata}
OIDCClientID {client_id}
OIDCClientSecret {client_secret}
OIDCRedirectURI http://localhost:8123/redirect_uri
OIDCCryptoPassphrase changeme
OIDCScope "openid email profile"
<Location />
Require valid-user
AuthType "openid-connect"
</Location>
"""
@pytest.fixture
def httpd(temp):
"""Test httpd server
Multiple servers may run and will all be closed at the end, they all share
a root directory"""
proc = []
rootDirectory = tempfile.TemporaryDirectory()
with open(os.path.join(rootDirectory.name, "index.html"), "w") as handle:
handle.write("Hello world!")
def start(config):
config_file = temp(HTTPD_CONFIG.format(root=rootDirectory.name) + config)
proc.append(subprocess.Popen(["httpd", "-DFOREGROUND", "-f", config_file]))
time.sleep(1) # Sleep so apache can start
yield start
for pid in proc:
pid.terminate()
pid.wait()
def do_login(page, username, password):
......@@ -18,7 +97,7 @@ def test_login(app, context, username, password):
pw.expect(page.get_by_text("Sign out")).to_be_visible()
def test_saml_auth(app, context, username, password, service_name, httpd, httpd_saml, temp):
def test_saml_auth(app, context, username, password, service_name, httpd, temp):
# First create a SAML service
page = context.new_page()
page.goto(app + "/service/create/saml")
......@@ -33,7 +112,7 @@ def test_saml_auth(app, context, username, password, service_name, httpd, httpd_
page.get_by_role("button", name="submit").click()
# Then access the service and extract useful data
page.get_by_text(service_name).click()
httpd(httpd_saml.format(
httpd(SAML_CONFIG.format(
metadata=temp(requests.get(page.get_by_label("SAML Metadata").text_content()).content),
key=temp(page.get_by_label("SP private key").text_content()),
cert=temp(page.get_by_label("SP certificate").text_content())
......@@ -44,7 +123,7 @@ def test_saml_auth(app, context, username, password, service_name, httpd, httpd_
pw.expect(page.get_by_text("Hello world")).to_be_visible()
def test_oidc_auth(app, context, username, password, service_name, httpd, httpd_oidc, temp):
def test_oidc_auth(app, context, username, password, service_name, httpd):
# First create an OIDC service
page = context.new_page()
page.goto(app + "/service/create/oidc")
......@@ -60,7 +139,7 @@ def test_oidc_auth(app, context, username, password, service_name, httpd, httpd_
page.get_by_role("button", name="submit").click()
# Then access the service and extract useful data
page.get_by_text(service_name).click()
httpd(httpd_oidc.format(
httpd(OIDC_CONFIG.format(
metadata=page.get_by_label("OIDC discovery endpoint").text_content(),
client_id=page.get_by_label("Client ID").text_content(),
client_secret=page.get_by_label("Client secret").text_content()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment