Skip to content

Commit 46171ff

Browse files
committed
tests: add tests for assert and string_code
1 parent 667c1fa commit 46171ff

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/otpauth/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class OTP(object, metaclass=ABCMeta):
1111
ALGORITHMS = ["SHA1", "SHA256", "SHA512"]
1212

1313
def __init__(self, secret: bytes, digit: int = 6, algorithm: str = "SHA1"):
14+
assert 0 < digit < 11
1415
assert algorithm in self.ALGORITHMS
1516

1617
self.secret = secret

tests/test_misc.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import unittest
2+
from otpauth import HOTP, TOTP
3+
4+
5+
class TestMisc(unittest.TestCase):
6+
def test_invalid_digit(self):
7+
self.assertRaises(AssertionError, HOTP, b"secret", 0)
8+
self.assertRaises(AssertionError, TOTP, b"secret", 0)
9+
10+
self.assertRaises(AssertionError, HOTP, b"secret", 11)
11+
self.assertRaises(AssertionError, TOTP, b"secret", 11)
12+
13+
def test_invalid_algorithm(self):
14+
self.assertRaises(AssertionError, HOTP, b"secret", algorithm="SHA2")
15+
self.assertRaises(AssertionError, TOTP, b"secret", algorithm="SHA2")
16+
17+
def test_string_code(self):
18+
htop = HOTP(b"secret")
19+
self.assertEqual(htop.string_code(123), "000123")

0 commit comments

Comments
 (0)