|
5 | 5 | from typing import TYPE_CHECKING, cast |
6 | 6 |
|
7 | 7 | if TYPE_CHECKING: |
| 8 | + from apify_client import ApifyClient, ApifyClientAsync |
8 | 9 | from apify_client._models import KeyValueStore, ListOfKeys |
9 | 10 |
|
10 | 11 | import json |
11 | | -from unittest import mock |
12 | | -from unittest.mock import Mock |
13 | 12 |
|
14 | 13 | import impit |
15 | 14 | import pytest |
16 | 15 |
|
17 | | -from .conftest import maybe_await, maybe_sleep |
18 | | -from .utils import KvsFixture, get_random_resource_name, parametrized_api_urls |
19 | | -from apify_client import ApifyClient, ApifyClientAsync |
20 | | -from apify_client._config import DEFAULT_API_URL |
21 | | -from apify_client._utils import create_hmac_signature, create_storage_content_signature |
| 16 | +from .conftest import KvsFixture, get_random_resource_name, maybe_await, maybe_sleep |
22 | 17 | from apify_client.errors import ApifyApiError |
23 | 18 |
|
24 | | -################################################## |
25 | | -# OLD TESTS - Tests with mocks and signed URLs |
26 | | -################################################## |
27 | | - |
28 | | -MOCKED_ID = 'someID' |
29 | | - |
30 | | - |
31 | | -def _get_mocked_api_kvs_response(signing_key: str | None = None) -> Mock: |
32 | | - response_data = { |
33 | | - 'data': { |
34 | | - 'id': MOCKED_ID, |
35 | | - 'name': 'name', |
36 | | - 'userId': 'userId', |
37 | | - 'createdAt': '2025-09-11T08:48:51.806Z', |
38 | | - 'modifiedAt': '2025-09-11T08:48:51.806Z', |
39 | | - 'accessedAt': '2025-09-11T08:48:51.806Z', |
40 | | - 'actId': None, |
41 | | - 'actRunId': None, |
42 | | - 'schema': None, |
43 | | - 'stats': {'readCount': 0, 'writeCount': 0, 'deleteCount': 0, 'listCount': 0, 'storageBytes': 0}, |
44 | | - 'consoleUrl': 'https://console.apify.com/storage/key-value-stores/someID', |
45 | | - 'keysPublicUrl': 'https://api.apify.com/v2/key-value-stores/someID/keys', |
46 | | - 'generalAccess': 'FOLLOW_USER_SETTING', |
47 | | - } |
48 | | - } |
49 | | - if signing_key: |
50 | | - response_data['data']['urlSigningSecretKey'] = signing_key |
51 | | - |
52 | | - mock_response = Mock() |
53 | | - mock_response.json.return_value = response_data |
54 | | - return mock_response |
55 | | - |
56 | 19 |
|
57 | 20 | async def test_key_value_store_should_create_expiring_keys_public_url_with_params( |
58 | 21 | client: ApifyClient | ApifyClientAsync, |
@@ -104,72 +67,6 @@ async def test_key_value_store_should_create_public_keys_non_expiring_url( |
104 | 67 | assert result is None |
105 | 68 |
|
106 | 69 |
|
107 | | -@pytest.mark.parametrize('signing_key', [None, 'custom-signing-key']) |
108 | | -@parametrized_api_urls |
109 | | -async def test_public_url( |
110 | | - client: ApifyClient | ApifyClientAsync, api_token: str, api_url: str, api_public_url: str, signing_key: str |
111 | | -) -> None: |
112 | | - """Test public URL generation for key-value stores (runs for both sync and async clients).""" |
113 | | - # Create a fresh client with the parametrized URL settings |
114 | | - if isinstance(client, ApifyClientAsync): |
115 | | - test_client: ApifyClient | ApifyClientAsync = ApifyClientAsync( |
116 | | - token=api_token, api_url=api_url, api_public_url=api_public_url |
117 | | - ) |
118 | | - else: |
119 | | - test_client = ApifyClient(token=api_token, api_url=api_url, api_public_url=api_public_url) |
120 | | - |
121 | | - kvs = test_client.key_value_store(MOCKED_ID) |
122 | | - |
123 | | - # Mock the API call to return predefined response |
124 | | - with mock.patch.object( |
125 | | - test_client._http_client, |
126 | | - 'call', |
127 | | - return_value=_get_mocked_api_kvs_response(signing_key=signing_key), |
128 | | - ): |
129 | | - public_url = await maybe_await(kvs.create_keys_public_url()) |
130 | | - if signing_key: |
131 | | - signature_value = create_storage_content_signature( |
132 | | - resource_id=MOCKED_ID, url_signing_secret_key=signing_key |
133 | | - ) |
134 | | - expected_signature = f'?signature={signature_value}' |
135 | | - else: |
136 | | - expected_signature = '' |
137 | | - assert public_url == ( |
138 | | - f'{(api_public_url or DEFAULT_API_URL).strip("/")}/v2/key-value-stores/someID/keys{expected_signature}' |
139 | | - ) |
140 | | - |
141 | | - |
142 | | -@pytest.mark.parametrize('signing_key', [None, 'custom-signing-key']) |
143 | | -@parametrized_api_urls |
144 | | -async def test_record_public_url( |
145 | | - client: ApifyClient | ApifyClientAsync, api_token: str, api_url: str, api_public_url: str, signing_key: str |
146 | | -) -> None: |
147 | | - """Test record public URL generation for key-value stores (runs for both sync and async clients).""" |
148 | | - # Create a fresh client with the parametrized URL settings |
149 | | - if isinstance(client, ApifyClientAsync): |
150 | | - test_client: ApifyClient | ApifyClientAsync = ApifyClientAsync( |
151 | | - token=api_token, api_url=api_url, api_public_url=api_public_url |
152 | | - ) |
153 | | - else: |
154 | | - test_client = ApifyClient(token=api_token, api_url=api_url, api_public_url=api_public_url) |
155 | | - |
156 | | - key = 'some_key' |
157 | | - kvs = test_client.key_value_store(MOCKED_ID) |
158 | | - |
159 | | - # Mock the API call to return predefined response |
160 | | - with mock.patch.object( |
161 | | - test_client._http_client, |
162 | | - 'call', |
163 | | - return_value=_get_mocked_api_kvs_response(signing_key=signing_key), |
164 | | - ): |
165 | | - public_url = await maybe_await(kvs.get_record_public_url(key=key)) |
166 | | - expected_signature = f'?signature={create_hmac_signature(signing_key, key)}' if signing_key else '' |
167 | | - assert public_url == ( |
168 | | - f'{(api_public_url or DEFAULT_API_URL).strip("/")}/v2/key-value-stores/someID/' |
169 | | - f'records/{key}{expected_signature}' |
170 | | - ) |
171 | | - |
172 | | - |
173 | 70 | async def test_list_keys_signature( |
174 | 71 | client: ApifyClient | ApifyClientAsync, test_kvs_of_another_user: KvsFixture |
175 | 72 | ) -> None: |
@@ -271,11 +168,6 @@ async def test_stream_record_signature( |
271 | 168 | assert test_kvs_of_another_user.expected_content[key] == value |
272 | 169 |
|
273 | 170 |
|
274 | | -############# |
275 | | -# NEW TESTS # |
276 | | -############# |
277 | | - |
278 | | - |
279 | 171 | async def test_key_value_store_get_or_create_and_get(client: ApifyClient | ApifyClientAsync) -> None: |
280 | 172 | """Test creating a key-value store and retrieving it.""" |
281 | 173 | store_name = get_random_resource_name('kvs') |
|
0 commit comments