import json from dx_productivity.dsh.issue_transport import IssueTransport TARGET = {'server': 'https://git.test', 'repository': 'owner/repo'} def test_transport_reports_revocable_static_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', 'instance_proof': 'proof', 'idempotency_key': 'event-1'}), encoding='utf8') monkeypatch.setattr('dx_productivity.dsh.issue_transport._post_broker', lambda *args, **kwargs: {**TARGET, 'token': 'static-broker-secret', 'credential_kind': 'revocable_static', 'expires_utc': None}) value = IssueTransport(config).credentials(TARGET) assert value['source'] == 'broker' assert value['headers']['Authorization'] == 'token static-broker-secret' assert value['rotation_required'] is True assert value['expires_utc'] is None and value['credential_kind'] == 'revocable_static' cache = list(tmp_path.rglob('*')) assert all('static-broker-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', 'instance_id': 'machine-1', 'instance_proof': 'proof', 'idempotency_key': 'event-1'}), 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', 'instance_id': 'machine-1', 'instance_proof': 'proof', 'idempotency_key': 'event-1'}), encoding='utf8') monkeypatch.setattr('dx_productivity.dsh.issue_transport._post_broker', lambda *args, **kwargs: {**TARGET, 'token': 'broker-token', 'credential_kind': 'revocable_static', 'expires_utc': None}) _, headers, source = GitIssuePublisher(config).connection() assert headers == {'Authorization': 'token broker-token'} assert source == 'broker' def test_transport_rejects_wrong_broker_response_target(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', 'instance_proof': 'proof', 'idempotency_key': 'event-1'}), encoding='utf8') monkeypatch.setattr('dx_productivity.dsh.issue_transport._post_broker', lambda *args, **kwargs: { **TARGET, 'repository': 'other/repo', 'token': 'private', 'credential_kind': 'revocable_static', 'expires_utc': None}) value = IssueTransport(config).credentials(TARGET) assert 'headers' not in value and value['source'] == 'broker_unavailable'