Skip to content

Commit f2087fe

Browse files
committed
test(sdk): add API contract regression tests
1 parent e27c541 commit f2087fe

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

‎tests/test_contracts.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from unittest.mock import Mock, patch
2+
3+
import pytest
4+
import requests
5+
6+
from keymint import KeyMint, KeyMintApiError
7+
8+
9+
def response(payload, status=200):
10+
result = Mock()
11+
result.json.return_value = payload
12+
result.status_code = status
13+
if status >= 400:
14+
error = requests.exceptions.HTTPError()
15+
error.response = result
16+
result.raise_for_status.side_effect = error
17+
return result
18+
19+
20+
@patch("keymint.requests.get")
21+
def test_get_key_uses_header_not_query(mock_get):
22+
mock_get.return_value = response({"code": 0, "data": {"license": {"productId": "product 123"}}})
23+
24+
KeyMint("readonly_test").get_key({"productId": "product 123", "licenseKey": "secret/license+key"})
25+
26+
_, kwargs = mock_get.call_args
27+
assert kwargs["params"] == {"productId": "product 123"}
28+
assert kwargs["headers"]["x-license-key"] == "secret/license+key"
29+
assert "licenseKey" not in kwargs["params"]
30+
31+
32+
@patch("keymint.requests.post")
33+
def test_sign_key_returns_serialized_file(mock_post):
34+
mock_post.return_value = response({"file": '{"signedKey":"signature","keyId":"key_123"}'})
35+
36+
result = KeyMint("admin_test").sign_key({
37+
"productId": "product_123", "licenseKey": "license_123", "hostId": "host_123"
38+
})
39+
40+
assert isinstance(result["file"], str)
41+
42+
43+
@patch("keymint.requests.post")
44+
def test_nested_error_preserves_server_message(mock_post):
45+
mock_post.return_value = response({
46+
"success": False,
47+
"error": {"code": "CUSTOMER_EMAIL_EXISTS", "message": "Customer email already exists in team"},
48+
"code": 1,
49+
}, 409)
50+
51+
with pytest.raises(KeyMintApiError, match="Customer email already exists in team"):
52+
KeyMint("admin_test").create_key({"productId": "product_123"})

0 commit comments

Comments
 (0)