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.py97
1 files changed, 96 insertions, 1 deletions
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 1bd9be1..884e676 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -1,5 +1,5 @@
1# etoolkit 1# etoolkit
2# Copyright (C) 2021 Simeon Simeonov 2# Copyright (C) 2021-2022 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,12 +14,86 @@
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__"""
17import errno
18import os
19import unittest.mock
20
17import pytest 21import pytest
18 22
19import etoolkit 23import etoolkit
20from etoolkit.__main__ import main 24from etoolkit.__main__ import main
21 25
22 26
27@unittest.mock.patch('os.urandom')
28@unittest.mock.patch('builtins.input')
29def test_decrypt(binput, urandom, capsys, non_random_bytes_32, config_file):
30 """Tests encryption via the CLI interface"""
31 urandom.return_value = non_random_bytes_32
32 binput.return_value = (
33 'enc-val$1$uYpZM1VfAGq0CDZL2duITs076CQj+'
34 'hIFEgx+F4mn80o=$xdF/1S+R2MGlEQMCOLG6OjEuzw=='
35 )
36 with unittest.mock.patch.dict(
37 os.environ, {'ETOOLKIT_MASTER_PASSWORD': 'the very secret passwd'}
38 ):
39 with pytest.raises(SystemExit) as exit_info:
40 main(['-c', f'{config_file}', '-d'])
41 assert exit_info.type == SystemExit
42 assert exit_info.value.code == 0
43 assert capsys.readouterr().out.strip() == 'Decrypted value: bar'
44
45
46@unittest.mock.patch('os.urandom')
47@unittest.mock.patch('builtins.input', lambda *args: 'bar')
48def test_encrypt_with_echo(urandom, capsys, non_random_bytes_32, config_file):
49 """Tests encryption via the CLI interface"""
50 urandom.return_value = non_random_bytes_32
51 with unittest.mock.patch.dict(
52 os.environ, {'ETOOLKIT_MASTER_PASSWORD': 'the very secret passwd'}
53 ):
54 with pytest.raises(SystemExit) as exit_info:
55 main(['-c', f'{config_file}', '-e', '-E'])
56 assert exit_info.type == SystemExit
57 assert exit_info.value.code == 0
58 assert capsys.readouterr().out.strip() == (
59 'Encrypted value: enc-val$1$uYpZM1VfAGq0CDZL2duITs076CQj+'
60 'hIFEgx+F4mn80o=$xdF/1S+R2MGlEQMCOLG6OjEuzw=='
61 )
62
63
64@unittest.mock.patch('os.urandom')
65@unittest.mock.patch('getpass.getpass', lambda *args: 'bar')
66def test_encrypt_without_echo(gpass, capsys, non_random_bytes_32, config_file):
67 """Tests encryption via the CLI interface"""
68 gpass.return_value = non_random_bytes_32
69 with unittest.mock.patch.dict(
70 os.environ, {'ETOOLKIT_MASTER_PASSWORD': 'the very secret passwd'}
71 ):
72 with pytest.raises(SystemExit) as exit_info:
73 main(['-c', f'{config_file}', '-e'])
74 assert exit_info.type == SystemExit
75 assert exit_info.value.code == 0
76 assert capsys.readouterr().out.strip() == (
77 'Encrypted value: enc-val$1$uYpZM1VfAGq0CDZL2duITs076CQj+'
78 'hIFEgx+F4mn80o=$xdF/1S+R2MGlEQMCOLG6OjEuzw=='
79 )
80
81
82def test_list(capsys, config_file):
83 """Tests list via the CLI interface"""
84 with pytest.raises(SystemExit) as exit_info:
85 main(['-l'])
86 assert exit_info.type == SystemExit
87 assert exit_info.value.code == errno.EIO
88 with pytest.raises(SystemExit) as exit_info:
89 main(['-c', f'{config_file}', '-l'])
90 assert exit_info.type == SystemExit
91 assert exit_info.value.code == 0
92 assert capsys.readouterr().out.strip() == (
93 f'default{os.linesep}dev{os.linesep}secret'
94 )
95
96
23def test_help(capsys): 97def test_help(capsys):
24 """Dummy test checking if the CLI is available at all""" 98 """Dummy test checking if the CLI is available at all"""
25 with pytest.raises(SystemExit) as exit_info: 99 with pytest.raises(SystemExit) as exit_info:
@@ -29,6 +103,27 @@ def test_help(capsys):
29 assert capsys.readouterr().out.startswith('usage: etoolkit') 103 assert capsys.readouterr().out.startswith('usage: etoolkit')
30 104
31 105
106@unittest.mock.patch('os.urandom')
107@unittest.mock.patch('getpass.getpass')
108def test_generate_master_password_hash(
109 gpass,
110 urandom,
111 capsys,
112 non_random_bytes_32,
113):
114 """Tests master password hash generation via the CLI interface"""
115 urandom.return_value = non_random_bytes_32
116 gpass.return_value = 'The very secret passwd'
117 with pytest.raises(SystemExit) as exit_info:
118 main(['--generate-master-password-hash'])
119 assert exit_info.type == SystemExit
120 assert exit_info.value.code == 0
121 assert capsys.readouterr().out.strip() == (
122 'Master password hash: pbkdf2_sha256$100000$uYpZM1VfAGq0CDZL2duITs076'
123 'CQj+hIFEgx+F4mn80o=$h3PSPLCd37fP15zKdW4CBGn7CXE+q5UiydaF3vbeZHo='
124 )
125
126
32def test_version(capsys): 127def test_version(capsys):
33 """Dummy test checking if the CLI is available at all""" 128 """Dummy test checking if the CLI is available at all"""
34 with pytest.raises(SystemExit) as exit_info: 129 with pytest.raises(SystemExit) as exit_info: