/ fastapi-template-users / tests / utils / test_passwords.py
test_passwords.py
 1  import pytest
 2  
 3  from api.utils import passwords
 4  
 5  
 6  @pytest.mark.parametrize("password", ["", "foobar42", "Ai,982d$" * 256, "π"])
 7  async def test__hash_password(password: str) -> None:
 8      res1 = await passwords.hash_password(password)
 9      res2 = await passwords.hash_password(password)
10  
11      assert res1 != res2
12      assert passwords.password_hasher.verify(res1, password)
13      assert passwords.password_hasher.verify(res1, password)
14  
15  
16  @pytest.mark.parametrize(
17      "pw,guess,ok",
18      [
19          ("", "", True),
20          ("", "x", False),
21          ("foobar42", "foobar42", True),
22          ("fooBar42", "foobar42", False),
23          ("Ai,982d$" * 256, "Ai,982d$" * 256, True),
24          ("Ai,982d$" * 256, "Ai,982d$" * 255, False),
25          ("π", "π", True),
26          ("π", "∞", False),
27      ],
28  )
29  async def test__verify_password(pw: str, guess: str, ok: bool) -> None:
30      pwhash = passwords.password_hasher.hash(pw)
31      assert await passwords.verify_password(guess, pwhash) is ok