|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import base64 |
3 | 4 | import io
|
4 | 5 | from datetime import datetime, timezone
|
5 | 6 | from enum import Enum
|
6 | 7 |
|
7 | 8 | from apify_shared.utils import (
|
8 | 9 | create_hmac_signature,
|
| 10 | + create_storage_content_signature, |
9 | 11 | encode_base62,
|
10 | 12 | filter_out_none_values_recursively,
|
11 | 13 | filter_out_none_values_recursively_internal,
|
@@ -171,3 +173,38 @@ def test_create_same_hmac() -> None:
|
171 | 173 | message = 'hmac-same-message-to-be-authenticated'
|
172 | 174 | assert create_hmac_signature(secret_key, message) == 'FYMcmTIm3idXqleF1Sw5'
|
173 | 175 | assert create_hmac_signature(secret_key, message) == 'FYMcmTIm3idXqleF1Sw5'
|
| 176 | + |
| 177 | + |
| 178 | +# This test ensures compatibility with the JavaScript version of the same method. |
| 179 | +# https://github.com/apify/apify-shared-js/blob/master/packages/utilities/src/storages.ts |
| 180 | +def test_create_storage_content_signature() -> None: |
| 181 | + # This test uses the same parameters as in JS tests. |
| 182 | + secret_key = 'hmac-secret-key' |
| 183 | + message = 'resource-id' |
| 184 | + |
| 185 | + signature = create_storage_content_signature( |
| 186 | + resource_id=message, |
| 187 | + url_signing_secret_key=secret_key, |
| 188 | + ) |
| 189 | + |
| 190 | + version, expires_at, hmac = base64.urlsafe_b64decode(signature).decode('utf-8').split('.') |
| 191 | + |
| 192 | + assert signature == 'MC4wLjNUd2ZFRTY1OXVmU05zbVM0N2xS' |
| 193 | + assert version == '0' |
| 194 | + assert expires_at == '0' |
| 195 | + assert hmac == '3TwfEE659ufSNsmS47lR' |
| 196 | + |
| 197 | + |
| 198 | +def test_create_storage_content_signature_with_expiration() -> None: |
| 199 | + secret_key = 'hmac-secret-key' |
| 200 | + message = 'resource-id' |
| 201 | + |
| 202 | + signature = create_storage_content_signature( |
| 203 | + resource_id=message, |
| 204 | + url_signing_secret_key=secret_key, |
| 205 | + expires_in_millis=10000, |
| 206 | + ) |
| 207 | + |
| 208 | + version, expires_at, hmac = base64.urlsafe_b64decode(signature).decode('utf-8').split('.') |
| 209 | + assert version == '0' |
| 210 | + assert expires_at != '0' |
0 commit comments