Skip to content

Commit e684b92

Browse files
committed
unit - crypto
1 parent 15d899a commit e684b92

File tree

1 file changed

+88
-84
lines changed

1 file changed

+88
-84
lines changed

tests/unit/test_crypto.py

Lines changed: 88 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -15,89 +15,93 @@
1515
PUBLIC_KEY = _load_public_key(PUBLIC_KEY_PEM_BASE64)
1616

1717

18-
class TestCrypto:
19-
def test_encrypt_decrypt_varions_string(self: TestCrypto) -> None:
20-
for value in [
21-
crypto_random_object_id(10),
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-
encrypted = public_encrypt(value, public_key=PUBLIC_KEY)
55-
decrypted_value = private_decrypt(**encrypted, private_key=PRIVATE_KEY)
56-
assert decrypted_value == value
57-
58-
def test_throw_if_password_is_not_valid(self: TestCrypto) -> None:
59-
test_value = 'test'
60-
encrypted = public_encrypt(test_value, public_key=PUBLIC_KEY)
61-
encrypted['encrypted_password'] = base64.b64encode(b'invalid_password').decode('utf-8')
62-
63-
with pytest.raises(ValueError, match='Ciphertext length must be equal to key size.'):
64-
private_decrypt(**encrypted, private_key=PRIVATE_KEY)
65-
66-
def test_throw_error_if_cipher_is_manipulated(self: TestCrypto) -> None:
67-
test_value = 'test2'
68-
encrypted = public_encrypt(test_value, public_key=PUBLIC_KEY)
69-
encrypted['encrypted_value'] = base64.b64encode(
70-
b'invalid_cipher' + base64.b64decode(encrypted['encrypted_value'].encode('utf-8')),
71-
).decode('utf-8')
72-
73-
with pytest.raises(ValueError, match='Decryption failed, malformed encrypted value or password.'):
74-
private_decrypt(**encrypted, private_key=PRIVATE_KEY)
75-
76-
def test_same_encrypted_value_should_return_deffirent_cipher(self: TestCrypto) -> None:
77-
test_value = 'test3'
78-
encrypted1 = public_encrypt(test_value, public_key=PUBLIC_KEY)
79-
encrypted2 = public_encrypt(test_value, public_key=PUBLIC_KEY)
80-
assert encrypted1['encrypted_value'] != encrypted2['encrypted_value']
81-
82-
# Check if the method is compatible with js version of the same method in:
83-
# https://github.com/apify/apify-shared-js/blob/master/packages/utilities/src/crypto.ts
84-
def test_private_encrypt_node_js_encrypted_value(self: TestCrypto) -> None:
85-
value = 'encrypted_with_node_js'
86-
# This was encrypted with nodejs version of the same method.
87-
encrypted_value_with_node_js = {
88-
'encrypted_password': 'lw0ez64/T1UcCQMLfhucZ6VIfMcf/TKni7PmXlL/ZRA4nmdGYz7/YQUzGWzKbLChrpqbG21DHxPIubUIQFDFE1ASkLvoSd0Ks8/wjKHMyhp+hsg5aSh9EZK6pBFpp6FeHoinV80+UURTvJuSVbWd1Orw5Frl41taP6RK3uNJlXikmgs8Xc7mShFEENgkz6y9+Pbe7jpcKkaJ2U/h7FN0eNON189kNFYVuAE1n2N6C3Q7dFnjl2e1btqErvg5Vu7ZS4BbX3wgC2qLYySGnqI3BNI5VGhAnncnQcjHb+85qG+LKoPekgY9I0s0kGMxiz/bmy1mYm9O+Lj1mbVUr7BDjQ==', # noqa: E501
89-
'encrypted_value': 'k8nkZDCi0hRfBc0RRefxeSHeGV0X60N03VCrhRhENKXBjrF/tEg=',
90-
}
91-
decrypted_value = private_decrypt(
92-
**encrypted_value_with_node_js,
93-
private_key=PRIVATE_KEY,
94-
)
95-
18+
def test_encrypt_decrypt_various_strings() -> None:
19+
for value in [
20+
crypto_random_object_id(10),
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+
encrypted = public_encrypt(value, public_key=PUBLIC_KEY)
54+
decrypted_value = private_decrypt(**encrypted, private_key=PRIVATE_KEY)
9655
assert decrypted_value == value
9756

98-
def test_crypto_random_object_id(self: TestCrypto) -> None:
99-
assert len(crypto_random_object_id()) == 17
100-
assert len(crypto_random_object_id(5)) == 5
101-
long_random_object_id = crypto_random_object_id(1000)
102-
for char in long_random_object_id:
103-
assert char in 'abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789'
57+
58+
def test_decryption_fails_with_invalid_password() -> None:
59+
test_value = 'test'
60+
encrypted = public_encrypt(test_value, public_key=PUBLIC_KEY)
61+
encrypted['encrypted_password'] = base64.b64encode(b'invalid_password').decode('utf-8')
62+
63+
with pytest.raises(ValueError, match='Ciphertext length must be equal to key size.'):
64+
private_decrypt(**encrypted, private_key=PRIVATE_KEY)
65+
66+
67+
def test_decryption_fails_with_manipulated_cipher() -> None:
68+
test_value = 'test2'
69+
encrypted = public_encrypt(test_value, public_key=PUBLIC_KEY)
70+
encrypted['encrypted_value'] = base64.b64encode(
71+
b'invalid_cipher' + base64.b64decode(encrypted['encrypted_value'].encode('utf-8')),
72+
).decode('utf-8')
73+
74+
with pytest.raises(ValueError, match='Decryption failed, malformed encrypted value or password.'):
75+
private_decrypt(**encrypted, private_key=PRIVATE_KEY)
76+
77+
78+
def test_same_value_produces_different_cipher_each_time() -> None:
79+
test_value = 'test3'
80+
encrypted1 = public_encrypt(test_value, public_key=PUBLIC_KEY)
81+
encrypted2 = public_encrypt(test_value, public_key=PUBLIC_KEY)
82+
assert encrypted1['encrypted_value'] != encrypted2['encrypted_value']
83+
84+
85+
# Check if the method is compatible with js version of the same method in:
86+
# https://github.com/apify/apify-shared-js/blob/master/packages/utilities/src/crypto.ts
87+
def test_private_decrypt_with_node_js_encrypted_value() -> None:
88+
value = 'encrypted_with_node_js'
89+
# This was encrypted with nodejs version of the same method.
90+
encrypted_value_with_node_js = {
91+
'encrypted_password': 'lw0ez64/T1UcCQMLfhucZ6VIfMcf/TKni7PmXlL/ZRA4nmdGYz7/YQUzGWzKbLChrpqbG21DHxPIubUIQFDFE1ASkLvoSd0Ks8/wjKHMyhp+hsg5aSh9EZK6pBFpp6FeHoinV80+UURTvJuSVbWd1Orw5Frl41taP6RK3uNJlXikmgs8Xc7mShFEENgkz6y9+Pbe7jpcKkaJ2U/h7FN0eNON189kNFYVuAE1n2N6C3Q7dFnjl2e1btqErvg5Vu7ZS4BbX3wgC2qLYySGnqI3BNI5VGhAnncnQcjHb+85qG+LKoPekgY9I0s0kGMxiz/bmy1mYm9O+Lj1mbVUr7BDjQ==', # noqa: E501
92+
'encrypted_value': 'k8nkZDCi0hRfBc0RRefxeSHeGV0X60N03VCrhRhENKXBjrF/tEg=',
93+
}
94+
decrypted_value = private_decrypt(
95+
**encrypted_value_with_node_js,
96+
private_key=PRIVATE_KEY,
97+
)
98+
99+
assert decrypted_value == value
100+
101+
102+
def test_crypto_random_object_id_length_and_charset() -> None:
103+
assert len(crypto_random_object_id()) == 17
104+
assert len(crypto_random_object_id(5)) == 5
105+
long_random_object_id = crypto_random_object_id(1000)
106+
for char in long_random_object_id:
107+
assert char in 'abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789'

0 commit comments

Comments
 (0)