69 lines
2.7 KiB
Python
69 lines
2.7 KiB
Python
import time
|
|
from datetime import timedelta
|
|
|
|
from tests.test_web import (ADMIN, VIEWER, admin, app, client, csrf_from, login_ok, post, # noqa: F401
|
|
users, viewer)
|
|
|
|
|
|
def active(app):
|
|
return app.state.runtime.mutes.active()
|
|
|
|
|
|
def mute_form(**overrides):
|
|
data = {"host_glob": "web*", "service_glob": "Disk*", "duration": "120", "custom_minutes": "",
|
|
"reason": "Wartung"}
|
|
data.update(overrides)
|
|
return data
|
|
|
|
|
|
def test_mutes_requires_login(client):
|
|
assert client.get("/mutes").status_code == 303
|
|
|
|
|
|
def test_viewer_sees_list_but_cannot_change(client, app, viewer):
|
|
app.state.runtime.mutes.create("web*", "", timedelta(hours=1), "x", "t", "ui")
|
|
login_ok(client, VIEWER)
|
|
page = client.get("/mutes")
|
|
assert page.status_code == 200 and "web*" in page.text and "Stummschaltung anlegen" not in page.text
|
|
token = csrf_from(client, "/mutes")
|
|
assert client.post("/mutes", data={"csrf": token, **mute_form()}).status_code == 403
|
|
assert client.post("/mutes/1/delete", data={"csrf": token}).status_code == 403
|
|
assert len(active(app)) == 1
|
|
|
|
|
|
def test_admin_creates_and_deletes_with_audit(client, app, admin):
|
|
login_ok(client)
|
|
assert post(client, "/mutes", "/mutes", **mute_form()).status_code == 303
|
|
mute = active(app)[0]
|
|
assert mute.host_glob == "web*" and mute.service_glob == "Disk*" and mute.source == "ui"
|
|
assert mute.created_by == ADMIN
|
|
assert abs(mute.expires_at - (time.time() + 7200)) < 60
|
|
assert "Wartung" in client.get("/mutes").text
|
|
assert post(client, f"/mutes/{mute.id}/delete", "/mutes").status_code == 303
|
|
assert active(app) == []
|
|
actions = [r["action"] for r in app.state.db.query("SELECT action FROM audit")]
|
|
assert "mute.create" in actions and "mute.delete" in actions
|
|
|
|
|
|
def test_free_minutes_override_selection(client, app, admin):
|
|
login_ok(client)
|
|
post(client, "/mutes", "/mutes", **mute_form(custom_minutes="15"))
|
|
assert abs(active(app)[0].expires_at - (time.time() + 900)) < 60
|
|
|
|
|
|
def test_invalid_input_rejected(client, app, admin):
|
|
login_ok(client)
|
|
for override in ({"host_glob": ""}, {"custom_minutes": "abc"}, {"custom_minutes": "0"},
|
|
{"custom_minutes": "99999999999999999999"}, {"duration": "x"}):
|
|
response = post(client, "/mutes", "/mutes", **mute_form(**override))
|
|
assert response.status_code == 303
|
|
assert active(app) == []
|
|
|
|
|
|
def test_expired_mutes_not_shown(client, app, admin):
|
|
service = app.state.runtime.mutes
|
|
service.create("old*", "", timedelta(minutes=1), "", "t", "ui")
|
|
app.state.db.execute("UPDATE mutes SET expires_at=1")
|
|
login_ok(client)
|
|
assert "old*" not in client.get("/mutes").text
|