openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dev/hayden/bedrock-aws-auth

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/api_resources/admin/organization/test_admin_api_keys.py

327lines · modecode

1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3from __future__ import annotations
4
5import os
6from typing import Any, cast
7
8import pytest
9
10from openai import OpenAI, AsyncOpenAI
11from tests.utils import assert_matches_type
12from openai.pagination import SyncCursorPage, AsyncCursorPage
13from openai.types.admin.organization import (
14 AdminAPIKey,
15 AdminAPIKeyCreateResponse,
16 AdminAPIKeyDeleteResponse,
17)
18
19base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
20
21
22class TestAdminAPIKeys:
23 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
24
25 @parametrize
26 def test_method_create(self, client: OpenAI) -> None:
27 admin_api_key = client.admin.organization.admin_api_keys.create(
28 name="New Admin Key",
29 )
30 assert_matches_type(AdminAPIKeyCreateResponse, admin_api_key, path=["response"])
31
32 @parametrize
33 def test_method_create_with_all_params(self, client: OpenAI) -> None:
34 admin_api_key = client.admin.organization.admin_api_keys.create(
35 name="New Admin Key",
36 expires_in_seconds=2592000,
37 )
38 assert_matches_type(AdminAPIKeyCreateResponse, admin_api_key, path=["response"])
39
40 @parametrize
41 def test_raw_response_create(self, client: OpenAI) -> None:
42 response = client.admin.organization.admin_api_keys.with_raw_response.create(
43 name="New Admin Key",
44 )
45
46 assert response.is_closed is True
47 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
48 admin_api_key = response.parse()
49 assert_matches_type(AdminAPIKeyCreateResponse, admin_api_key, path=["response"])
50
51 @parametrize
52 def test_streaming_response_create(self, client: OpenAI) -> None:
53 with client.admin.organization.admin_api_keys.with_streaming_response.create(
54 name="New Admin Key",
55 ) as response:
56 assert not response.is_closed
57 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
58
59 admin_api_key = response.parse()
60 assert_matches_type(AdminAPIKeyCreateResponse, admin_api_key, path=["response"])
61
62 assert cast(Any, response.is_closed) is True
63
64 @parametrize
65 def test_method_retrieve(self, client: OpenAI) -> None:
66 admin_api_key = client.admin.organization.admin_api_keys.retrieve(
67 "key_id",
68 )
69 assert_matches_type(AdminAPIKey, admin_api_key, path=["response"])
70
71 @parametrize
72 def test_raw_response_retrieve(self, client: OpenAI) -> None:
73 response = client.admin.organization.admin_api_keys.with_raw_response.retrieve(
74 "key_id",
75 )
76
77 assert response.is_closed is True
78 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
79 admin_api_key = response.parse()
80 assert_matches_type(AdminAPIKey, admin_api_key, path=["response"])
81
82 @parametrize
83 def test_streaming_response_retrieve(self, client: OpenAI) -> None:
84 with client.admin.organization.admin_api_keys.with_streaming_response.retrieve(
85 "key_id",
86 ) as response:
87 assert not response.is_closed
88 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
89
90 admin_api_key = response.parse()
91 assert_matches_type(AdminAPIKey, admin_api_key, path=["response"])
92
93 assert cast(Any, response.is_closed) is True
94
95 @parametrize
96 def test_path_params_retrieve(self, client: OpenAI) -> None:
97 with pytest.raises(ValueError, match=r"Expected a non-empty value for `key_id` but received ''"):
98 client.admin.organization.admin_api_keys.with_raw_response.retrieve(
99 "",
100 )
101
102 @parametrize
103 def test_method_list(self, client: OpenAI) -> None:
104 admin_api_key = client.admin.organization.admin_api_keys.list()
105 assert_matches_type(SyncCursorPage[AdminAPIKey], admin_api_key, path=["response"])
106
107 @parametrize
108 def test_method_list_with_all_params(self, client: OpenAI) -> None:
109 admin_api_key = client.admin.organization.admin_api_keys.list(
110 after="after",
111 limit=0,
112 order="asc",
113 )
114 assert_matches_type(SyncCursorPage[AdminAPIKey], admin_api_key, path=["response"])
115
116 @parametrize
117 def test_raw_response_list(self, client: OpenAI) -> None:
118 response = client.admin.organization.admin_api_keys.with_raw_response.list()
119
120 assert response.is_closed is True
121 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
122 admin_api_key = response.parse()
123 assert_matches_type(SyncCursorPage[AdminAPIKey], admin_api_key, path=["response"])
124
125 @parametrize
126 def test_streaming_response_list(self, client: OpenAI) -> None:
127 with client.admin.organization.admin_api_keys.with_streaming_response.list() as response:
128 assert not response.is_closed
129 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
130
131 admin_api_key = response.parse()
132 assert_matches_type(SyncCursorPage[AdminAPIKey], admin_api_key, path=["response"])
133
134 assert cast(Any, response.is_closed) is True
135
136 @parametrize
137 def test_method_delete(self, client: OpenAI) -> None:
138 admin_api_key = client.admin.organization.admin_api_keys.delete(
139 "key_id",
140 )
141 assert_matches_type(AdminAPIKeyDeleteResponse, admin_api_key, path=["response"])
142
143 @parametrize
144 def test_raw_response_delete(self, client: OpenAI) -> None:
145 response = client.admin.organization.admin_api_keys.with_raw_response.delete(
146 "key_id",
147 )
148
149 assert response.is_closed is True
150 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
151 admin_api_key = response.parse()
152 assert_matches_type(AdminAPIKeyDeleteResponse, admin_api_key, path=["response"])
153
154 @parametrize
155 def test_streaming_response_delete(self, client: OpenAI) -> None:
156 with client.admin.organization.admin_api_keys.with_streaming_response.delete(
157 "key_id",
158 ) as response:
159 assert not response.is_closed
160 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
161
162 admin_api_key = response.parse()
163 assert_matches_type(AdminAPIKeyDeleteResponse, admin_api_key, path=["response"])
164
165 assert cast(Any, response.is_closed) is True
166
167 @parametrize
168 def test_path_params_delete(self, client: OpenAI) -> None:
169 with pytest.raises(ValueError, match=r"Expected a non-empty value for `key_id` but received ''"):
170 client.admin.organization.admin_api_keys.with_raw_response.delete(
171 "",
172 )
173
174
175class TestAsyncAdminAPIKeys:
176 parametrize = pytest.mark.parametrize(
177 "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
178 )
179
180 @parametrize
181 async def test_method_create(self, async_client: AsyncOpenAI) -> None:
182 admin_api_key = await async_client.admin.organization.admin_api_keys.create(
183 name="New Admin Key",
184 )
185 assert_matches_type(AdminAPIKeyCreateResponse, admin_api_key, path=["response"])
186
187 @parametrize
188 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
189 admin_api_key = await async_client.admin.organization.admin_api_keys.create(
190 name="New Admin Key",
191 expires_in_seconds=2592000,
192 )
193 assert_matches_type(AdminAPIKeyCreateResponse, admin_api_key, path=["response"])
194
195 @parametrize
196 async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
197 response = await async_client.admin.organization.admin_api_keys.with_raw_response.create(
198 name="New Admin Key",
199 )
200
201 assert response.is_closed is True
202 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
203 admin_api_key = response.parse()
204 assert_matches_type(AdminAPIKeyCreateResponse, admin_api_key, path=["response"])
205
206 @parametrize
207 async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
208 async with async_client.admin.organization.admin_api_keys.with_streaming_response.create(
209 name="New Admin Key",
210 ) as response:
211 assert not response.is_closed
212 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
213
214 admin_api_key = await response.parse()
215 assert_matches_type(AdminAPIKeyCreateResponse, admin_api_key, path=["response"])
216
217 assert cast(Any, response.is_closed) is True
218
219 @parametrize
220 async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
221 admin_api_key = await async_client.admin.organization.admin_api_keys.retrieve(
222 "key_id",
223 )
224 assert_matches_type(AdminAPIKey, admin_api_key, path=["response"])
225
226 @parametrize
227 async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
228 response = await async_client.admin.organization.admin_api_keys.with_raw_response.retrieve(
229 "key_id",
230 )
231
232 assert response.is_closed is True
233 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
234 admin_api_key = response.parse()
235 assert_matches_type(AdminAPIKey, admin_api_key, path=["response"])
236
237 @parametrize
238 async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
239 async with async_client.admin.organization.admin_api_keys.with_streaming_response.retrieve(
240 "key_id",
241 ) as response:
242 assert not response.is_closed
243 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
244
245 admin_api_key = await response.parse()
246 assert_matches_type(AdminAPIKey, admin_api_key, path=["response"])
247
248 assert cast(Any, response.is_closed) is True
249
250 @parametrize
251 async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
252 with pytest.raises(ValueError, match=r"Expected a non-empty value for `key_id` but received ''"):
253 await async_client.admin.organization.admin_api_keys.with_raw_response.retrieve(
254 "",
255 )
256
257 @parametrize
258 async def test_method_list(self, async_client: AsyncOpenAI) -> None:
259 admin_api_key = await async_client.admin.organization.admin_api_keys.list()
260 assert_matches_type(AsyncCursorPage[AdminAPIKey], admin_api_key, path=["response"])
261
262 @parametrize
263 async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
264 admin_api_key = await async_client.admin.organization.admin_api_keys.list(
265 after="after",
266 limit=0,
267 order="asc",
268 )
269 assert_matches_type(AsyncCursorPage[AdminAPIKey], admin_api_key, path=["response"])
270
271 @parametrize
272 async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
273 response = await async_client.admin.organization.admin_api_keys.with_raw_response.list()
274
275 assert response.is_closed is True
276 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
277 admin_api_key = response.parse()
278 assert_matches_type(AsyncCursorPage[AdminAPIKey], admin_api_key, path=["response"])
279
280 @parametrize
281 async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
282 async with async_client.admin.organization.admin_api_keys.with_streaming_response.list() as response:
283 assert not response.is_closed
284 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
285
286 admin_api_key = await response.parse()
287 assert_matches_type(AsyncCursorPage[AdminAPIKey], admin_api_key, path=["response"])
288
289 assert cast(Any, response.is_closed) is True
290
291 @parametrize
292 async def test_method_delete(self, async_client: AsyncOpenAI) -> None:
293 admin_api_key = await async_client.admin.organization.admin_api_keys.delete(
294 "key_id",
295 )
296 assert_matches_type(AdminAPIKeyDeleteResponse, admin_api_key, path=["response"])
297
298 @parametrize
299 async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None:
300 response = await async_client.admin.organization.admin_api_keys.with_raw_response.delete(
301 "key_id",
302 )
303
304 assert response.is_closed is True
305 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
306 admin_api_key = response.parse()
307 assert_matches_type(AdminAPIKeyDeleteResponse, admin_api_key, path=["response"])
308
309 @parametrize
310 async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None:
311 async with async_client.admin.organization.admin_api_keys.with_streaming_response.delete(
312 "key_id",
313 ) as response:
314 assert not response.is_closed
315 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
316
317 admin_api_key = await response.parse()
318 assert_matches_type(AdminAPIKeyDeleteResponse, admin_api_key, path=["response"])
319
320 assert cast(Any, response.is_closed) is True
321
322 @parametrize
323 async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None:
324 with pytest.raises(ValueError, match=r"Expected a non-empty value for `key_id` but received ''"):
325 await async_client.admin.organization.admin_api_keys.with_raw_response.delete(
326 "",
327 )
328