|
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 (
|
| 9 | + create_hmac_signature, |
| 10 | + create_storage_content_signature, |
| 11 | + encode_base62, |
8 | 12 | filter_out_none_values_recursively,
|
9 | 13 | filter_out_none_values_recursively_internal,
|
10 | 14 | ignore_docs,
|
@@ -146,3 +150,61 @@ def testing_function(_a: str, _b: str) -> str:
|
146 | 150 | return 'dummy'
|
147 | 151 |
|
148 | 152 | assert testing_function is ignore_docs(testing_function)
|
| 153 | + |
| 154 | + |
| 155 | +def test_encode_base62() -> None: |
| 156 | + assert encode_base62(0) == '0' |
| 157 | + assert encode_base62(10) == 'a' |
| 158 | + assert encode_base62(999999999) == '15FTGf' |
| 159 | + |
| 160 | + |
| 161 | +# This test ensures compatibility with the JavaScript version of the same method. |
| 162 | +# https://github.com/apify/apify-shared-js/blob/master/packages/utilities/src/hmac.ts |
| 163 | +def test_create_valid_hmac_signature() -> None: |
| 164 | + # This test uses the same secret key and message as in JS tests. |
| 165 | + secret_key = 'hmac-secret-key' |
| 166 | + message = 'hmac-message-to-be-authenticated' |
| 167 | + assert create_hmac_signature(secret_key, message) == 'pcVagAsudj8dFqdlg7mG' |
| 168 | + |
| 169 | + |
| 170 | +def test_create_same_hmac() -> None: |
| 171 | + # This test uses the same secret key and message as in JS tests. |
| 172 | + secret_key = 'hmac-same-secret-key' |
| 173 | + message = 'hmac-same-message-to-be-authenticated' |
| 174 | + assert create_hmac_signature(secret_key, message) == 'FYMcmTIm3idXqleF1Sw5' |
| 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