import json import unittest from shared_backend.coreconfig import ( MIN_INTERNAL_SECRET_LENGTH, Info, Store, follow_omnilogger, handle_core_services_push, internal_secret_matches, internal_secret_usable, ) class StoreDefaultsTests(unittest.TestCase): def test_seeds_from_fallback(self): store = Store( Info( auth_guard_url="http://auth.example.com", carrier_node_url="http://carrier.example.com", ) ) 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): store = Store( Info( auth_guard_url="http://auth.example.com", carrier_node_url="http://carrier.example.com", ) ) 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) self.assertEqual( info.carrier_node_urls, ["http://carrier.example.com", "http://carrier2.example.com"] ) 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")) 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") class OmniLoggerStoreTests(unittest.TestCase): def test_from_json_reads_omni_fields_and_to_json_never_carries_the_token(self): info = Info.from_json( {"omniLoggerUrl": "http://omni.example.com", "omniLoggerIngestToken": "tok-123"} ) self.assertEqual(info.omni_logger_url, "http://omni.example.com") self.assertEqual(info.omni_logger_ingest_token, "tok-123") self.assertEqual(info.to_json()["omniLoggerUrl"], "http://omni.example.com") self.assertNotIn("omniLoggerIngestToken", info.to_json()) self.assertNotIn("tok-123", json.dumps(info.to_json())) self.assertNotIn("tok-123", repr(info)) def test_keeps_last_non_blank_values_and_strips_the_trailing_slash(self): store = Store() store.set( Info(omni_logger_url="http://omni.example.com/", omni_logger_ingest_token="tok-1") ) store.set(Info(omni_logger_url=" ", omni_logger_ingest_token="")) info = store.get() self.assertEqual(info.omni_logger_url, "http://omni.example.com") self.assertEqual(info.omni_logger_ingest_token, "tok-1") def test_a_bad_omni_url_is_refused_and_nothing_changes(self): store = Store() store.set(Info(omni_logger_url="http://omni.example.com", omni_logger_ingest_token="tok-1")) for bad in ["omni.example.com", "ftp://omni.example.com", "http://"]: with self.subTest(url=bad), self.assertRaises(ValueError): store.set(Info(omni_logger_url=bad, omni_logger_ingest_token="tok-2")) self.assertEqual(store.get().omni_logger_url, "http://omni.example.com") self.assertEqual(store.get().omni_logger_ingest_token, "tok-1") def test_subscribers_and_on_update_both_see_every_set(self): store = Store() from_on_update, from_subscriber = [], [] store.on_update(from_on_update.append) store.subscribe(from_subscriber.append) store.set(Info(auth_guard_url="http://auth.example.com")) self.assertEqual(len(from_on_update), 1) self.assertEqual(len(from_subscriber), 1) self.assertEqual(from_subscriber[0].auth_guard_url, "http://auth.example.com") class _RecordingShipper: def __init__(self): self.calls = [] def set_credentials(self, base_url, token): self.calls.append((base_url, token)) class FollowOmniLoggerTests(unittest.TestCase): def setUp(self): self.store = Store() self.shipper = _RecordingShipper() follow_omnilogger(self.store, self.shipper) def test_a_push_with_both_values_sets_the_shipper(self): self.store.set( Info(omni_logger_url="http://omni.example.com", omni_logger_ingest_token="tok-1") ) self.assertEqual(self.shipper.calls, [("http://omni.example.com", "tok-1")]) def test_a_token_without_a_url_leaves_the_shipper_alone(self): self.store.set(Info(omni_logger_ingest_token="tok-1")) self.assertEqual(self.shipper.calls, []) def test_a_url_pushed_after_a_token_applies_both(self): self.store.set(Info(omni_logger_ingest_token="tok-1")) self.store.set(Info(omni_logger_url="http://omni.example.com")) self.assertEqual(self.shipper.calls, [("http://omni.example.com", "tok-1")]) def test_a_later_token_only_push_reuses_the_stored_url(self): self.store.set( Info(omni_logger_url="http://omni.example.com", omni_logger_ingest_token="tok-1") ) self.store.set(Info(omni_logger_ingest_token="tok-2")) self.assertEqual( self.shipper.calls, [("http://omni.example.com", "tok-1"), ("http://omni.example.com", "tok-2")], ) def test_pushes_without_omni_fields_do_not_touch_the_shipper(self): self.store.set(Info(auth_guard_url="http://auth.example.com")) self.assertEqual(self.shipper.calls, []) def test_a_store_without_omni_values_does_not_apply_at_startup(self): follow_omnilogger(Store(), self.shipper) self.assertEqual(self.shipper.calls, []) INTERNAL_SECRET = "a-sufficiently-long-secret-value" PUSH_BODY = {"omniLoggerUrl": "http://omni.example.com", "omniLoggerIngestToken": "tok-secret-1"} AUTH_ERROR = {"error": "internal service authentication required"} JSON_ERROR = {"error": "invalid JSON body"} URL_ERROR = {"error": "core service URL must be an http or https URL with a host"} class HandleCoreServicesPushTests(unittest.TestCase): def setUp(self): self.store = Store() self.shipper = _RecordingShipper() follow_omnilogger(self.store, self.shipper) def push(self, body, auth=INTERNAL_SECRET, secret=INTERNAL_SECRET): raw = body if isinstance(body, bytes) else json.dumps(body).encode("utf-8") return handle_core_services_push(self.store, secret, auth, raw) def test_a_missing_or_wrong_x_internal_auth_gets_401(self): for auth in (None, "", "wrong-secret-value-here", INTERNAL_SECRET + "x"): with self.subTest(auth=auth): self.assertEqual(self.push(PUSH_BODY, auth=auth), (401, AUTH_ERROR)) self.assertEqual(self.shipper.calls, []) self.assertEqual(self.store.get().omni_logger_url, "") def test_an_unusable_secret_refuses_every_caller(self): for secret in (None, "", "SECRET", "change-me-to-a-long-random-value", "short"): with self.subTest(secret=secret): status, _ = self.push(PUSH_BODY, auth=secret, secret=secret) self.assertEqual(status, 401) status, _ = self.push(PUSH_BODY, auth=INTERNAL_SECRET, secret=secret) self.assertEqual(status, 401) self.assertEqual(self.shipper.calls, []) def test_a_valid_push_sets_the_shipper_and_never_echoes_the_token(self): status, body = self.push(PUSH_BODY) self.assertEqual((status, body), (200, {"ok": True})) self.assertEqual(self.shipper.calls, [("http://omni.example.com", "tok-secret-1")]) self.assertNotIn("tok-secret-1", json.dumps(body)) def test_a_token_without_a_url_does_not_change_the_shipper(self): status, body = self.push({"omniLoggerIngestToken": "tok-secret-1"}) self.assertEqual((status, body), (200, {"ok": True})) self.assertEqual(self.shipper.calls, []) def test_a_bad_omni_url_gets_400_and_changes_nothing(self): for bad in ["omni.example.com", "ftp://omni.example.com", "javascript:alert(1)"]: with self.subTest(url=bad): body = {"omniLoggerUrl": bad, "omniLoggerIngestToken": "tok-secret-1"} self.assertEqual(self.push(body), (400, URL_ERROR)) self.assertEqual(self.shipper.calls, []) def test_a_bad_auth_guard_url_gets_400(self): self.assertEqual(self.push({"authGuardUrl": "auth.example.com"}), (400, URL_ERROR)) def test_a_field_of_the_wrong_type_gets_400_not_a_server_error(self): for body in ( {"carrierNodeUrls": 5}, {"carrierNodeUrls": True}, {"carrierNodeUrls": "http://carrier.example.com"}, {"carrierNodeUrl": ["http://carrier.example.com"]}, {"authGuardUrl": 5}, ): with self.subTest(body=body): self.assertEqual(self.push(body), (400, JSON_ERROR)) self.assertEqual(self.shipper.calls, []) def test_an_oversized_body_or_a_body_that_is_not_a_json_object_gets_400(self): cases = [ json.dumps({"x": "a" * 4096}).encode("utf-8"), b"not json", b"[1, 2]", b'"a string"', b"\xff\xfe\x00", json.dumps({"omniLoggerUrl": 5}).encode("utf-8"), json.dumps({"omniLoggerIngestToken": ["tok"]}).encode("utf-8"), ] for raw in cases: with self.subTest(raw=raw[:20]): self.assertEqual(self.push(raw), (400, JSON_ERROR)) def test_a_body_of_exactly_4096_bytes_is_read(self): raw = json.dumps({"x": "a" * 4087}).encode("utf-8") self.assertEqual(len(raw), 4096) self.assertEqual(self.push(raw), (200, {"ok": True})) def test_other_fields_and_null_values_are_ignored(self): body = { "contentScannerUrl": "not a url", "comfyUiUrl": 7, "carrierNodeSecret": "not-used-here", "omniLoggerIngestToken": None, "omniLoggerUrl": "http://omni.example.com", } self.assertEqual(self.push(body), (200, {"ok": True})) self.assertEqual(self.shipper.calls, []) if __name__ == "__main__": unittest.main()