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