Debian Codegraph @

tests/test_itsdangerous/test_signer.py

raw
1 import hashlib
2 from functools import partial
3
4 import pytest
5
6 from itsdangerous.exc import BadSignature
7 from itsdangerous.signer import HMACAlgorithm
8 from itsdangerous.signer import NoneAlgorithm
9 from itsdangerous.signer import Signer
10 from itsdangerous.signer import SigningAlgorithm
11
12
13 class _ReverseAlgorithm(SigningAlgorithm):
14 def get_signature(self, key, value):
15 return (key + value)[::-1]
16
17
18 class TestSigner:
19 @pytest.fixture()
20 def signer_factory(self):
21 return partial(Signer, secret_key="secret-key")
22
23 @pytest.fixture()
24 def signer(self, signer_factory):
25 return signer_factory()
26
27 def test_signer(self, signer):
28 signed = signer.sign("my string")
29 assert isinstance(signed, bytes)
30 assert signer.validate(signed)
31 out = signer.unsign(signed)
32 assert out == b"my string"
33
34 def test_no_separator(self, signer):
35 signed = signer.sign("my string")
36 signed = signed.replace(signer.sep, b"*", 1)
37 assert not signer.validate(signed)
38
39 with pytest.raises(BadSignature):
40 signer.unsign(signed)
41
42 def test_broken_signature(self, signer):
43 signed = signer.sign("b")
44 bad_signed = signed[:-1]
45 bad_sig = bad_signed.rsplit(b".", 1)[1]
46 assert not signer.verify_signature(b"b", bad_sig)
47
48 with pytest.raises(BadSignature) as exc_info:
49 signer.unsign(bad_signed)
50
51 assert exc_info.value.payload == b"b"
52
53 def test_changed_value(self, signer):
54 signed = signer.sign("my string")
55 signed = signed.replace(b"my", b"other", 1)
56 assert not signer.validate(signed)
57
58 with pytest.raises(BadSignature):
59 signer.unsign(signed)
60
61 def test_invalid_separator(self, signer_factory):
62 with pytest.raises(ValueError) as exc_info:
63 signer_factory(sep="-")
64
65 assert "separator cannot be used" in str(exc_info.value)
66
67 @pytest.mark.parametrize(
68 "key_derivation", ("concat", "django-concat", "hmac", "none")
69 )
70 def test_key_derivation(self, signer_factory, key_derivation):
71 signer = signer_factory(key_derivation=key_derivation)
72 assert signer.unsign(signer.sign("value")) == b"value"
73
74 def test_invalid_key_derivation(self, signer_factory):
75 signer = signer_factory(key_derivation="invalid")
76
77 with pytest.raises(TypeError):
78 signer.derive_key()
79
80 def test_digest_method(self, signer_factory):
81 signer = signer_factory(digest_method=hashlib.md5)
82 assert signer.unsign(signer.sign("value")) == b"value"
83
84 @pytest.mark.parametrize(
85 "algorithm", (None, NoneAlgorithm(), HMACAlgorithm(), _ReverseAlgorithm())
86 )
87 def test_algorithm(self, signer_factory, algorithm):
88 signer = signer_factory(algorithm=algorithm)
89 assert signer.unsign(signer.sign("value")) == b"value"
90
91 if algorithm is None:
92 assert signer.algorithm.digest_method == signer.digest_method
93
94 def test_secret_keys(self):
95 signer = Signer("a")
96 signed = signer.sign("my string")
97 assert isinstance(signed, bytes)
98
99 signer = Signer(["a", "b"])
100 assert signer.validate(signed)
101 out = signer.unsign(signed)
102 assert out == b"my string"
103
104
105 def test_abstract_algorithm():
106 alg = SigningAlgorithm()
107
108 with pytest.raises(NotImplementedError):
109 alg.get_signature(b"a", b"b")