dataset/seed/reverse-proxy-client/tests/revprox_client.py

107 lines
3.9 KiB
Python
Raw Normal View History

2022-07-16 22:16:24 +02:00
from requests import get, post, session
2023-01-17 21:43:32 +01:00
from requests.exceptions import SSLError
2022-07-16 22:16:24 +02:00
from mookdns import MookDns
2023-01-17 21:43:32 +01:00
from os import environ
from os.path import join
from yaml import load, SafeLoader
from glob import glob
VERIFY = True
2022-07-16 22:16:24 +02:00
class Authentication:
def __init__(self,
auth_url,
portal_server,
ip,
username,
password,
title,
):
self.ip = ip
with session() as req:
with MookDns(self.ip):
self.is_lemonldap(req,
auth_url,
)
self.auth_lemonldap(req,
portal_server,
username,
password,
title,
)
self.cookies = dict(req.cookies)
# @staticmethod
def is_lemonldap(self,
req,
url,
):
2023-01-17 21:43:32 +01:00
global VERIFY
try:
ret = req.get(url, verify=VERIFY)
except SSLError:
2023-07-31 15:30:32 +02:00
conf_file = f'{environ["MACHINE_TEST_DIR"]}/reverse-proxy-client.yml'
2023-01-17 21:43:32 +01:00
with open(conf_file) as yaml:
data = load(yaml, Loader=SafeLoader)
path = join(environ["MACHINE_TEST_DIR"], data["ca_certificate"])
cert = glob(path)
if len(cert) != 1:
raise Exception(f'{path} should find one and one certificate but found: {cert}')
VERIFY=cert[0]
ret = req.get(url, verify=VERIFY)
2022-07-16 22:16:24 +02:00
code = ret.status_code
content = ret.content
2022-08-19 20:30:13 +02:00
assert code == 200, f"cannot access to lemonldap; {content}"
2022-07-18 17:06:12 +02:00
assert b'<title trspan="authPortal">Authentication portal</title>' in content, f'cannot find LemonLdap title: {content}'
2022-07-16 22:16:24 +02:00
def auth_lemonldap(self,
req,
portal_server,
username,
password,
title,
):
# authentification
json = {'user': username,
'password': password,
}
headers = {"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
}
portal_url = f'https://{portal_server}/oauth2/'
2023-01-17 21:43:32 +01:00
ret = req.post(portal_url, data=json, headers=headers, verify=VERIFY)
2022-07-16 22:16:24 +02:00
json = ret.json()
assert json['error']
assert json['result'] == 1
assert json['id'] == ret.cookies.get('lemonldap')
# authorization code
# curl -X POST -d user=dwho -d password=dwho -H 'Accept: application/json' 'https://oidctest.wsweet.org/oauth2/'
# curl -s -D - -o /dev/null -b lemonldap=0640f95827111f00ba7ad5863ba819fe46cfbcecdb18ce525836369fb4c8350b 'https://oidctest.wsweet.org/oauth2/authorize?response_type=code&client_id=private&scope=openid+profile+email&redirect_uri=http://localhost' | grep '^location'
authorize_url = f'{portal_url}authorize'
2023-01-17 21:43:32 +01:00
ret = req.get(authorize_url, verify=VERIFY)
2022-07-16 22:16:24 +02:00
assert ret.status_code == 200
2022-08-21 18:59:02 +02:00
content = ret.content.decode()
assert title in content, f'cannot find {title} in {content}'
2022-07-16 22:16:24 +02:00
def get(self,
url,
json=False,
):
with MookDns(self.ip):
2023-01-17 21:43:32 +01:00
ret = get(url, cookies=self.cookies, verify=VERIFY)
2022-07-16 22:16:24 +02:00
assert ret.status_code == 200, f'return code is {ret.status_code}'
if json:
return ret.json()
return ret.content.decode()
def post(self,
url,
data,
2022-08-21 18:59:02 +02:00
headers=None,
2022-07-16 22:16:24 +02:00
):
with MookDns(self.ip):
2023-01-17 21:43:32 +01:00
ret = post(url, cookies=self.cookies, data=data, headers=headers, verify=VERIFY)
2022-07-16 22:16:24 +02:00
assert ret.status_code == 200, f'return code is {ret.status_code}'