Newer
Older
import tempfile
import subprocess
import random
import os
import time
import requests
"""Write a temporary directory with module scope and offer
to write files in it, with optional subpath
#directory = tempfile.TemporaryDirectory()
directory = tempfile.mkdtemp()
def write_and_get_filename(data, path=None):
if type(data) is str:
data = data.encode("utf8")
if path is None:
path = str(random.randbytes(3).hex())
filepath = os.path.join(directory, path)
os.makedirs(os.path.dirname(filepath), 0o755, True)
with open(filepath, "wb") as handle:
handle.write(data)
return filepath
return write_and_get_filename
"""Run an isolated application instance and return the URL"""
port = 5000 + random.randint(1, 999) # Port = 5XXX
url = f"http://localhost:{port}"
env.update(FLASK_APP="hiboo", SQLALCHEMY_DATABASE_URI=f"sqlite:///{db}")
subprocess.run(["flask", "db", "upgrade"], env=env)
subprocess.run(["flask", "user", "create", username, password], env=env)
subprocess.run(["flask", "user", "promote", username], env=env)
proc = subprocess.Popen(["flask", "run", "-p", str(port)], env=env)
# Wait for the server to be ready
for _ in range(30):
try:
assert requests.get(url).status_code == 200
except Exception as e:
print(e)
time.sleep(1)
else:
yield url
break
proc.terminate()
proc.wait()
@pytest.fixture(scope="session")
def username():
"""Default username for tests"""
return "admin"
def password():
"""Default password for tests"""
return "admin"