summaryrefslogtreecommitdiff
path: root/tests/test_cli.py
diff options
context:
space:
mode:
authorSimeon Simeonov2024-04-30 15:39:29 +0200
committerSimeon Simeonov2024-04-30 15:39:29 +0200
commitbb9a844e22134a2537652ea14f93e82acb4ee380 (patch)
treebdc8921ba20bf7c9f3d31e8cad4dd4b2abe86df8 /tests/test_cli.py
parentc31fa58c85e866b3a5ab04882c7aff655f0b5477 (diff)
Improve tests
Diffstat (limited to 'tests/test_cli.py')
-rw-r--r--tests/test_cli.py67
1 files changed, 46 insertions, 21 deletions
diff --git a/tests/test_cli.py b/tests/test_cli.py
index e5db244..1693a2b 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -26,14 +26,15 @@ from etoolkit.__main__ import main
26 26
27 27
28@unittest.mock.patch('builtins.input') 28@unittest.mock.patch('builtins.input')
29def test_decrypt_v1(binput, capsys, config_file): 29def test_decrypt_v1(binput, capsys, config_file, master_password):
30 """Tests v1 decryption via the CLI interface""" 30 """Tests v1 decryption via the CLI interface"""
31
31 binput.return_value = ( 32 binput.return_value = (
32 'enc-val$1$uYpZM1VfAGq0CDZL2duITs076CQj+' 33 'enc-val$1$rye0sMGEnd35gOWyISE1FQa6dzS+8/jf6aopMO5tPr4=$'
33 'hIFEgx+F4mn80o=$xdF/1S+R2MGlEQMCOLG6OjEuzw==' 34 'RjnRY0bUJWFOiejTlM3OhKNimQ=='
34 ) 35 )
35 with unittest.mock.patch.dict( 36 with unittest.mock.patch.dict(
36 os.environ, {'ETOOLKIT_MASTER_PASSWORD': 'the very secret passwd'} 37 os.environ, {'ETOOLKIT_MASTER_PASSWORD': master_password}
37 ): 38 ):
38 with pytest.raises(SystemExit) as exit_info: 39 with pytest.raises(SystemExit) as exit_info:
39 main(['-c', f'{config_file}', '-d']) 40 main(['-c', f'{config_file}', '-d'])
@@ -43,15 +44,16 @@ def test_decrypt_v1(binput, capsys, config_file):
43 44
44 45
45@unittest.mock.patch('builtins.input') 46@unittest.mock.patch('builtins.input')
46def test_decrypt_v2(binput, capsys, config_file): 47def test_decrypt_v2(binput, capsys, config_file, master_password):
47 """Tests v2 decryption via the CLI interface""" 48 """Tests v2 decryption via the CLI interface"""
49
48 binput.return_value = ( 50 binput.return_value = (
49 'enc-val$2$RCSZqq9pWrRDoCVYVHopyu1LzaJGfv8roVviq' 51 'enc-val$2$RCSZqq9pWrRDoCVYVHopyu1LzaJGfv8roVviq'
50 'rLTBxM=$VW3UZ6l12yDtyaqWHb7i0QEDiS9s9np' 52 'rLTBxM=$+Yo6Ya2MAVcBLTQHuATkyFc+dzYsL/E'
51 '7huAACK54BtZVV7RZoIhbu4K6zZuz+LRCyio=' 53 'SvA6ofOUDsiKZvIff35cUHAmoNxVuGG+MXv4='
52 ) 54 )
53 with unittest.mock.patch.dict( 55 with unittest.mock.patch.dict(
54 os.environ, {'ETOOLKIT_MASTER_PASSWORD': 'the very secret passwd'} 56 os.environ, {'ETOOLKIT_MASTER_PASSWORD': master_password}
55 ): 57 ):
56 with pytest.raises(SystemExit) as exit_info: 58 with pytest.raises(SystemExit) as exit_info:
57 main(['-c', f'{config_file}', '-d']) 59 main(['-c', f'{config_file}', '-d'])
@@ -61,12 +63,16 @@ def test_decrypt_v2(binput, capsys, config_file):
61 63
62 64
63@unittest.mock.patch('os.urandom') 65@unittest.mock.patch('os.urandom')
64@unittest.mock.patch('builtins.input', lambda *args: 'bar') 66@unittest.mock.patch('builtins.input')
65def test_encrypt_with_echo(urandom, capsys, non_random_bytes_61, config_file): 67def test_encrypt_with_echo(
68 binput, urandom, capsys, non_random_bytes_61, config_file, master_password
69):
66 """Tests encryption via the CLI interface""" 70 """Tests encryption via the CLI interface"""
71
72 binput.return_value = 'bar'
67 urandom.return_value = non_random_bytes_61 73 urandom.return_value = non_random_bytes_61
68 with unittest.mock.patch.dict( 74 with unittest.mock.patch.dict(
69 os.environ, {'ETOOLKIT_MASTER_PASSWORD': 'the very secret passwd'} 75 os.environ, {'ETOOLKIT_MASTER_PASSWORD': master_password}
70 ): 76 ):
71 with pytest.raises(SystemExit) as exit_info: 77 with pytest.raises(SystemExit) as exit_info:
72 main(['-c', f'{config_file}', '-e', '-E']) 78 main(['-c', f'{config_file}', '-e', '-E'])
@@ -74,18 +80,22 @@ def test_encrypt_with_echo(urandom, capsys, non_random_bytes_61, config_file):
74 assert exit_info.value.code == 0 80 assert exit_info.value.code == 0
75 assert capsys.readouterr().out.strip() == ( 81 assert capsys.readouterr().out.strip() == (
76 'Encrypted value: enc-val$2$RCSZqq9pWrRDoCVYVHopyu1LzaJGfv8roVviq' 82 'Encrypted value: enc-val$2$RCSZqq9pWrRDoCVYVHopyu1LzaJGfv8roVviq'
77 'rLTBxM=$VW3UZ6l12yDtyaqWHb7i0QEDiS9s9np' 83 'rLTBxM=$+Yo6Ya2MAVcBLTQHuATkyFc+dzYsL/E'
78 '7huAACK54BtZVV7RZoIhbu4K6zZuz+LRCyio=' 84 'SvA6ofOUDsiKZvIff35cUHAmoNxVuGG+MXv4='
79 ) 85 )
80 86
81 87
82@unittest.mock.patch('os.urandom') 88@unittest.mock.patch('os.urandom')
83@unittest.mock.patch('getpass.getpass', lambda *args: 'bar') 89@unittest.mock.patch('getpass.getpass')
84def test_encrypt_without_echo(gpass, capsys, non_random_bytes_61, config_file): 90def test_encrypt_without_echo(
91 getpass, urandom, capsys, non_random_bytes_61, config_file, master_password
92):
85 """Tests encryption via the CLI interface""" 93 """Tests encryption via the CLI interface"""
86 gpass.return_value = non_random_bytes_61 94
95 getpass.return_value = 'bar'
96 urandom.return_value = non_random_bytes_61
87 with unittest.mock.patch.dict( 97 with unittest.mock.patch.dict(
88 os.environ, {'ETOOLKIT_MASTER_PASSWORD': 'the very secret passwd'} 98 os.environ, {'ETOOLKIT_MASTER_PASSWORD': master_password}
89 ): 99 ):
90 with pytest.raises(SystemExit) as exit_info: 100 with pytest.raises(SystemExit) as exit_info:
91 main(['-c', f'{config_file}', '-e']) 101 main(['-c', f'{config_file}', '-e'])
@@ -93,13 +103,25 @@ def test_encrypt_without_echo(gpass, capsys, non_random_bytes_61, config_file):
93 assert exit_info.value.code == 0 103 assert exit_info.value.code == 0
94 assert capsys.readouterr().out.strip() == ( 104 assert capsys.readouterr().out.strip() == (
95 'Encrypted value: enc-val$2$RCSZqq9pWrRDoCVYVHopyu1LzaJGfv8roVviq' 105 'Encrypted value: enc-val$2$RCSZqq9pWrRDoCVYVHopyu1LzaJGfv8roVviq'
96 'rLTBxM=$VW3UZ6l12yDtyaqWHb7i0QEDiS9s9np' 106 'rLTBxM=$+Yo6Ya2MAVcBLTQHuATkyFc+dzYsL/E'
97 '7huAACK54BtZVV7RZoIhbu4K6zZuz+LRCyio=' 107 'SvA6ofOUDsiKZvIff35cUHAmoNxVuGG+MXv4='
98 ) 108 )
99 109
100 110
111def test_fetch_encrypted_value(config_file, master_password):
112 """Tests decryption of encrypted value"""
113
114 with unittest.mock.patch.dict(
115 os.environ, {'ETOOLKIT_MASTER_PASSWORD': master_password}
116 ):
117 assert os.environ.get('ETOOLKIT_TEST_PASSWORD') is None
118 main(['-c', f'{config_file}', '-q', '-s', '/bin/false', 'secret'])
119 assert os.environ.get('ETOOLKIT_TEST_PASSWORD') == 'bar'
120
121
101def test_list(capsys, config_file, nonexistent_config_file): 122def test_list(capsys, config_file, nonexistent_config_file):
102 """Tests list via the CLI interface""" 123 """Tests list via the CLI interface"""
124
103 with pytest.raises(SystemExit) as exit_info: 125 with pytest.raises(SystemExit) as exit_info:
104 main(['-c', nonexistent_config_file, '-l']) 126 main(['-c', nonexistent_config_file, '-l'])
105 assert exit_info.type == SystemExit 127 assert exit_info.type == SystemExit
@@ -113,6 +135,7 @@ def test_list(capsys, config_file, nonexistent_config_file):
113 135
114def test_help(capsys): 136def test_help(capsys):
115 """Dummy test checking if the CLI is available at all""" 137 """Dummy test checking if the CLI is available at all"""
138
116 with pytest.raises(SystemExit) as exit_info: 139 with pytest.raises(SystemExit) as exit_info:
117 main(['-h']) 140 main(['-h'])
118 assert exit_info.type == SystemExit 141 assert exit_info.type == SystemExit
@@ -123,11 +146,12 @@ def test_help(capsys):
123@unittest.mock.patch('os.urandom') 146@unittest.mock.patch('os.urandom')
124@unittest.mock.patch('getpass.getpass') 147@unittest.mock.patch('getpass.getpass')
125def test_generate_master_password_hash( 148def test_generate_master_password_hash(
126 gpass, urandom, capsys, non_random_bytes_32 149 getpass, urandom, capsys, non_random_bytes_32
127): 150):
128 """Tests master password hash generation via the CLI interface""" 151 """Tests master password hash generation via the CLI interface"""
152
153 getpass.return_value = 'The very secret passwd'
129 urandom.return_value = non_random_bytes_32 154 urandom.return_value = non_random_bytes_32
130 gpass.return_value = 'The very secret passwd'
131 with pytest.raises(SystemExit) as exit_info: 155 with pytest.raises(SystemExit) as exit_info:
132 main(['--generate-master-password-hash']) 156 main(['--generate-master-password-hash'])
133 assert exit_info.type == SystemExit 157 assert exit_info.type == SystemExit
@@ -140,6 +164,7 @@ def test_generate_master_password_hash(
140 164
141def test_version(capsys): 165def test_version(capsys):
142 """Dummy test checking if the CLI is available at all""" 166 """Dummy test checking if the CLI is available at all"""
167
143 with pytest.raises(SystemExit) as exit_info: 168 with pytest.raises(SystemExit) as exit_info:
144 main(['-v']) 169 main(['-v'])
145 assert exit_info.type == SystemExit 170 assert exit_info.type == SystemExit