Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_object_service_keys.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_object_service_keys.py · 6.0 KB · 153 lines
1"""Tests for per-user service API keys: storage module and identity routes."""
2
3import json
4
5import pytest
6
7import object_server
8import object_service_keys
9
10from test_object_server import (
11 auth_headers,
12 create_identity_session,
13 enable_admin_token,
14 request,
15)
16
17
18def test_set_list_and_remove_service_keys(tmp_path):
19 result = object_service_keys.set_service_key("dan", "anthropic", "sk-test-123", base_dir=tmp_path)
20 assert result["operation"] == "created"
21
22 replaced = object_service_keys.set_service_key("dan", "anthropic", "sk-test-456", base_dir=tmp_path)
23 assert replaced["operation"] == "replaced"
24 object_service_keys.set_service_key("dan", "openai", "sk-other", base_dir=tmp_path)
25
26 statuses = object_service_keys.list_service_key_status("dan", base_dir=tmp_path)
27 assert [status["service"] for status in statuses] == ["anthropic", "openai"]
28 assert all("key" not in status for status in statuses)
29
30 assert object_service_keys.get_service_key("dan", "anthropic", base_dir=tmp_path) == "sk-test-456"
31 assert object_service_keys.get_service_key("dan", "missing", base_dir=tmp_path) is None
32 assert object_service_keys.get_service_key("other", "anthropic", base_dir=tmp_path) is None
33
34 assert object_service_keys.remove_service_key("dan", "openai", base_dir=tmp_path) is True
35 assert object_service_keys.remove_service_key("dan", "openai", base_dir=tmp_path) is False
36 assert object_service_keys.remove_all_service_keys("dan", base_dir=tmp_path) == 1
37
38
39def test_service_keys_file_is_owner_only(tmp_path):
40 object_service_keys.set_service_key("dan", "anthropic", "sk-secret", base_dir=tmp_path)
41 path = object_service_keys.service_keys_path(tmp_path)
42 assert path.stat().st_mode & 0o777 == 0o600
43
44
45@pytest.mark.parametrize(
46 ("service", "key"),
47 [
48 ("Bad Service", "sk-x"),
49 ("", "sk-x"),
50 ("anthropic", ""),
51 ("anthropic", "has\ttab"),
52 ("anthropic", "x" * 5000),
53 ],
54)
55def test_invalid_service_key_payloads_rejected(tmp_path, service, key):
56 with pytest.raises(object_service_keys.InvalidServiceKeyError):
57 object_service_keys.set_service_key("dan", service, key, base_dir=tmp_path)
58
59
60def test_is_reserved_service_marks_platform_prefixes():
61 assert object_service_keys.is_reserved_service("sys-mailbox-abc") is True
62 assert object_service_keys.is_reserved_service("sys-anything") is True
63 # a user's own BYO key is never reserved
64 assert object_service_keys.is_reserved_service("openai") is False
65 assert object_service_keys.is_reserved_service("anthropic") is False
66 assert object_service_keys.is_reserved_service("") is False
67 assert object_service_keys.is_reserved_service(None) is False
68
69
70def test_reserved_service_names_are_server_side_only(tmp_path, monkeypatch):
71 data_dir = tmp_path / "data"
72 monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir))
73 enable_admin_token(monkeypatch)
74 token, _ = create_identity_session({"user_id": "dan"})
75 bearer = [("authorization", f"Bearer {token}")]
76
77 # A user cannot SET a platform-owned (reserved) secret via the self-service
78 # endpoint -- so they can never overwrite one the platform provisioned.
79 status, _, denied = request(
80 "/identity/users/dan/service-keys",
81 method="PUT",
82 body=json.dumps({"service": "sys-mailbox-1", "key": "hunter2"}).encode(),
83 headers=bearer + [("content-type", "application/json")],
84 )
85 assert status == 403 and "reserved" in denied["error"].lower()
86
87 # Nor DELETE one.
88 status, _, denied = request(
89 "/identity/users/dan/service-keys/sys-mailbox-1",
90 method="DELETE",
91 headers=bearer,
92 )
93 assert status == 403
94
95 # But in-process server code (a connector/provisioning verb) still may --
96 # the restriction lives only at the HTTP boundary, and the value round-trips
97 # for the server to use, while the endpoint never exposes it.
98 object_service_keys.set_service_key("dan", "sys-mailbox-1", "provisioned-pw", base_dir=data_dir)
99 assert object_service_keys.get_service_key("dan", "sys-mailbox-1", base_dir=data_dir) == "provisioned-pw"
100
101
102def test_service_key_routes_are_self_service(tmp_path, monkeypatch):
103 data_dir = tmp_path / "data"
104 monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir))
105 enable_admin_token(monkeypatch)
106 token, _ = create_identity_session({"user_id": "dan"})
107
108 bearer = [("authorization", f"Bearer {token}")]
109
110 status, _, set_response = request(
111 "/identity/users/dan/service-keys",
112 method="PUT",
113 body=json.dumps({"service": "anthropic", "key": "sk-live-1"}).encode(),
114 headers=bearer + [("content-type", "application/json")],
115 )
116 assert status == 200
117 assert set_response["operation"] == "created"
118 assert "key" not in set_response and "sk-live-1" not in json.dumps(set_response)
119
120 status, _, listed = request("/identity/users/dan/service-keys", headers=bearer)
121 assert status == 200
122 assert [item["service"] for item in listed["services"]] == ["anthropic"]
123 assert "sk-live-1" not in json.dumps(listed)
124
125 # Another user cannot touch dan's keys without the admin gate.
126 other_token, _ = create_identity_session({"user_id": "mallory"})
127 status, _, denied = request(
128 "/identity/users/dan/service-keys",
129 headers=[("authorization", f"Bearer {other_token}")],
130 )
131 assert status == 401
132
133 # No auth at all is denied too.
134 status, _, _ = request("/identity/users/dan/service-keys")
135 assert status == 401
136
137 # Admin token can manage on behalf of a user.
138 status, _, _ = request("/identity/users/dan/service-keys", headers=auth_headers())
139 assert status == 200
140
141 status, _, deleted = request(
142 "/identity/users/dan/service-keys/anthropic",
143 method="DELETE",
144 headers=bearer,
145 )
146 assert status == 200 and deleted["deleted"] is True
147
148 status, _, _ = request(
149 "/identity/users/dan/service-keys/anthropic",
150 method="DELETE",
151 headers=bearer,
152 )
153 assert status == 404