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