62 lines
2 KiB
Python
62 lines
2 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from l4d2web.app import create_app
|
|
from l4d2web.auth import hash_password
|
|
from l4d2web.db import init_db, session_scope
|
|
from l4d2web.models import Blueprint, BlueprintOverlay, Overlay, Server, User
|
|
|
|
|
|
@pytest.fixture
|
|
def server_with_blueprint(tmp_path, monkeypatch):
|
|
db_url = f"sqlite:///{tmp_path/'facade.db'}"
|
|
monkeypatch.setenv("DATABASE_URL", db_url)
|
|
app = create_app({"TESTING": True, "DATABASE_URL": db_url, "SECRET_KEY": "test"})
|
|
init_db()
|
|
|
|
with app.app_context():
|
|
with session_scope() as session:
|
|
user = User(username="alice", password_digest=hash_password("secret"), admin=False)
|
|
session.add(user)
|
|
session.flush()
|
|
|
|
overlay = Overlay(name="standard", path="/opt/l4d2/overlays/standard")
|
|
session.add(overlay)
|
|
session.flush()
|
|
|
|
blueprint = Blueprint(
|
|
user_id=user.id,
|
|
name="default",
|
|
arguments='["-tickrate 100"]',
|
|
config='["sv_consistency 1"]',
|
|
)
|
|
session.add(blueprint)
|
|
session.flush()
|
|
|
|
session.add(BlueprintOverlay(blueprint_id=blueprint.id, overlay_id=overlay.id, position=0))
|
|
|
|
server = Server(user_id=user.id, blueprint_id=blueprint.id, name="alpha", port=27015)
|
|
session.add(server)
|
|
session.flush()
|
|
server_id = server.id
|
|
|
|
return server_id
|
|
|
|
|
|
def test_initialize_uses_latest_blueprint_data(monkeypatch: pytest.MonkeyPatch, server_with_blueprint) -> None:
|
|
called: dict[str, str] = {}
|
|
|
|
def fake_initialize(name, spec_path, **kwargs):
|
|
del kwargs
|
|
called["name"] = name
|
|
called["spec"] = Path(spec_path).read_text()
|
|
|
|
monkeypatch.setattr("l4d2web.services.l4d2_facade.initialize_instance", fake_initialize)
|
|
|
|
from l4d2web.services.l4d2_facade import initialize_server
|
|
|
|
initialize_server(server_with_blueprint)
|
|
|
|
assert called["name"] == "alpha"
|
|
assert "sv_consistency 1" in called["spec"]
|