Files

55 lines
1.6 KiB
Python

from app import totp
RFC_SECRET = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"
RFC_TIME = 59
RFC_CODE = "287082"
def test_rfc6238_vector():
assert totp.current_code(RFC_SECRET, now=RFC_TIME) == RFC_CODE
def test_verify_accepts_current_code():
assert totp.verify(RFC_SECRET, RFC_CODE, now=RFC_TIME)
def test_verify_accepts_spaces():
assert totp.verify(RFC_SECRET, "287 082", now=RFC_TIME)
def test_window_previous_and_next_step():
assert totp.verify(RFC_SECRET, RFC_CODE, now=RFC_TIME + totp.STEP_SECONDS)
assert totp.verify(RFC_SECRET, RFC_CODE, now=RFC_TIME - totp.STEP_SECONDS)
def test_outside_window_rejected():
assert not totp.verify(RFC_SECRET, RFC_CODE, now=RFC_TIME + 2 * totp.STEP_SECONDS)
assert not totp.verify(RFC_SECRET, RFC_CODE, now=RFC_TIME - 2 * totp.STEP_SECONDS)
def test_wrong_code_rejected():
assert not totp.verify(RFC_SECRET, "000000", now=RFC_TIME)
def test_short_code_rejected():
assert not totp.verify(RFC_SECRET, RFC_CODE[:5], now=RFC_TIME)
def test_non_numeric_code_rejected():
assert not totp.verify(RFC_SECRET, "28708a", now=RFC_TIME)
assert not totp.verify(RFC_SECRET, "", now=RFC_TIME)
def test_generated_secret_roundtrip():
secret = totp.generate_secret()
assert totp.verify(secret, totp.current_code(secret))
def test_provisioning_uri():
uri = totp.provisioning_uri(RFC_SECRET, "max mustermann")
assert uri.startswith("otpauth://totp/checkbot:max%20mustermann?")
assert f"secret={RFC_SECRET}" in uri
assert "issuer=checkbot" in uri
assert f"digits={totp.DIGITS}" in uri
assert f"period={totp.STEP_SECONDS}" in uri