73 lines
3.5 KiB
Python
73 lines
3.5 KiB
Python
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'
|