Files
CollabTable/shared/packages/python-backend/tests/test_coreconfig.py
T

121 lines
4.7 KiB
Python
Raw Normal View History

2026-09-14 09:35:45 +02:00
import unittest
from shared_backend.coreconfig import (
MIN_INTERNAL_SECRET_LENGTH,
Info,
Store,
internal_secret_matches,
internal_secret_usable,
)
2026-09-14 09:35:45 +02:00
class StoreDefaultsTests(unittest.TestCase):
def test_seeds_from_fallback(self):
2026-09-18 13:11:49 +02:00
store = Store(
Info(
auth_guard_url="http://auth.example.com",
carrier_node_url="http://carrier.example.com",
)
)
2026-09-14 09:35:45 +02:00
info = store.get()
self.assertEqual(info.auth_guard_url, "http://auth.example.com")
self.assertEqual(info.carrier_node_url, "http://carrier.example.com")
self.assertEqual(info.carrier_node_urls, ["http://carrier.example.com"])
def test_defaults_to_empty(self):
store = Store()
info = store.get()
self.assertEqual(info.auth_guard_url, "")
self.assertEqual(info.carrier_node_urls, [])
class StoreSetTests(unittest.TestCase):
def test_partial_push_does_not_clobber_other_field(self):
2026-09-18 13:11:49 +02:00
store = Store(
Info(
auth_guard_url="http://auth.example.com",
carrier_node_url="http://carrier.example.com",
)
)
2026-09-14 09:35:45 +02:00
store.set(Info(carrier_node_url="http://carrier2.example.com"))
info = store.get()
self.assertEqual(info.auth_guard_url, "http://auth.example.com")
self.assertEqual(info.carrier_node_url, "http://carrier2.example.com")
def test_blank_auth_guard_url_ignored(self):
store = Store(Info(auth_guard_url="http://auth.example.com"))
store.set(Info(auth_guard_url=""))
self.assertEqual(store.auth_guard_url, "http://auth.example.com")
def test_on_update_called_with_resulting_info(self):
store = Store()
seen = []
store.on_update(seen.append)
store.set(Info(auth_guard_url="http://auth.example.com"))
self.assertEqual(len(seen), 1)
self.assertEqual(seen[0].auth_guard_url, "http://auth.example.com")
def test_from_json_round_trip(self):
payload = {
"authGuardUrl": "http://auth.example.com",
"carrierNodeUrl": "http://carrier.example.com",
"carrierNodeUrls": ["http://carrier.example.com", "http://carrier2.example.com"],
}
info = Info.from_json(payload)
2026-09-18 13:11:49 +02:00
self.assertEqual(
info.carrier_node_urls, ["http://carrier.example.com", "http://carrier2.example.com"]
)
2026-09-14 09:35:45 +02:00
self.assertEqual(info.to_json()["carrierNodeUrl"], "http://carrier.example.com")
LONG_SECRET = "shared-secret-value"
class InternalSecretTests(unittest.TestCase):
def test_unset_or_empty_secret_never_matches(self):
self.assertFalse(internal_secret_matches(None, None))
self.assertFalse(internal_secret_matches(None, ""))
self.assertFalse(internal_secret_matches("", ""))
self.assertFalse(internal_secret_matches("", None))
def test_compares_the_whole_secret(self):
self.assertTrue(internal_secret_matches(LONG_SECRET, LONG_SECRET))
self.assertFalse(internal_secret_matches("shared-secret-valuE", LONG_SECRET))
self.assertFalse(internal_secret_matches(None, LONG_SECRET))
def test_short_secret_never_matches(self):
short = "x" * (MIN_INTERNAL_SECRET_LENGTH - 1)
self.assertFalse(internal_secret_matches(short, short))
def test_placeholder_secret_never_matches(self):
for placeholder in ("SECRET", "change-me-to-a-long-random-value"):
with self.subTest(placeholder=placeholder):
self.assertFalse(internal_secret_matches(placeholder, placeholder))
def test_usable_requires_minimum_length_and_no_placeholder(self):
self.assertFalse(internal_secret_usable(None))
self.assertFalse(internal_secret_usable(""))
self.assertFalse(internal_secret_usable("x" * (MIN_INTERNAL_SECRET_LENGTH - 1)))
self.assertTrue(internal_secret_usable("x" * MIN_INTERNAL_SECRET_LENGTH))
self.assertFalse(internal_secret_usable("change-me-to-a-long-random-value"))
self.assertTrue(internal_secret_usable(LONG_SECRET))
class UrlValidationTests(unittest.TestCase):
def test_rejects_urls_that_are_not_http_or_https_with_a_host(self):
store = Store(Info(auth_guard_url="http://auth.example.com"))
2026-11-10 14:42:45 +01:00
for bad in [
"javascript:alert(1)",
"auth.example.com",
"ftp://carrier.example.com",
"http://",
]:
with self.subTest(url=bad), self.assertRaises(ValueError):
store.set(Info(auth_guard_url=bad))
with self.assertRaises(ValueError):
store.set(Info(carrier_node_urls=["http://carrier.example.com:99999"]))
self.assertEqual(store.auth_guard_url, "http://auth.example.com")
2026-09-14 09:35:45 +02:00
if __name__ == "__main__":
unittest.main()