forked from Neoteroi/BlackSheep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dataprotection.py
60 lines (39 loc) · 1.39 KB
/
test_dataprotection.py
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
import os
import pytest
from itsdangerous import BadSignature
from blacksheep.server.dataprotection import generate_secret, get_keys, get_serializer
def test_get_keys_creates_default_keys():
default_keys = get_keys()
assert default_keys is not None
assert len(default_keys) == 3
assert default_keys != get_keys()
def test_get_keys_returns_keys_configured_as_env_variables():
env_variables = []
for i in range(4):
key = generate_secret()
os.environ[f"APP_SECRET_{i}"] = key
env_variables.append(key)
assert get_keys() == env_variables
assert get_keys() == env_variables
def test_get_serializer():
serializer = get_serializer(get_keys())
data = {"id": "0000"}
secret = serializer.dumps(data)
assert isinstance(secret, str)
parsed = serializer.loads(secret)
assert data == parsed
def test_get_serializer_with_different_purpose():
keys = get_keys()
serializer = get_serializer(keys)
other_serializer = get_serializer(keys, purpose="test")
data = {"id": "0000"}
secret = serializer.dumps(data)
with pytest.raises(BadSignature):
other_serializer.loads(secret)
def test_get_serializer_with_default_keys():
serializer = get_serializer()
data = {"id": "0000"}
secret = serializer.dumps(data)
assert isinstance(secret, str)
parsed = serializer.loads(secret)
assert data == parsed