fa6125de4455fb187db8888e510400e9e9820b3b
Marco Ricci Move exporter command-line...

Marco Ricci authored 2 weeks ago

1) # SPDX-FileCopyrightText: 2024 Marco Ricci <m@the13thletter.info>
2) #
3) # SPDX-License-Identifier: MIT
4) 
5) from __future__ import annotations
6) 
7) import json
Marco Ricci Test exporter data loading...

Marco Ricci authored 2 weeks ago

8) from typing import TYPE_CHECKING
Marco Ricci Move exporter command-line...

Marco Ricci authored 2 weeks ago

9) 
10) import click.testing
11) import pytest
12) 
13) import tests
14) from derivepassphrase.exporter import cli
15) 
16) cryptography = pytest.importorskip('cryptography', minversion='38.0')
17) 
Marco Ricci Test exporter data loading...

Marco Ricci authored 2 weeks ago

18) if TYPE_CHECKING:
19)     from typing import Any
20) 
Marco Ricci Move exporter command-line...

Marco Ricci authored 2 weeks ago

21) 
22) class TestCLI:
23)     def test_200_path_parameter(self, monkeypatch: pytest.MonkeyPatch) -> None:
24)         runner = click.testing.CliRunner(mix_stderr=False)
25)         with tests.isolated_vault_exporter_config(
26)             monkeypatch=monkeypatch,
27)             runner=runner,
28)             vault_config=tests.VAULT_V03_CONFIG,
29)             vault_key=tests.VAULT_MASTER_KEY,
30)         ):
31)             monkeypatch.setenv('VAULT_KEY', tests.VAULT_MASTER_KEY)
32)             result = runner.invoke(
33)                 cli.derivepassphrase_export,
34)                 ['VAULT_PATH'],
35)             )
36)         assert not result.exception
37)         assert (result.exit_code, result.stderr_bytes) == (0, b'')
38)         assert json.loads(result.stdout) == tests.VAULT_V03_CONFIG_DATA
39) 
40)     def test_201_key_parameter(self, monkeypatch: pytest.MonkeyPatch) -> None:
41)         runner = click.testing.CliRunner(mix_stderr=False)
42)         with tests.isolated_vault_exporter_config(
43)             monkeypatch=monkeypatch,
44)             runner=runner,
45)             vault_config=tests.VAULT_V03_CONFIG,
46)         ):
47)             result = runner.invoke(
48)                 cli.derivepassphrase_export,
49)                 ['-k', tests.VAULT_MASTER_KEY, '.vault'],
50)             )
51)         assert not result.exception
52)         assert (result.exit_code, result.stderr_bytes) == (0, b'')
53)         assert json.loads(result.stdout) == tests.VAULT_V03_CONFIG_DATA
54) 
55)     @pytest.mark.parametrize(
56)         ['format', 'config', 'config_data'],
57)         [
58)             pytest.param(
59)                 'v0.2',
60)                 tests.VAULT_V02_CONFIG,
61)                 tests.VAULT_V02_CONFIG_DATA,
62)                 id='0.2',
63)             ),
64)             pytest.param(
65)                 'v0.3',
66)                 tests.VAULT_V03_CONFIG,
67)                 tests.VAULT_V03_CONFIG_DATA,
68)                 id='0.3',
69)             ),
70)             pytest.param(
71)                 'storeroom',
72)                 tests.VAULT_STOREROOM_CONFIG_ZIPPED,
73)                 tests.VAULT_STOREROOM_CONFIG_DATA,
74)                 id='storeroom',
75)             ),
76)         ],
77)     )
78)     def test_210_load_vault_v02_v03_storeroom(
79)         self,
80)         monkeypatch: pytest.MonkeyPatch,
81)         format: str,
82)         config: str | bytes,
83)         config_data: dict[str, Any],
84)     ) -> None:
85)         runner = click.testing.CliRunner(mix_stderr=False)
86)         with tests.isolated_vault_exporter_config(
87)             monkeypatch=monkeypatch,
88)             runner=runner,
89)             vault_config=config,
90)         ):
91)             result = runner.invoke(
92)                 cli.derivepassphrase_export,
93)                 ['-f', format, '-k', tests.VAULT_MASTER_KEY, 'VAULT_PATH'],
94)             )
95)         assert not result.exception
96)         assert (result.exit_code, result.stderr_bytes) == (0, b'')
97)         assert json.loads(result.stdout) == config_data
98) 
99)     # test_300_invalid_format is found in
100)     # tests.test_derivepassphrase_export::Test002CLI
101) 
102)     def test_301_vault_config_not_found(
103)         self,
104)         monkeypatch: pytest.MonkeyPatch,
105)     ) -> None:
106)         runner = click.testing.CliRunner(mix_stderr=False)
107)         with tests.isolated_vault_exporter_config(
108)             monkeypatch=monkeypatch,
109)             runner=runner,
110)             vault_config=tests.VAULT_V03_CONFIG,
111)             vault_key=tests.VAULT_MASTER_KEY,
112)         ):
113)             result = runner.invoke(
114)                 cli.derivepassphrase_export,
115)                 ['does-not-exist.txt'],
116)             )
117)         assert isinstance(result.exception, SystemExit)
118)         assert result.exit_code
119)         assert result.stderr_bytes
120)         assert (
121)             b"Cannot parse 'does-not-exist.txt' as a valid config"
122)             in result.stderr_bytes
123)         )
124)         assert tests.CANNOT_LOAD_CRYPTOGRAPHY not in result.stderr_bytes
125) 
126)     def test_302_vault_config_invalid(
127)         self,
128)         monkeypatch: pytest.MonkeyPatch,
129)     ) -> None:
130)         runner = click.testing.CliRunner(mix_stderr=False)
131)         with tests.isolated_vault_exporter_config(
132)             monkeypatch=monkeypatch,
133)             runner=runner,
134)             vault_config='',
135)             vault_key=tests.VAULT_MASTER_KEY,
136)         ):
137)             result = runner.invoke(
138)                 cli.derivepassphrase_export,
139)                 ['.vault'],
140)             )
141)         assert isinstance(result.exception, SystemExit)
142)         assert result.exit_code
143)         assert result.stderr_bytes
144)         assert (
145)             b"Cannot parse '.vault' as a valid config." in result.stderr_bytes
146)         )
147)         assert tests.CANNOT_LOAD_CRYPTOGRAPHY not in result.stderr_bytes