feat: prefill isolated issue-only reporter for clients without Git
This commit is contained in:
1 parent
6c708224d2
commit
e9b77186dc
15 files changed
+435
-20
No files matched your search
@@ -0,0 +1,72 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
from unittest.mock import Mock
|
||||
|
||||
from dx_productivity.dsh import issue_credentials
|
||||
from dx_productivity.dsh.git_issues import GitIssuePublisher
|
||||
|
||||
|
||||
def bootstrap(tmp_path, monkeypatch):
|
||||
path = tmp_path / 'issue-bootstrap.json'
|
||||
path.write_text(json.dumps({'server': 'https://git.example', 'repository': 'org/tool',
|
||||
'username': 'dsh-issue-reporter', 'token': 'test-issue-token', 'scopes': ['write:issue']}), encoding='utf8')
|
||||
monkeypatch.setattr(issue_credentials, 'bootstrap_path', lambda: path)
|
||||
return path
|
||||
|
||||
|
||||
def test_other_machine_uses_prefilled_token_without_git_or_local_config(tmp_path, monkeypatch):
|
||||
bootstrap(tmp_path, monkeypatch)
|
||||
monkeypatch.setattr(subprocess, 'run', Mock(side_effect=AssertionError('Git must not be called')))
|
||||
publisher = GitIssuePublisher(tmp_path / 'fresh-user' / 'issue-config.json')
|
||||
config, headers, source = publisher.connection()
|
||||
assert config['repository'] == 'org/tool'
|
||||
assert headers == {'Authorization': 'token test-issue-token'}
|
||||
assert source == 'prefilled_issue_token'
|
||||
|
||||
|
||||
def test_prefilled_token_cannot_be_sent_to_another_server_or_repository(tmp_path, monkeypatch):
|
||||
bootstrap(tmp_path, monkeypatch)
|
||||
path = tmp_path / 'issue-config.json'
|
||||
path.write_text(json.dumps({'server': 'https://other.example', 'repository': 'other/project', 'enabled': True}))
|
||||
monkeypatch.setattr(subprocess, 'run', Mock(side_effect=FileNotFoundError('git absent')))
|
||||
config, headers, source = GitIssuePublisher(path).connection()
|
||||
assert not headers
|
||||
assert source == 'git_not_installed'
|
||||
|
||||
|
||||
def test_missing_git_becomes_actionable_status_without_losing_incident(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(issue_credentials, 'bootstrap_path', lambda: tmp_path / 'absent.json')
|
||||
monkeypatch.setattr(subprocess, 'run', Mock(side_effect=FileNotFoundError('git absent')))
|
||||
result = GitIssuePublisher(tmp_path / 'absent-config.json')({}, tmp_path / 'evidence.zip')
|
||||
assert result['reason'] == 'git_not_installed'
|
||||
|
||||
|
||||
def test_explicit_disable_remains_respected_with_prefilled_token(tmp_path, monkeypatch):
|
||||
bootstrap(tmp_path, monkeypatch)
|
||||
path = tmp_path / 'issue-config.json'
|
||||
path.write_text('{"enabled":false}')
|
||||
result = GitIssuePublisher(path)({}, tmp_path / 'evidence.zip')
|
||||
assert result['reason'] == 'issue_submission_disabled'
|
||||
|
||||
|
||||
def test_bootstrap_refuses_excessive_token_scopes(tmp_path, monkeypatch):
|
||||
path = bootstrap(tmp_path, monkeypatch)
|
||||
row = json.loads(path.read_text()); row['scopes'] = ['all']
|
||||
path.write_text(json.dumps(row))
|
||||
assert issue_credentials.load_bootstrap() == {}
|
||||
|
||||
|
||||
def test_packaged_bootstrap_works_on_another_computer_without_dpapi_or_git(tmp_path, monkeypatch):
|
||||
import sys
|
||||
bundle = tmp_path / 'dsh-runtime'
|
||||
bundle.mkdir()
|
||||
(bundle / 'issue-bootstrap.json').write_text(json.dumps({'server': 'https://git.example',
|
||||
'repository': 'org/tool', 'username': 'dsh-issue-reporter', 'token': 'test-portable-issue-token',
|
||||
'scopes': ['write:issue']}))
|
||||
monkeypatch.setattr(sys, 'frozen', True, raising=False)
|
||||
monkeypatch.setattr(sys, '_MEIPASS', str(tmp_path), raising=False)
|
||||
monkeypatch.setattr(subprocess, 'run', Mock(side_effect=AssertionError('Git must not be used')))
|
||||
config, headers, source = GitIssuePublisher(tmp_path / 'new-user-data/issue-config.json').connection()
|
||||
assert source == 'prefilled_issue_token'
|
||||
assert headers['Authorization'] == 'token test-portable-issue-token'
|
||||
@@ -17,3 +17,25 @@ def test_launch_does_not_inherit_unrelated_credentials_or_user_harness_home(tmp_
|
||||
assert 'OPENAI_API_KEY' not in env
|
||||
assert 'DSH_HOME' not in env
|
||||
assert env['DX_DSH_HOME'].startswith(str(tmp_path))
|
||||
|
||||
|
||||
def test_context_publication_survives_a_real_windows_reader_lock(tmp_path):
|
||||
import ctypes
|
||||
from ctypes import wintypes
|
||||
from threading import Timer
|
||||
write_context(tmp_path, TaskSnapshot('t', 'draft'))
|
||||
kernel = ctypes.WinDLL('kernel32', use_last_error=True)
|
||||
kernel.CreateFileW.argtypes = (wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, ctypes.c_void_p,
|
||||
wintypes.DWORD, wintypes.DWORD, wintypes.HANDLE)
|
||||
kernel.CreateFileW.restype = wintypes.HANDLE
|
||||
kernel.CloseHandle.argtypes = (wintypes.HANDLE,)
|
||||
handle = kernel.CreateFileW(str(tmp_path / 'task-context.json'), 0x80000000, 3, None, 3, 0, None)
|
||||
assert handle != ctypes.c_void_p(-1).value
|
||||
release = Timer(.03, lambda: kernel.CloseHandle(handle))
|
||||
release.start()
|
||||
try:
|
||||
value = write_context(tmp_path, TaskSnapshot('t', 'failed', issue_code='controlled'))
|
||||
assert json.loads((tmp_path / 'task-context.json').read_text())['revision'] == value['revision']
|
||||
assert not list(tmp_path.glob('context-*.tmp'))
|
||||
finally:
|
||||
release.join()
|
||||
Reference in new issue
Block a user