mirror of
https://github.com/agentrhq/authsome.git
synced 2026-09-19 01:34:19 +08:00
99 lines
3.9 KiB
Python
99 lines
3.9 KiB
Python
"""Tests for `authsome whoami`."""
|
|
|
|
import json
|
|
|
|
from authsome.cli.main import cli
|
|
|
|
|
|
def _make_whoami(version: str = "1.2.3") -> dict:
|
|
return {
|
|
"version": version,
|
|
"home": "/home/test/.authsome",
|
|
"identity": "steady-wisely-boldly-0042",
|
|
"principal_id": "principal_1",
|
|
"vault_id": "vault_default",
|
|
"did": "did:key:z6MkTest",
|
|
"registration_status": "registered",
|
|
"configured_encryption_mode": "auto",
|
|
"effective_encryption_source": "keyring",
|
|
"encryption_backend": "OS Keyring",
|
|
}
|
|
|
|
|
|
def _make_ready_ok() -> dict:
|
|
return {"status": "ready", "checks": {"vault": "ok"}, "issues": []}
|
|
|
|
|
|
def _make_ready_fail() -> dict:
|
|
return {"status": "not_ready", "checks": {"vault": "error"}, "issues": ["vault corrupted"]}
|
|
|
|
|
|
class TestWhoamiCommand:
|
|
"""Tests for `authsome whoami`."""
|
|
|
|
def test_json_output_shape(self, runner, mock_client) -> None:
|
|
mock_client.whoami.return_value = _make_whoami()
|
|
mock_client.doctor.return_value = _make_ready_ok()
|
|
mock_client.list_connections.return_value = {"connections": [], "by_source": {"bundled": [], "custom": []}}
|
|
|
|
result = runner.invoke(cli, ["--log-file", "", "whoami"])
|
|
assert result.exit_code == 0, result.output
|
|
data = json.loads(result.output)
|
|
assert data["authsome_version"] == "1.2.3"
|
|
assert data["agent"] == "steady-wisely-boldly-0042"
|
|
assert "profile" not in data
|
|
assert data["principal_id"] == "principal_1"
|
|
assert data["vault_id"] == "vault_default"
|
|
assert data["vault_status"] == "OK"
|
|
assert data["configured_encryption_mode"] == "auto"
|
|
assert data["effective_encryption_source"] == "keyring"
|
|
assert data["issues"] == []
|
|
|
|
def test_vault_fail_shown_in_json(self, runner, mock_client) -> None:
|
|
mock_client.whoami.return_value = _make_whoami()
|
|
mock_client.doctor.return_value = _make_ready_fail()
|
|
mock_client.list_connections.return_value = {"connections": [], "by_source": {"bundled": [], "custom": []}}
|
|
|
|
result = runner.invoke(cli, ["--log-file", "", "whoami"])
|
|
assert result.exit_code == 0
|
|
data = json.loads(result.output)
|
|
assert data["vault_status"] == "ERROR"
|
|
|
|
def test_connected_providers_counted(self, runner, mock_client) -> None:
|
|
from datetime import UTC, datetime, timedelta
|
|
|
|
future = (datetime.now(UTC) + timedelta(hours=1)).isoformat()
|
|
mock_client.whoami.return_value = _make_whoami()
|
|
mock_client.doctor.return_value = _make_ready_ok()
|
|
mock_client.list_connections.return_value = {
|
|
"connections": [
|
|
{
|
|
"name": "github",
|
|
"default_connection": "default",
|
|
"connections": [{"connection_name": "default", "status": "connected", "expires_at": future}],
|
|
}
|
|
],
|
|
"by_source": {"bundled": [], "custom": []},
|
|
}
|
|
|
|
result = runner.invoke(cli, ["--log-file", "", "whoami"])
|
|
assert result.exit_code == 0
|
|
data = json.loads(result.output)
|
|
assert data["connected_providers_count"] == 1
|
|
assert data["connected_providers"][0]["name"] == "github"
|
|
|
|
def test_connections_failure_keeps_whoami_usable(self, runner, mock_client) -> None:
|
|
mock_client.whoami.return_value = _make_whoami()
|
|
mock_client.doctor.return_value = _make_ready_fail()
|
|
mock_client.list_connections.side_effect = Exception("EncryptionUnavailableError: Decryption failed: ")
|
|
|
|
result = runner.invoke(cli, ["--log-file", "", "whoami"])
|
|
|
|
assert result.exit_code == 0
|
|
data = json.loads(result.output)
|
|
assert data["agent"] == "steady-wisely-boldly-0042"
|
|
assert "profile" not in data
|
|
assert data["vault_status"] == "ERROR"
|
|
assert data["connected_providers_count"] == 0
|
|
assert any("connections:" in issue for issue in data["issues"])
|