61f4c67ce11a1dd77b59613e550822d64877c904
Marco Ricci Change the author e-mail ad...

Marco Ricci authored 1 week ago

1) # SPDX-FileCopyrightText: 2024 Marco Ricci <software@the13thletter.info>
Marco Ricci Add preliminary tests for t...

Marco Ricci authored 3 weeks ago

2) #
3) # SPDX-License-Identifier: MIT
4) 
5) from __future__ import annotations
6) 
7) import os
8) 
9) import click.testing
10) import pytest
11) 
12) import tests
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 1 week ago

13) from derivepassphrase import cli, exporter
Marco Ricci Add preliminary tests for t...

Marco Ricci authored 3 weeks ago

14) 
15) 
16) class Test001ExporterUtils:
17)     @pytest.mark.parametrize(
18)         ['expected', 'vault_key', 'logname', 'user', 'username'],
19)         [
20)             ('4username', None, None, None, '4username'),
21)             ('3user', None, None, '3user', None),
22)             ('3user', None, None, '3user', '4username'),
23)             ('2logname', None, '2logname', None, None),
24)             ('2logname', None, '2logname', None, '4username'),
25)             ('2logname', None, '2logname', '3user', None),
26)             ('2logname', None, '2logname', '3user', '4username'),
27)             ('1vault_key', '1vault_key', None, None, None),
28)             ('1vault_key', '1vault_key', None, None, '4username'),
29)             ('1vault_key', '1vault_key', None, '3user', None),
30)             ('1vault_key', '1vault_key', None, '3user', '4username'),
31)             ('1vault_key', '1vault_key', '2logname', None, None),
32)             ('1vault_key', '1vault_key', '2logname', None, '4username'),
33)             ('1vault_key', '1vault_key', '2logname', '3user', None),
34)             ('1vault_key', '1vault_key', '2logname', '3user', '4username'),
35)         ],
36)     )
37)     def test200_get_vault_key(
38)         self,
39)         monkeypatch: pytest.MonkeyPatch,
40)         expected: str,
41)         vault_key: str | None,
42)         logname: str | None,
43)         user: str | None,
44)         username: str | None,
45)     ) -> None:
46)         priority_list = [
47)             ('VAULT_KEY', vault_key),
48)             ('LOGNAME', logname),
49)             ('USER', user),
50)             ('USERNAME', username),
51)         ]
52)         runner = click.testing.CliRunner(mix_stderr=False)
53)         with tests.isolated_vault_exporter_config(
54)             monkeypatch=monkeypatch, runner=runner
55)         ):
56)             for key, value in priority_list:
57)                 if value is not None:
58)                     monkeypatch.setenv(key, value)
59)             assert os.fsdecode(exporter.get_vault_key()) == expected
60) 
61)     @pytest.mark.parametrize(
62)         ['expected', 'path'],
63)         [
64)             ('/tmp', '/tmp'),
65)             ('~', os.path.curdir),
66)             ('~/.vault', None),
67)         ],
68)     )
69)     def test_210_get_vault_path(
70)         self,
71)         monkeypatch: pytest.MonkeyPatch,
72)         expected: str,
73)         path: str | None,
74)     ) -> None:
75)         runner = click.testing.CliRunner(mix_stderr=False)
76)         with tests.isolated_vault_exporter_config(
77)             monkeypatch=monkeypatch, runner=runner
78)         ):
79)             if path:
80)                 monkeypatch.setenv('VAULT_PATH', path)
81)             assert os.fsdecode(
82)                 os.path.realpath(exporter.get_vault_path())
83)             ) == os.path.realpath(os.path.expanduser(expected))
84) 
85)     def test_300_get_vault_key_without_envs(
86)         self, monkeypatch: pytest.MonkeyPatch
87)     ) -> None:
88)         monkeypatch.delenv('VAULT_KEY', raising=False)
89)         monkeypatch.delenv('LOGNAME', raising=False)
90)         monkeypatch.delenv('USER', raising=False)
91)         monkeypatch.delenv('USERNAME', raising=False)
92)         with pytest.raises(KeyError, match='VAULT_KEY'):
93)             exporter.get_vault_key()
94) 
95)     def test_310_get_vault_path_without_home(
96)         self, monkeypatch: pytest.MonkeyPatch
97)     ) -> None:
98)         monkeypatch.setattr(os.path, 'expanduser', lambda x: x)
99)         with pytest.raises(
100)             RuntimeError, match='[Cc]annot determine home directory'
101)         ):
102)             exporter.get_vault_path()
103) 
104) 
105) class Test002CLI:
106)     def test_300_invalid_format(
107)         self,
108)         monkeypatch: pytest.MonkeyPatch,
109)     ) -> None:
110)         runner = click.testing.CliRunner(mix_stderr=False)
111)         with tests.isolated_vault_exporter_config(
112)             monkeypatch=monkeypatch,
113)             runner=runner,
Marco Ricci Move exporter command-line...

Marco Ricci authored 2 weeks ago

114)             vault_config=tests.VAULT_V03_CONFIG,
115)             vault_key=tests.VAULT_MASTER_KEY,
Marco Ricci Add preliminary tests for t...

Marco Ricci authored 3 weeks ago

116)         ):
Marco Ricci Clean up testing machinery...

Marco Ricci authored 2 weeks ago

117)             _result = runner.invoke(
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 1 week ago

118)                 cli.derivepassphrase_export_vault,
Marco Ricci Add preliminary tests for t...

Marco Ricci authored 3 weeks ago

119)                 ['-f', 'INVALID', 'VAULT_PATH'],
120)                 catch_exceptions=False,
121)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 2 weeks ago

122)         result = tests.ReadableResult.parse(_result)
123)         for snippet in ('Invalid value for', '-f', '--format', 'INVALID'):
124)             assert result.error_exit(
125)                 error=snippet
126)             ), 'expected error exit and known error message'
Marco Ricci Test exporter data loading...

Marco Ricci authored 2 weeks ago

127) 
128)     @tests.skip_if_cryptography_support
129)     @pytest.mark.parametrize(
130)         ['format', 'config', 'key'],
131)         [
132)             pytest.param(
133)                 'v0.2',
134)                 tests.VAULT_V02_CONFIG,
135)                 tests.VAULT_MASTER_KEY,
136)                 id='v0.2',
137)             ),
138)             pytest.param(
139)                 'v0.3',
140)                 tests.VAULT_V03_CONFIG,
141)                 tests.VAULT_MASTER_KEY,
142)                 id='v0.3',
143)             ),
144)             pytest.param(
145)                 'storeroom',
146)                 tests.VAULT_STOREROOM_CONFIG_ZIPPED,
147)                 tests.VAULT_MASTER_KEY,
148)                 id='storeroom',
149)             ),
150)         ],
151)     )
152)     def test_999_no_cryptography_error_message(
153)         self,
154)         monkeypatch: pytest.MonkeyPatch,
155)         format: str,
156)         config: str | bytes,
157)         key: str,
158)     ) -> None:
159)         runner = click.testing.CliRunner(mix_stderr=False)
160)         with tests.isolated_vault_exporter_config(
161)             monkeypatch=monkeypatch,
162)             runner=runner,
163)             vault_config=config,
164)             vault_key=key,
165)         ):
Marco Ricci Clean up testing machinery...

Marco Ricci authored 2 weeks ago

166)             _result = runner.invoke(
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 1 week ago

167)                 cli.derivepassphrase_export_vault,
Marco Ricci Test exporter data loading...

Marco Ricci authored 2 weeks ago

168)                 ['-f', format, 'VAULT_PATH'],
169)                 catch_exceptions=False,
170)             )