-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathtest_cloud_api_client_and_utils.py
More file actions
338 lines (276 loc) · 11.6 KB
/
test_cloud_api_client_and_utils.py
File metadata and controls
338 lines (276 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
from contextlib import asynccontextmanager
import json
import httpx
import pytest
from basic_memory.cli.auth import CLIAuth
from basic_memory.cli.commands.cloud.api_client import (
SubscriptionRequiredError,
make_api_request,
)
from basic_memory.cli.commands.cloud.cloud_utils import (
CloudUtilsError,
create_cloud_project,
fetch_cloud_projects,
project_exists,
)
from basic_memory.config import ProjectMode
@pytest.mark.asyncio
async def test_make_api_request_success_injects_auth_and_accept_encoding(
config_home, config_manager
):
# Arrange: create a token on disk so CLIAuth can authenticate without any network.
auth = CLIAuth(client_id="cid", authkit_domain="https://auth.example.test")
auth.token_file.parent.mkdir(parents=True, exist_ok=True)
auth.token_file.write_text(
'{"access_token":"token-123","refresh_token":null,"expires_at":9999999999,"token_type":"Bearer"}',
encoding="utf-8",
)
async def handler(request: httpx.Request) -> httpx.Response:
assert request.headers.get("authorization") == "Bearer token-123"
assert request.headers.get("accept-encoding") == "identity"
return httpx.Response(200, json={"ok": True})
transport = httpx.MockTransport(handler)
@asynccontextmanager
async def http_client_factory():
async with httpx.AsyncClient(transport=transport) as client:
yield client
# Act
resp = await make_api_request(
method="GET",
url="https://cloud.example.test/proxy/health",
auth=auth,
http_client_factory=http_client_factory,
)
# Assert
assert resp.json()["ok"] is True
@pytest.mark.asyncio
async def test_make_api_request_raises_subscription_required(config_home, config_manager):
auth = CLIAuth(client_id="cid", authkit_domain="https://auth.example.test")
auth.token_file.parent.mkdir(parents=True, exist_ok=True)
auth.token_file.write_text(
'{"access_token":"token-123","refresh_token":null,"expires_at":9999999999,"token_type":"Bearer"}',
encoding="utf-8",
)
async def handler(_request: httpx.Request) -> httpx.Response:
return httpx.Response(
403,
json={
"detail": {
"error": "subscription_required",
"message": "Need subscription",
"subscribe_url": "https://example.test/subscribe",
}
},
)
transport = httpx.MockTransport(handler)
@asynccontextmanager
async def http_client_factory():
async with httpx.AsyncClient(transport=transport) as client:
yield client
with pytest.raises(SubscriptionRequiredError) as exc:
await make_api_request(
method="GET",
url="https://cloud.example.test/proxy/health",
auth=auth,
http_client_factory=http_client_factory,
)
assert exc.value.subscribe_url == "https://example.test/subscribe"
@pytest.mark.asyncio
async def test_cloud_utils_fetch_and_exists_and_create_project(
config_home, config_manager, monkeypatch
):
# Point config.cloud_host at our mocked base URL
config = config_manager.load_config()
config.cloud_host = "https://cloud.example.test"
config_manager.save_config(config)
auth = CLIAuth(client_id="cid", authkit_domain="https://auth.example.test")
auth.token_file.parent.mkdir(parents=True, exist_ok=True)
auth.token_file.write_text(
'{"access_token":"token-123","refresh_token":null,"expires_at":9999999999,"token_type":"Bearer"}',
encoding="utf-8",
)
seen = {"create_payload": None}
async def handler(request: httpx.Request) -> httpx.Response:
if request.method == "GET" and request.url.path == "/proxy/v2/projects/":
return httpx.Response(
200,
json={
"projects": [
{"id": 1, "name": "alpha", "path": "alpha", "is_default": True},
{"id": 2, "name": "beta", "path": "beta", "is_default": False},
]
},
)
if request.method == "POST" and request.url.path == "/proxy/v2/projects/":
# httpx.Request doesn't have .json(); parse bytes payload.
seen["create_payload"] = json.loads(request.content.decode("utf-8"))
return httpx.Response(
200,
json={
"message": "created",
"status": "success",
"default": False,
"old_project": None,
"new_project": {
"name": seen["create_payload"]["name"],
"path": seen["create_payload"]["path"],
},
},
)
raise AssertionError(f"Unexpected request: {request.method} {request.url}")
transport = httpx.MockTransport(handler)
@asynccontextmanager
async def http_client_factory():
async with httpx.AsyncClient(
transport=transport, base_url="https://cloud.example.test"
) as client:
yield client
async def api_request(**kwargs):
return await make_api_request(auth=auth, http_client_factory=http_client_factory, **kwargs)
projects = await fetch_cloud_projects(api_request=api_request)
assert [p.name for p in projects.projects] == ["alpha", "beta"]
assert await project_exists("alpha", api_request=api_request) is True
assert await project_exists("missing", api_request=api_request) is False
created = await create_cloud_project("My Project", api_request=api_request)
assert created.new_project is not None
assert created.new_project["name"] == "My Project"
# Path should be permalink-like (kebab)
assert seen["create_payload"]["path"] == "my-project"
assert seen["create_payload"]["visibility"] == "workspace"
@pytest.mark.asyncio
async def test_create_cloud_project_accepts_visibility_override(config_home, config_manager):
"""Shared cloud helper should pass explicit visibility through to the API payload."""
config = config_manager.load_config()
config.cloud_host = "https://cloud.example.test"
config_manager.save_config(config)
seen_payload: dict | None = None
async def api_request(**kwargs):
nonlocal seen_payload
seen_payload = kwargs["json_data"]
return httpx.Response(
200,
json={
"message": "created",
"status": "success",
"default": False,
"old_project": None,
"new_project": {"name": "shared-project", "path": "shared-project"},
},
)
created = await create_cloud_project(
"Shared Project",
visibility="shared",
api_request=api_request,
)
assert created.new_project is not None
assert seen_payload == {
"name": "Shared Project",
"path": "shared-project",
"set_default": False,
"visibility": "shared",
}
@pytest.mark.asyncio
async def test_cloud_utils_use_configured_workspace_headers(config_home, config_manager):
"""Workspace-aware cloud helpers should prefer project workspace over global default."""
config = config_manager.load_config()
config.cloud_host = "https://cloud.example.test"
config.default_workspace = "default-workspace"
config.set_project_mode("alpha", ProjectMode.CLOUD)
config.projects["alpha"].workspace_id = "project-workspace"
config_manager.save_config(config)
seen: list[tuple[str, str | None]] = []
async def api_request(**kwargs):
seen.append(
(
kwargs["method"],
(kwargs.get("headers") or {}).get("X-Workspace-ID"),
)
)
if kwargs["method"] == "GET":
return httpx.Response(
200,
json={
"projects": [{"id": 1, "name": "alpha", "path": "alpha", "is_default": True}]
},
)
return httpx.Response(
200,
json={
"message": "created",
"status": "success",
"default": False,
"old_project": None,
"new_project": {"name": "alpha", "path": "alpha"},
},
)
assert await project_exists("alpha", api_request=api_request) is True
await create_cloud_project("alpha", api_request=api_request)
await fetch_cloud_projects(project_name="missing", api_request=api_request)
assert seen == [
("GET", "project-workspace"),
("POST", "project-workspace"),
("GET", "default-workspace"),
]
@pytest.mark.asyncio
async def test_project_exists_surfaces_cloud_lookup_failures(config_home, config_manager):
"""project_exists should surface lookup failures instead of pretending the project is missing."""
config = config_manager.load_config()
config.cloud_host = "https://cloud.example.test"
config_manager.save_config(config)
async def api_request(**_kwargs):
raise httpx.ConnectError("boom")
with pytest.raises(CloudUtilsError, match="Failed to fetch cloud projects"):
await project_exists("alpha", api_request=api_request)
@pytest.mark.asyncio
async def test_make_api_request_prefers_api_key_over_oauth(config_home, config_manager):
"""API key in config should be used without needing an OAuth token on disk."""
# Arrange: set an API key in config, no OAuth token on disk
config = config_manager.load_config()
config.cloud_api_key = "bmc_test_key_12345"
config_manager.save_config(config)
async def handler(request: httpx.Request) -> httpx.Response:
# Verify the API key is sent as the Bearer token
assert request.headers.get("authorization") == "Bearer bmc_test_key_12345"
return httpx.Response(200, json={"ok": True})
transport = httpx.MockTransport(handler)
@asynccontextmanager
async def http_client_factory():
async with httpx.AsyncClient(transport=transport) as client:
yield client
# Act — no auth= parameter, no OAuth token file; should use API key from config
resp = await make_api_request(
method="GET",
url="https://cloud.example.test/proxy/health",
http_client_factory=http_client_factory,
)
# Assert
assert resp.json()["ok"] is True
@pytest.mark.asyncio
async def test_make_api_request_falls_back_to_oauth_when_no_api_key(config_home, config_manager):
"""When no API key is configured, should fall back to OAuth token."""
# Arrange: no API key, but OAuth token on disk
config = config_manager.load_config()
config.cloud_api_key = None
config_manager.save_config(config)
auth = CLIAuth(client_id="cid", authkit_domain="https://auth.example.test")
auth.token_file.parent.mkdir(parents=True, exist_ok=True)
auth.token_file.write_text(
'{"access_token":"oauth-token-456","refresh_token":null,'
'"expires_at":9999999999,"token_type":"Bearer"}',
encoding="utf-8",
)
async def handler(request: httpx.Request) -> httpx.Response:
assert request.headers.get("authorization") == "Bearer oauth-token-456"
return httpx.Response(200, json={"ok": True})
transport = httpx.MockTransport(handler)
@asynccontextmanager
async def http_client_factory():
async with httpx.AsyncClient(transport=transport) as client:
yield client
resp = await make_api_request(
method="GET",
url="https://cloud.example.test/proxy/health",
auth=auth,
http_client_factory=http_client_factory,
)
assert resp.json()["ok"] is True