Skip to content

Commit 02385f0

Browse files
authored
Add more tests for the user interface (#7)
* Add more tests for the user interface * Add command line tests * Add print statement * Only write config if it does not exist * black formating * clean up two factor code * fix test config * try to include command line tests in coverage statistics * Remove multiprocessing flag * no need to combine * test cmd separately * Remove cmd * black formating * separate coverage calls * separate coverage calls * Update coverage.yml * Update unittest.yml * Update coverage.yml * integrate in test * multi flag * Add period
1 parent 8f2602f commit 02385f0

File tree

8 files changed

+83
-8
lines changed

8 files changed

+83
-8
lines changed

.coveragerc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# .coveragerc to control coverage.py
22
[run]
33
source = pyauthenticator
4-
omit = pyauthenticator/_version.py
4+
omit = pyauthenticator/_version.py
5+
concurrency = multiprocessing

.github/workflows/coverage.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ jobs:
2626
pip install --no-deps .
2727
- name: Test
2828
shell: bash -l {0}
29-
run: coverage run --omit pyauthenticator/_version.py -m unittest discover tests
29+
run: |
30+
coverage run -a -m unittest discover tests
31+
coverage combine
3032
- name: Coverage
3133
shell: bash -l {0}
3234
run: |
33-
coveralls
35+
coveralls

.github/workflows/unittest.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ jobs:
3232
pip install --no-deps .
3333
- name: Test
3434
shell: bash -l {0}
35-
run: coverage run --omit pyauthenticator/_version.py -m unittest discover tests
35+
run: |
36+
coverage run -a -m unittest discover tests

pyauthenticator/cmd.py pyauthenticator/__main__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,4 @@ def main():
4444
print(get_two_factor_code(key=args.service, config_dict=load_config()))
4545

4646

47-
if __name__ == "__main__":
48-
main()
47+
main()

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
cmdclass=versioneer.get_cmdclass(),
2424
entry_points={
2525
"console_scripts": [
26-
'pyauthenticator=pyauthenticator.cmd:main'
26+
'pyauthenticator=pyauthenticator:main'
2727
]
2828
}
2929
)

tests/test_cmd.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import unittest
2+
import os
3+
import subprocess
4+
import json
5+
from pyauthenticator.share import expand_path, write_config, config_file
6+
7+
8+
class MyTestCase(unittest.TestCase):
9+
@classmethod
10+
def setUpClass(cls):
11+
cls.config_dict = {
12+
"test": "otpauth://totp/Test%3A%20root%40github.com?secret=6IQXETC4ADOSMMUN&issuer=Test"
13+
}
14+
cls.config_path = expand_path(config_file)
15+
if not os.path.exists(cls.config_path):
16+
write_config(
17+
config_dict=cls.config_dict
18+
)
19+
20+
def test_main_generate_two_factor(self):
21+
code = subprocess.check_output(
22+
["coverage", "run", "-a", "pyauthenticator", "test"],
23+
universal_newlines=True
24+
)
25+
self.assertEqual(len(code.replace("\n", "")), 6)
26+
27+
def test_main_generate_qr_code(self):
28+
subprocess.check_output(
29+
["coverage", "run", "-a", "pyauthenticator", "-qr", "test"],
30+
universal_newlines=True
31+
)
32+
self.assertTrue(os.path.exists("test.png"))
33+
subprocess.check_output(
34+
["coverage", "run", "-a", "pyauthenticator", "-a", "test.png", "test2"],
35+
universal_newlines=True
36+
)
37+
with open(self.config_path, "r") as f:
38+
config_dict = json.load(f)
39+
self.assertEqual(len(config_dict.keys()), 2)
40+
41+
42+
if __name__ == '__main__':
43+
unittest.main()

tests/test_core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TestCore(unittest.TestCase):
1111
def setUpClass(cls):
1212
cls.qr_code_png = "test.png"
1313
cls.config_dict = {
14-
"test": "otpauth://totp/Test%3A%20root%40github.com?secret=6IQXETC4ADOSMMUN&issuer=Test"
14+
"test": "otpauth://totp/Test%3A%20root%40github.com?secret=6IQXETC4ADOSMMUN&issuer=Test&period=60"
1515
}
1616
generate_qrcode(
1717
key="test",

tests/test_user_interface.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import unittest
2+
import os
3+
from pyauthenticator import get_two_factor_code, write_qrcode_to_file
4+
from pyauthenticator.share import expand_path, write_config, config_file
5+
6+
7+
class TestUserInterface(unittest.TestCase):
8+
@classmethod
9+
def setUpClass(cls):
10+
cls.config_dict = {
11+
"test": "otpauth://totp/Test%3A%20root%40github.com?secret=6IQXETC4ADOSMMUN&issuer=Test"
12+
}
13+
cls.config_path = expand_path(config_file)
14+
if not os.path.exists(cls.config_path):
15+
write_config(
16+
config_dict=cls.config_dict
17+
)
18+
19+
def test_get_two_factor_code(self):
20+
code = get_two_factor_code(service="test")
21+
self.assertEqual(len(code), 6)
22+
23+
def test_write_qrcode_to_file(self):
24+
write_qrcode_to_file(service="test")
25+
self.assertTrue(os.path.exists("test.png"))
26+
27+
28+
if __name__ == '__main__':
29+
unittest.main()

0 commit comments

Comments
 (0)