Initial Awawawa
This commit is contained in:
55
web/tests/helpers.py
Normal file
55
web/tests/helpers.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from patchbay_web import create_app
|
||||
|
||||
ED25519 = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINJ2M1DCOTYJkGHaPO2iMGqF7isZXmwqN5HxtLP5+xq1 test@host"
|
||||
|
||||
|
||||
class AppCase(unittest.TestCase):
|
||||
"""Fresh config + database per test; mails are captured instead of sent."""
|
||||
|
||||
def setUp(self):
|
||||
self.dir = tempfile.mkdtemp(prefix="pbtest.")
|
||||
self.conf_path = os.path.join(self.dir, "patchbay.conf")
|
||||
with open(self.conf_path, "w") as f:
|
||||
f.write("# test config\nRole = target\n"
|
||||
f"Database = {self.dir}/patchbay.db\nRunDir = {self.dir}\n"
|
||||
"SysopUser = admin\nSysopEmail = admin@example.org\n"
|
||||
"SysopPassword = correct horse battery\nMailHost = smtp.invalid\n")
|
||||
self.mails = []
|
||||
patcher = mock.patch("patchbay_web.mailer.send",
|
||||
side_effect=lambda conf, to, subj, body: self.mails.append((to, body)))
|
||||
patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
self.app = create_app(self.conf_path, testing=True)
|
||||
self.client = self.app.test_client()
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.dir)
|
||||
|
||||
def csrf(self):
|
||||
page = self.client.get("/login").get_data(as_text=True)
|
||||
m = re.search(r'name="csrf-token" content="([^"]+)"', page)
|
||||
return m.group(1)
|
||||
|
||||
def login(self, user="admin", password="correct horse battery"):
|
||||
tok = self.csrf()
|
||||
r = self.client.post("/login", data={"username": user, "password": password, "csrf_token": tok})
|
||||
if r.status_code != 302:
|
||||
return r
|
||||
code = re.search(r"code is: (\d{6})", self.mails[-1][1]).group(1)
|
||||
page = self.client.get("/verify").get_data(as_text=True)
|
||||
tok = re.search(r'name="csrf-token" content="([^"]+)"', page).group(1)
|
||||
return self.client.post("/verify", data={"code": code, "csrf_token": tok})
|
||||
|
||||
def token_from(self, path):
|
||||
page = self.client.get(path).get_data(as_text=True)
|
||||
return re.search(r'name="csrf-token" content="([^"]+)"', page).group(1)
|
||||
|
||||
def token(self):
|
||||
return self.token_from("/settings")
|
||||
Reference in New Issue
Block a user