summaryrefslogtreecommitdiff
path: root/tests/test_cli.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_cli.py')
-rw-r--r--tests/test_cli.py60
1 files changed, 38 insertions, 22 deletions
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 684c2e7..e5db244 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -1,5 +1,5 @@
1# etoolkit 1# etoolkit
2# Copyright (C) 2021-2022 Simeon Simeonov 2# Copyright (C) 2021-2024 Simeon Simeonov
3 3
4# This program is free software: you can redistribute it and/or modify 4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by 5# it under the terms of the GNU General Public License as published by
@@ -14,6 +14,7 @@
14# You should have received a copy of the GNU General Public License 14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>. 15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16"""Tests for the CLI (etoolkit.__main__""" 16"""Tests for the CLI (etoolkit.__main__"""
17
17import errno 18import errno
18import os 19import os
19import unittest.mock 20import unittest.mock
@@ -24,11 +25,9 @@ import etoolkit
24from etoolkit.__main__ import main 25from etoolkit.__main__ import main
25 26
26 27
27@unittest.mock.patch('os.urandom')
28@unittest.mock.patch('builtins.input') 28@unittest.mock.patch('builtins.input')
29def test_decrypt(binput, urandom, capsys, non_random_bytes_32, config_file): 29def test_decrypt_v1(binput, capsys, config_file):
30 """Tests encryption via the CLI interface""" 30 """Tests v1 decryption via the CLI interface"""
31 urandom.return_value = non_random_bytes_32
32 binput.return_value = ( 31 binput.return_value = (
33 'enc-val$1$uYpZM1VfAGq0CDZL2duITs076CQj+' 32 'enc-val$1$uYpZM1VfAGq0CDZL2duITs076CQj+'
34 'hIFEgx+F4mn80o=$xdF/1S+R2MGlEQMCOLG6OjEuzw==' 33 'hIFEgx+F4mn80o=$xdF/1S+R2MGlEQMCOLG6OjEuzw=='
@@ -43,11 +42,29 @@ def test_decrypt(binput, urandom, capsys, non_random_bytes_32, config_file):
43 assert capsys.readouterr().out.strip() == 'Decrypted value: bar' 42 assert capsys.readouterr().out.strip() == 'Decrypted value: bar'
44 43
45 44
45@unittest.mock.patch('builtins.input')
46def test_decrypt_v2(binput, capsys, config_file):
47 """Tests v2 decryption via the CLI interface"""
48 binput.return_value = (
49 'enc-val$2$RCSZqq9pWrRDoCVYVHopyu1LzaJGfv8roVviq'
50 'rLTBxM=$VW3UZ6l12yDtyaqWHb7i0QEDiS9s9np'
51 '7huAACK54BtZVV7RZoIhbu4K6zZuz+LRCyio='
52 )
53 with unittest.mock.patch.dict(
54 os.environ, {'ETOOLKIT_MASTER_PASSWORD': 'the very secret passwd'}
55 ):
56 with pytest.raises(SystemExit) as exit_info:
57 main(['-c', f'{config_file}', '-d'])
58 assert exit_info.type == SystemExit
59 assert exit_info.value.code == 0
60 assert capsys.readouterr().out.strip() == 'Decrypted value: bar'
61
62
46@unittest.mock.patch('os.urandom') 63@unittest.mock.patch('os.urandom')
47@unittest.mock.patch('builtins.input', lambda *args: 'bar') 64@unittest.mock.patch('builtins.input', lambda *args: 'bar')
48def test_encrypt_with_echo(urandom, capsys, non_random_bytes_32, config_file): 65def test_encrypt_with_echo(urandom, capsys, non_random_bytes_61, config_file):
49 """Tests encryption via the CLI interface""" 66 """Tests encryption via the CLI interface"""
50 urandom.return_value = non_random_bytes_32 67 urandom.return_value = non_random_bytes_61
51 with unittest.mock.patch.dict( 68 with unittest.mock.patch.dict(
52 os.environ, {'ETOOLKIT_MASTER_PASSWORD': 'the very secret passwd'} 69 os.environ, {'ETOOLKIT_MASTER_PASSWORD': 'the very secret passwd'}
53 ): 70 ):
@@ -56,16 +73,17 @@ def test_encrypt_with_echo(urandom, capsys, non_random_bytes_32, config_file):
56 assert exit_info.type == SystemExit 73 assert exit_info.type == SystemExit
57 assert exit_info.value.code == 0 74 assert exit_info.value.code == 0
58 assert capsys.readouterr().out.strip() == ( 75 assert capsys.readouterr().out.strip() == (
59 'Encrypted value: enc-val$1$uYpZM1VfAGq0CDZL2duITs076CQj+' 76 'Encrypted value: enc-val$2$RCSZqq9pWrRDoCVYVHopyu1LzaJGfv8roVviq'
60 'hIFEgx+F4mn80o=$xdF/1S+R2MGlEQMCOLG6OjEuzw==' 77 'rLTBxM=$VW3UZ6l12yDtyaqWHb7i0QEDiS9s9np'
78 '7huAACK54BtZVV7RZoIhbu4K6zZuz+LRCyio='
61 ) 79 )
62 80
63 81
64@unittest.mock.patch('os.urandom') 82@unittest.mock.patch('os.urandom')
65@unittest.mock.patch('getpass.getpass', lambda *args: 'bar') 83@unittest.mock.patch('getpass.getpass', lambda *args: 'bar')
66def test_encrypt_without_echo(gpass, capsys, non_random_bytes_32, config_file): 84def test_encrypt_without_echo(gpass, capsys, non_random_bytes_61, config_file):
67 """Tests encryption via the CLI interface""" 85 """Tests encryption via the CLI interface"""
68 gpass.return_value = non_random_bytes_32 86 gpass.return_value = non_random_bytes_61
69 with unittest.mock.patch.dict( 87 with unittest.mock.patch.dict(
70 os.environ, {'ETOOLKIT_MASTER_PASSWORD': 'the very secret passwd'} 88 os.environ, {'ETOOLKIT_MASTER_PASSWORD': 'the very secret passwd'}
71 ): 89 ):
@@ -74,19 +92,20 @@ def test_encrypt_without_echo(gpass, capsys, non_random_bytes_32, config_file):
74 assert exit_info.type == SystemExit 92 assert exit_info.type == SystemExit
75 assert exit_info.value.code == 0 93 assert exit_info.value.code == 0
76 assert capsys.readouterr().out.strip() == ( 94 assert capsys.readouterr().out.strip() == (
77 'Encrypted value: enc-val$1$uYpZM1VfAGq0CDZL2duITs076CQj+' 95 'Encrypted value: enc-val$2$RCSZqq9pWrRDoCVYVHopyu1LzaJGfv8roVviq'
78 'hIFEgx+F4mn80o=$xdF/1S+R2MGlEQMCOLG6OjEuzw==' 96 'rLTBxM=$VW3UZ6l12yDtyaqWHb7i0QEDiS9s9np'
97 '7huAACK54BtZVV7RZoIhbu4K6zZuz+LRCyio='
79 ) 98 )
80 99
81 100
82def test_list(capsys, config_file): 101def test_list(capsys, config_file, nonexistent_config_file):
83 """Tests list via the CLI interface""" 102 """Tests list via the CLI interface"""
84 with pytest.raises(SystemExit) as exit_info: 103 with pytest.raises(SystemExit) as exit_info:
85 main(['-l']) 104 main(['-c', nonexistent_config_file, '-l'])
86 assert exit_info.type == SystemExit 105 assert exit_info.type == SystemExit
87 assert exit_info.value.code == errno.EIO 106 assert exit_info.value.code == errno.EIO
88 with pytest.raises(SystemExit) as exit_info: 107 with pytest.raises(SystemExit) as exit_info:
89 main(['-c', f'{config_file}', '-l']) 108 main(['-c', config_file, '-l'])
90 assert exit_info.type == SystemExit 109 assert exit_info.type == SystemExit
91 assert exit_info.value.code == 0 110 assert exit_info.value.code == 0
92 assert capsys.readouterr().out.strip() == f'dev{os.linesep}secret' 111 assert capsys.readouterr().out.strip() == f'dev{os.linesep}secret'
@@ -104,10 +123,7 @@ def test_help(capsys):
104@unittest.mock.patch('os.urandom') 123@unittest.mock.patch('os.urandom')
105@unittest.mock.patch('getpass.getpass') 124@unittest.mock.patch('getpass.getpass')
106def test_generate_master_password_hash( 125def test_generate_master_password_hash(
107 gpass, 126 gpass, urandom, capsys, non_random_bytes_32
108 urandom,
109 capsys,
110 non_random_bytes_32,
111): 127):
112 """Tests master password hash generation via the CLI interface""" 128 """Tests master password hash generation via the CLI interface"""
113 urandom.return_value = non_random_bytes_32 129 urandom.return_value = non_random_bytes_32
@@ -117,8 +133,8 @@ def test_generate_master_password_hash(
117 assert exit_info.type == SystemExit 133 assert exit_info.type == SystemExit
118 assert exit_info.value.code == 0 134 assert exit_info.value.code == 0
119 assert capsys.readouterr().out.strip() == ( 135 assert capsys.readouterr().out.strip() == (
120 'Master password hash: pbkdf2_sha256$100000$uYpZM1VfAGq0CDZL2duITs076' 136 'Master password hash: pbkdf2_sha256$500000$uYpZM1VfAGq0CDZL2duITs076'
121 'CQj+hIFEgx+F4mn80o=$h3PSPLCd37fP15zKdW4CBGn7CXE+q5UiydaF3vbeZHo=' 137 'CQj+hIFEgx+F4mn80o=$Msl8/5nOBj0TRchykMzXmCXR8VQVyBqUPHe1PDWeJi8='
122 ) 138 )
123 139
124 140