Lesender Selbsttest (Version, Services, Hosts, Downtimes) beim Start und täglich 04:00, Seite /selftest, Teilcheck checkmk_api in /health, Chat-Meldung bei Wechsel. Toleranter Antwort-Parser (value/members, state_type int/String), Fallbacks für Host-Abfrage und Downtime-Liste, Fehlerhinweise je HTTP-Status. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
79 lines
2.7 KiB
Python
79 lines
2.7 KiB
Python
from app.selftest import CHECK_VERSION, STATUS_OK, CheckResult, SelftestRun
|
|
from tests.test_web import VIEWER, admin, app, client, login_ok, post, users, viewer # noqa: F401
|
|
|
|
PAGE = "/selftest"
|
|
RUN_PATH = "/selftest/run"
|
|
|
|
|
|
def test_page_requires_login(client):
|
|
assert client.get(PAGE).status_code == 303
|
|
|
|
|
|
def test_viewer_sees_page_without_run_button(client, viewer):
|
|
login_ok(client, VIEWER)
|
|
page = client.get(PAGE)
|
|
assert page.status_code == 200 and "Noch kein Lauf" in page.text
|
|
assert 'action="/selftest/run"' not in page.text
|
|
|
|
|
|
def test_admin_sees_run_button(client, admin):
|
|
login_ok(client)
|
|
page = client.get(PAGE)
|
|
assert page.status_code == 200 and 'action="/selftest/run"' in page.text
|
|
|
|
|
|
def test_viewer_cannot_trigger_run(client, viewer):
|
|
login_ok(client, VIEWER)
|
|
assert post(client, RUN_PATH, PAGE).status_code == 403
|
|
|
|
|
|
def test_run_requires_csrf(client, admin):
|
|
login_ok(client)
|
|
assert client.post(RUN_PATH).status_code == 403
|
|
|
|
|
|
def test_manual_run_not_configured_flashes_error(client, admin):
|
|
login_ok(client)
|
|
response = post(client, RUN_PATH, PAGE)
|
|
assert response.status_code == 303
|
|
assert "nicht konfiguriert" in client.get(PAGE).text
|
|
|
|
|
|
def test_manual_run_shows_result_and_writes_audit(client, app, admin, monkeypatch):
|
|
login_ok(client)
|
|
run = SelftestRun(1_800_000_000, "2.5.0", [CheckResult(CHECK_VERSION, STATUS_OK, "Checkmk 2.5.0")])
|
|
|
|
async def fake_run(self):
|
|
return run
|
|
|
|
service = app.state.runtime.selftest
|
|
monkeypatch.setattr(type(service), "run", fake_run)
|
|
post(client, RUN_PATH, PAGE)
|
|
page = client.get(PAGE)
|
|
assert "Checkmk 2.5.0" in page.text and "2.5.0" in page.text
|
|
rows = app.state.db.query("SELECT user, action, target FROM audit WHERE action='selftest.run'")
|
|
assert len(rows) == 1 and rows[0]["user"] == "admin1" and rows[0]["target"] == "selftest"
|
|
|
|
|
|
def test_manual_run_is_rate_limited(client, admin, monkeypatch):
|
|
login_ok(client)
|
|
post(client, RUN_PATH, PAGE)
|
|
response = post(client, RUN_PATH, PAGE)
|
|
assert "warten" in client.get(PAGE).text
|
|
|
|
|
|
def test_settings_section_toggles_require_admin(client, viewer):
|
|
login_ok(client, VIEWER)
|
|
assert client.get("/settings/selftest").status_code == 403
|
|
|
|
|
|
def test_settings_section_save_updates_flags(client, app, admin):
|
|
login_ok(client)
|
|
page = client.get("/settings/selftest")
|
|
assert page.status_code == 200
|
|
response = post(client, "/settings/selftest", "/settings/selftest",
|
|
selftest_enabled="", selftest_notify="1")
|
|
assert response.status_code == 303
|
|
store = app.state.store
|
|
assert store.get("selftest.enabled") == "0" and store.get("selftest.notify") == "1"
|