|
| 1 | +from unittest import mock |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from descope import AuthException, DescopeClient |
| 5 | +from descope.common import DEFAULT_TIMEOUT_SECONDS |
| 6 | +from descope.management.common import MgmtV1 |
| 7 | + |
| 8 | +from .. import common |
| 9 | +from ..testutils import SSLMatcher |
| 10 | + |
| 11 | + |
| 12 | +class TestLicense(common.DescopeTest): |
| 13 | + def setUp(self) -> None: |
| 14 | + super().setUp() |
| 15 | + self.dummy_project_id = "dummy" |
| 16 | + self.dummy_management_key = "key" |
| 17 | + self.public_key_dict = { |
| 18 | + "alg": "ES384", |
| 19 | + "crv": "P-384", |
| 20 | + "kid": "P2CtzUhdqpIF2ys9gg7ms06UvtC4", |
| 21 | + "kty": "EC", |
| 22 | + "use": "sig", |
| 23 | + "x": "pX1l7nT2turcK5_Cdzos8SKIhpLh1Wy9jmKAVyMFiOCURoj-WQX1J0OUQqMsQO0s", |
| 24 | + "y": "B0_nWAv2pmG_PzoH3-bSYZZzLNKUA0RoE2SH7DaS0KV4rtfWZhYd0MEr0xfdGKx0", |
| 25 | + } |
| 26 | + |
| 27 | + def test_get_failure(self): |
| 28 | + client = DescopeClient( |
| 29 | + self.dummy_project_id, |
| 30 | + self.public_key_dict, |
| 31 | + False, |
| 32 | + self.dummy_management_key, |
| 33 | + ) |
| 34 | + with patch("httpx.get") as mock_get: |
| 35 | + mock_get.return_value.is_success = False |
| 36 | + self.assertRaises(AuthException, client.mgmt.license.get) |
| 37 | + |
| 38 | + def test_get_success(self): |
| 39 | + client = DescopeClient( |
| 40 | + self.dummy_project_id, |
| 41 | + self.public_key_dict, |
| 42 | + False, |
| 43 | + self.dummy_management_key, |
| 44 | + ) |
| 45 | + with patch("httpx.get") as mock_get: |
| 46 | + network_resp = mock.Mock() |
| 47 | + network_resp.is_success = True |
| 48 | + network_resp.json.return_value = {"rateLimitTier": "tier4"} |
| 49 | + mock_get.return_value = network_resp |
| 50 | + |
| 51 | + resp = client.mgmt.license.get() |
| 52 | + self.assertEqual(resp, {"rateLimitTier": "tier4"}) |
| 53 | + |
| 54 | + mock_get.assert_called_with( |
| 55 | + f"{client._mgmt_http_client.base_url}{MgmtV1.license_get_path}", |
| 56 | + headers=mock.ANY, |
| 57 | + params=None, |
| 58 | + follow_redirects=True, |
| 59 | + verify=SSLMatcher(), |
| 60 | + timeout=DEFAULT_TIMEOUT_SECONDS, |
| 61 | + ) |
| 62 | + |
| 63 | + def test_header_injected_after_handshake(self): |
| 64 | + client = DescopeClient( |
| 65 | + self.dummy_project_id, |
| 66 | + self.public_key_dict, |
| 67 | + False, |
| 68 | + self.dummy_management_key, |
| 69 | + ) |
| 70 | + # Simulate a completed handshake by setting the cached tier directly. |
| 71 | + client._mgmt_http_client.rate_limit_tier = "tier2" |
| 72 | + headers = client._mgmt_http_client._get_default_headers() |
| 73 | + self.assertEqual(headers.get("x-descope-license"), "tier2") |
| 74 | + |
| 75 | + def test_header_absent_when_tier_not_cached(self): |
| 76 | + client = DescopeClient( |
| 77 | + self.dummy_project_id, |
| 78 | + self.public_key_dict, |
| 79 | + False, |
| 80 | + self.dummy_management_key, |
| 81 | + ) |
| 82 | + # Default state has no rate limit tier yet. |
| 83 | + client._mgmt_http_client.rate_limit_tier = None |
| 84 | + headers = client._mgmt_http_client._get_default_headers() |
| 85 | + self.assertNotIn("x-descope-license", headers) |
0 commit comments