58 行
1.9 KiB
Python
58 行
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Any
|
|
|
|
from backend.services.settings_service import _mask_sensitive, _merge_masked
|
|
|
|
|
|
def _snapshot_payload(config: dict[str, Any]) -> str:
|
|
"""Mirror what ``_record_snapshot`` writes into ``config_snapshots``."""
|
|
|
|
return json.dumps(_mask_sensitive(dict(config)), ensure_ascii=False, sort_keys=True)
|
|
|
|
|
|
def test_snapshot_payload_carries_no_plaintext_secret() -> None:
|
|
"""config_snapshots rows are long-lived; a DB dump must not replay secrets."""
|
|
|
|
config = {
|
|
"generation": {"provider": {"api_key_plaintext": "sk-real-secret-value"}},
|
|
"database": {"password": "hunter2-real"},
|
|
"plugin": {"display_name": "ImageStudio"},
|
|
}
|
|
|
|
payload = _snapshot_payload(config)
|
|
|
|
assert "sk-real-secret-value" not in payload
|
|
assert "hunter2-real" not in payload
|
|
# Non-secret structure must survive, otherwise rollback is worthless.
|
|
assert "ImageStudio" in payload
|
|
|
|
|
|
def test_rollback_merge_restores_live_secrets_over_masked_markers() -> None:
|
|
"""Masked snapshots must still produce a usable config on rollback."""
|
|
|
|
live = {
|
|
"generation": {
|
|
"provider": {"api_key_plaintext": "sk-current", "model_id": "new-model"}
|
|
},
|
|
"plugin": {"display_name": "Renamed"},
|
|
}
|
|
snapshot = json.loads(
|
|
_snapshot_payload(
|
|
{
|
|
"generation": {
|
|
"provider": {"api_key_plaintext": "sk-old", "model_id": "old-model"}
|
|
},
|
|
"plugin": {"display_name": "Original"},
|
|
}
|
|
)
|
|
)
|
|
|
|
merged = _merge_masked(live, snapshot)
|
|
|
|
# Reverted: the non-secret values come from the snapshot.
|
|
assert merged["generation"]["provider"]["model_id"] == "old-model"
|
|
assert merged["plugin"]["display_name"] == "Original"
|
|
# Preserved: the credential in effect right now, not the retired one.
|
|
assert merged["generation"]["provider"]["api_key_plaintext"] == "sk-current"
|