Debian Codegraph @

tests/api_resources/beta/organization/test_api_keys.py

raw
1 from __future__ import annotations
2
3 import os
4 from typing import Any, cast
5
6 import pytest
7
8 from anthropic import Anthropic, AsyncAnthropic
9 from tests.utils import assert_matches_type
10 from anthropic.pagination import SyncPage, AsyncPage
11 from anthropic.types.beta.organization import BetaAPIKey
12
13 base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
14
15
16 class TestAPIKeys:
17 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
18
19 @parametrize
20 def test_method_retrieve(self, client: Anthropic) -> None:
21 api_key = client.beta.organization.api_keys.retrieve(
22 "api_key_id",
23 )
24 assert_matches_type(BetaAPIKey, api_key, path=["response"])
25
26 @parametrize
27 def test_raw_response_retrieve(self, client: Anthropic) -> None:
28 response = client.beta.organization.api_keys.with_raw_response.retrieve(
29 "api_key_id",
30 )
31
32 assert response.is_closed is True
33 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
34 api_key = response.parse()
35 assert_matches_type(BetaAPIKey, api_key, path=["response"])
36
37 @parametrize
38 def test_streaming_response_retrieve(self, client: Anthropic) -> None:
39 with client.beta.organization.api_keys.with_streaming_response.retrieve(
40 "api_key_id",
41 ) as response:
42 assert not response.is_closed
43 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
44
45 api_key = response.parse()
46 assert_matches_type(BetaAPIKey, api_key, path=["response"])
47
48 assert cast(Any, response.is_closed) is True
49
50 @parametrize
51 def test_path_params_retrieve(self, client: Anthropic) -> None:
52 with pytest.raises(ValueError, match=r"Expected a non-empty value for `api_key_id` but received ''"):
53 client.beta.organization.api_keys.with_raw_response.retrieve(
54 "",
55 )
56
57 @parametrize
58 def test_method_update(self, client: Anthropic) -> None:
59 api_key = client.beta.organization.api_keys.update(
60 api_key_id="api_key_id",
61 )
62 assert_matches_type(BetaAPIKey, api_key, path=["response"])
63
64 @parametrize
65 def test_method_update_with_all_params(self, client: Anthropic) -> None:
66 api_key = client.beta.organization.api_keys.update(
67 api_key_id="api_key_id",
68 name="x",
69 status="active",
70 )
71 assert_matches_type(BetaAPIKey, api_key, path=["response"])
72
73 @parametrize
74 def test_raw_response_update(self, client: Anthropic) -> None:
75 response = client.beta.organization.api_keys.with_raw_response.update(
76 api_key_id="api_key_id",
77 )
78
79 assert response.is_closed is True
80 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
81 api_key = response.parse()
82 assert_matches_type(BetaAPIKey, api_key, path=["response"])
83
84 @parametrize
85 def test_streaming_response_update(self, client: Anthropic) -> None:
86 with client.beta.organization.api_keys.with_streaming_response.update(
87 api_key_id="api_key_id",
88 ) as response:
89 assert not response.is_closed
90 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
91
92 api_key = response.parse()
93 assert_matches_type(BetaAPIKey, api_key, path=["response"])
94
95 assert cast(Any, response.is_closed) is True
96
97 @parametrize
98 def test_path_params_update(self, client: Anthropic) -> None:
99 with pytest.raises(ValueError, match=r"Expected a non-empty value for `api_key_id` but received ''"):
100 client.beta.organization.api_keys.with_raw_response.update(
101 api_key_id="",
102 )
103
104 @parametrize
105 def test_method_list(self, client: Anthropic) -> None:
106 api_key = client.beta.organization.api_keys.list()
107 assert_matches_type(SyncPage[BetaAPIKey], api_key, path=["response"])
108
109 @parametrize
110 def test_method_list_with_all_params(self, client: Anthropic) -> None:
111 api_key = client.beta.organization.api_keys.list(
112 after_id="after_id",
113 before_id="before_id",
114 created_by_user_id="created_by_user_id",
115 limit=1,
116 status="active",
117 workspace_id="workspace_id",
118 )
119 assert_matches_type(SyncPage[BetaAPIKey], api_key, path=["response"])
120
121 @parametrize
122 def test_raw_response_list(self, client: Anthropic) -> None:
123 response = client.beta.organization.api_keys.with_raw_response.list()
124
125 assert response.is_closed is True
126 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
127 api_key = response.parse()
128 assert_matches_type(SyncPage[BetaAPIKey], api_key, path=["response"])
129
130 @parametrize
131 def test_streaming_response_list(self, client: Anthropic) -> None:
132 with client.beta.organization.api_keys.with_streaming_response.list() as response:
133 assert not response.is_closed
134 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
135
136 api_key = response.parse()
137 assert_matches_type(SyncPage[BetaAPIKey], api_key, path=["response"])
138
139 assert cast(Any, response.is_closed) is True
140
141
142 class TestAsyncAPIKeys:
143 parametrize = pytest.mark.parametrize(
144 "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
145 )
146
147 @parametrize
148 async def test_method_retrieve(self, async_client: AsyncAnthropic) -> None:
149 api_key = await async_client.beta.organization.api_keys.retrieve(
150 "api_key_id",
151 )
152 assert_matches_type(BetaAPIKey, api_key, path=["response"])
153
154 @parametrize
155 async def test_raw_response_retrieve(self, async_client: AsyncAnthropic) -> None:
156 response = await async_client.beta.organization.api_keys.with_raw_response.retrieve(
157 "api_key_id",
158 )
159
160 assert response.is_closed is True
161 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
162 api_key = await response.parse()
163 assert_matches_type(BetaAPIKey, api_key, path=["response"])
164
165 @parametrize
166 async def test_streaming_response_retrieve(self, async_client: AsyncAnthropic) -> None:
167 async with async_client.beta.organization.api_keys.with_streaming_response.retrieve(
168 "api_key_id",
169 ) as response:
170 assert not response.is_closed
171 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
172
173 api_key = await response.parse()
174 assert_matches_type(BetaAPIKey, api_key, path=["response"])
175
176 assert cast(Any, response.is_closed) is True
177
178 @parametrize
179 async def test_path_params_retrieve(self, async_client: AsyncAnthropic) -> None:
180 with pytest.raises(ValueError, match=r"Expected a non-empty value for `api_key_id` but received ''"):
181 await async_client.beta.organization.api_keys.with_raw_response.retrieve(
182 "",
183 )
184
185 @parametrize
186 async def test_method_update(self, async_client: AsyncAnthropic) -> None:
187 api_key = await async_client.beta.organization.api_keys.update(
188 api_key_id="api_key_id",
189 )
190 assert_matches_type(BetaAPIKey, api_key, path=["response"])
191
192 @parametrize
193 async def test_method_update_with_all_params(self, async_client: AsyncAnthropic) -> None:
194 api_key = await async_client.beta.organization.api_keys.update(
195 api_key_id="api_key_id",
196 name="x",
197 status="active",
198 )
199 assert_matches_type(BetaAPIKey, api_key, path=["response"])
200
201 @parametrize
202 async def test_raw_response_update(self, async_client: AsyncAnthropic) -> None:
203 response = await async_client.beta.organization.api_keys.with_raw_response.update(
204 api_key_id="api_key_id",
205 )
206
207 assert response.is_closed is True
208 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
209 api_key = await response.parse()
210 assert_matches_type(BetaAPIKey, api_key, path=["response"])
211
212 @parametrize
213 async def test_streaming_response_update(self, async_client: AsyncAnthropic) -> None:
214 async with async_client.beta.organization.api_keys.with_streaming_response.update(
215 api_key_id="api_key_id",
216 ) as response:
217 assert not response.is_closed
218 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
219
220 api_key = await response.parse()
221 assert_matches_type(BetaAPIKey, api_key, path=["response"])
222
223 assert cast(Any, response.is_closed) is True
224
225 @parametrize
226 async def test_path_params_update(self, async_client: AsyncAnthropic) -> None:
227 with pytest.raises(ValueError, match=r"Expected a non-empty value for `api_key_id` but received ''"):
228 await async_client.beta.organization.api_keys.with_raw_response.update(
229 api_key_id="",
230 )
231
232 @parametrize
233 async def test_method_list(self, async_client: AsyncAnthropic) -> None:
234 api_key = await async_client.beta.organization.api_keys.list()
235 assert_matches_type(AsyncPage[BetaAPIKey], api_key, path=["response"])
236
237 @parametrize
238 async def test_method_list_with_all_params(self, async_client: AsyncAnthropic) -> None:
239 api_key = await async_client.beta.organization.api_keys.list(
240 after_id="after_id",
241 before_id="before_id",
242 created_by_user_id="created_by_user_id",
243 limit=1,
244 status="active",
245 workspace_id="workspace_id",
246 )
247 assert_matches_type(AsyncPage[BetaAPIKey], api_key, path=["response"])
248
249 @parametrize
250 async def test_raw_response_list(self, async_client: AsyncAnthropic) -> None:
251 response = await async_client.beta.organization.api_keys.with_raw_response.list()
252
253 assert response.is_closed is True
254 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
255 api_key = await response.parse()
256 assert_matches_type(AsyncPage[BetaAPIKey], api_key, path=["response"])
257
258 @parametrize
259 async def test_streaming_response_list(self, async_client: AsyncAnthropic) -> None:
260 async with async_client.beta.organization.api_keys.with_streaming_response.list() as response:
261 assert not response.is_closed
262 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
263
264 api_key = await response.parse()
265 assert_matches_type(AsyncPage[BetaAPIKey], api_key, path=["response"])
266
267 assert cast(Any, response.is_closed) is True