Files
dxdesigner-part-replacement/tests/dsh/test_issue_transport.py
T

47 lines
2.6 KiB
Python

import json
from dx_productivity.dsh.issue_transport import IssueTransport
TARGET = {'server': 'https://git.test', 'repository': 'owner/repo'}
def test_transport_uses_short_lived_broker_credential_without_caching_token(tmp_path, monkeypatch):
config = tmp_path / 'issue-config.json'
config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange', 'instance_id': 'machine-1'}), encoding='utf8')
monkeypatch.setattr('dx_productivity.dsh.issue_transport._post_broker', lambda *args, **kwargs: {'token': 'short-secret', 'expires_in': 120})
value = IssueTransport(config).credentials(TARGET)
assert value['source'] == 'broker'
assert value['headers']['Authorization'] == 'token short-secret'
assert value['rotation_required'] is False
cache = list(tmp_path.rglob('*'))
assert all('short-secret' not in p.read_text(encoding='utf8', errors='ignore') for p in cache if p.is_file())
def test_transport_marks_bootstrap_fallback_for_rotation(tmp_path, monkeypatch):
config = tmp_path / 'issue-config.json'
config.write_text(json.dumps(TARGET), encoding='utf8')
monkeypatch.setattr('dx_productivity.dsh.issue_transport.load_bootstrap', lambda: {**TARGET, 'token': 'static-secret', 'username': 'u', 'scopes': ['write:issue']})
value = IssueTransport(config).credentials(TARGET)
assert value['source'] == 'prefilled_issue_token'
assert value['rotation_required'] is True
def test_transport_rejects_target_before_broker_or_credentials(tmp_path, monkeypatch):
config = tmp_path / 'issue-config.json'
config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange'}), encoding='utf8')
monkeypatch.setattr('dx_productivity.dsh.issue_transport._post_broker', lambda *args, **kwargs: (_ for _ in ()).throw(AssertionError('network called')))
value = IssueTransport(config).credentials({'server': 'https://evil.test', 'repository': 'owner/repo'})
assert value['source'] == 'invalid_target'
assert 'headers' not in value
def test_git_publisher_connection_prefers_broker_transport(tmp_path, monkeypatch):
from dx_productivity.dsh.git_issues import GitIssuePublisher
config = tmp_path / 'issue-config.json'
config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange'}), encoding='utf8')
monkeypatch.setattr('dx_productivity.dsh.issue_transport._post_broker', lambda *args, **kwargs: {'token': 'ephemeral', 'expires_in': 60})
_, headers, source = GitIssuePublisher(config).connection()
assert headers == {'Authorization': 'token ephemeral'}
assert source == 'broker'