Debian Codegraph @

tests/test_utils.py

raw
1 from contextlib import nullcontext
2
3 import pytest
4
5 from contextlib import AbstractContextManager
6
7 from jwt.utils import force_bytes, from_base64url_uint, is_ssh_key, to_base64url_uint
8
9
10 @pytest.mark.parametrize(
11 "inputval,expected",
12 [
13 (0, nullcontext(b"AA")),
14 (1, nullcontext(b"AQ")),
15 (255, nullcontext(b"_w")),
16 (65537, nullcontext(b"AQAB")),
17 (123456789, nullcontext(b"B1vNFQ")),
18 (-1, pytest.raises(ValueError)),
19 ],
20 )
21 def test_to_base64url_uint(
22 inputval: int, expected: AbstractContextManager[bytes]
23 ) -> None:
24 with expected as e:
25 actual = to_base64url_uint(inputval)
26 assert actual == e
27
28
29 @pytest.mark.parametrize(
30 "inputval,expected",
31 [
32 (b"AA", 0),
33 (b"AQ", 1),
34 (b"_w", 255),
35 (b"AQAB", 65537),
36 (b"B1vNFQ", 123456789),
37 ],
38 )
39 def test_from_base64url_uint(inputval: bytes, expected: int) -> None:
40 actual = from_base64url_uint(inputval)
41 assert actual == expected
42
43
44 def test_force_bytes_raises_error_on_invalid_object() -> None:
45 with pytest.raises(TypeError):
46 force_bytes({}) # type: ignore[arg-type]
47
48
49 @pytest.mark.parametrize(
50 "key_format",
51 (
52 b"ssh-ed25519",
53 b"ssh-rsa",
54 b"ssh-dss",
55 b"ecdsa-sha2-nistp256",
56 b"ecdsa-sha2-nistp384",
57 b"ecdsa-sha2-nistp521",
58 ),
59 )
60 def test_is_ssh_key(key_format: bytes) -> None:
61 assert is_ssh_key(key_format + b" any") is True
62 assert is_ssh_key(b"not a ssh key") is False