-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_utils.py
More file actions
63 lines (46 loc) · 2.24 KB
/
test_utils.py
File metadata and controls
63 lines (46 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from __future__ import annotations
import base64
from apify_shared.utils import create_hmac_signature, create_storage_content_signature, encode_base62
def test_encode_base62() -> None:
assert encode_base62(0) == '0'
assert encode_base62(10) == 'a'
assert encode_base62(999999999) == '15FTGf'
# This test ensures compatibility with the JavaScript version of the same method.
# https://github.com/apify/apify-shared-js/blob/master/packages/utilities/src/hmac.ts
def test_create_valid_hmac_signature() -> None:
# This test uses the same secret key and message as in JS tests.
secret_key = 'hmac-secret-key'
message = 'hmac-message-to-be-authenticated'
assert create_hmac_signature(secret_key, message) == 'pcVagAsudj8dFqdlg7mG'
def test_create_same_hmac() -> None:
# This test uses the same secret key and message as in JS tests.
secret_key = 'hmac-same-secret-key'
message = 'hmac-same-message-to-be-authenticated'
assert create_hmac_signature(secret_key, message) == 'FYMcmTIm3idXqleF1Sw5'
assert create_hmac_signature(secret_key, message) == 'FYMcmTIm3idXqleF1Sw5'
# This test ensures compatibility with the JavaScript version of the same method.
# https://github.com/apify/apify-shared-js/blob/master/packages/utilities/src/storages.ts
def test_create_storage_content_signature() -> None:
# This test uses the same parameters as in JS tests.
secret_key = 'hmac-secret-key'
message = 'resource-id'
signature = create_storage_content_signature(
resource_id=message,
url_signing_secret_key=secret_key,
)
version, expires_at, hmac = base64.urlsafe_b64decode(signature).decode('utf-8').split('.')
assert signature == 'MC4wLjNUd2ZFRTY1OXVmU05zbVM0N2xS'
assert version == '0'
assert expires_at == '0'
assert hmac == '3TwfEE659ufSNsmS47lR'
def test_create_storage_content_signature_with_expiration() -> None:
secret_key = 'hmac-secret-key'
message = 'resource-id'
signature = create_storage_content_signature(
resource_id=message,
url_signing_secret_key=secret_key,
expires_in_millis=10000,
)
version, expires_at, _ = base64.urlsafe_b64decode(signature).decode('utf-8').split('.')
assert version == '0'
assert expires_at != '0'