import pytest RDP = { "typ": "rdp", "anzeigename": "Fileserver Test", "ziel": "test-server-01.example.local", "benutzer": "test_admin", "sichtbarkeit": "privat", } @pytest.fixture def alice(client, als): return als("alice") @pytest.fixture def bob(client, als): return als("bob") def anlegen(c, **aenderungen): r = c.post("/api/connections", json={**RDP, **aenderungen}) assert r.status_code == 201, r.text return r.json() def test_ohne_login_kein_zugriff(client): assert client.get("/api/connections").status_code == 401 def test_anlegen_ohne_csrf_abgelehnt(alice): del alice.headers["X-CSRF-Token"] assert alice.post("/api/connections", json=RDP).status_code == 403 def test_anlegen_setzt_standardport_und_besitzer(alice): c = anlegen(alice) assert c["port"] == 3389 assert c["besitzer_name"] == "alice" assert anlegen(alice, typ="ssh")["port"] == 22 def test_web_eintrag(alice): c = anlegen(alice, typ="web", ziel="https://intranet.example.local/wiki", port=8443, benutzer="x") assert c["ziel"] == "https://intranet.example.local/wiki" assert c["port"] is None and c["benutzer"] is None @pytest.mark.parametrize("ziel", [ "test-server-01.example.local", "192.0.2.10", "2001:db8::1", "fe80::1", "srv01", ]) def test_gueltige_ziele(alice, ziel): anlegen(alice, ziel=ziel) @pytest.mark.parametrize("ziel", [ "server; calc.exe", "server&whoami", "server|x", "$(id)", "`id`", "a b", "-server", "server-.example.local", "[2001:db8::1]", "fe80::1%eth0", "host/path", "host\\x", "'host'", "\"host\"", "ho\nst", "", ]) def test_ungueltige_ziele(alice, ziel): r = alice.post("/api/connections", json={**RDP, "ziel": ziel}) assert r.status_code == 422 @pytest.mark.parametrize("benutzer", ["admin;x", "DOMAIN\\user", "a b", "user@domain", "$x", "a" * 65]) def test_ungueltige_benutzer(alice, benutzer): assert alice.post("/api/connections", json={**RDP, "benutzer": benutzer}).status_code == 422 @pytest.mark.parametrize("port", [0, 65536, -1]) def test_ungueltige_ports(alice, port): assert alice.post("/api/connections", json={**RDP, "port": port}).status_code == 422 @pytest.mark.parametrize("url", [ "javascript:alert(1)", "ftp://x.example.local", "intranet.example.local", "https://", "https://a b.example.local", "https://x.example.local:99999/", ]) def test_ungueltige_urls(alice, url): assert alice.post("/api/connections", json={**RDP, "typ": "web", "ziel": url}).status_code == 422 def test_privat_nur_fuer_besitzer(alice, bob): c = anlegen(alice) assert [x["id"] for x in alice.get("/api/connections").json()] == [c["id"]] assert bob.get("/api/connections").json() == [] assert bob.get(f"/api/connections/{c['id']}").status_code == 404 assert bob.put(f"/api/connections/{c['id']}", json=RDP).status_code == 404 assert bob.delete(f"/api/connections/{c['id']}").status_code == 404 assert alice.get(f"/api/connections/{c['id']}").status_code == 200 def test_team_fuer_alle_sicht_und_bearbeitbar(alice, bob): c = anlegen(alice, sichtbarkeit="team") assert [x["id"] for x in bob.get("/api/connections").json()] == [c["id"]] r = bob.put(f"/api/connections/{c['id']}", json={**RDP, "sichtbarkeit": "team", "anzeigename": "Neu"}) assert r.status_code == 200 assert r.json()["anzeigename"] == "Neu" assert r.json()["besitzer_name"] == "alice" assert bob.delete(f"/api/connections/{c['id']}").status_code == 204 assert alice.get(f"/api/connections/{c['id']}").status_code == 404 def test_nur_besitzer_darf_team_auf_privat_setzen(alice, bob): c = anlegen(alice, sichtbarkeit="team") r = bob.put(f"/api/connections/{c['id']}", json={**RDP, "sichtbarkeit": "privat"}) assert r.status_code == 403 r = alice.put(f"/api/connections/{c['id']}", json={**RDP, "sichtbarkeit": "privat"}) assert r.status_code == 200 assert bob.get("/api/connections").json() == [] def test_filter(alice, bob): p = anlegen(alice) t = anlegen(alice, typ="ssh", sichtbarkeit="team") anlegen(bob) # fremder privater Eintrag, darf nie auftauchen ids = lambda q: [x["id"] for x in alice.get("/api/connections" + q).json()] # noqa: E731 assert sorted(ids("")) == sorted([p["id"], t["id"]]) assert ids("?sichtbarkeit=privat") == [p["id"]] assert ids("?sichtbarkeit=team") == [t["id"]] assert ids("?typ=ssh") == [t["id"]] assert alice.get("/api/connections?sichtbarkeit=alle").status_code == 422 def test_anzeigename_wird_nicht_als_html_interpretiert(alice): # Server speichert unverändert; das Frontend setzt Texte nur per textContent c = anlegen(alice, anzeigename="") assert c["anzeigename"] == ""