File: test_bcrypt.py

package info (click to toggle)
python-pwdlib 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 632 kB
  • sloc: python: 403; makefile: 14
file content (87 lines) | stat: -rw-r--r-- 2,837 bytes parent folder | download
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import pytest

from pwdlib.hashers.bcrypt import BcryptHasher

_PASSWORD = "herminetincture"

_HASHER = BcryptHasher()
_HASH_STR = _HASHER.hash(_PASSWORD)
_HASH_BYTES = _HASH_STR.encode("ascii")


@pytest.fixture
def bcrypt_hasher() -> BcryptHasher:
    return BcryptHasher()


@pytest.mark.parametrize(
    "hash,result",
    [
        (_HASH_STR, True),
        (_HASH_BYTES, True),
        ("INVALID_HASH", False),
        (b"INVALID_HASH", False),
    ],
)
def test_identify(hash: str | bytes, result: bool) -> None:
    assert BcryptHasher.identify(hash) == result


def test_hash(bcrypt_hasher: BcryptHasher) -> None:
    hash = bcrypt_hasher.hash("herminetincture")
    assert isinstance(hash, str)


@pytest.mark.parametrize(
    "hash,password,result",
    [
        (_HASH_STR, _PASSWORD, True),
        (_HASH_BYTES, _PASSWORD, True),
        (_HASH_STR, "INVALID_PASSWORD", False),
        (_HASH_BYTES, "INVALID_PASSWORD", False),
    ],
)
def test_verify(
    hash: str | bytes,
    password: str,
    result: bool,
    bcrypt_hasher: BcryptHasher,
) -> None:
    assert bcrypt_hasher.verify(password, hash) == result


def test_check_needs_rehash(bcrypt_hasher: BcryptHasher) -> None:
    assert not bcrypt_hasher.check_needs_rehash(_HASH_STR)
    assert not bcrypt_hasher.check_needs_rehash(_HASH_BYTES)
    assert bcrypt_hasher.check_needs_rehash("INVALID_HASH")
    assert bcrypt_hasher.check_needs_rehash(b"INVALID_HASH")

    bcrypt_hasher_different_rounds = BcryptHasher(rounds=10)
    hash = bcrypt_hasher_different_rounds.hash("herminetincture")
    assert bcrypt_hasher.check_needs_rehash(hash)

    bcrypt_hasher_different_prefix = BcryptHasher(prefix="2a")
    hash = bcrypt_hasher_different_prefix.hash("herminetincture")
    assert bcrypt_hasher.check_needs_rehash(hash)


@pytest.mark.parametrize(
    "invalid_value",
    [
        pytest.param(123, id="int"),
        pytest.param(None, id="None"),
        pytest.param([], id="list"),
        pytest.param({}, id="dict"),
    ],
)
def test_invalid_type(invalid_value: object, bcrypt_hasher: BcryptHasher) -> None:
    with pytest.raises(TypeError, match="hash must be str or bytes"):
        BcryptHasher.identify(invalid_value)  # type: ignore[arg-type]
    with pytest.raises(TypeError, match="password must be str or bytes"):
        bcrypt_hasher.hash(invalid_value)  # type: ignore[arg-type]
    with pytest.raises(TypeError, match="password must be str or bytes"):
        bcrypt_hasher.verify(invalid_value, _HASH_STR)  # type: ignore[arg-type]
    with pytest.raises(TypeError, match="hash must be str or bytes"):
        bcrypt_hasher.verify(_PASSWORD, invalid_value)  # type: ignore[arg-type]
    with pytest.raises(TypeError, match="hash must be str or bytes"):
        bcrypt_hasher.check_needs_rehash(invalid_value)  # type: ignore[arg-type]