Grotti 2.2.0: Auth, API-Tokens, MCP und Doku-Trennung.
This commit is contained in:
1 parent
b20f8a7779
commit
5a3716b26a
65 files changed
+2212
-198
No files matched your search
@@ -0,0 +1,64 @@
|
||||
"""Tests für Benutzer-/Token-Helfer und Kamera-Felder."""
|
||||
|
||||
import unittest
|
||||
|
||||
from grotti import auth
|
||||
from grotti import moonraker
|
||||
|
||||
|
||||
class AuthHelpersTest(unittest.TestCase):
|
||||
def test_password_roundtrip(self):
|
||||
hashed = auth.hash_password("geheim123")
|
||||
self.assertTrue(auth.verify_password(hashed, "geheim123"))
|
||||
self.assertFalse(auth.verify_password(hashed, "falsch"))
|
||||
|
||||
def test_username_rules(self):
|
||||
self.assertEqual(auth.validate_username("admin"), "admin")
|
||||
with self.assertRaises(Exception):
|
||||
auth.validate_username("a")
|
||||
with self.assertRaises(Exception):
|
||||
auth.validate_username("bad name")
|
||||
|
||||
def test_role_rank(self):
|
||||
self.assertTrue(auth.role_at_least("admin", "operator"))
|
||||
self.assertTrue(auth.role_at_least("admin", "admin"))
|
||||
self.assertFalse(auth.role_at_least("operator", "admin"))
|
||||
|
||||
def test_token_hash_stable(self):
|
||||
raw = auth.new_api_token()
|
||||
self.assertTrue(raw.startswith("grt_"))
|
||||
self.assertEqual(auth.hash_token(raw), auth.hash_token(raw))
|
||||
self.assertNotEqual(auth.hash_token(raw), auth.hash_token(raw + "x"))
|
||||
|
||||
def test_clamp_token_role(self):
|
||||
self.assertEqual(auth.clamp_token_role("admin", "operator"), "operator")
|
||||
with self.assertRaises(Exception):
|
||||
auth.clamp_token_role("operator", "admin")
|
||||
|
||||
def test_login_rate(self):
|
||||
key = "test-rate-%s" % id(self)
|
||||
for _ in range(auth.LOGIN_MAX_ATTEMPTS):
|
||||
self.assertTrue(auth.login_rate_ok(key))
|
||||
auth.login_rate_hit(key)
|
||||
self.assertFalse(auth.login_rate_ok(key))
|
||||
|
||||
|
||||
class CameraFieldsTest(unittest.TestCase):
|
||||
def test_unknown_offers_url(self):
|
||||
moonraker.CAMERA_CACHE.clear()
|
||||
fields = moonraker._camera_fields("10.0.0.1", "10.0.0.1")
|
||||
self.assertTrue(fields["camera"])
|
||||
self.assertIsNone(fields["has_camera"])
|
||||
|
||||
def test_known_dead_hides_url(self):
|
||||
import time
|
||||
|
||||
moonraker.CAMERA_CACHE["10.0.0.2"] = (time.time(), 30, None)
|
||||
fields = moonraker._camera_fields("10.0.0.2", "10.0.0.2")
|
||||
self.assertEqual(fields["camera"], "")
|
||||
self.assertFalse(fields["has_camera"])
|
||||
moonraker.CAMERA_CACHE.pop("10.0.0.2", None)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user