-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: adding tests, and fix to_uri method
- Loading branch information
Showing
6 changed files
with
93 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
*.pyc | ||
*.pyo | ||
*.egg-info | ||
*.coverage | ||
__pycache__ | ||
bin | ||
build | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import unittest | ||
from otpauth import HOTP | ||
|
||
|
||
class TestHOTP(unittest.TestCase): | ||
def setUp(self): | ||
self.hotp = HOTP(b"python") | ||
|
||
def test_generate(self): | ||
value = self.hotp.generate(0) | ||
self.assertEqual(value, 170566) | ||
|
||
def test_verify(self): | ||
# due to number too long | ||
self.assertFalse(self.hotp.verify(12345678, 0)) | ||
|
||
# due to not match | ||
self.assertFalse(self.hotp.verify(12345, 0)) | ||
|
||
self.assertTrue(self.hotp.verify(170566, 0)) | ||
|
||
def test_to_uri(self): | ||
uri = self.hotp.to_uri("Typlog:lepture.com", "Authlib", 0) | ||
expected = "otpauth://hotp/Typlog:lepture.com?secret=OB4XI2DPNY&issuer=Authlib&algorithm=SHA1&digits=6&counter=0" | ||
self.assertEqual(uri, expected) | ||
|
||
def test_from_b32encode(self): | ||
expected = "otpauth://hotp/Typlog:lepture.com?secret=OB4XI2DPNY&issuer=Authlib&algorithm=SHA1&digits=6&counter=0" | ||
|
||
hotp = HOTP.from_b32encode("OB4XI2DPNY") | ||
value = hotp.generate(0) | ||
self.assertEqual(value, 170566) | ||
uri = self.hotp.to_uri("Typlog:lepture.com", "Authlib", 0) | ||
self.assertEqual(uri, expected) | ||
|
||
hotp = HOTP.from_b32encode("OB4XI2DPNY======") | ||
value = hotp.generate(0) | ||
self.assertEqual(value, 170566) | ||
uri = self.hotp.to_uri("Typlog:lepture.com", "Authlib", 0) | ||
self.assertEqual(uri, expected) | ||
|
||
hotp = HOTP.from_b32encode(b"OB4XI2DPNY======") | ||
value = hotp.generate(0) | ||
self.assertEqual(value, 170566) | ||
uri = self.hotp.to_uri("Typlog:lepture.com", "Authlib", 0) | ||
self.assertEqual(uri, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import unittest | ||
import time | ||
from otpauth import TOTP | ||
|
||
FIXED_TIME = 1679576495 | ||
|
||
|
||
class TestTOTP(unittest.TestCase): | ||
def setUp(self): | ||
self.totp = TOTP(b"python") | ||
|
||
def test_generate(self): | ||
value = self.totp.generate(FIXED_TIME) | ||
self.assertEqual(value, 129815) | ||
|
||
def test_verify(self): | ||
# due to number too long | ||
self.assertFalse(self.totp.verify(12345678, FIXED_TIME)) | ||
|
||
# due to not match | ||
self.assertFalse(self.totp.verify(12345, FIXED_TIME)) | ||
|
||
self.assertTrue(self.totp.verify(129815, FIXED_TIME)) | ||
|
||
def test_to_uri(self): | ||
uri = self.totp.to_uri("Typlog:lepture.com", "Authlib") | ||
expected = "otpauth://totp/Typlog:lepture.com?secret=OB4XI2DPNY&issuer=Authlib&algorithm=SHA1&digits=6&period=30" | ||
self.assertEqual(uri, expected) | ||
|
||
def test_current_timestamp(self): | ||
value = self.totp.generate() | ||
self.assertTrue(self.totp.verify(value)) |