quantumapi 0.22.0__tar.gz → 0.22.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {quantumapi-0.22.0/quantumapi.egg-info → quantumapi-0.22.1}/PKG-INFO +1 -1
- {quantumapi-0.22.0 → quantumapi-0.22.1}/pyproject.toml +1 -1
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/__init__.py +2 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/client/base.py +7 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/client/keys.py +106 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/errors/__init__.py +2 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/errors/exceptions.py +17 -4
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/models/keys.py +28 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1/quantumapi.egg-info}/PKG-INFO +1 -1
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi.egg-info/SOURCES.txt +1 -0
- quantumapi-0.22.1/tests/test_data_keys.py +121 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/LICENSE +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/README.md +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/client/__init__.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/client/applications.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/client/async_client.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/client/audit.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/client/encryption.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/client/end_users.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/client/health.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/client/qrng.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/client/resource_groups.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/client/roles.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/client/secrets.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/client/sync_client.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/models/__init__.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/models/applications.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/models/audit.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/models/encryption.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/models/end_users.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/models/health.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/models/qrng.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/models/resource_groups.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/models/roles.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/models/secrets.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi/py.typed +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi.egg-info/dependency_links.txt +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi.egg-info/requires.txt +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/quantumapi.egg-info/top_level.txt +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/setup.cfg +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/tests/test_audit.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/tests/test_end_users.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/tests/test_exceptions.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/tests/test_models.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/tests/test_parity.py +0 -0
- {quantumapi-0.22.0 → quantumapi-0.22.1}/tests/test_qrng.py +0 -0
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "quantumapi"
|
|
7
|
-
version = "0.22.
|
|
7
|
+
version = "0.22.1"
|
|
8
8
|
description = "Official Python SDK for QuantumAPI - Quantum-safe encryption and identity platform"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.9"
|
|
@@ -18,6 +18,7 @@ from quantumapi.client.sync_client import QuantumAPISyncClient
|
|
|
18
18
|
from quantumapi.errors.exceptions import (
|
|
19
19
|
AuthenticationError,
|
|
20
20
|
ConflictError,
|
|
21
|
+
KeyRevokedError,
|
|
21
22
|
NetworkError,
|
|
22
23
|
NotFoundError,
|
|
23
24
|
PermissionError,
|
|
@@ -37,6 +38,7 @@ __all__ = [
|
|
|
37
38
|
"QuantumAPIError",
|
|
38
39
|
"AuthenticationError",
|
|
39
40
|
"PermissionError",
|
|
41
|
+
"KeyRevokedError",
|
|
40
42
|
"NotFoundError",
|
|
41
43
|
"ConflictError",
|
|
42
44
|
"RateLimitError",
|
|
@@ -10,6 +10,7 @@ from httpx import Response
|
|
|
10
10
|
from quantumapi.errors import (
|
|
11
11
|
AuthenticationError,
|
|
12
12
|
ConflictError,
|
|
13
|
+
KeyRevokedError,
|
|
13
14
|
NetworkError,
|
|
14
15
|
NotFoundError,
|
|
15
16
|
PermissionError,
|
|
@@ -129,6 +130,12 @@ class BaseClient:
|
|
|
129
130
|
status_code=401,
|
|
130
131
|
)
|
|
131
132
|
elif response.status_code == 403:
|
|
133
|
+
if error_code == "key_revoked":
|
|
134
|
+
raise KeyRevokedError(
|
|
135
|
+
message=error_message,
|
|
136
|
+
error_code=error_code,
|
|
137
|
+
details=error_details,
|
|
138
|
+
)
|
|
132
139
|
raise PermissionError(
|
|
133
140
|
message=error_message,
|
|
134
141
|
error_code=error_code,
|
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
from datetime import datetime, timezone
|
|
4
4
|
from typing import List, Optional
|
|
5
|
+
from urllib.parse import quote
|
|
5
6
|
from uuid import UUID
|
|
6
7
|
|
|
7
8
|
from quantumapi.client.base import BaseClient
|
|
8
9
|
from quantumapi.models.keys import (
|
|
10
|
+
GenerateDataKeyResponse,
|
|
9
11
|
GenerateKeyRequest,
|
|
10
12
|
ImportKeyRequest,
|
|
11
13
|
KeyHealthReport,
|
|
@@ -14,6 +16,7 @@ from quantumapi.models.keys import (
|
|
|
14
16
|
KeyType,
|
|
15
17
|
PublicKeyResponse,
|
|
16
18
|
RotateKeyRequest,
|
|
19
|
+
UnwrapKeyResponse,
|
|
17
20
|
UpdateKeyRequest,
|
|
18
21
|
)
|
|
19
22
|
|
|
@@ -329,6 +332,109 @@ class KeysClient:
|
|
|
329
332
|
)
|
|
330
333
|
return PublicKeyResponse(**response)
|
|
331
334
|
|
|
335
|
+
@staticmethod
|
|
336
|
+
def _data_key_body(
|
|
337
|
+
key_bytes: Optional[int], algorithm: Optional[str], context: Optional[str]
|
|
338
|
+
) -> dict:
|
|
339
|
+
body: dict = {}
|
|
340
|
+
if algorithm is not None:
|
|
341
|
+
body["algorithm"] = algorithm
|
|
342
|
+
if key_bytes is not None:
|
|
343
|
+
body["keyBytes"] = key_bytes
|
|
344
|
+
if context is not None:
|
|
345
|
+
body["context"] = context
|
|
346
|
+
return body
|
|
347
|
+
|
|
348
|
+
async def generate_data_key(
|
|
349
|
+
self,
|
|
350
|
+
key_id: UUID,
|
|
351
|
+
key_bytes: Optional[int] = None,
|
|
352
|
+
algorithm: Optional[str] = None,
|
|
353
|
+
context: Optional[str] = None,
|
|
354
|
+
) -> GenerateDataKeyResponse:
|
|
355
|
+
"""
|
|
356
|
+
Generate a data key for envelope encryption
|
|
357
|
+
(``POST /api/v1/keys/{key_id}/generate-data-key``).
|
|
358
|
+
|
|
359
|
+
Args:
|
|
360
|
+
key_id: Master key that wraps the data key
|
|
361
|
+
key_bytes: Data key length: 16, 24 or 32 (server default 32)
|
|
362
|
+
algorithm: Data key algorithm label (server default AES-256-GCM)
|
|
363
|
+
context: Optional base64 encryption context (AAD); pass it again on unwrap
|
|
364
|
+
|
|
365
|
+
Returns:
|
|
366
|
+
Plaintext and encrypted data key
|
|
367
|
+
"""
|
|
368
|
+
response = await self._client.post(
|
|
369
|
+
f"/api/v1/keys/{key_id}/generate-data-key",
|
|
370
|
+
json_data=self._data_key_body(key_bytes, algorithm, context),
|
|
371
|
+
)
|
|
372
|
+
return GenerateDataKeyResponse(**response)
|
|
373
|
+
|
|
374
|
+
async def generate_data_key_without_plaintext(
|
|
375
|
+
self,
|
|
376
|
+
key_id: UUID,
|
|
377
|
+
key_bytes: Optional[int] = None,
|
|
378
|
+
algorithm: Optional[str] = None,
|
|
379
|
+
context: Optional[str] = None,
|
|
380
|
+
) -> GenerateDataKeyResponse:
|
|
381
|
+
"""Like :meth:`generate_data_key` but ``plaintext_key`` is None
|
|
382
|
+
(``POST /api/v1/keys/{key_id}/generate-data-key-without-plaintext``)."""
|
|
383
|
+
response = await self._client.post(
|
|
384
|
+
f"/api/v1/keys/{key_id}/generate-data-key-without-plaintext",
|
|
385
|
+
json_data=self._data_key_body(key_bytes, algorithm, context),
|
|
386
|
+
)
|
|
387
|
+
return GenerateDataKeyResponse(**response)
|
|
388
|
+
|
|
389
|
+
async def generate_transit_data_key(
|
|
390
|
+
self,
|
|
391
|
+
key_name: str,
|
|
392
|
+
key_bytes: Optional[int] = None,
|
|
393
|
+
algorithm: Optional[str] = None,
|
|
394
|
+
context: Optional[str] = None,
|
|
395
|
+
) -> GenerateDataKeyResponse:
|
|
396
|
+
"""Generate a data key with a named Transit key
|
|
397
|
+
(``POST /api/v1/transit/datakey/{key_name}``)."""
|
|
398
|
+
response = await self._client.post(
|
|
399
|
+
f"/api/v1/transit/datakey/{quote(key_name, safe='')}",
|
|
400
|
+
json_data=self._data_key_body(key_bytes, algorithm, context),
|
|
401
|
+
)
|
|
402
|
+
return GenerateDataKeyResponse(**response)
|
|
403
|
+
|
|
404
|
+
async def generate_transit_data_key_without_plaintext(
|
|
405
|
+
self,
|
|
406
|
+
key_name: str,
|
|
407
|
+
key_bytes: Optional[int] = None,
|
|
408
|
+
algorithm: Optional[str] = None,
|
|
409
|
+
context: Optional[str] = None,
|
|
410
|
+
) -> GenerateDataKeyResponse:
|
|
411
|
+
"""Transit data key without plaintext
|
|
412
|
+
(``POST /api/v1/transit/datakey/{key_name}/without-plaintext``)."""
|
|
413
|
+
response = await self._client.post(
|
|
414
|
+
f"/api/v1/transit/datakey/{quote(key_name, safe='')}/without-plaintext",
|
|
415
|
+
json_data=self._data_key_body(key_bytes, algorithm, context),
|
|
416
|
+
)
|
|
417
|
+
return GenerateDataKeyResponse(**response)
|
|
418
|
+
|
|
419
|
+
async def unwrap(
|
|
420
|
+
self,
|
|
421
|
+
key_id: UUID,
|
|
422
|
+
wrapped_key: str,
|
|
423
|
+
context: Optional[str] = None,
|
|
424
|
+
) -> UnwrapKeyResponse:
|
|
425
|
+
"""
|
|
426
|
+
Unwrap a wrapped key, or the ``encrypted_key`` of a data key, with the master key
|
|
427
|
+
(``POST /api/v1/keys/{key_id}/unwrap``).
|
|
428
|
+
|
|
429
|
+
Raises:
|
|
430
|
+
KeyRevokedError: HTTP 403 ``key_revoked`` when the master key was revoked
|
|
431
|
+
"""
|
|
432
|
+
body = {"wrappedKey": wrapped_key}
|
|
433
|
+
if context is not None:
|
|
434
|
+
body["context"] = context
|
|
435
|
+
response = await self._client.post(f"/api/v1/keys/{key_id}/unwrap", json_data=body)
|
|
436
|
+
return UnwrapKeyResponse(**response)
|
|
437
|
+
|
|
332
438
|
async def check(self, key_id: UUID) -> KeyHealthReport:
|
|
333
439
|
"""
|
|
334
440
|
Check the health of a key.
|
|
@@ -4,6 +4,7 @@ from quantumapi.errors.exceptions import (
|
|
|
4
4
|
AuthenticationError,
|
|
5
5
|
ConflictError,
|
|
6
6
|
InvalidRequestError,
|
|
7
|
+
KeyRevokedError,
|
|
7
8
|
NetworkError,
|
|
8
9
|
NotFoundError,
|
|
9
10
|
PermissionError,
|
|
@@ -18,6 +19,7 @@ __all__ = [
|
|
|
18
19
|
"QuantumAPIError",
|
|
19
20
|
"AuthenticationError",
|
|
20
21
|
"PermissionError",
|
|
22
|
+
"KeyRevokedError",
|
|
21
23
|
"NotFoundError",
|
|
22
24
|
"ConflictError",
|
|
23
25
|
"ValidationError",
|
|
@@ -40,7 +40,8 @@ class AuthenticationError(QuantumAPIError):
|
|
|
40
40
|
|
|
41
41
|
def __init__(self, message: str = "Authentication failed", **kwargs: Any) -> None:
|
|
42
42
|
"""Initialize AuthenticationError."""
|
|
43
|
-
|
|
43
|
+
kwargs.setdefault("status_code", 401)
|
|
44
|
+
super().__init__(message, **kwargs)
|
|
44
45
|
|
|
45
46
|
|
|
46
47
|
class PermissionError(QuantumAPIError):
|
|
@@ -48,7 +49,17 @@ class PermissionError(QuantumAPIError):
|
|
|
48
49
|
|
|
49
50
|
def __init__(self, message: str = "Permission denied", **kwargs: Any) -> None:
|
|
50
51
|
"""Initialize PermissionError."""
|
|
51
|
-
|
|
52
|
+
kwargs.setdefault("status_code", 403)
|
|
53
|
+
super().__init__(message, **kwargs)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class KeyRevokedError(PermissionError):
|
|
57
|
+
"""Raised on HTTP 403 ``key_revoked``: the key used for the operation was revoked."""
|
|
58
|
+
|
|
59
|
+
def __init__(self, message: str = "Key has been revoked", **kwargs: Any) -> None:
|
|
60
|
+
"""Initialize KeyRevokedError."""
|
|
61
|
+
kwargs.setdefault("error_code", "key_revoked")
|
|
62
|
+
super().__init__(message, **kwargs)
|
|
52
63
|
|
|
53
64
|
|
|
54
65
|
class NotFoundError(QuantumAPIError):
|
|
@@ -56,7 +67,8 @@ class NotFoundError(QuantumAPIError):
|
|
|
56
67
|
|
|
57
68
|
def __init__(self, message: str = "Resource not found", **kwargs: Any) -> None:
|
|
58
69
|
"""Initialize NotFoundError."""
|
|
59
|
-
|
|
70
|
+
kwargs.setdefault("status_code", 404)
|
|
71
|
+
super().__init__(message, **kwargs)
|
|
60
72
|
|
|
61
73
|
|
|
62
74
|
class ConflictError(QuantumAPIError):
|
|
@@ -73,7 +85,8 @@ class ValidationError(QuantumAPIError):
|
|
|
73
85
|
|
|
74
86
|
def __init__(self, message: str = "Validation failed", **kwargs: Any) -> None:
|
|
75
87
|
"""Initialize ValidationError."""
|
|
76
|
-
|
|
88
|
+
kwargs.setdefault("status_code", 400)
|
|
89
|
+
super().__init__(message, **kwargs)
|
|
77
90
|
|
|
78
91
|
|
|
79
92
|
class RateLimitError(QuantumAPIError):
|
|
@@ -155,3 +155,31 @@ class KeyHealthReport(BaseModel):
|
|
|
155
155
|
healthy: bool = Field(..., description="Whether the key is healthy")
|
|
156
156
|
issues: List[str] = Field(default_factory=list, description="Critical issues found")
|
|
157
157
|
warnings: List[str] = Field(default_factory=list, description="Warnings found")
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class GenerateDataKeyResponse(BaseModel):
|
|
161
|
+
"""Response of the generate-data-key routes (envelope encryption)."""
|
|
162
|
+
|
|
163
|
+
plaintext_key: Optional[str] = Field(
|
|
164
|
+
default=None,
|
|
165
|
+
alias="plaintextKey",
|
|
166
|
+
description="Plaintext data key (base64); None for the without-plaintext variants",
|
|
167
|
+
)
|
|
168
|
+
encrypted_key: str = Field(
|
|
169
|
+
..., alias="encryptedKey", description="Data key encrypted by the master key (base64)"
|
|
170
|
+
)
|
|
171
|
+
key_id: UUID = Field(..., alias="keyId", description="Master key that wrapped the data key")
|
|
172
|
+
key_version: int = Field(..., alias="keyVersion", description="Master key version used")
|
|
173
|
+
algorithm: str = Field(..., description="Data key algorithm, e.g. AES-256-GCM")
|
|
174
|
+
|
|
175
|
+
model_config = {"populate_by_name": True}
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
class UnwrapKeyResponse(BaseModel):
|
|
179
|
+
"""Response of ``POST /api/v1/keys/{keyId}/unwrap``."""
|
|
180
|
+
|
|
181
|
+
key_material: str = Field(..., alias="keyMaterial", description="Unwrapped key material (base64)")
|
|
182
|
+
key_id: UUID = Field(..., alias="keyId", description="Wrapping key used")
|
|
183
|
+
key_version: int = Field(..., alias="keyVersion", description="Wrapping key version used")
|
|
184
|
+
|
|
185
|
+
model_config = {"populate_by_name": True}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"""Tests for data key / unwrap routes and the 403 key_revoked mapping."""
|
|
2
|
+
|
|
3
|
+
from unittest.mock import AsyncMock, MagicMock
|
|
4
|
+
from uuid import uuid4
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
from quantumapi.client.base import BaseClient
|
|
9
|
+
from quantumapi.client.keys import KeysClient
|
|
10
|
+
from quantumapi.errors import KeyRevokedError, PermissionError
|
|
11
|
+
|
|
12
|
+
KEY_ID = uuid4()
|
|
13
|
+
DATA_KEY = {
|
|
14
|
+
"plaintextKey": "cA==",
|
|
15
|
+
"encryptedKey": "ZW5j",
|
|
16
|
+
"keyId": str(KEY_ID),
|
|
17
|
+
"keyVersion": 3,
|
|
18
|
+
"algorithm": "AES-256-GCM",
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@pytest.mark.asyncio
|
|
23
|
+
async def test_generate_data_key_route_and_body():
|
|
24
|
+
base = AsyncMock()
|
|
25
|
+
base.post.return_value = DATA_KEY
|
|
26
|
+
resp = await KeysClient(base).generate_data_key(KEY_ID, key_bytes=16, context="Y3R4")
|
|
27
|
+
base.post.assert_awaited_once_with(
|
|
28
|
+
f"/api/v1/keys/{KEY_ID}/generate-data-key",
|
|
29
|
+
json_data={"keyBytes": 16, "context": "Y3R4"},
|
|
30
|
+
)
|
|
31
|
+
assert resp.plaintext_key == "cA==" and resp.encrypted_key == "ZW5j"
|
|
32
|
+
assert resp.key_id == KEY_ID and resp.key_version == 3 and resp.algorithm == "AES-256-GCM"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@pytest.mark.asyncio
|
|
36
|
+
async def test_generate_data_key_default_body_is_empty():
|
|
37
|
+
base = AsyncMock()
|
|
38
|
+
base.post.return_value = DATA_KEY
|
|
39
|
+
await KeysClient(base).generate_data_key(KEY_ID)
|
|
40
|
+
base.post.assert_awaited_once_with(f"/api/v1/keys/{KEY_ID}/generate-data-key", json_data={})
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@pytest.mark.asyncio
|
|
44
|
+
async def test_generate_data_key_without_plaintext_route():
|
|
45
|
+
base = AsyncMock()
|
|
46
|
+
base.post.return_value = {**DATA_KEY, "plaintextKey": None}
|
|
47
|
+
resp = await KeysClient(base).generate_data_key_without_plaintext(KEY_ID, algorithm="AES-256-GCM")
|
|
48
|
+
base.post.assert_awaited_once_with(
|
|
49
|
+
f"/api/v1/keys/{KEY_ID}/generate-data-key-without-plaintext",
|
|
50
|
+
json_data={"algorithm": "AES-256-GCM"},
|
|
51
|
+
)
|
|
52
|
+
assert resp.plaintext_key is None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@pytest.mark.asyncio
|
|
56
|
+
async def test_transit_data_key_routes():
|
|
57
|
+
base = AsyncMock()
|
|
58
|
+
base.post.return_value = DATA_KEY
|
|
59
|
+
client = KeysClient(base)
|
|
60
|
+
await client.generate_transit_data_key("my key", key_bytes=32)
|
|
61
|
+
base.post.assert_awaited_with("/api/v1/transit/datakey/my%20key", json_data={"keyBytes": 32})
|
|
62
|
+
await client.generate_transit_data_key_without_plaintext("orders")
|
|
63
|
+
base.post.assert_awaited_with("/api/v1/transit/datakey/orders/without-plaintext", json_data={})
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@pytest.mark.asyncio
|
|
67
|
+
async def test_unwrap_route_and_body():
|
|
68
|
+
base = AsyncMock()
|
|
69
|
+
base.post.return_value = {"keyMaterial": "bWF0", "keyId": str(KEY_ID), "keyVersion": 2}
|
|
70
|
+
resp = await KeysClient(base).unwrap(KEY_ID, "ZW5j", context="Y3R4")
|
|
71
|
+
base.post.assert_awaited_once_with(
|
|
72
|
+
f"/api/v1/keys/{KEY_ID}/unwrap", json_data={"wrappedKey": "ZW5j", "context": "Y3R4"}
|
|
73
|
+
)
|
|
74
|
+
assert resp.key_material == "bWF0" and resp.key_version == 2
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@pytest.mark.asyncio
|
|
78
|
+
async def test_unwrap_omits_context_when_absent():
|
|
79
|
+
base = AsyncMock()
|
|
80
|
+
base.post.return_value = {"keyMaterial": "bWF0", "keyId": str(KEY_ID), "keyVersion": 2}
|
|
81
|
+
await KeysClient(base).unwrap(KEY_ID, "ZW5j")
|
|
82
|
+
base.post.assert_awaited_once_with(f"/api/v1/keys/{KEY_ID}/unwrap", json_data={"wrappedKey": "ZW5j"})
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _resp(status, payload):
|
|
86
|
+
r = MagicMock()
|
|
87
|
+
r.status_code = status
|
|
88
|
+
r.json.return_value = payload
|
|
89
|
+
return r
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@pytest.mark.asyncio
|
|
93
|
+
async def test_403_key_revoked_maps_to_key_revoked_error():
|
|
94
|
+
client = BaseClient(api_key="qapi_x", base_url="https://example.test")
|
|
95
|
+
with pytest.raises(KeyRevokedError) as exc:
|
|
96
|
+
await client._handle_response(
|
|
97
|
+
_resp(403, {"error": "key_revoked", "message": "Key 'k' has been revoked"})
|
|
98
|
+
)
|
|
99
|
+
assert exc.value.error_code == "key_revoked"
|
|
100
|
+
assert exc.value.status_code == 403
|
|
101
|
+
assert isinstance(exc.value, PermissionError)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@pytest.mark.asyncio
|
|
105
|
+
async def test_other_403_stays_permission_error():
|
|
106
|
+
client = BaseClient(api_key="qapi_x", base_url="https://example.test")
|
|
107
|
+
with pytest.raises(PermissionError) as exc:
|
|
108
|
+
await client._handle_response(_resp(403, {"error": "forbidden", "message": "no"}))
|
|
109
|
+
assert not isinstance(exc.value, KeyRevokedError)
|
|
110
|
+
assert exc.value.status_code == 403
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@pytest.mark.asyncio
|
|
114
|
+
async def test_404_and_401_do_not_raise_type_error():
|
|
115
|
+
client = BaseClient(api_key="qapi_x", base_url="https://example.test")
|
|
116
|
+
from quantumapi.errors import AuthenticationError, NotFoundError, ValidationError
|
|
117
|
+
|
|
118
|
+
for status, cls in [(401, AuthenticationError), (404, NotFoundError), (400, ValidationError)]:
|
|
119
|
+
with pytest.raises(cls) as exc:
|
|
120
|
+
await client._handle_response(_resp(status, {"error": "e", "message": "m"}))
|
|
121
|
+
assert exc.value.status_code == status
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|