e8f3ec854c425cc36565a40adbf00d22a2febeec
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
13) from derivepassphrase import exporter
Marco Ricci Move exporter command-line...

Marco Ricci authored 2 weeks ago

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

Marco Ricci authored 3 weeks ago

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

Marco Ricci authored 2 weeks ago

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

Marco Ricci authored 3 weeks ago

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

Marco Ricci authored 2 weeks ago

118)             _result = runner.invoke(
Marco Ricci Move exporter command-line...

Marco Ricci authored 2 weeks ago

119)                 cli.derivepassphrase_export,
Marco Ricci Add preliminary tests for t...

Marco Ricci authored 3 weeks ago

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

Marco Ricci authored 2 weeks ago

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

Marco Ricci authored 2 weeks ago

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

Marco Ricci authored 2 weeks ago

167)             _result = runner.invoke(
Marco Ricci Test exporter data loading...

Marco Ricci authored 2 weeks ago

168)                 cli.derivepassphrase_export,
169)                 ['-f', format, 'VAULT_PATH'],
170)                 catch_exceptions=False,
171)             )