summaryrefslogtreecommitdiff
path: root/tests/test_cli.py
blob: 6e2837838ee992df628996846ae538a6839c7574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# etoolkit
# Copyright (C) 2021-2026 Simeon Simeonov

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""Tests for the CLI (etoolkit.__main__"""

import errno
import os
import unittest.mock

import pytest

import etoolkit
from etoolkit.__main__ import main


@unittest.mock.patch('builtins.input')
def test_decrypt_v1(
    binput: unittest.mock.MagicMock,
    capsys: pytest.CaptureFixture,
    config_file: str,
    master_password: str,
) -> None:
    """Tests v1 decryption via the CLI interface"""
    binput.return_value = (
        'enc-val$1$rye0sMGEnd35gOWyISE1FQa6dzS+8/jf6aopMO5tPr4=$'
        'RjnRY0bUJWFOiejTlM3OhKNimQ=='
    )
    with unittest.mock.patch.dict(
        os.environ, {'ETOOLKIT_MASTER_PASSWORD': master_password}
    ):
        with pytest.raises(SystemExit) as exit_info:
            main(['-c', f'{config_file}', '-d'])
        assert exit_info.value.code == 0
        assert capsys.readouterr().out.strip() == 'Decrypted value: bar'


@unittest.mock.patch('builtins.input')
def test_decrypt_v2(
    binput: unittest.mock.MagicMock,
    capsys: pytest.CaptureFixture,
    config_file: str,
    master_password: str,
    short_encrypted_value: str,
    short_value: str,
) -> None:
    """Tests v2 decryption via the CLI interface"""
    binput.return_value = short_encrypted_value
    with unittest.mock.patch.dict(
        os.environ, {'ETOOLKIT_MASTER_PASSWORD': master_password}
    ):
        with pytest.raises(SystemExit) as exit_info:
            main(['-c', f'{config_file}', '-d'])
        assert exit_info.value.code == 0
        assert capsys.readouterr().out.strip() == (
            f'Decrypted value: {short_value}'
        )


@unittest.mock.patch('os.urandom')
@unittest.mock.patch('builtins.input')
def test_encrypt_with_echo(
    binput: unittest.mock.MagicMock,
    urandom: unittest.mock.MagicMock,
    capsys: pytest.CaptureFixture,
    non_random_bytes_57: bytes,
    config_file: str,
    master_password: str,
    short_encrypted_value: str,
    short_value: str,
) -> None:
    """Tests encryption via the CLI interface"""
    binput.return_value = short_value
    urandom.return_value = non_random_bytes_57
    with unittest.mock.patch.dict(
        os.environ, {'ETOOLKIT_MASTER_PASSWORD': master_password}
    ):
        with pytest.raises(SystemExit) as exit_info:
            main(['-c', f'{config_file}', '-e', '-E'])
        assert exit_info.value.code == 0
        assert capsys.readouterr().out.strip() == (
            f'Encrypted value: {short_encrypted_value}'
        )


@unittest.mock.patch('os.urandom')
@unittest.mock.patch('getpass.getpass')
def test_encrypt_without_echo(
    getpass: unittest.mock.MagicMock,
    urandom: unittest.mock.MagicMock,
    capsys: pytest.CaptureFixture,
    non_random_bytes_57: bytes,
    config_file: str,
    master_password: str,
    short_encrypted_value: str,
    short_value: str,
) -> None:
    """Tests encryption via the CLI interface"""
    getpass.return_value = short_value
    urandom.return_value = non_random_bytes_57
    with unittest.mock.patch.dict(
        os.environ, {'ETOOLKIT_MASTER_PASSWORD': master_password}
    ):
        with pytest.raises(SystemExit) as exit_info:
            main(['-c', f'{config_file}', '-e'])
        assert exit_info.value.code == 0
        assert capsys.readouterr().out.strip() == (
            f'Encrypted value: {short_encrypted_value}'
        )


def test_fetch_encrypted_value(
    config_file: str, master_password: str, short_value: str
) -> None:
    """Tests decryption of encrypted value"""
    with unittest.mock.patch.dict(
        os.environ, {'ETOOLKIT_MASTER_PASSWORD': master_password}
    ):
        assert os.environ.get('ETOOLKIT_TEST_PASSWORD') is None
        with pytest.raises(SystemExit) as exit_info:
            main(['-c', f'{config_file}', '-q', '-s', '/bin/false', 'secret'])
        assert exit_info.value.code == 0
        assert os.environ.get('ETOOLKIT_TEST_PASSWORD') == short_value


def test_list(
    capsys: pytest.CaptureFixture,
    config_file: str,
    nonexistent_config_file: str,
) -> None:
    """Tests list via the CLI interface"""
    with pytest.raises(SystemExit) as exit_info:
        main(['-c', nonexistent_config_file, '-l'])
    assert exit_info.value.code == errno.EIO
    with pytest.raises(SystemExit) as exit_info:
        main(['-c', config_file, '-l'])
    assert exit_info.value.code == 0
    assert capsys.readouterr().out.strip() == f'dev{os.linesep}secret'


def test_help(capsys: pytest.CaptureFixture) -> None:
    """Dummy test checking if the CLI is available at all"""
    with pytest.raises(SystemExit) as exit_info:
        main(['-h'])
    assert exit_info.value.code == 0
    assert capsys.readouterr().out.startswith('usage: etoolkit')


@unittest.mock.patch('os.urandom')
@unittest.mock.patch('getpass.getpass')
def test_generate_master_password_hash(
    getpass: unittest.mock.MagicMock,
    urandom: unittest.mock.MagicMock,
    capsys: pytest.CaptureFixture,
    non_random_bytes_32: bytes,
) -> None:
    """Tests master password hash generation via the CLI interface"""
    getpass.return_value = 'The very secret passwd'
    urandom.return_value = non_random_bytes_32
    with pytest.raises(SystemExit) as exit_info:
        main(['--generate-master-password-hash'])
    assert exit_info.value.code == 0
    assert capsys.readouterr().out.strip() == (
        'Master password hash: pbkdf2_sha256$500000$uYpZM1VfAGq0CDZL2duITs076'
        'CQj+hIFEgx+F4mn80o=$Msl8/5nOBj0TRchykMzXmCXR8VQVyBqUPHe1PDWeJi8='
    )


def test_version(capsys: pytest.CaptureFixture) -> None:
    """Dummy test checking if the CLI is available at all"""
    with pytest.raises(SystemExit) as exit_info:
        main(['-v'])
    assert exit_info.value.code == 0
    assert capsys.readouterr().out.startswith(
        f'etoolkit {etoolkit.__version__}'
    )