|
| 1 | +"""Tests for cloud status command.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import time |
| 6 | + |
| 7 | +import httpx |
| 8 | +import pytest |
| 9 | +from typer.testing import CliRunner |
| 10 | + |
| 11 | +from basic_memory.cli.app import app |
| 12 | +from basic_memory.cli.commands.cloud.api_client import CloudAPIError |
| 13 | + |
| 14 | + |
| 15 | +# --- status command integration tests --- |
| 16 | + |
| 17 | + |
| 18 | +class _FakeTokens: |
| 19 | + """Provides canned token data for CLIAuth stubs.""" |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def valid(cls) -> dict: |
| 23 | + return { |
| 24 | + "access_token": "fake-access-token", |
| 25 | + "refresh_token": "rt_test", |
| 26 | + "expires_at": int(time.time()) + 3600, |
| 27 | + } |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def expired(cls) -> dict: |
| 31 | + return { |
| 32 | + "access_token": "fake-access-token", |
| 33 | + "refresh_token": "rt_test", |
| 34 | + "expires_at": int(time.time()) - 3600, |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | +def _patch_status_deps(monkeypatch, *, tokens=None, api_side_effect=None): |
| 39 | + """Patch ConfigManager and CLIAuth for the status command.""" |
| 40 | + |
| 41 | + class FakeConfig: |
| 42 | + cloud_client_id = "cid" |
| 43 | + cloud_domain = "https://auth.example.com" |
| 44 | + cloud_host = "https://cloud.example.com" |
| 45 | + cloud_api_key = "bmc_test123" |
| 46 | + |
| 47 | + class FakeConfigManager: |
| 48 | + config = FakeConfig() |
| 49 | + |
| 50 | + def load_config(self): |
| 51 | + return self.config |
| 52 | + |
| 53 | + class FakeAuth: |
| 54 | + def __init__(self, **_kwargs): |
| 55 | + pass |
| 56 | + |
| 57 | + def load_tokens(self): |
| 58 | + return tokens |
| 59 | + |
| 60 | + def is_token_valid(self, t): |
| 61 | + return t.get("expires_at", 0) > time.time() |
| 62 | + |
| 63 | + monkeypatch.setattr( |
| 64 | + "basic_memory.cli.commands.cloud.core_commands.ConfigManager", FakeConfigManager |
| 65 | + ) |
| 66 | + monkeypatch.setattr( |
| 67 | + "basic_memory.cli.commands.cloud.core_commands.CLIAuth", FakeAuth |
| 68 | + ) |
| 69 | + monkeypatch.setattr( |
| 70 | + "basic_memory.cli.commands.cloud.core_commands.get_cloud_config", |
| 71 | + lambda: ("cid", "domain", "https://cloud.example.com"), |
| 72 | + ) |
| 73 | + |
| 74 | + if api_side_effect is None: |
| 75 | + # Default: cloud is reachable |
| 76 | + async def _ok(*_a, **_kw): |
| 77 | + return httpx.Response(200, json={"status": "ok"}) |
| 78 | + |
| 79 | + api_side_effect = _ok |
| 80 | + |
| 81 | + monkeypatch.setattr( |
| 82 | + "basic_memory.cli.commands.cloud.core_commands.make_api_request", api_side_effect |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +class TestStatusCommand: |
| 87 | + def test_status_connected(self, monkeypatch): |
| 88 | + _patch_status_deps(monkeypatch, tokens=_FakeTokens.valid()) |
| 89 | + runner = CliRunner() |
| 90 | + result = runner.invoke(app, ["cloud", "status"]) |
| 91 | + |
| 92 | + assert result.exit_code == 0 |
| 93 | + assert "Cloud Status" in result.stdout |
| 94 | + assert "cloud.example.com" in result.stdout |
| 95 | + assert "token valid" in result.stdout |
| 96 | + assert "Cloud connected" in result.stdout |
| 97 | + |
| 98 | + def test_status_expired_token(self, monkeypatch): |
| 99 | + _patch_status_deps(monkeypatch, tokens=_FakeTokens.expired()) |
| 100 | + runner = CliRunner() |
| 101 | + result = runner.invoke(app, ["cloud", "status"]) |
| 102 | + |
| 103 | + assert result.exit_code == 0 |
| 104 | + assert "token expired" in result.stdout |
| 105 | + |
| 106 | + def test_status_no_credentials(self, monkeypatch): |
| 107 | + _patch_status_deps(monkeypatch, tokens=None) |
| 108 | + |
| 109 | + # Also clear the API key so there are no credentials at all |
| 110 | + class FakeConfig: |
| 111 | + cloud_client_id = "cid" |
| 112 | + cloud_domain = "https://auth.example.com" |
| 113 | + cloud_host = "https://cloud.example.com" |
| 114 | + cloud_api_key = "" |
| 115 | + |
| 116 | + class FakeConfigManager: |
| 117 | + config = FakeConfig() |
| 118 | + |
| 119 | + def load_config(self): |
| 120 | + return self.config |
| 121 | + |
| 122 | + monkeypatch.setattr( |
| 123 | + "basic_memory.cli.commands.cloud.core_commands.ConfigManager", FakeConfigManager |
| 124 | + ) |
| 125 | + |
| 126 | + runner = CliRunner() |
| 127 | + result = runner.invoke(app, ["cloud", "status"]) |
| 128 | + |
| 129 | + assert result.exit_code == 0 |
| 130 | + assert "No cloud credentials found" in result.stdout |
| 131 | + |
| 132 | + @pytest.mark.parametrize( |
| 133 | + "exc", |
| 134 | + [ |
| 135 | + CloudAPIError("connection refused"), |
| 136 | + Exception("network timeout"), |
| 137 | + ], |
| 138 | + ) |
| 139 | + def test_status_cloud_not_connected(self, monkeypatch, exc): |
| 140 | + async def _fail(*_a, **_kw): |
| 141 | + raise exc |
| 142 | + |
| 143 | + _patch_status_deps(monkeypatch, tokens=_FakeTokens.valid(), api_side_effect=_fail) |
| 144 | + runner = CliRunner() |
| 145 | + result = runner.invoke(app, ["cloud", "status"]) |
| 146 | + |
| 147 | + assert result.exit_code == 0 |
| 148 | + assert "Cloud not connected" in result.stdout |
0 commit comments