feat: implement Fil-A-Management Django MVP
This commit is contained in:
commit
1127e7ecf4
66 files changed
+3774
No files matched your search
@@ -0,0 +1,143 @@
|
||||
import http.cookiejar
|
||||
import json
|
||||
import re
|
||||
import secrets
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
from .support import ROOT, clean_environment
|
||||
|
||||
|
||||
def free_port():
|
||||
with socket.socket() as sock:
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
return sock.getsockname()[1]
|
||||
|
||||
|
||||
def prepare_database(directory):
|
||||
directory = Path(directory)
|
||||
directory.mkdir(parents=True, exist_ok=True)
|
||||
password = secrets.token_urlsafe(24)
|
||||
env = clean_environment() | {
|
||||
"FAM_DATA_DIR": str(directory / "data"),
|
||||
"DJANGO_SECRET_KEY": secrets.token_urlsafe(64),
|
||||
"DJANGO_COOKIE_SECURE": "0",
|
||||
"DJANGO_ALLOWED_HOSTS": "127.0.0.1,localhost",
|
||||
"FAM_TEST_PASSWORD": password,
|
||||
}
|
||||
|
||||
def manage(*args):
|
||||
result = subprocess.run(
|
||||
[sys.executable, "manage.py", *args],
|
||||
cwd=ROOT,
|
||||
env=env,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=60,
|
||||
)
|
||||
if result.returncode:
|
||||
raise RuntimeError(result.stdout + result.stderr)
|
||||
return result.stdout
|
||||
|
||||
manage("migrate", "--noinput")
|
||||
manage("seed_catalog")
|
||||
manage("collectstatic", "--noinput", "--verbosity", "0")
|
||||
manage(
|
||||
"shell",
|
||||
"-c",
|
||||
"import os; from django.contrib.auth import get_user_model; U=get_user_model(); U.objects.create_user('synthetic-one',password=os.environ['FAM_TEST_PASSWORD']); U.objects.create_user('synthetic-two',password=os.environ['FAM_TEST_PASSWORD'])",
|
||||
)
|
||||
return env, password, manage
|
||||
|
||||
|
||||
class LiveServer:
|
||||
def __init__(self, env, directory):
|
||||
self.env = env
|
||||
self.port = free_port()
|
||||
self.url = f"http://127.0.0.1:{self.port}"
|
||||
self.log = Path(directory) / "http-server.log"
|
||||
self.process = None
|
||||
self.handle = None
|
||||
|
||||
def start(self):
|
||||
self.handle = self.log.open("a")
|
||||
executable = str(Path(sys.executable).with_name("gunicorn"))
|
||||
self.process = subprocess.Popen(
|
||||
[
|
||||
executable,
|
||||
"config.wsgi:application",
|
||||
"--bind",
|
||||
f"127.0.0.1:{self.port}",
|
||||
"--workers",
|
||||
"1",
|
||||
"--worker-class",
|
||||
"gthread",
|
||||
"--threads",
|
||||
"4",
|
||||
"--no-control-socket",
|
||||
"--forwarded-allow-ips=",
|
||||
],
|
||||
cwd=ROOT,
|
||||
env=self.env,
|
||||
stdout=self.handle,
|
||||
stderr=subprocess.STDOUT,
|
||||
)
|
||||
for _ in range(100):
|
||||
try:
|
||||
with urllib.request.urlopen(self.url + "/healthz/", timeout=1) as response:
|
||||
if json.load(response) == {"status": "ok"}:
|
||||
return
|
||||
except (OSError, ValueError):
|
||||
if self.process.poll() is not None:
|
||||
break
|
||||
time.sleep(0.1)
|
||||
self.stop()
|
||||
raise RuntimeError(self.log.read_text())
|
||||
|
||||
def stop(self):
|
||||
if self.process and self.process.poll() is None:
|
||||
self.process.terminate()
|
||||
try:
|
||||
self.process.wait(timeout=15)
|
||||
except subprocess.TimeoutExpired:
|
||||
self.process.kill()
|
||||
self.process.wait(timeout=5)
|
||||
if self.handle:
|
||||
self.handle.close()
|
||||
|
||||
|
||||
class WebSession:
|
||||
def __init__(self, base):
|
||||
self.base = base
|
||||
self.cookies = http.cookiejar.CookieJar()
|
||||
self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cookies))
|
||||
|
||||
def request(self, path, data=None):
|
||||
body = urllib.parse.urlencode(data).encode() if data is not None else None
|
||||
try:
|
||||
response = self.opener.open(self.base + path, body, timeout=10)
|
||||
except urllib.error.HTTPError as error:
|
||||
response = error
|
||||
with response:
|
||||
return response.status, response.read().decode(), response.headers, response.url
|
||||
|
||||
def get(self, path):
|
||||
return self.request(path)
|
||||
|
||||
def post(self, path, data):
|
||||
status, html, _, _ = self.get(path)
|
||||
if status != 200:
|
||||
raise AssertionError(f"GET {path}: {status}")
|
||||
token = re.search(r'name="csrfmiddlewaretoken" value="([^"]+)"', html).group(1)
|
||||
return self.request(path, data | {"csrfmiddlewaretoken": token})
|
||||
|
||||
def login(self, username, password):
|
||||
status, _, _, url = self.post("/anmelden/", {"username": username, "password": password})
|
||||
if status != 200 or not url.endswith("/bestand/"):
|
||||
raise AssertionError("Synthetic HTTP login failed")
|
||||
Reference in new issue
Block a user