File: test_utils.py

package info (click to toggle)
pyjwt 2.10.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 708 kB
  • sloc: python: 4,592; makefile: 141
file content (58 lines) | stat: -rw-r--r-- 1,383 bytes parent folder | download | duplicates (2)
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
from contextlib import nullcontext

import pytest

from jwt.utils import force_bytes, from_base64url_uint, is_ssh_key, to_base64url_uint


@pytest.mark.parametrize(
    "inputval,expected",
    [
        (0, nullcontext(b"AA")),
        (1, nullcontext(b"AQ")),
        (255, nullcontext(b"_w")),
        (65537, nullcontext(b"AQAB")),
        (123456789, nullcontext(b"B1vNFQ")),
        (-1, pytest.raises(ValueError)),
    ],
)
def test_to_base64url_uint(inputval, expected):
    with expected as e:
        actual = to_base64url_uint(inputval)
        assert actual == e


@pytest.mark.parametrize(
    "inputval,expected",
    [
        (b"AA", 0),
        (b"AQ", 1),
        (b"_w", 255),
        (b"AQAB", 65537),
        (b"B1vNFQ", 123456789),
    ],
)
def test_from_base64url_uint(inputval, expected):
    actual = from_base64url_uint(inputval)
    assert actual == expected


def test_force_bytes_raises_error_on_invalid_object():
    with pytest.raises(TypeError):
        force_bytes({})  # type: ignore[arg-type]


@pytest.mark.parametrize(
    "key_format",
    (
        b"ssh-ed25519",
        b"ssh-rsa",
        b"ssh-dss",
        b"ecdsa-sha2-nistp256",
        b"ecdsa-sha2-nistp384",
        b"ecdsa-sha2-nistp521",
    ),
)
def test_is_ssh_key(key_format):
    assert is_ssh_key(key_format + b" any") is True
    assert is_ssh_key(b"not a ssh key") is False