6
6
7
7
import impit
8
8
import pytest
9
+ from apify_shared .utils import create_hmac_signature , create_storage_content_signature
9
10
10
11
from integration .integration_test_utils import parametrized_api_urls , random_resource_name
11
12
12
13
from apify_client import ApifyClient , ApifyClientAsync
13
14
from apify_client .client import DEFAULT_API_URL
14
15
16
+ MOCKED_ID = 'someID'
17
+
15
18
16
19
def _get_mocked_api_kvs_response (signing_key : str | None = None ) -> str :
17
20
response_data = {
18
21
'data' : {
19
- 'id' : 'someID' ,
22
+ 'id' : MOCKED_ID ,
20
23
'name' : 'name' ,
21
24
'userId' : 'userId' ,
22
25
'createdAt' : '2025-09-11T08:48:51.806Z' ,
@@ -78,7 +81,7 @@ def test_key_value_store_should_create_public_keys_non_expiring_url(self, apify_
78
81
@parametrized_api_urls
79
82
def test_public_url (self , api_token : str , api_url : str , api_public_url : str , signing_key : str ) -> None :
80
83
apify_client = ApifyClient (token = api_token , api_url = api_url , api_public_url = api_public_url )
81
- kvs = apify_client .key_value_store ('someID' )
84
+ kvs = apify_client .key_value_store (MOCKED_ID )
82
85
83
86
# Mock the API call to return predefined response
84
87
with mock .patch .object (
@@ -87,7 +90,13 @@ def test_public_url(self, api_token: str, api_url: str, api_public_url: str, sig
87
90
return_value = Mock (text = _get_mocked_api_kvs_response (signing_key = signing_key )),
88
91
):
89
92
public_url = kvs .create_keys_public_url ()
90
- expected_signature = f'?signature={ public_url .split ("signature=" )[1 ]} ' if signing_key else ''
93
+ expected_signature = (
94
+ f'?signature={
95
+ create_storage_content_signature (resource_id = MOCKED_ID , url_signing_secret_key = signing_key )
96
+ } '
97
+ if signing_key
98
+ else ''
99
+ )
91
100
assert public_url == (
92
101
f'{ (api_public_url or DEFAULT_API_URL ).strip ("/" )} /v2/key-value-stores/someID/keys{ expected_signature } '
93
102
)
@@ -96,19 +105,20 @@ def test_public_url(self, api_token: str, api_url: str, api_public_url: str, sig
96
105
@parametrized_api_urls
97
106
def test_record_public_url (self , api_token : str , api_url : str , api_public_url : str , signing_key : str ) -> None :
98
107
apify_client = ApifyClient (token = api_token , api_url = api_url , api_public_url = api_public_url )
99
- kvs = apify_client .key_value_store ('someID' )
108
+ key = 'some_key'
109
+ kvs = apify_client .key_value_store (MOCKED_ID )
100
110
101
111
# Mock the API call to return predefined response
102
112
with mock .patch .object (
103
113
apify_client .http_client ,
104
114
'call' ,
105
115
return_value = Mock (text = _get_mocked_api_kvs_response (signing_key = signing_key )),
106
116
):
107
- public_url = kvs .get_record_public_url (key = ' key' )
108
- expected_signature = f'?signature={ public_url . split ( "signature=" )[ 1 ] } ' if signing_key else ''
117
+ public_url = kvs .get_record_public_url (key = key )
118
+ expected_signature = f'?signature={ create_hmac_signature ( signing_key , key ) } ' if signing_key else ''
109
119
assert public_url == (
110
120
f'{ (api_public_url or DEFAULT_API_URL ).strip ("/" )} /v2/key-value-stores/someID/'
111
- f'records/key{ expected_signature } '
121
+ f'records/{ key } { expected_signature } '
112
122
)
113
123
114
124
@@ -157,37 +167,45 @@ async def test_key_value_store_should_create_public_keys_non_expiring_url(
157
167
158
168
@pytest .mark .parametrize ('signing_key' , [None , 'custom-signing-key' ])
159
169
@parametrized_api_urls
160
- async def test_record_public_url (self , api_token : str , api_url : str , api_public_url : str , signing_key : str ) -> None :
170
+ async def test_public_url (self , api_token : str , api_url : str , api_public_url : str , signing_key : str ) -> None :
161
171
apify_client = ApifyClientAsync (token = api_token , api_url = api_url , api_public_url = api_public_url )
162
- kvs = apify_client .key_value_store ('someID' )
172
+ kvs = apify_client .key_value_store (MOCKED_ID )
173
+ mocked_response = _get_mocked_api_kvs_response (signing_key = signing_key )
163
174
164
175
# Mock the API call to return predefined response
165
176
with mock .patch .object (
166
177
apify_client .http_client ,
167
178
'call' ,
168
- return_value = Mock (text = _get_mocked_api_kvs_response ( signing_key = signing_key ) ),
179
+ return_value = Mock (text = mocked_response ),
169
180
):
170
181
public_url = await kvs .create_keys_public_url ()
171
- expected_signature = f'?signature={ public_url .split ("signature=" )[1 ]} ' if signing_key else ''
182
+ expected_signature = (
183
+ f'?signature={
184
+ create_storage_content_signature (resource_id = MOCKED_ID , url_signing_secret_key = signing_key )
185
+ } '
186
+ if signing_key
187
+ else ''
188
+ )
172
189
assert public_url == (
173
190
f'{ (api_public_url or DEFAULT_API_URL ).strip ("/" )} /v2/key-value-stores/someID/keys{ expected_signature } '
174
191
)
175
192
176
193
@pytest .mark .parametrize ('signing_key' , [None , 'custom-signing-key' ])
177
194
@parametrized_api_urls
178
- async def test_public_url (self , api_token : str , api_url : str , api_public_url : str , signing_key : str ) -> None :
195
+ async def test_record_public_url (self , api_token : str , api_url : str , api_public_url : str , signing_key : str ) -> None :
179
196
apify_client = ApifyClientAsync (token = api_token , api_url = api_url , api_public_url = api_public_url )
180
- kvs = apify_client .key_value_store ('someID' )
197
+ key = 'some_key'
198
+ kvs = apify_client .key_value_store (MOCKED_ID )
181
199
182
200
# Mock the API call to return predefined response
183
201
with mock .patch .object (
184
202
apify_client .http_client ,
185
203
'call' ,
186
204
return_value = Mock (text = _get_mocked_api_kvs_response (signing_key = signing_key )),
187
205
):
188
- public_url = await kvs .get_record_public_url (key = ' key' )
189
- expected_signature = f'?signature={ public_url . split ( "signature=" )[ 1 ] } ' if signing_key else ''
206
+ public_url = await kvs .get_record_public_url (key = key )
207
+ expected_signature = f'?signature={ create_hmac_signature ( signing_key , key ) } ' if signing_key else ''
190
208
assert public_url == (
191
209
f'{ (api_public_url or DEFAULT_API_URL ).strip ("/" )} /v2/key-value-stores/someID/'
192
- f'records/key{ expected_signature } '
210
+ f'records/{ key } { expected_signature } '
193
211
)
0 commit comments