git.schokokeks.org
Repositories
Help
Report an Issue
derivepassphrase.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
fa1083e
Branches
Tags
documentation-tree
master
0.1.0
0.1.1
0.1.2
0.1.3
0.2.0
0.3.0
0.3.1
0.3.2
derivepassphrase.git
tests
test_derivepassphrase_exporter.py
Add preliminary tests for the exporter
Marco Ricci
commited
fa1083e
at 2024-08-25 23:50:03
test_derivepassphrase_exporter.py
Blame
History
Raw
# SPDX-FileCopyrightText: 2024 Marco Ricci <m@the13thletter.info> # # SPDX-License-Identifier: MIT from __future__ import annotations import json import os from typing import Any import click.testing import pytest import tests from derivepassphrase import exporter VAULT_MASTER_KEY = 'vault key' VAULT_V02_CONFIG = 'P7xeh5y4jmjpJ2pFq4KUcTVoaE9ZOEkwWmpVTURSSWQxbGt6emN4aFE4eFM3anVPbDRNTGpOLzY3eDF5aE1YTm5LNWh5Q1BwWTMwM3M5S083MWRWRFlmOXNqSFJNcStGMWFOS3c2emhiOUNNenZYTmNNMnZxaUErdlRoOGF2ZHdGT1ZLNTNLOVJQcU9jWmJrR3g5N09VcVBRZ0ZnSFNUQy9HdFVWWnFteVhRVkY3MHNBdnF2ZWFEbFBseWRGelE1c3BFTnVUckRQdWJSL29wNjFxd2Y2ZVpob3VyVzRod3FKTElTenJ1WTZacTJFOFBtK3BnVzh0QWVxcWtyWFdXOXYyenNQeFNZbWt1MDU2Vm1kVGtISWIxWTBpcWRFbyswUVJudVVhZkVlNVpGWDA4WUQ2Q2JTWW81SnlhQ2Zxa3cxNmZoQjJES0Uyd29rNXpSck5iWVBrVmEwOXFya1NpMi9saU5LL3F0M3N3MjZKekNCem9ER2svWkZ0SUJLdmlHRno0VlQzQ3pqZTBWcTM3YmRiNmJjTkhqUHZoQ0NxMW1ldW1XOFVVK3pQMEtUMkRMVGNvNHFlOG40ck5KcGhsYXg1b1VzZ1NYU1B2T3RXdEkwYzg4NWE3YWUzOWI1MDI0MThhMWZjODQ3MDA2OTJmNDQ0MDkxNGFiNmRlMGQ2YjZiNjI5NGMwN2IwMmI4MGZi' # noqa: E501 VAULT_V02_CONFIG_DATA = { 'global': { 'phrase': tests.DUMMY_PASSPHRASE.decode('utf-8').rstrip('\n'), }, 'services': { '(meta)': { 'notes': 'This config was originally in v0.2 format.', }, tests.DUMMY_SERVICE: tests.DUMMY_CONFIG_SETTINGS.copy(), }, } VAULT_V03_CONFIG = 'sBPBrr8BFHPxSJkV/A53zk9zwDQHFxLe6UIusCVvzFQre103pcj5xxmE11lMTA0U2QTYjkhRXKkH5WegSmYpAnzReuRsYZlWWp6N4kkubf+twZ9C3EeggPm7as2Af4TICHVbX4uXpIHeQJf9y1OtqrO+SRBrgPBzgItoxsIxebxVKgyvh1CZQOSkn7BIzt9xKhDng3ubS4hQ91fB0QCumlldTbUl8tj4Xs5JbvsSlUMxRlVzZ0OgAOrSsoWELXmsp6zXFa9K6wIuZa4wQuMLQFHiA64JO1CR3I+rviWCeMlbTOuJNx6vMB5zotKJqA2hIUpN467TQ9vI4g/QTo40m5LT2EQKbIdTvBQAzcV4lOcpr5Lqt4LHED5mKvm/4YfpuuT3I3XCdWfdG5SB7ciiB4Go+xQdddy3zZMiwm1fEwIB8XjFf2cxoJdccLQ2yxf+9diedBP04EsMHrvxKDhQ7/vHl7xF2MMFTDKl3WFd23vvcjpR1JgNAKYprG/e1p/7' # noqa: E501 VAULT_V03_CONFIG_DATA = { 'global': { 'phrase': tests.DUMMY_PASSPHRASE.decode('utf-8').rstrip('\n'), }, 'services': { '(meta)': { 'notes': 'This config was originally in v0.3 format.', }, tests.DUMMY_SERVICE: tests.DUMMY_CONFIG_SETTINGS.copy(), }, } VAULT_STOREROOM_CONFIG_ZIPPED = b""" UEsDBBQAAAAIAJ1WGVnTVFGT0gAAAOYAAAAFAAAALmtleXMFwclSgzAAANC7n9GrBzBldcYDE5Al EKbFAvGWklBAtqYsBcd/973fw8LFox76w/vb34tzhD5OATeEAk6tJ6Fbp3WrvkJO7l0KIjtxCLfY ORm8Sc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VFmFqr6+/gFQ SwECHgMUAAAACACdVhlZ01RRk9IAAADmAAAABQAAAAAAAAABAAAApIEAAAAALmtleXNQSwECHgMU AAAACACdVhlZ3Wlf4QUDAADUAwAAAgAAAAAAAAABAAAApIH1AAAAMDBQSwECHgMUAAAACACdVhlZ /aaiwcoBAAArAgAAAgAAAAAAAAABAAAApIEaBAAAMDNQSwECHgMUAAAACACdVhlZs14oCcgBAAAr AgAAAgAAAAAAAAABAAAApIEEBgAAMDlQSwECHgMUAAAACACdVhlZyjtiYvgBAABrAgAAAgAAAAAA AAABAAAApIHsBwAAMWFQSwUGAAAAAAUABQDzAAAABAoAAAAA """ VAULT_STOREROOM_CONFIG_DATA = { 'global': { 'phrase': tests.DUMMY_PASSPHRASE.decode('utf-8').rstrip('\n'), }, 'services': { '(meta)': { 'notes': 'This config was originally in storeroom format.', }, tests.DUMMY_SERVICE: tests.DUMMY_CONFIG_SETTINGS.copy(), }, } try: from cryptography.hazmat.primitives import ciphers except ModuleNotFoundError: CRYPTOGRAPHY_SUPPORT = False else: CRYPTOGRAPHY_SUPPORT = True del ciphers CANNOT_LOAD_CRYPTOGRAPHY = ( b'Cannot load the required Python module "cryptography".' ) class Test001ExporterUtils: @pytest.mark.parametrize( ['expected', 'vault_key', 'logname', 'user', 'username'], [ ('4username', None, None, None, '4username'), ('3user', None, None, '3user', None), ('3user', None, None, '3user', '4username'), ('2logname', None, '2logname', None, None), ('2logname', None, '2logname', None, '4username'), ('2logname', None, '2logname', '3user', None), ('2logname', None, '2logname', '3user', '4username'), ('1vault_key', '1vault_key', None, None, None), ('1vault_key', '1vault_key', None, None, '4username'), ('1vault_key', '1vault_key', None, '3user', None), ('1vault_key', '1vault_key', None, '3user', '4username'), ('1vault_key', '1vault_key', '2logname', None, None), ('1vault_key', '1vault_key', '2logname', None, '4username'), ('1vault_key', '1vault_key', '2logname', '3user', None), ('1vault_key', '1vault_key', '2logname', '3user', '4username'), ], ) def test200_get_vault_key( self, monkeypatch: pytest.MonkeyPatch, expected: str, vault_key: str | None, logname: str | None, user: str | None, username: str | None, ) -> None: priority_list = [ ('VAULT_KEY', vault_key), ('LOGNAME', logname), ('USER', user), ('USERNAME', username), ] runner = click.testing.CliRunner(mix_stderr=False) with tests.isolated_vault_exporter_config( monkeypatch=monkeypatch, runner=runner ): for key, value in priority_list: if value is not None: monkeypatch.setenv(key, value) assert os.fsdecode(exporter.get_vault_key()) == expected @pytest.mark.parametrize( ['expected', 'path'], [ ('/tmp', '/tmp'), ('~', os.path.curdir), ('~/.vault', None), ], ) def test_210_get_vault_path( self, monkeypatch: pytest.MonkeyPatch, expected: str, path: str | None, ) -> None: runner = click.testing.CliRunner(mix_stderr=False) with tests.isolated_vault_exporter_config( monkeypatch=monkeypatch, runner=runner ): if path: monkeypatch.setenv('VAULT_PATH', path) assert os.fsdecode( os.path.realpath(exporter.get_vault_path()) ) == os.path.realpath(os.path.expanduser(expected)) def test_300_get_vault_key_without_envs( self, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.delenv('VAULT_KEY', raising=False) monkeypatch.delenv('LOGNAME', raising=False) monkeypatch.delenv('USER', raising=False) monkeypatch.delenv('USERNAME', raising=False) with pytest.raises(KeyError, match='VAULT_KEY'): exporter.get_vault_key() def test_310_get_vault_path_without_home( self, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.setattr(os.path, 'expanduser', lambda x: x) with pytest.raises( RuntimeError, match='[Cc]annot determine home directory' ): exporter.get_vault_path() class Test002CLI: @pytest.mark.xfail( not CRYPTOGRAPHY_SUPPORT, reason='cryptography module not found' ) def test_200_path_parameter(self, monkeypatch: pytest.MonkeyPatch) -> None: runner = click.testing.CliRunner(mix_stderr=False) with tests.isolated_vault_exporter_config( monkeypatch=monkeypatch, runner=runner, vault_config=VAULT_V03_CONFIG, vault_key=VAULT_MASTER_KEY, ): monkeypatch.setenv('VAULT_KEY', VAULT_MASTER_KEY) result = runner.invoke( exporter.derivepassphrase_export, ['VAULT_PATH'], ) assert not result.exception assert (result.exit_code, result.stderr_bytes) == (0, b'') assert json.loads(result.stdout) == VAULT_V03_CONFIG_DATA @pytest.mark.xfail( not CRYPTOGRAPHY_SUPPORT, reason='cryptography module not found' ) def test_201_key_parameter(self, monkeypatch: pytest.MonkeyPatch) -> None: runner = click.testing.CliRunner(mix_stderr=False) with tests.isolated_vault_exporter_config( monkeypatch=monkeypatch, runner=runner, vault_config=VAULT_V03_CONFIG, ): result = runner.invoke( exporter.derivepassphrase_export, ['-k', VAULT_MASTER_KEY, '.vault'], ) assert not result.exception assert (result.exit_code, result.stderr_bytes) == (0, b'') assert json.loads(result.stdout) == VAULT_V03_CONFIG_DATA @pytest.mark.xfail( not CRYPTOGRAPHY_SUPPORT, reason='cryptography module not found' ) @pytest.mark.parametrize( ['version', 'config', 'config_data'], [ pytest.param( '0.2', VAULT_V02_CONFIG, VAULT_V02_CONFIG_DATA, id='0.2' ), pytest.param( '0.3', VAULT_V03_CONFIG, VAULT_V03_CONFIG_DATA, id='0.3' ), pytest.param( 'storeroom', VAULT_STOREROOM_CONFIG_ZIPPED, VAULT_STOREROOM_CONFIG_DATA, id='storeroom', ), ], ) def test_210_load_vault_v02_v03_storeroom( self, monkeypatch: pytest.MonkeyPatch, version: str, config: str | bytes, config_data: dict[str, Any], ) -> None: runner = click.testing.CliRunner(mix_stderr=False) with tests.isolated_vault_exporter_config( monkeypatch=monkeypatch, runner=runner, vault_config=config, ): result = runner.invoke( exporter.derivepassphrase_export, [ '-f', f'v{version}' if version.startswith('0') else version, '-k', VAULT_MASTER_KEY, 'VAULT_PATH', ], ) assert not result.exception assert (result.exit_code, result.stderr_bytes) == (0, b'') assert json.loads(result.stdout) == config_data def test_300_invalid_format( self, monkeypatch: pytest.MonkeyPatch, ) -> None: runner = click.testing.CliRunner(mix_stderr=False) with tests.isolated_vault_exporter_config( monkeypatch=monkeypatch, runner=runner, vault_config=VAULT_V03_CONFIG, vault_key=VAULT_MASTER_KEY, ): result = runner.invoke( exporter.derivepassphrase_export, ['-f', 'INVALID', 'VAULT_PATH'], catch_exceptions=False, ) assert isinstance(result.exception, SystemExit) assert result.exit_code assert result.stderr_bytes assert b'Invalid value for' in result.stderr_bytes assert b'-f' in result.stderr_bytes assert b'--format' in result.stderr_bytes assert b'INVALID' in result.stderr_bytes @pytest.mark.xfail( not CRYPTOGRAPHY_SUPPORT, reason='cryptography module not found' ) def test_301_vault_config_not_found( self, monkeypatch: pytest.MonkeyPatch, ) -> None: runner = click.testing.CliRunner(mix_stderr=False) with tests.isolated_vault_exporter_config( monkeypatch=monkeypatch, runner=runner, vault_config=VAULT_V03_CONFIG, vault_key=VAULT_MASTER_KEY, ): result = runner.invoke( exporter.derivepassphrase_export, ['does-not-exist.txt'], ) assert isinstance(result.exception, SystemExit) assert result.exit_code assert result.stderr_bytes assert ( b"Cannot parse 'does-not-exist.txt' as a valid config" in result.stderr_bytes ) assert CANNOT_LOAD_CRYPTOGRAPHY not in result.stderr_bytes @pytest.mark.xfail( not CRYPTOGRAPHY_SUPPORT, reason='cryptography module not found' ) def test_302_vault_config_invalid( self, monkeypatch: pytest.MonkeyPatch, ) -> None: runner = click.testing.CliRunner(mix_stderr=False) with tests.isolated_vault_exporter_config( monkeypatch=monkeypatch, runner=runner, vault_config='', vault_key=VAULT_MASTER_KEY, ): result = runner.invoke( exporter.derivepassphrase_export, ['.vault'], ) assert isinstance(result.exception, SystemExit) assert result.exit_code assert result.stderr_bytes assert ( b"Cannot parse '.vault' as a valid config." in result.stderr_bytes ) assert CANNOT_LOAD_CRYPTOGRAPHY not in result.stderr_bytes class TestStoreroomExporter: pass # TODO(the-13th-letter): Fill in once design is stable. class TestV02Exporter: pass # TODO(the-13th-letter): Fill in once design is stable. class TestV03Exporter: pass # TODO(the-13th-letter): Fill in once design is stable.