Debian Codegraph @

tests/apps/mfa/totp/test_unit.py

raw
1 from unittest import mock
2
3 from allauth.mfa import app_settings
4 from allauth.mfa.totp.internal.auth import (
5 format_hotp_value,
6 generate_totp_secret,
7 hotp_value,
8 validate_totp_code,
9 yield_hotp_counters_from_time,
10 )
11
12
13 @mock.patch("time.time", mock.MagicMock(return_value=1731948631))
14 def test_totp_counters_from_time():
15 app_settings.TOTP_TOLERANCE = 0
16 counters = list(yield_hotp_counters_from_time())
17 assert len(counters) == 1
18
19
20 @mock.patch("time.time", mock.MagicMock(return_value=1731948631))
21 def test_totp_counters_from_time_with_tolerance():
22 app_settings.TOTP_TOLERANCE = 1
23 counters = list(yield_hotp_counters_from_time())
24 assert len(counters) == 3
25
26
27 @mock.patch("time.time", mock.MagicMock(return_value=1731948631))
28 def test_validate_with_tolerance():
29 app_settings.TOTP_TOLERANCE = 1
30 test_secret = generate_totp_secret()
31 expected_value = format_hotp_value(hotp_value(test_secret, 57731621))
32 assert validate_totp_code(test_secret, expected_value)
33
34 before_value = format_hotp_value(hotp_value(test_secret, 57731620))
35 assert validate_totp_code(test_secret, before_value)
36
37 after_value = format_hotp_value(hotp_value(test_secret, 57731622))
38 assert validate_totp_code(test_secret, after_value)
39
40 two_before_value = format_hotp_value(hotp_value(test_secret, 57731619))
41 assert not validate_totp_code(test_secret, two_before_value)
42
43 two_after_value = format_hotp_value(hotp_value(test_secret, 57731623))
44 assert not validate_totp_code(test_secret, two_after_value)