merge: sync provider snapshot fixes and document PR11 verification
This commit is contained in:
commit
5dc299c478
7 files changed
+244
-12
No files matched your search
@@ -8,7 +8,8 @@ def test_provider_key_is_encrypted_and_models_survive_reload(tmp_path):
|
||||
key = 'test-credential-not-a-real-key'
|
||||
save_provider(tmp_path, base_url='https://example.test/v1/', models=['a', 'b'], default_model='b', api_key=key)
|
||||
assert key not in (tmp_path / 'provider.json').read_text(encoding='utf8')
|
||||
assert key.encode() not in (tmp_path / 'provider-key.dpapi').read_bytes()
|
||||
metadata=json.loads((tmp_path/'provider.json').read_text(encoding='utf8'))
|
||||
assert key.encode() not in (tmp_path/metadata['key_file']).read_bytes()
|
||||
restored = load_provider(tmp_path)
|
||||
assert restored['DSH_API_KEY'] == key
|
||||
assert restored['DSH_MODEL'] == 'b'
|
||||
@@ -35,6 +36,130 @@ def test_metadata_replace_failure_preserves_existing_json(tmp_path,monkeypatch):
|
||||
assert not list(tmp_path.glob('provider.json*.tmp'))
|
||||
|
||||
|
||||
def seed_legacy_provider(directory,values):
|
||||
import win32crypt
|
||||
directory.mkdir(parents=True,exist_ok=True)
|
||||
key=win32crypt.CryptProtectData(values['api_key'].encode('utf8'),'Legacy provider',None,None,None,0)
|
||||
(directory/'provider-key.dpapi').write_bytes(key)
|
||||
(directory/'provider.json').write_text(json.dumps({k:v for k,v in values.items()if k!='api_key'}),encoding='utf8')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('legacy',[False,True])
|
||||
@pytest.mark.parametrize('stage',['write','flush','replace'])
|
||||
def test_failed_provider_switch_never_combines_old_endpoint_with_new_key(tmp_path,monkeypatch,legacy,stage):
|
||||
from dx_productivity.dsh import provider_config
|
||||
old=dict(base_url='https://old-provider.test/v1',models=['old'],default_model='old',api_key='old-fake-key')
|
||||
new=dict(base_url='https://new-provider.test/v1',models=['new'],default_model='new',api_key='new-fake-key')
|
||||
if legacy:seed_legacy_provider(tmp_path,old)
|
||||
else:save_provider(tmp_path,**old)
|
||||
expected=load_provider(tmp_path)
|
||||
old_json=(tmp_path/'provider.json').read_bytes()
|
||||
old_keys={p.name:p.read_bytes()for p in tmp_path.glob('provider-key*.dpapi')}
|
||||
dump,fsync,replace=provider_config.json.dump,provider_config.os.fsync,provider_config.os.replace
|
||||
metadata_written=False
|
||||
def failing_dump(value,stream,**kwargs):
|
||||
nonlocal metadata_written
|
||||
assert load_provider(tmp_path)==expected
|
||||
if stage=='write':
|
||||
stream.write('{"base_url":')
|
||||
raise OSError('Controlled provider publication failure')
|
||||
dump(value,stream,**kwargs)
|
||||
metadata_written=True
|
||||
def failing_fsync(fd):
|
||||
if stage=='flush'and metadata_written:
|
||||
assert load_provider(tmp_path)==expected
|
||||
raise OSError('Controlled provider publication failure')
|
||||
return fsync(fd)
|
||||
def failing_replace(source,destination):
|
||||
if stage=='replace'and Path(destination)==tmp_path/'provider.json':
|
||||
assert load_provider(tmp_path)==expected
|
||||
raise OSError('Controlled provider publication failure')
|
||||
return replace(source,destination)
|
||||
monkeypatch.setattr(provider_config.json,'dump',failing_dump)
|
||||
monkeypatch.setattr(provider_config.os,'fsync',failing_fsync)
|
||||
monkeypatch.setattr(provider_config.os,'replace',failing_replace)
|
||||
with pytest.raises(OSError,match='Controlled provider publication failure'):
|
||||
save_provider(tmp_path,**new)
|
||||
assert load_provider(tmp_path)==expected
|
||||
assert (tmp_path/'provider.json').read_bytes()==old_json
|
||||
assert {p.name:p.read_bytes()for p in tmp_path.glob('provider-key*.dpapi')}==old_keys
|
||||
assert not list(tmp_path.glob('provider.json*.tmp'))
|
||||
|
||||
|
||||
@pytest.mark.parametrize('legacy',[False,True])
|
||||
def test_successful_provider_switch_loads_one_complete_new_configuration(tmp_path,legacy):
|
||||
old=dict(base_url='https://old-provider.test/v1',models=['old'],default_model='old',api_key='old-fake-key')
|
||||
if legacy:seed_legacy_provider(tmp_path,old)
|
||||
else:save_provider(tmp_path,**old)
|
||||
save_provider(tmp_path,base_url='https://new-provider.test/v1/',models=['new'],default_model='new',api_key='new-fake-key')
|
||||
loaded=load_provider(tmp_path)
|
||||
assert loaded=={'DSH_API_BASE':'https://new-provider.test/v1','DSH_API_KEY':'new-fake-key',
|
||||
'DSH_MODEL':'new','DX_DSH_MODELS':json.dumps(['new'])}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('legacy',[False,True])
|
||||
def test_reader_of_previous_snapshot_keeps_its_matching_key_during_switch(tmp_path,monkeypatch,legacy):
|
||||
old=dict(base_url='https://old-provider.test/v1',models=['old'],default_model='old',api_key='old-fake-key')
|
||||
if legacy:seed_legacy_provider(tmp_path,old)
|
||||
else:save_provider(tmp_path,**old)
|
||||
expected=load_provider(tmp_path)
|
||||
original_read=Path.read_text
|
||||
switched=False
|
||||
def read_then_switch(path,*args,**kwargs):
|
||||
nonlocal switched
|
||||
text=original_read(path,*args,**kwargs)
|
||||
if path==tmp_path/'provider.json'and not switched:
|
||||
switched=True
|
||||
save_provider(tmp_path,base_url='https://new-provider.test/v1',models=['new'],default_model='new',api_key='new-fake-key')
|
||||
return text
|
||||
monkeypatch.setattr(Path,'read_text',read_then_switch)
|
||||
assert load_provider(tmp_path)==expected
|
||||
assert load_provider(tmp_path)['DSH_API_KEY']=='new-fake-key'
|
||||
|
||||
|
||||
def test_missing_versioned_key_never_falls_back_to_legacy_key(tmp_path):
|
||||
old=dict(base_url='https://old-provider.test/v1',models=['old'],default_model='old',api_key='old-fake-key')
|
||||
seed_legacy_provider(tmp_path,old)
|
||||
save_provider(tmp_path,base_url='https://new-provider.test/v1',models=['new'],default_model='new',api_key='new-fake-key')
|
||||
metadata=json.loads((tmp_path/'provider.json').read_text(encoding='utf8'))
|
||||
(tmp_path/metadata['key_file']).unlink()
|
||||
with pytest.raises(FileNotFoundError):load_provider(tmp_path)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('reference',['../provider-key.dpapi','C:/external.dpapi','provider-key.dpapi',None])
|
||||
def test_invalid_key_reference_is_rejected(tmp_path,reference):
|
||||
save_provider(tmp_path,base_url='https://example.test/v1',models=['model'],default_model='model',api_key='fake-key')
|
||||
path=tmp_path/'provider.json';metadata=json.loads(path.read_text(encoding='utf8'))
|
||||
metadata['key_file']=reference
|
||||
path.write_text(json.dumps(metadata),encoding='utf8')
|
||||
with pytest.raises(ValueError,match='invalid_provider_key_reference'):load_provider(tmp_path)
|
||||
|
||||
|
||||
def test_failed_first_publication_does_not_expose_uncommitted_key(tmp_path,monkeypatch):
|
||||
from dx_productivity.dsh import provider_config
|
||||
def fail_replace(*args):raise OSError('Controlled first publication failure')
|
||||
monkeypatch.setattr(provider_config.os,'replace',fail_replace)
|
||||
with pytest.raises(OSError,match='Controlled first publication failure'):
|
||||
save_provider(tmp_path,base_url='https://example.test/v1',models=['model'],default_model='model',api_key='fake-key')
|
||||
assert load_provider(tmp_path)=={}
|
||||
assert not list(tmp_path.iterdir())
|
||||
|
||||
|
||||
@pytest.mark.parametrize('legacy',[False,True])
|
||||
def test_desktop_acceptance_copies_a_complete_provider_snapshot(tmp_path,legacy):
|
||||
import importlib.util
|
||||
tool=Path(__file__).resolve().parents[2]/'tools/accept_dsh_desktop.py'
|
||||
spec=importlib.util.spec_from_file_location('accept_dsh_desktop_config_test',tool)
|
||||
module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module)
|
||||
source,destination=tmp_path/'source',tmp_path/'copied'
|
||||
values=dict(base_url='https://example.test/v1',models=['模型'],default_model='模型',api_key='fake-key')
|
||||
if legacy:seed_legacy_provider(source,values)
|
||||
else:save_provider(source,**values)
|
||||
module.copy_provider_configuration(source,destination)
|
||||
assert load_provider(destination)==load_provider(source)
|
||||
assert 'fake-key'not in (destination/'provider.json').read_text(encoding='utf8')
|
||||
|
||||
|
||||
def test_metadata_flush_failure_preserves_existing_json(tmp_path,monkeypatch):
|
||||
from dx_productivity.dsh import provider_config
|
||||
values=dict(base_url='https://example.test/v1',models=['old'],default_model='old',api_key='local-test-key')
|
||||
|
||||
Reference in new issue
Block a user