|
| 1 | +import pyotp |
| 2 | +import pytest |
| 3 | + |
| 4 | +from app.utility.security import verify_otp |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def totp_secret(): |
| 9 | + return pyotp.random_base32() |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def hotp_secret(): |
| 14 | + return pyotp.random_base32() |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def unsupported_secret(): |
| 19 | + return pyotp.random_base32() |
| 20 | + |
| 21 | + |
| 22 | +def test_verify_otp_valid_totp(totp_secret): |
| 23 | + totp = pyotp.TOTP(totp_secret) |
| 24 | + otp_code = totp.now() |
| 25 | + assert verify_otp(totp_secret, otp_code, "TOTP") is True |
| 26 | + |
| 27 | + |
| 28 | +def test_verify_otp_invalid_totp(totp_secret): |
| 29 | + otp_code = "000000" |
| 30 | + assert verify_otp(totp_secret, otp_code, "TOTP") is False |
| 31 | + |
| 32 | + |
| 33 | +def test_verify_otp_valid_hotp(hotp_secret): |
| 34 | + hotp = pyotp.HOTP(hotp_secret) |
| 35 | + counter = 0 |
| 36 | + otp_code = hotp.at(counter) |
| 37 | + assert verify_otp(hotp_secret, otp_code, "HOTP", counter) is True |
| 38 | + |
| 39 | + |
| 40 | +def test_verify_otp_invalid_hotp(hotp_secret): |
| 41 | + counter = 0 |
| 42 | + otp_code = "000000" |
| 43 | + assert verify_otp(hotp_secret, otp_code, "HOTP", counter) is False |
| 44 | + |
| 45 | + |
| 46 | +def test_verify_otp_unsupported_method(unsupported_secret): |
| 47 | + otp_code = "123456" |
| 48 | + assert verify_otp(unsupported_secret, otp_code, "SMS") is False |
| 49 | + |
| 50 | + |
| 51 | +def test_verify_otp_invalid_secret(): |
| 52 | + secret = "not_a_valid_secret" |
| 53 | + otp_code = "123456" |
| 54 | + assert verify_otp(secret, otp_code, "TOTP") is False |
| 55 | + |
| 56 | + |
| 57 | +def test_verify_otp_invalid_code_type(totp_secret): |
| 58 | + otp_code = None |
| 59 | + assert verify_otp(totp_secret, otp_code, "TOTP") is False |
0 commit comments