diff options
Diffstat (limited to 'tests/test_cli.py')
| -rw-r--r-- | tests/test_cli.py | 91 |
1 files changed, 49 insertions, 42 deletions
diff --git a/tests/test_cli.py b/tests/test_cli.py index b7913da..6e28378 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py | |||
| @@ -26,9 +26,13 @@ from etoolkit.__main__ import main | |||
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | @unittest.mock.patch('builtins.input') | 28 | @unittest.mock.patch('builtins.input') |
| 29 | def test_decrypt_v1(binput, capsys, config_file, master_password): | 29 | def test_decrypt_v1( |
| 30 | binput: unittest.mock.MagicMock, | ||
| 31 | capsys: pytest.CaptureFixture, | ||
| 32 | config_file: str, | ||
| 33 | master_password: str, | ||
| 34 | ) -> None: | ||
| 30 | """Tests v1 decryption via the CLI interface""" | 35 | """Tests v1 decryption via the CLI interface""" |
| 31 | |||
| 32 | binput.return_value = ( | 36 | binput.return_value = ( |
| 33 | 'enc-val$1$rye0sMGEnd35gOWyISE1FQa6dzS+8/jf6aopMO5tPr4=$' | 37 | 'enc-val$1$rye0sMGEnd35gOWyISE1FQa6dzS+8/jf6aopMO5tPr4=$' |
| 34 | 'RjnRY0bUJWFOiejTlM3OhKNimQ==' | 38 | 'RjnRY0bUJWFOiejTlM3OhKNimQ==' |
| @@ -44,15 +48,14 @@ def test_decrypt_v1(binput, capsys, config_file, master_password): | |||
| 44 | 48 | ||
| 45 | @unittest.mock.patch('builtins.input') | 49 | @unittest.mock.patch('builtins.input') |
| 46 | def test_decrypt_v2( | 50 | def test_decrypt_v2( |
| 47 | binput, | 51 | binput: unittest.mock.MagicMock, |
| 48 | capsys, | 52 | capsys: pytest.CaptureFixture, |
| 49 | config_file, | 53 | config_file: str, |
| 50 | master_password, | 54 | master_password: str, |
| 51 | short_encrypted_value, | 55 | short_encrypted_value: str, |
| 52 | short_value, | 56 | short_value: str, |
| 53 | ): | 57 | ) -> None: |
| 54 | """Tests v2 decryption via the CLI interface""" | 58 | """Tests v2 decryption via the CLI interface""" |
| 55 | |||
| 56 | binput.return_value = short_encrypted_value | 59 | binput.return_value = short_encrypted_value |
| 57 | with unittest.mock.patch.dict( | 60 | with unittest.mock.patch.dict( |
| 58 | os.environ, {'ETOOLKIT_MASTER_PASSWORD': master_password} | 61 | os.environ, {'ETOOLKIT_MASTER_PASSWORD': master_password} |
| @@ -68,17 +71,16 @@ def test_decrypt_v2( | |||
| 68 | @unittest.mock.patch('os.urandom') | 71 | @unittest.mock.patch('os.urandom') |
| 69 | @unittest.mock.patch('builtins.input') | 72 | @unittest.mock.patch('builtins.input') |
| 70 | def test_encrypt_with_echo( | 73 | def test_encrypt_with_echo( |
| 71 | binput, | 74 | binput: unittest.mock.MagicMock, |
| 72 | urandom, | 75 | urandom: unittest.mock.MagicMock, |
| 73 | capsys, | 76 | capsys: pytest.CaptureFixture, |
| 74 | non_random_bytes_57, | 77 | non_random_bytes_57: bytes, |
| 75 | config_file, | 78 | config_file: str, |
| 76 | master_password, | 79 | master_password: str, |
| 77 | short_encrypted_value, | 80 | short_encrypted_value: str, |
| 78 | short_value, | 81 | short_value: str, |
| 79 | ): | 82 | ) -> None: |
| 80 | """Tests encryption via the CLI interface""" | 83 | """Tests encryption via the CLI interface""" |
| 81 | |||
| 82 | binput.return_value = short_value | 84 | binput.return_value = short_value |
| 83 | urandom.return_value = non_random_bytes_57 | 85 | urandom.return_value = non_random_bytes_57 |
| 84 | with unittest.mock.patch.dict( | 86 | with unittest.mock.patch.dict( |
| @@ -95,17 +97,16 @@ def test_encrypt_with_echo( | |||
| 95 | @unittest.mock.patch('os.urandom') | 97 | @unittest.mock.patch('os.urandom') |
| 96 | @unittest.mock.patch('getpass.getpass') | 98 | @unittest.mock.patch('getpass.getpass') |
| 97 | def test_encrypt_without_echo( | 99 | def test_encrypt_without_echo( |
| 98 | getpass, | 100 | getpass: unittest.mock.MagicMock, |
| 99 | urandom, | 101 | urandom: unittest.mock.MagicMock, |
| 100 | capsys, | 102 | capsys: pytest.CaptureFixture, |
| 101 | non_random_bytes_57, | 103 | non_random_bytes_57: bytes, |
| 102 | config_file, | 104 | config_file: str, |
| 103 | master_password, | 105 | master_password: str, |
| 104 | short_encrypted_value, | 106 | short_encrypted_value: str, |
| 105 | short_value, | 107 | short_value: str, |
| 106 | ): | 108 | ) -> None: |
| 107 | """Tests encryption via the CLI interface""" | 109 | """Tests encryption via the CLI interface""" |
| 108 | |||
| 109 | getpass.return_value = short_value | 110 | getpass.return_value = short_value |
| 110 | urandom.return_value = non_random_bytes_57 | 111 | urandom.return_value = non_random_bytes_57 |
| 111 | with unittest.mock.patch.dict( | 112 | with unittest.mock.patch.dict( |
| @@ -119,20 +120,26 @@ def test_encrypt_without_echo( | |||
| 119 | ) | 120 | ) |
| 120 | 121 | ||
| 121 | 122 | ||
| 122 | def test_fetch_encrypted_value(config_file, master_password, short_value): | 123 | def test_fetch_encrypted_value( |
| 124 | config_file: str, master_password: str, short_value: str | ||
| 125 | ) -> None: | ||
| 123 | """Tests decryption of encrypted value""" | 126 | """Tests decryption of encrypted value""" |
| 124 | |||
| 125 | with unittest.mock.patch.dict( | 127 | with unittest.mock.patch.dict( |
| 126 | os.environ, {'ETOOLKIT_MASTER_PASSWORD': master_password} | 128 | os.environ, {'ETOOLKIT_MASTER_PASSWORD': master_password} |
| 127 | ): | 129 | ): |
| 128 | assert os.environ.get('ETOOLKIT_TEST_PASSWORD') is None | 130 | assert os.environ.get('ETOOLKIT_TEST_PASSWORD') is None |
| 129 | main(['-c', f'{config_file}', '-q', '-s', '/bin/false', 'secret']) | 131 | with pytest.raises(SystemExit) as exit_info: |
| 132 | main(['-c', f'{config_file}', '-q', '-s', '/bin/false', 'secret']) | ||
| 133 | assert exit_info.value.code == 0 | ||
| 130 | assert os.environ.get('ETOOLKIT_TEST_PASSWORD') == short_value | 134 | assert os.environ.get('ETOOLKIT_TEST_PASSWORD') == short_value |
| 131 | 135 | ||
| 132 | 136 | ||
| 133 | def test_list(capsys, config_file, nonexistent_config_file): | 137 | def test_list( |
| 138 | capsys: pytest.CaptureFixture, | ||
| 139 | config_file: str, | ||
| 140 | nonexistent_config_file: str, | ||
| 141 | ) -> None: | ||
| 134 | """Tests list via the CLI interface""" | 142 | """Tests list via the CLI interface""" |
| 135 | |||
| 136 | with pytest.raises(SystemExit) as exit_info: | 143 | with pytest.raises(SystemExit) as exit_info: |
| 137 | main(['-c', nonexistent_config_file, '-l']) | 144 | main(['-c', nonexistent_config_file, '-l']) |
| 138 | assert exit_info.value.code == errno.EIO | 145 | assert exit_info.value.code == errno.EIO |
| @@ -142,9 +149,8 @@ def test_list(capsys, config_file, nonexistent_config_file): | |||
| 142 | assert capsys.readouterr().out.strip() == f'dev{os.linesep}secret' | 149 | assert capsys.readouterr().out.strip() == f'dev{os.linesep}secret' |
| 143 | 150 | ||
| 144 | 151 | ||
| 145 | def test_help(capsys): | 152 | def test_help(capsys: pytest.CaptureFixture) -> None: |
| 146 | """Dummy test checking if the CLI is available at all""" | 153 | """Dummy test checking if the CLI is available at all""" |
| 147 | |||
| 148 | with pytest.raises(SystemExit) as exit_info: | 154 | with pytest.raises(SystemExit) as exit_info: |
| 149 | main(['-h']) | 155 | main(['-h']) |
| 150 | assert exit_info.value.code == 0 | 156 | assert exit_info.value.code == 0 |
| @@ -154,10 +160,12 @@ def test_help(capsys): | |||
| 154 | @unittest.mock.patch('os.urandom') | 160 | @unittest.mock.patch('os.urandom') |
| 155 | @unittest.mock.patch('getpass.getpass') | 161 | @unittest.mock.patch('getpass.getpass') |
| 156 | def test_generate_master_password_hash( | 162 | def test_generate_master_password_hash( |
| 157 | getpass, urandom, capsys, non_random_bytes_32 | 163 | getpass: unittest.mock.MagicMock, |
| 158 | ): | 164 | urandom: unittest.mock.MagicMock, |
| 165 | capsys: pytest.CaptureFixture, | ||
| 166 | non_random_bytes_32: bytes, | ||
| 167 | ) -> None: | ||
| 159 | """Tests master password hash generation via the CLI interface""" | 168 | """Tests master password hash generation via the CLI interface""" |
| 160 | |||
| 161 | getpass.return_value = 'The very secret passwd' | 169 | getpass.return_value = 'The very secret passwd' |
| 162 | urandom.return_value = non_random_bytes_32 | 170 | urandom.return_value = non_random_bytes_32 |
| 163 | with pytest.raises(SystemExit) as exit_info: | 171 | with pytest.raises(SystemExit) as exit_info: |
| @@ -169,9 +177,8 @@ def test_generate_master_password_hash( | |||
| 169 | ) | 177 | ) |
| 170 | 178 | ||
| 171 | 179 | ||
| 172 | def test_version(capsys): | 180 | def test_version(capsys: pytest.CaptureFixture) -> None: |
| 173 | """Dummy test checking if the CLI is available at all""" | 181 | """Dummy test checking if the CLI is available at all""" |
| 174 | |||
| 175 | with pytest.raises(SystemExit) as exit_info: | 182 | with pytest.raises(SystemExit) as exit_info: |
| 176 | main(['-v']) | 183 | main(['-v']) |
| 177 | assert exit_info.value.code == 0 | 184 | assert exit_info.value.code == 0 |
