e97b8541c96a93d1a819e7f88770e5c90abf3863
Marco Ricci Change the author e-mail ad...

Marco Ricci authored 3 months ago

1) # SPDX-FileCopyrightText: 2024 Marco Ricci <software@the13thletter.info>
Marco Ricci Add prototype command-line...

Marco Ricci authored 6 months ago

2) #
3) # SPDX-License-Identifier: MIT
4) 
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

5) from __future__ import annotations
6) 
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

7) import contextlib
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

8) import copy
Marco Ricci Remove `click` handling of...

Marco Ricci authored 3 months ago

9) import errno
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

10) import json
11) import os
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

12) import shutil
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

13) import socket
Marco Ricci Add a stateful hypothesis t...

Marco Ricci authored 2 months ago

14) from typing import TYPE_CHECKING, cast
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

15) 
Marco Ricci Add prototype command-line...

Marco Ricci authored 6 months ago

16) import click.testing
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

17) import hypothesis
Marco Ricci Add prototype command-line...

Marco Ricci authored 6 months ago

18) import pytest
Marco Ricci Add a stateful hypothesis t...

Marco Ricci authored 2 months ago

19) from hypothesis import stateful, strategies
20) from typing_extensions import Any, NamedTuple
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

21) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

22) import tests
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

23) from derivepassphrase import _types, cli, ssh_agent, vault
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

24) 
25) if TYPE_CHECKING:
Marco Ricci Add a stateful hypothesis t...

Marco Ricci authored 2 months ago

26)     from collections.abc import Callable, Iterable
Marco Ricci Add prototype command-line...

Marco Ricci authored 6 months ago

27) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

28) DUMMY_SERVICE = tests.DUMMY_SERVICE
29) DUMMY_PASSPHRASE = tests.DUMMY_PASSPHRASE
30) DUMMY_CONFIG_SETTINGS = tests.DUMMY_CONFIG_SETTINGS
31) DUMMY_RESULT_PASSPHRASE = tests.DUMMY_RESULT_PASSPHRASE
32) DUMMY_RESULT_KEY1 = tests.DUMMY_RESULT_KEY1
33) DUMMY_PHRASE_FROM_KEY1_RAW = tests.DUMMY_PHRASE_FROM_KEY1_RAW
34) DUMMY_PHRASE_FROM_KEY1 = tests.DUMMY_PHRASE_FROM_KEY1
35) 
36) DUMMY_KEY1 = tests.DUMMY_KEY1
37) DUMMY_KEY1_B64 = tests.DUMMY_KEY1_B64
38) DUMMY_KEY2 = tests.DUMMY_KEY2
Marco Ricci Avoid crashing when overrid...

Marco Ricci authored 4 months ago

39) DUMMY_KEY2_B64 = tests.DUMMY_KEY2_B64
40) DUMMY_KEY3 = tests.DUMMY_KEY3
41) DUMMY_KEY3_B64 = tests.DUMMY_KEY3_B64
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

42) 
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

43) TEST_CONFIGS = tests.TEST_CONFIGS
44) 
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

45) 
46) class IncompatibleConfiguration(NamedTuple):
47)     other_options: list[tuple[str, ...]]
48)     needs_service: bool | None
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

49)     input: str | None
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

50) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

51) 
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

52) class SingleConfiguration(NamedTuple):
53)     needs_service: bool | None
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

54)     input: str | None
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

55)     check_success: bool
56) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

57) 
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

58) class OptionCombination(NamedTuple):
59)     options: list[str]
60)     incompatible: bool
61)     needs_service: bool | None
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

62)     input: str | None
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

63)     check_success: bool
64) 
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

65) 
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

66) PASSWORD_GENERATION_OPTIONS: list[tuple[str, ...]] = [
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

67)     ('--phrase',),
68)     ('--key',),
69)     ('--length', '20'),
70)     ('--repeat', '20'),
71)     ('--lower', '1'),
72)     ('--upper', '1'),
73)     ('--number', '1'),
74)     ('--space', '1'),
75)     ('--dash', '1'),
76)     ('--symbol', '1'),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

77) ]
78) CONFIGURATION_OPTIONS: list[tuple[str, ...]] = [
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

79)     ('--notes',),
80)     ('--config',),
81)     ('--delete',),
82)     ('--delete-globals',),
83)     ('--clear',),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

84) ]
85) CONFIGURATION_COMMANDS: list[tuple[str, ...]] = [
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

86)     ('--notes',),
87)     ('--delete',),
88)     ('--delete-globals',),
89)     ('--clear',),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

90) ]
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

91) STORAGE_OPTIONS: list[tuple[str, ...]] = [('--export', '-'), ('--import', '-')]
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

92) INCOMPATIBLE: dict[tuple[str, ...], IncompatibleConfiguration] = {
93)     ('--phrase',): IncompatibleConfiguration(
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

94)         [('--key',), *CONFIGURATION_COMMANDS, *STORAGE_OPTIONS],
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

95)         True,
96)         DUMMY_PASSPHRASE,
97)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

98)     ('--key',): IncompatibleConfiguration(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

99)         CONFIGURATION_COMMANDS + STORAGE_OPTIONS, True, DUMMY_PASSPHRASE
100)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

101)     ('--length', '20'): IncompatibleConfiguration(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

102)         CONFIGURATION_COMMANDS + STORAGE_OPTIONS, True, DUMMY_PASSPHRASE
103)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

104)     ('--repeat', '20'): IncompatibleConfiguration(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

105)         CONFIGURATION_COMMANDS + STORAGE_OPTIONS, True, DUMMY_PASSPHRASE
106)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

107)     ('--lower', '1'): IncompatibleConfiguration(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

108)         CONFIGURATION_COMMANDS + STORAGE_OPTIONS, True, DUMMY_PASSPHRASE
109)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

110)     ('--upper', '1'): IncompatibleConfiguration(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

111)         CONFIGURATION_COMMANDS + STORAGE_OPTIONS, True, DUMMY_PASSPHRASE
112)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

113)     ('--number', '1'): IncompatibleConfiguration(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

114)         CONFIGURATION_COMMANDS + STORAGE_OPTIONS, True, DUMMY_PASSPHRASE
115)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

116)     ('--space', '1'): IncompatibleConfiguration(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

117)         CONFIGURATION_COMMANDS + STORAGE_OPTIONS, True, DUMMY_PASSPHRASE
118)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

119)     ('--dash', '1'): IncompatibleConfiguration(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

120)         CONFIGURATION_COMMANDS + STORAGE_OPTIONS, True, DUMMY_PASSPHRASE
121)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

122)     ('--symbol', '1'): IncompatibleConfiguration(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

123)         CONFIGURATION_COMMANDS + STORAGE_OPTIONS, True, DUMMY_PASSPHRASE
124)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

125)     ('--notes',): IncompatibleConfiguration(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

126)         [
127)             ('--config',),
128)             ('--delete',),
129)             ('--delete-globals',),
130)             ('--clear',),
131)             *STORAGE_OPTIONS,
132)         ],
133)         True,
134)         None,
135)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

136)     ('--config', '-p'): IncompatibleConfiguration(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

137)         [('--delete',), ('--delete-globals',), ('--clear',), *STORAGE_OPTIONS],
138)         None,
139)         DUMMY_PASSPHRASE,
140)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

141)     ('--delete',): IncompatibleConfiguration(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

142)         [('--delete-globals',), ('--clear',), *STORAGE_OPTIONS], True, None
143)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

144)     ('--delete-globals',): IncompatibleConfiguration(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

145)         [('--clear',), *STORAGE_OPTIONS], False, None
146)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

147)     ('--clear',): IncompatibleConfiguration(STORAGE_OPTIONS, False, None),
148)     ('--export', '-'): IncompatibleConfiguration(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

149)         [('--import', '-')], False, None
150)     ),
151)     ('--import', '-'): IncompatibleConfiguration([], False, None),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

152) }
153) SINGLES: dict[tuple[str, ...], SingleConfiguration] = {
154)     ('--phrase',): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
155)     ('--key',): SingleConfiguration(True, None, False),
156)     ('--length', '20'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
157)     ('--repeat', '20'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
158)     ('--lower', '1'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
159)     ('--upper', '1'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
160)     ('--number', '1'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
161)     ('--space', '1'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
162)     ('--dash', '1'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
163)     ('--symbol', '1'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
164)     ('--notes',): SingleConfiguration(True, None, False),
165)     ('--config', '-p'): SingleConfiguration(None, DUMMY_PASSPHRASE, False),
166)     ('--delete',): SingleConfiguration(True, None, False),
167)     ('--delete-globals',): SingleConfiguration(False, None, True),
168)     ('--clear',): SingleConfiguration(False, None, True),
169)     ('--export', '-'): SingleConfiguration(False, None, True),
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

170)     ('--import', '-'): SingleConfiguration(False, '{"services": {}}', True),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

171) }
172) INTERESTING_OPTION_COMBINATIONS: list[OptionCombination] = []
Marco Ricci Fix miscellaneous type chec...

Marco Ricci authored 5 months ago

173) config: IncompatibleConfiguration | SingleConfiguration
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

174) for opt, config in INCOMPATIBLE.items():
175)     for opt2 in config.other_options:
176)         INTERESTING_OPTION_COMBINATIONS.extend([
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

177)             OptionCombination(
178)                 options=list(opt + opt2),
179)                 incompatible=True,
180)                 needs_service=config.needs_service,
181)                 input=config.input,
182)                 check_success=False,
183)             ),
184)             OptionCombination(
185)                 options=list(opt2 + opt),
186)                 incompatible=True,
187)                 needs_service=config.needs_service,
188)                 input=config.input,
189)                 check_success=False,
190)             ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

191)         ])
192) for opt, config in SINGLES.items():
193)     INTERESTING_OPTION_COMBINATIONS.append(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

194)         OptionCombination(
195)             options=list(opt),
196)             incompatible=False,
197)             needs_service=config.needs_service,
198)             input=config.input,
199)             check_success=config.check_success,
200)         )
201)     )
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

202) 
203) 
Marco Ricci Fix missing consideration o...

Marco Ricci authored 2 months ago

204) def is_harmless_config_import_warning_line(line: str) -> bool:
205)     """Return true if the warning line is harmless, during config import."""
206)     possible_warnings = [
207)         'Replacing invalid value ',
208)         'Removing ineffective setting ',
209)         (
210)             'Setting a global passphrase is ineffective '
211)             'because a key is also set.'
212)         ),
213)         (
214)             'Setting a service passphrase is ineffective '
215)             'because a key is also set.'
216)         ),
217)     ]
218)     return any(  # pragma: no branch
219)         (' Warning: ' + w) in line for w in possible_warnings
220)     )
221) 
222) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

223) class TestCLI:
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

224)     def test_200_help_output(self, monkeypatch: pytest.MonkeyPatch) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

225)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

226)         with tests.isolated_config(
227)             monkeypatch=monkeypatch,
228)             runner=runner,
229)         ):
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

231)                 cli.derivepassphrase_vault, ['--help'], catch_exceptions=False
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

232)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

233)             result = tests.ReadableResult.parse(_result)
234)         assert result.clean_exit(
235)             empty_stderr=True, output='Password generation:\n'
236)         ), 'expected clean exit, and option groups in help text'
237)         assert result.clean_exit(
238)             empty_stderr=True, output='Use NUMBER=0, e.g. "--symbol 0"'
239)         ), 'expected clean exit, and option group epilog in help text'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

240) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

241)     @pytest.mark.parametrize(
242)         'charset_name', ['lower', 'upper', 'number', 'space', 'dash', 'symbol']
243)     )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

244)     def test_201_disable_character_set(
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

245)         self, monkeypatch: pytest.MonkeyPatch, charset_name: str
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

246)     ) -> None:
247)         monkeypatch.setattr(cli, '_prompt_for_passphrase', tests.auto_prompt)
248)         option = f'--{charset_name}'
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

249)         charset = vault.Vault._CHARSETS[charset_name].decode('ascii')
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

250)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

251)         with tests.isolated_config(
252)             monkeypatch=monkeypatch,
253)             runner=runner,
254)         ):
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

256)                 cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

257)                 [option, '0', '-p', '--', DUMMY_SERVICE],
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

258)                 input=DUMMY_PASSPHRASE,
259)                 catch_exceptions=False,
260)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

261)             result = tests.ReadableResult.parse(_result)
262)         assert result.clean_exit(empty_stderr=True), 'expected clean exit:'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

263)         for c in charset:
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

264)             assert (
265)                 c not in result.output
266)             ), f'derived password contains forbidden character {c!r}'
Marco Ricci Add prototype command-line...

Marco Ricci authored 6 months ago

267) 
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

268)     def test_202_disable_repetition(
269)         self, monkeypatch: pytest.MonkeyPatch
270)     ) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

271)         monkeypatch.setattr(cli, '_prompt_for_passphrase', tests.auto_prompt)
272)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

273)         with tests.isolated_config(
274)             monkeypatch=monkeypatch,
275)             runner=runner,
276)         ):
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

278)                 cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

279)                 ['--repeat', '0', '-p', '--', DUMMY_SERVICE],
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

280)                 input=DUMMY_PASSPHRASE,
281)                 catch_exceptions=False,
282)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

283)             result = tests.ReadableResult.parse(_result)
284)         assert result.clean_exit(
285)             empty_stderr=True
286)         ), 'expected clean exit and empty stderr'
287)         passphrase = result.output.rstrip('\r\n')
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

288)         for i in range(len(passphrase) - 1):
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

289)             assert passphrase[i : i + 1] != passphrase[i + 1 : i + 2], (
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

290)                 f'derived password contains repeated character '
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

291)                 f'at position {i}: {result.output!r}'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

292)             )
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

293) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

294)     @pytest.mark.parametrize(
295)         'config',
296)         [
297)             pytest.param(
298)                 {
299)                     'global': {'key': DUMMY_KEY1_B64},
300)                     'services': {DUMMY_SERVICE: DUMMY_CONFIG_SETTINGS},
301)                 },
302)                 id='global',
303)             ),
304)             pytest.param(
305)                 {
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

306)                     'global': {'phrase': DUMMY_PASSPHRASE.rstrip('\n')},
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

307)                     'services': {
308)                         DUMMY_SERVICE: {
309)                             'key': DUMMY_KEY1_B64,
310)                             **DUMMY_CONFIG_SETTINGS,
311)                         }
312)                     },
313)                 },
314)                 id='service',
315)             ),
316)         ],
317)     )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

318)     def test_204a_key_from_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

319)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

320)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Consolidate `types` submodu...

Marco Ricci authored 4 months ago

321)         config: _types.VaultConfig,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

322)     ) -> None:
323)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

324)         with tests.isolated_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

325)             monkeypatch=monkeypatch, runner=runner, config=config
326)         ):
327)             monkeypatch.setattr(
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

328)                 vault.Vault, 'phrase_from_key', tests.phrase_from_key
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

329)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

331)                 cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

332)                 ['--', DUMMY_SERVICE],
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

333)                 catch_exceptions=False,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

334)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

335)         result = tests.ReadableResult.parse(_result)
336)         assert result.clean_exit(
337)             empty_stderr=True
338)         ), 'expected clean exit and empty stderr'
339)         assert _result.stdout_bytes
340)         assert (
341)             _result.stdout_bytes.rstrip(b'\n') != DUMMY_RESULT_PASSPHRASE
342)         ), 'known false output: phrase-based instead of key-based'
343)         assert (
344)             _result.stdout_bytes.rstrip(b'\n') == DUMMY_RESULT_KEY1
345)         ), 'expected known output'
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

346) 
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

347)     def test_204b_key_from_command_line(
348)         self, monkeypatch: pytest.MonkeyPatch
349)     ) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

350)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

351)         with tests.isolated_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

352)             monkeypatch=monkeypatch,
353)             runner=runner,
354)             config={'services': {DUMMY_SERVICE: DUMMY_CONFIG_SETTINGS}},
355)         ):
356)             monkeypatch.setattr(
357)                 cli, '_get_suitable_ssh_keys', tests.suitable_ssh_keys
358)             )
359)             monkeypatch.setattr(
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

360)                 vault.Vault, 'phrase_from_key', tests.phrase_from_key
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

361)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

363)                 cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

364)                 ['-k', '--', DUMMY_SERVICE],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

365)                 input='1\n',
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

366)                 catch_exceptions=False,
367)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

368)         result = tests.ReadableResult.parse(_result)
369)         assert result.clean_exit(), 'expected clean exit'
370)         assert _result.stdout_bytes, 'expected program output'
371)         last_line = _result.stdout_bytes.splitlines(True)[-1]
372)         assert (
373)             last_line.rstrip(b'\n') != DUMMY_RESULT_PASSPHRASE
374)         ), 'known false output: phrase-based instead of key-based'
375)         assert (
376)             last_line.rstrip(b'\n') == DUMMY_RESULT_KEY1
377)         ), 'expected known output'
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

378) 
Marco Ricci Avoid crashing when overrid...

Marco Ricci authored 4 months ago

379)     @pytest.mark.parametrize(
380)         'config',
381)         [
382)             pytest.param(
383)                 {
384)                     'global': {'key': DUMMY_KEY1_B64},
385)                     'services': {DUMMY_SERVICE: {}},
386)                 },
387)                 id='global_config',
388)             ),
389)             pytest.param(
390)                 {'services': {DUMMY_SERVICE: {'key': DUMMY_KEY2_B64}}},
391)                 id='service_config',
392)             ),
393)             pytest.param(
394)                 {
395)                     'global': {'key': DUMMY_KEY1_B64},
396)                     'services': {DUMMY_SERVICE: {'key': DUMMY_KEY2_B64}},
397)                 },
398)                 id='full_config',
399)             ),
400)         ],
401)     )
402)     @pytest.mark.parametrize('key_index', [1, 2, 3], ids=lambda i: f'index{i}')
403)     def test_204c_key_override_on_command_line(
404)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

405)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Add test fixture for manual...

Marco Ricci authored 3 months ago

406)         running_ssh_agent: str,
Marco Ricci Avoid crashing when overrid...

Marco Ricci authored 4 months ago

407)         config: dict[str, Any],
408)         key_index: int,
409)     ) -> None:
Marco Ricci Add test fixture for manual...

Marco Ricci authored 3 months ago

410)         with monkeypatch.context():
411)             monkeypatch.setenv('SSH_AUTH_SOCK', running_ssh_agent)
412)             monkeypatch.setattr(
413)                 ssh_agent.SSHAgentClient, 'list_keys', tests.list_keys
Marco Ricci Avoid crashing when overrid...

Marco Ricci authored 4 months ago

414)             )
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

415)             monkeypatch.setattr(ssh_agent.SSHAgentClient, 'sign', tests.sign)
Marco Ricci Add test fixture for manual...

Marco Ricci authored 3 months ago

416)             runner = click.testing.CliRunner(mix_stderr=False)
417)             with tests.isolated_vault_config(
418)                 monkeypatch=monkeypatch, runner=runner, config=config
419)             ):
420)                 _result = runner.invoke(
421)                     cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

422)                     ['-k', '--', DUMMY_SERVICE],
Marco Ricci Add test fixture for manual...

Marco Ricci authored 3 months ago

423)                     input=f'{key_index}\n',
424)                 )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

425)         result = tests.ReadableResult.parse(_result)
426)         assert result.clean_exit(), 'expected clean exit'
427)         assert result.output, 'expected program output'
428)         assert result.stderr, 'expected stderr'
429)         assert (
430)             'Error:' not in result.stderr
431)         ), 'expected no error messages on stderr'
Marco Ricci Avoid crashing when overrid...

Marco Ricci authored 4 months ago

432) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

433)     def test_205_service_phrase_if_key_in_global_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

434)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

435)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

436)         running_ssh_agent: str,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

437)     ) -> None:
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

438)         with monkeypatch.context():
439)             monkeypatch.setenv('SSH_AUTH_SOCK', running_ssh_agent)
440)             monkeypatch.setattr(
441)                 ssh_agent.SSHAgentClient, 'list_keys', tests.list_keys
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

442)             )
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

443)             monkeypatch.setattr(ssh_agent.SSHAgentClient, 'sign', tests.sign)
444)             runner = click.testing.CliRunner(mix_stderr=False)
445)             with tests.isolated_vault_config(
446)                 monkeypatch=monkeypatch,
447)                 runner=runner,
448)                 config={
449)                     'global': {'key': DUMMY_KEY1_B64},
450)                     'services': {
451)                         DUMMY_SERVICE: {
452)                             'phrase': DUMMY_PASSPHRASE.rstrip('\n'),
453)                             **DUMMY_CONFIG_SETTINGS,
454)                         }
455)                     },
456)                 },
457)             ):
458)                 _result = runner.invoke(
459)                     cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

460)                     ['--', DUMMY_SERVICE],
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

461)                     catch_exceptions=False,
462)                 )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

463)         result = tests.ReadableResult.parse(_result)
464)         assert result.clean_exit(), 'expected clean exit'
465)         assert _result.stdout_bytes, 'expected program output'
466)         last_line = _result.stdout_bytes.splitlines(True)[-1]
467)         assert (
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

468)             last_line.rstrip(b'\n') != DUMMY_RESULT_PASSPHRASE
469)         ), 'known false output: phrase-based instead of key-based'
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

470)         assert (
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

471)             last_line.rstrip(b'\n') == DUMMY_RESULT_KEY1
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

472)         ), 'expected known output'
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

473) 
Marco Ricci Fix missing consideration o...

Marco Ricci authored 2 months ago

474)     @pytest.mark.parametrize(
475)         'config',
476)         [
477)             {
478)                 'services': {
479)                     DUMMY_SERVICE: {
480)                         'key': DUMMY_KEY1_B64,
481)                         **DUMMY_CONFIG_SETTINGS,
482)                     },
483)                 },
484)             },
485)             {
486)                 'global': {'key': DUMMY_KEY1_B64},
Marco Ricci Add a stateful hypothesis t...

Marco Ricci authored 2 months ago

487)                 'services': {DUMMY_SERVICE: DUMMY_CONFIG_SETTINGS.copy()},
Marco Ricci Fix missing consideration o...

Marco Ricci authored 2 months ago

488)             },
489)         ],
490)     )
491)     def test_206_setting_service_phrase_thus_overriding_key_in_config(
492)         self,
493)         monkeypatch: pytest.MonkeyPatch,
494)         running_ssh_agent: str,
495)         config: _types.VaultConfig,
496)     ) -> None:
497)         with monkeypatch.context():
498)             monkeypatch.setenv('SSH_AUTH_SOCK', running_ssh_agent)
499)             monkeypatch.setattr(
500)                 ssh_agent.SSHAgentClient, 'list_keys', tests.list_keys
501)             )
502)             monkeypatch.setattr(ssh_agent.SSHAgentClient, 'sign', tests.sign)
503)             runner = click.testing.CliRunner(mix_stderr=False)
504)             with tests.isolated_vault_config(
505)                 monkeypatch=monkeypatch,
506)                 runner=runner,
507)                 config=config,
508)             ):
509)                 _result = runner.invoke(
510)                     cli.derivepassphrase_vault,
511)                     ['--config', '-p', '--', DUMMY_SERVICE],
512)                     input=DUMMY_PASSPHRASE,
513)                     catch_exceptions=False,
514)                 )
515)         result = tests.ReadableResult.parse(_result)
516)         assert result.clean_exit(), 'expected clean exit'
517)         assert not result.output.strip(), 'expected no program output'
518)         assert result.stderr, 'expected known error output'
519)         err_lines = result.stderr.splitlines(False)
520)         assert err_lines[0].startswith('Passphrase:')
521)         assert any(  # pragma: no branch
522)             ' Warning: Setting a service passphrase is ineffective ' in line
523)             for line in err_lines
524)         ), 'expected known warning message'
525)         assert all(  # pragma: no branch
526)             is_harmless_config_import_warning_line(line)
527)             for line in result.stderr.splitlines(True)
528)         ), 'unexpected error output'
529) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

530)     @pytest.mark.parametrize(
531)         'option',
532)         [
533)             '--lower',
534)             '--upper',
535)             '--number',
536)             '--space',
537)             '--dash',
538)             '--symbol',
539)             '--repeat',
540)             '--length',
541)         ],
542)     )
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

543)     def test_210_invalid_argument_range(
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

544)         self, monkeypatch: pytest.MonkeyPatch, option: str
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

545)     ) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

546)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

547)         with tests.isolated_config(
548)             monkeypatch=monkeypatch,
549)             runner=runner,
550)         ):
551)             for value in '-42', 'invalid':
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

553)                     cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

554)                     [option, value, '-p', '--', DUMMY_SERVICE],
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

555)                     input=DUMMY_PASSPHRASE,
556)                     catch_exceptions=False,
557)                 )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

558)                 result = tests.ReadableResult.parse(_result)
559)                 assert result.error_exit(
560)                     error='Error: Invalid value'
561)                 ), 'expected error exit and known error message'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

562) 
563)     @pytest.mark.parametrize(
564)         ['options', 'service', 'input', 'check_success'],
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

565)         [
566)             (o.options, o.needs_service, o.input, o.check_success)
567)             for o in INTERESTING_OPTION_COMBINATIONS
568)             if not o.incompatible
569)         ],
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

570)     )
571)     def test_211_service_needed(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

572)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

573)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

574)         options: list[str],
575)         service: bool | None,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

576)         input: str | None,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

577)         check_success: bool,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

578)     ) -> None:
579)         monkeypatch.setattr(cli, '_prompt_for_passphrase', tests.auto_prompt)
580)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

581)         with tests.isolated_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

582)             monkeypatch=monkeypatch,
583)             runner=runner,
584)             config={'global': {'phrase': 'abc'}, 'services': {}},
585)         ):
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

587)                 cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

588)                 options if service else [*options, '--', DUMMY_SERVICE],
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

589)                 input=input,
590)                 catch_exceptions=False,
591)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

592)             result = tests.ReadableResult.parse(_result)
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

593)             if service is not None:
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

594)                 err_msg = (
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

595)                     ' requires a SERVICE'
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

596)                     if service
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

597)                     else ' does not take a SERVICE argument'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

598)                 )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

599)                 assert result.error_exit(
600)                     error=err_msg
601)                 ), 'expected error exit and known error message'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

602)             else:
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

603)                 assert result.clean_exit(
604)                     empty_stderr=True
605)                 ), 'expected clean exit'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

606)         if check_success:
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

607)             with tests.isolated_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

608)                 monkeypatch=monkeypatch,
609)                 runner=runner,
610)                 config={'global': {'phrase': 'abc'}, 'services': {}},
611)             ):
612)                 monkeypatch.setattr(
613)                     cli, '_prompt_for_passphrase', tests.auto_prompt
614)                 )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

616)                     cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

617)                     [*options, '--', DUMMY_SERVICE] if service else options,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

618)                     input=input,
619)                     catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

620)                 )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

621)                 result = tests.ReadableResult.parse(_result)
622)             assert result.clean_exit(empty_stderr=True), 'expected clean exit'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

623) 
Marco Ricci Warn the user upon supplyin...

Marco Ricci authored 2 months ago

624)     def test_211a_empty_service_name_causes_warning(
625)         self,
626)         monkeypatch: pytest.MonkeyPatch,
627)     ) -> None:
628)         def expected_warning_line(line: str) -> bool:
629)             return is_harmless_config_import_warning_line(line) or (
630)                 ' Warning: An empty SERVICE is not supported by vault(1)'
631)                 in line
632)             )
633) 
634)         monkeypatch.setattr(cli, '_prompt_for_passphrase', tests.auto_prompt)
635)         runner = click.testing.CliRunner(mix_stderr=False)
636)         with tests.isolated_vault_config(
637)             monkeypatch=monkeypatch,
638)             runner=runner,
639)             config={'services': {}},
640)         ):
641)             _result = runner.invoke(
642)                 cli.derivepassphrase_vault,
643)                 ['--config', '--length=30', '--', ''],
644)                 catch_exceptions=False,
645)             )
646)             result = tests.ReadableResult.parse(_result)
647)             assert result.clean_exit(empty_stderr=False), 'expected clean exit'
648)             assert result.stderr is not None, 'expected known error output'
649)             assert all(
650)                 expected_warning_line(line)
651)                 for line in result.stderr.splitlines(False)
652)             ), 'expected known error output'
653)             assert cli._load_config() == {
654)                 'global': {'length': 30},
655)                 'services': {},
656)             }, 'requested configuration change was not applied'
657)             _result = runner.invoke(
658)                 cli.derivepassphrase_vault,
659)                 ['--import', '-'],
660)                 input=json.dumps({'services': {'': {'length': 40}}}),
661)                 catch_exceptions=False,
662)             )
663)             result = tests.ReadableResult.parse(_result)
664)             assert result.clean_exit(empty_stderr=False), 'expected clean exit'
665)             assert result.stderr is not None, 'expected known error output'
666)             assert all(
667)                 expected_warning_line(line)
668)                 for line in result.stderr.splitlines(False)
669)             ), 'expected known error output'
670)             assert cli._load_config() == {
671)                 'global': {'length': 30},
672)                 'services': {'': {'length': 40}},
673)             }, 'requested configuration change was not applied'
674) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

675)     @pytest.mark.parametrize(
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

676)         ['options', 'service'],
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

677)         [
678)             (o.options, o.needs_service)
679)             for o in INTERESTING_OPTION_COMBINATIONS
680)             if o.incompatible
681)         ],
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

682)     )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

683)     def test_212_incompatible_options(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

684)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

685)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

686)         options: list[str],
687)         service: bool | None,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

688)     ) -> None:
689)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

690)         with tests.isolated_config(
691)             monkeypatch=monkeypatch,
692)             runner=runner,
693)         ):
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

695)                 cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

696)                 [*options, '--', DUMMY_SERVICE] if service else options,
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

697)                 input=DUMMY_PASSPHRASE,
698)                 catch_exceptions=False,
699)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

700)         result = tests.ReadableResult.parse(_result)
701)         assert result.error_exit(
702)             error='mutually exclusive with '
703)         ), 'expected error exit and known error message'
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

704) 
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

705)     @pytest.mark.parametrize(
706)         'config',
707)         [
708)             conf.config
Marco Ricci Fix missing consideration o...

Marco Ricci authored 2 months ago

709)             for conf in TEST_CONFIGS
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

710)             if tests.is_valid_test_config(conf)
711)         ],
712)     )
713)     def test_213_import_config_success(
714)         self,
715)         monkeypatch: pytest.MonkeyPatch,
716)         config: Any,
717)     ) -> None:
718)         runner = click.testing.CliRunner(mix_stderr=False)
719)         with tests.isolated_vault_config(
720)             monkeypatch=monkeypatch,
721)             runner=runner,
722)             config={'services': {}},
723)         ):
724)             _result = runner.invoke(
725)                 cli.derivepassphrase_vault,
726)                 ['--import', '-'],
727)                 input=json.dumps(config),
728)                 catch_exceptions=False,
729)             )
730)             with open(
731)                 cli._config_filename(subsystem='vault'), encoding='UTF-8'
732)             ) as infile:
733)                 config2 = json.load(infile)
734)         result = tests.ReadableResult.parse(_result)
Marco Ricci Fix missing consideration o...

Marco Ricci authored 2 months ago

735)         assert result.clean_exit(empty_stderr=False), 'expected clean exit'
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

736)         assert config2 == config, 'config not imported correctly'
Marco Ricci Fix missing consideration o...

Marco Ricci authored 2 months ago

737)         assert not result.stderr or all(  # pragma: no branch
738)             is_harmless_config_import_warning_line(line)
739)             for line in result.stderr.splitlines(True)
740)         ), 'unexpected error output'
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

741) 
Marco Ricci Permit one flaky test and f...

Marco Ricci authored 2 months ago

742)     @tests.hypothesis_settings_coverage_compatible
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

743)     @hypothesis.given(
744)         conf=tests.smudged_vault_test_config(
745)             strategies.sampled_from(TEST_CONFIGS).filter(
746)                 tests.is_valid_test_config
747)             )
748)         )
749)     )
750)     def test_213a_import_config_success(
751)         self,
752)         conf: tests.VaultTestConfig,
753)     ) -> None:
754)         config = conf.config
755)         config2 = copy.deepcopy(config)
756)         _types.clean_up_falsy_vault_config_values(config2)
757)         runner = click.testing.CliRunner(mix_stderr=False)
758)         with tests.isolated_vault_config(
759)             monkeypatch=pytest.MonkeyPatch(),
760)             runner=runner,
761)             config={'services': {}},
762)         ):
763)             _result = runner.invoke(
764)                 cli.derivepassphrase_vault,
765)                 ['--import', '-'],
766)                 input=json.dumps(config),
767)                 catch_exceptions=False,
768)             )
769)             with open(
770)                 cli._config_filename(subsystem='vault'), encoding='UTF-8'
771)             ) as infile:
772)                 config3 = json.load(infile)
773)         result = tests.ReadableResult.parse(_result)
Marco Ricci Signal and list falsy value...

Marco Ricci authored 2 months ago

774)         assert result.clean_exit(empty_stderr=False), 'expected clean exit'
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

775)         assert config3 == config2, 'config not imported correctly'
Marco Ricci Signal and list falsy value...

Marco Ricci authored 2 months ago

776)         assert not result.stderr or all(
Marco Ricci Fix missing consideration o...

Marco Ricci authored 2 months ago

777)             is_harmless_config_import_warning_line(line)
Marco Ricci Signal and list falsy value...

Marco Ricci authored 2 months ago

778)             for line in result.stderr.splitlines(True)
779)         ), 'unexpected error output'
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

780) 
781)     def test_213b_import_bad_config_not_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

782)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

783)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

784)     ) -> None:
785)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

786)         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

788)                 cli.derivepassphrase_vault,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

789)                 ['--import', '-'],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

790)                 input='null',
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

791)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

792)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

793)         result = tests.ReadableResult.parse(_result)
794)         assert result.error_exit(
795)             error='Invalid vault config'
796)         ), 'expected error exit and known error message'
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

797) 
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

798)     def test_213c_import_bad_config_not_json_data(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

799)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

800)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

801)     ) -> None:
802)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

803)         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

805)                 cli.derivepassphrase_vault,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

806)                 ['--import', '-'],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

807)                 input='This string is not valid JSON.',
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

808)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

809)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

810)         result = tests.ReadableResult.parse(_result)
811)         assert result.error_exit(
812)             error='cannot decode JSON'
813)         ), 'expected error exit and known error message'
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

814) 
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

815)     def test_213d_import_bad_config_not_a_file(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

816)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

817)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

818)     ) -> None:
819)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

820)         # `isolated_vault_config` validates the configuration.  So, to
821)         # pass an actual broken configuration, we must open the
822)         # configuration file ourselves afterwards, inside the context.
823)         # We also might as well use `isolated_config` instead.
824)         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

825)             with open(
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

826)                 cli._config_filename(subsystem='vault'), 'w', encoding='UTF-8'
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

827)             ) as outfile:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

828)                 print('This string is not valid JSON.', file=outfile)
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

829)             dname = cli._config_filename(subsystem=None)
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

831)                 cli.derivepassphrase_vault,
Marco Ricci Fix miscellaneous type chec...

Marco Ricci authored 5 months ago

832)                 ['--import', os.fsdecode(dname)],
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

833)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

834)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

835)         result = tests.ReadableResult.parse(_result)
836)         assert result.error_exit(
837)             error=os.strerror(errno.EISDIR)
838)         ), 'expected error exit and known error message'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

839) 
840)     def test_214_export_settings_no_stored_settings(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

841)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

842)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

843)     ) -> None:
844)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

845)         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

846)             with contextlib.suppress(FileNotFoundError):
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

847)                 os.remove(cli._config_filename(subsystem='vault'))
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

849)                 cli.derivepassphrase_vault,
850)                 ['--export', '-'],
851)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

852)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

853)         result = tests.ReadableResult.parse(_result)
854)         assert result.clean_exit(empty_stderr=True), 'expected clean exit'
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

855) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

856)     def test_214a_export_settings_bad_stored_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

857)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

858)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

859)     ) -> None:
860)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

861)         with tests.isolated_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

862)             monkeypatch=monkeypatch, runner=runner, config={}
863)         ):
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

865)                 cli.derivepassphrase_vault,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

866)                 ['--export', '-'],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

867)                 input='null',
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

868)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

869)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

870)         result = tests.ReadableResult.parse(_result)
871)         assert result.error_exit(
872)             error='Cannot load config'
873)         ), 'expected error exit and known error message'
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

874) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

875)     def test_214b_export_settings_not_a_file(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

876)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

877)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

878)     ) -> None:
879)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

880)         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

881)             with contextlib.suppress(FileNotFoundError):
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

882)                 os.remove(cli._config_filename(subsystem='vault'))
883)             os.makedirs(cli._config_filename(subsystem='vault'))
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

885)                 cli.derivepassphrase_vault,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

886)                 ['--export', '-'],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

887)                 input='null',
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

888)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

889)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

890)         result = tests.ReadableResult.parse(_result)
891)         assert result.error_exit(
892)             error='Cannot load config'
893)         ), 'expected error exit and known error message'
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

894) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

895)     def test_214c_export_settings_target_not_a_file(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

896)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

897)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

898)     ) -> None:
899)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

900)         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

901)             dname = cli._config_filename(subsystem=None)
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

903)                 cli.derivepassphrase_vault,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

904)                 ['--export', os.fsdecode(dname)],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

905)                 input='null',
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

906)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

907)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

908)         result = tests.ReadableResult.parse(_result)
909)         assert result.error_exit(
910)             error='Cannot store config'
911)         ), 'expected error exit and known error message'
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

912) 
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

913)     def test_214d_export_settings_settings_directory_not_a_directory(
914)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

915)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

916)     ) -> None:
917)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

918)         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

919)             with contextlib.suppress(FileNotFoundError):
920)                 shutil.rmtree('.derivepassphrase')
921)             with open('.derivepassphrase', 'w', encoding='UTF-8') as outfile:
922)                 print('Obstruction!!', file=outfile)
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

924)                 cli.derivepassphrase_vault,
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

925)                 ['--export', '-'],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

926)                 input='null',
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

927)                 catch_exceptions=False,
928)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

929)         result = tests.ReadableResult.parse(_result)
930)         assert result.error_exit(
931)             error='Cannot load config'
932)         ), 'expected error exit and known error message'
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

933) 
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

934)     def test_220_edit_notes_successfully(
935)         self, monkeypatch: pytest.MonkeyPatch
936)     ) -> None:
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

937)         edit_result = """
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

938) 
939) # - - - - - >8 - - - - - >8 - - - - - >8 - - - - - >8 - - - - -
940) contents go here
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

941) """
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

942)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

943)         with tests.isolated_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

944)             monkeypatch=monkeypatch,
945)             runner=runner,
946)             config={'global': {'phrase': 'abc'}, 'services': {}},
947)         ):
948)             monkeypatch.setattr(click, 'edit', lambda *a, **kw: edit_result)  # noqa: ARG005
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

950)                 cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

951)                 ['--notes', '--', 'sv'],
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

952)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

953)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

954)             result = tests.ReadableResult.parse(_result)
955)             assert result.clean_exit(empty_stderr=True), 'expected clean exit'
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

956)             with open(
957)                 cli._config_filename(subsystem='vault'), encoding='UTF-8'
958)             ) as infile:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

959)                 config = json.load(infile)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

960)             assert config == {
961)                 'global': {'phrase': 'abc'},
962)                 'services': {'sv': {'notes': 'contents go here'}},
963)             }
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

964) 
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

965)     def test_221_edit_notes_noop(
966)         self, monkeypatch: pytest.MonkeyPatch
967)     ) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

968)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

969)         with tests.isolated_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

970)             monkeypatch=monkeypatch,
971)             runner=runner,
972)             config={'global': {'phrase': 'abc'}, 'services': {}},
973)         ):
974)             monkeypatch.setattr(click, 'edit', lambda *a, **kw: None)  # noqa: ARG005
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

976)                 cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

977)                 ['--notes', '--', 'sv'],
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

978)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

979)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

980)             result = tests.ReadableResult.parse(_result)
981)             assert result.clean_exit(empty_stderr=True), 'expected clean exit'
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

982)             with open(
983)                 cli._config_filename(subsystem='vault'), encoding='UTF-8'
984)             ) as infile:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

985)                 config = json.load(infile)
986)             assert config == {'global': {'phrase': 'abc'}, 'services': {}}
987) 
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

988)     def test_222_edit_notes_marker_removed(
989)         self, monkeypatch: pytest.MonkeyPatch
990)     ) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

991)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

992)         with tests.isolated_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

993)             monkeypatch=monkeypatch,
994)             runner=runner,
995)             config={'global': {'phrase': 'abc'}, 'services': {}},
996)         ):
997)             monkeypatch.setattr(click, 'edit', lambda *a, **kw: 'long\ntext')  # noqa: ARG005
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

999)                 cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

1000)                 ['--notes', '--', 'sv'],
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

1001)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1002)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1003)             result = tests.ReadableResult.parse(_result)
1004)             assert result.clean_exit(empty_stderr=True), 'expected clean exit'
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

1005)             with open(
1006)                 cli._config_filename(subsystem='vault'), encoding='UTF-8'
1007)             ) as infile:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1008)                 config = json.load(infile)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1009)             assert config == {
1010)                 'global': {'phrase': 'abc'},
1011)                 'services': {'sv': {'notes': 'long\ntext'}},
1012)             }
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1013) 
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1014)     def test_223_edit_notes_abort(
1015)         self, monkeypatch: pytest.MonkeyPatch
1016)     ) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1017)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

1018)         with tests.isolated_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1019)             monkeypatch=monkeypatch,
1020)             runner=runner,
1021)             config={'global': {'phrase': 'abc'}, 'services': {}},
1022)         ):
1023)             monkeypatch.setattr(click, 'edit', lambda *a, **kw: '\n\n')  # noqa: ARG005
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1025)                 cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

1026)                 ['--notes', '--', 'sv'],
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

1027)                 catch_exceptions=False,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1028)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1029)             result = tests.ReadableResult.parse(_result)
1030)             assert result.error_exit(
1031)                 error='user aborted request'
1032)             ), 'expected known error message'
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

1033)             with open(
1034)                 cli._config_filename(subsystem='vault'), encoding='UTF-8'
1035)             ) as infile:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1036)                 config = json.load(infile)
1037)             assert config == {'global': {'phrase': 'abc'}, 'services': {}}
1038) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1039)     @pytest.mark.parametrize(
1040)         ['command_line', 'input', 'result_config'],
1041)         [
1042)             (
1043)                 ['--phrase'],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1044)                 'my passphrase\n',
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1045)                 {'global': {'phrase': 'my passphrase'}, 'services': {}},
1046)             ),
1047)             (
1048)                 ['--key'],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1049)                 '1\n',
Marco Ricci Fix missing consideration o...

Marco Ricci authored 2 months ago

1050)                 {
1051)                     'global': {'key': DUMMY_KEY1_B64, 'phrase': 'abc'},
1052)                     'services': {},
1053)                 },
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1054)             ),
1055)             (
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

1056)                 ['--phrase', '--', 'sv'],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1057)                 'my passphrase\n',
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1058)                 {
1059)                     'global': {'phrase': 'abc'},
1060)                     'services': {'sv': {'phrase': 'my passphrase'}},
1061)                 },
1062)             ),
1063)             (
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

1064)                 ['--key', '--', 'sv'],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1065)                 '1\n',
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1066)                 {
1067)                     'global': {'phrase': 'abc'},
1068)                     'services': {'sv': {'key': DUMMY_KEY1_B64}},
1069)                 },
1070)             ),
1071)             (
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

1072)                 ['--key', '--length', '15', '--', 'sv'],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1073)                 '1\n',
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1074)                 {
1075)                     'global': {'phrase': 'abc'},
1076)                     'services': {'sv': {'key': DUMMY_KEY1_B64, 'length': 15}},
1077)                 },
1078)             ),
1079)         ],
1080)     )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1081)     def test_224_store_config_good(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1082)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1083)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1084)         command_line: list[str],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1085)         input: str,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1086)         result_config: Any,
1087)     ) -> None:
1088)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

1089)         with tests.isolated_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1090)             monkeypatch=monkeypatch,
1091)             runner=runner,
1092)             config={'global': {'phrase': 'abc'}, 'services': {}},
1093)         ):
1094)             monkeypatch.setattr(
1095)                 cli, '_get_suitable_ssh_keys', tests.suitable_ssh_keys
1096)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1098)                 cli.derivepassphrase_vault,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1099)                 ['--config', *command_line],
1100)                 catch_exceptions=False,
1101)                 input=input,
1102)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1103)             result = tests.ReadableResult.parse(_result)
1104)             assert result.clean_exit(), 'expected clean exit'
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

1105)             with open(
1106)                 cli._config_filename(subsystem='vault'), encoding='UTF-8'
1107)             ) as infile:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1108)                 config = json.load(infile)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1109)             assert (
1110)                 config == result_config
1111)             ), 'stored config does not match expectation'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1112) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1113)     @pytest.mark.parametrize(
1114)         ['command_line', 'input', 'err_text'],
1115)         [
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1116)             ([], '', 'Cannot update global settings without actual settings'),
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1117)             (
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

1118)                 ['--', 'sv'],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1119)                 '',
1120)                 'Cannot update service settings without actual settings',
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1121)             ),
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

1122)             (['--phrase', '--', 'sv'], '', 'No passphrase given'),
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1123)             (['--key'], '', 'No valid SSH key selected'),
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1124)         ],
1125)     )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1126)     def test_225_store_config_fail(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1127)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1128)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1129)         command_line: list[str],
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1130)         input: str,
1131)         err_text: str,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1132)     ) -> None:
1133)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

1134)         with tests.isolated_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1135)             monkeypatch=monkeypatch,
1136)             runner=runner,
1137)             config={'global': {'phrase': 'abc'}, 'services': {}},
1138)         ):
1139)             monkeypatch.setattr(
1140)                 cli, '_get_suitable_ssh_keys', tests.suitable_ssh_keys
1141)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1143)                 cli.derivepassphrase_vault,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1144)                 ['--config', *command_line],
1145)                 catch_exceptions=False,
1146)                 input=input,
1147)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1148)         result = tests.ReadableResult.parse(_result)
1149)         assert result.error_exit(
1150)             error=err_text
1151)         ), 'expected error exit and known error message'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1152) 
1153)     def test_225a_store_config_fail_manual_no_ssh_key_selection(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1154)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1155)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1156)     ) -> None:
1157)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

1158)         with tests.isolated_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1159)             monkeypatch=monkeypatch,
1160)             runner=runner,
1161)             config={'global': {'phrase': 'abc'}, 'services': {}},
1162)         ):
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

1163)             custom_error = 'custom error message'
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1164) 
Marco Ricci Fix typing issues in mypy s...

Marco Ricci authored 5 months ago

1165)             def raiser() -> None:
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

1166)                 raise RuntimeError(custom_error)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1167) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1168)             monkeypatch.setattr(cli, '_select_ssh_key', raiser)
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1170)                 cli.derivepassphrase_vault,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1171)                 ['--key', '--config'],
1172)                 catch_exceptions=False,
1173)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1174)         result = tests.ReadableResult.parse(_result)
1175)         assert result.error_exit(
1176)             error=custom_error
1177)         ), 'expected error exit and known error message'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1178) 
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1179)     def test_225b_store_config_fail_manual_no_ssh_agent(
1180)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1181)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Fail gracefully if UNIX dom...

Marco Ricci authored 2 months ago

1182)         skip_if_no_af_unix_support: None,
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1183)     ) -> None:
Marco Ricci Fail gracefully if UNIX dom...

Marco Ricci authored 2 months ago

1184)         del skip_if_no_af_unix_support
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1185)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

1186)         with tests.isolated_vault_config(
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1187)             monkeypatch=monkeypatch,
1188)             runner=runner,
1189)             config={'global': {'phrase': 'abc'}, 'services': {}},
1190)         ):
1191)             monkeypatch.delenv('SSH_AUTH_SOCK', raising=False)
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1193)                 cli.derivepassphrase_vault,
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1194)                 ['--key', '--config'],
1195)                 catch_exceptions=False,
1196)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1197)         result = tests.ReadableResult.parse(_result)
1198)         assert result.error_exit(
1199)             error='Cannot find running SSH agent'
1200)         ), 'expected error exit and known error message'
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1201) 
1202)     def test_225c_store_config_fail_manual_bad_ssh_agent_connection(
1203)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1204)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1205)     ) -> None:
1206)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

1207)         with tests.isolated_vault_config(
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1208)             monkeypatch=monkeypatch,
1209)             runner=runner,
1210)             config={'global': {'phrase': 'abc'}, 'services': {}},
1211)         ):
1212)             monkeypatch.setenv('SSH_AUTH_SOCK', os.getcwd())
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1214)                 cli.derivepassphrase_vault,
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1215)                 ['--key', '--config'],
1216)                 catch_exceptions=False,
1217)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1218)         result = tests.ReadableResult.parse(_result)
1219)         assert result.error_exit(
1220)             error='Cannot connect to SSH agent'
1221)         ), 'expected error exit and known error message'
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1222) 
1223)     @pytest.mark.parametrize('try_race_free_implementation', [True, False])
1224)     def test_225d_store_config_fail_manual_read_only_file(
1225)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1226)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1227)         try_race_free_implementation: bool,
1228)     ) -> None:
1229)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

1230)         with tests.isolated_vault_config(
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1231)             monkeypatch=monkeypatch,
1232)             runner=runner,
1233)             config={'global': {'phrase': 'abc'}, 'services': {}},
1234)         ):
1235)             tests.make_file_readonly(
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

1236)                 cli._config_filename(subsystem='vault'),
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1237)                 try_race_free_implementation=try_race_free_implementation,
1238)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1240)                 cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

1241)                 ['--config', '--length=15', '--', DUMMY_SERVICE],
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1242)                 catch_exceptions=False,
1243)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1244)         result = tests.ReadableResult.parse(_result)
1245)         assert result.error_exit(
1246)             error='Cannot store config'
1247)         ), 'expected error exit and known error message'
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1248) 
1249)     def test_225e_store_config_fail_manual_custom_error(
1250)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1251)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1252)     ) -> None:
1253)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

1254)         with tests.isolated_vault_config(
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1255)             monkeypatch=monkeypatch,
1256)             runner=runner,
1257)             config={'global': {'phrase': 'abc'}, 'services': {}},
1258)         ):
1259)             custom_error = 'custom error message'
1260) 
1261)             def raiser(config: Any) -> None:
1262)                 del config
1263)                 raise RuntimeError(custom_error)
1264) 
1265)             monkeypatch.setattr(cli, '_save_config', raiser)
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1267)                 cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

1268)                 ['--config', '--length=15', '--', DUMMY_SERVICE],
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1269)                 catch_exceptions=False,
1270)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1271)         result = tests.ReadableResult.parse(_result)
1272)         assert result.error_exit(
1273)             error=custom_error
1274)         ), 'expected error exit and known error message'
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1275) 
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1276)     def test_226_no_arguments(self, monkeypatch: pytest.MonkeyPatch) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1277)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

1278)         with tests.isolated_config(
1279)             monkeypatch=monkeypatch,
1280)             runner=runner,
1281)         ):
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1283)                 cli.derivepassphrase_vault, [], catch_exceptions=False
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

1284)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1285)         result = tests.ReadableResult.parse(_result)
1286)         assert result.error_exit(
1287)             error='SERVICE is required'
1288)         ), 'expected error exit and known error message'
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

1289) 
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1290)     def test_226a_no_passphrase_or_key(
1291)         self, monkeypatch: pytest.MonkeyPatch
1292)     ) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1293)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

1294)         with tests.isolated_config(
1295)             monkeypatch=monkeypatch,
1296)             runner=runner,
1297)         ):
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1299)                 cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

1300)                 ['--', DUMMY_SERVICE],
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

1301)                 catch_exceptions=False,
Marco Ricci Isolate tests properly and...

Marco Ricci authored 4 months ago

1302)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1303)         result = tests.ReadableResult.parse(_result)
1304)         assert result.error_exit(
1305)             error='No passphrase or key given'
1306)         ), 'expected error exit and known error message'
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

1307) 
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1308)     def test_230_config_directory_nonexistant(
1309)         self, monkeypatch: pytest.MonkeyPatch
1310)     ) -> None:
Marco Ricci Fix minor typo, formatting...

Marco Ricci authored 3 months ago

1311)         """https://github.com/the-13th-letter/derivepassphrase/issues/6"""
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

1312)         runner = click.testing.CliRunner(mix_stderr=False)
1313)         with tests.isolated_config(
1314)             monkeypatch=monkeypatch,
1315)             runner=runner,
1316)         ):
Marco Ricci Fix minor typo, formatting...

Marco Ricci authored 3 months ago

1317)             shutil.rmtree('.derivepassphrase')
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

1318)             os_makedirs_called = False
1319)             real_os_makedirs = os.makedirs
1320) 
1321)             def makedirs(*args: Any, **kwargs: Any) -> Any:
1322)                 nonlocal os_makedirs_called
1323)                 os_makedirs_called = True
1324)                 return real_os_makedirs(*args, **kwargs)
1325) 
1326)             monkeypatch.setattr(os, 'makedirs', makedirs)
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1328)                 cli.derivepassphrase_vault,
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

1329)                 ['--config', '-p'],
1330)                 catch_exceptions=False,
1331)                 input='abc\n',
1332)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1333)             result = tests.ReadableResult.parse(_result)
1334)             assert result.clean_exit(), 'expected clean exit'
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

1335)             assert (
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1336)                 result.stderr == 'Passphrase:'
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

1337)             ), 'program unexpectedly failed?!'
1338)             assert os_makedirs_called, 'os.makedirs has not been called?!'
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

1339)             with open(
1340)                 cli._config_filename(subsystem='vault'), encoding='UTF-8'
1341)             ) as infile:
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

1342)                 config_readback = json.load(infile)
1343)             assert config_readback == {
1344)                 'global': {'phrase': 'abc'},
1345)                 'services': {},
1346)             }, 'config mismatch'
1347) 
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1348)     def test_230a_config_directory_not_a_file(
1349)         self, monkeypatch: pytest.MonkeyPatch
1350)     ) -> None:
Marco Ricci Fix minor typo, formatting...

Marco Ricci authored 3 months ago

1351)         """https://github.com/the-13th-letter/derivepassphrase/issues/6"""
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

1352)         runner = click.testing.CliRunner(mix_stderr=False)
1353)         with tests.isolated_config(
1354)             monkeypatch=monkeypatch,
1355)             runner=runner,
1356)         ):
1357)             _save_config = cli._save_config
1358) 
1359)             def obstruct_config_saving(*args: Any, **kwargs: Any) -> Any:
1360)                 with contextlib.suppress(FileNotFoundError):
1361)                     shutil.rmtree('.derivepassphrase')
1362)                 with open(
1363)                     '.derivepassphrase', 'w', encoding='UTF-8'
1364)                 ) as outfile:
1365)                     print('Obstruction!!', file=outfile)
1366)                 monkeypatch.setattr(cli, '_save_config', _save_config)
1367)                 return _save_config(*args, **kwargs)
1368) 
1369)             monkeypatch.setattr(cli, '_save_config', obstruct_config_saving)
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1371)                 cli.derivepassphrase_vault,
Marco Ricci Fix error bubbling in outda...

Marco Ricci authored 4 months ago

1372)                 ['--config', '-p'],
1373)                 catch_exceptions=False,
1374)                 input='abc\n',
1375)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1376)             result = tests.ReadableResult.parse(_result)
1377)             assert result.error_exit(
1378)                 error='Cannot store config'
1379)             ), 'expected error exit and known error message'
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1380) 
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1381)     def test_230b_store_config_custom_error(
1382)         self, monkeypatch: pytest.MonkeyPatch
1383)     ) -> None:
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1384)         runner = click.testing.CliRunner(mix_stderr=False)
1385)         with tests.isolated_config(
1386)             monkeypatch=monkeypatch,
1387)             runner=runner,
1388)         ):
Marco Ricci Fix minor typo, formatting...

Marco Ricci authored 3 months ago

1389)             custom_error = 'custom error message'
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1390) 
Marco Ricci Fix minor typo, formatting...

Marco Ricci authored 3 months ago

1391)             def raiser(config: Any) -> None:
1392)                 del config
1393)                 raise RuntimeError(custom_error)
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1394) 
Marco Ricci Fix minor typo, formatting...

Marco Ricci authored 3 months ago

1395)             monkeypatch.setattr(cli, '_save_config', raiser)
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1397)                 cli.derivepassphrase_vault,
Marco Ricci Add missing tests for rewor...

Marco Ricci authored 4 months ago

1398)                 ['--config', '-p'],
1399)                 catch_exceptions=False,
1400)                 input='abc\n',
1401)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1402)             result = tests.ReadableResult.parse(_result)
1403)             assert result.error_exit(
Marco Ricci Fix minor typo, formatting...

Marco Ricci authored 3 months ago

1404)                 error=custom_error
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1405)             ), 'expected error exit and known error message'
Marco Ricci Create the configuration di...

Marco Ricci authored 4 months ago

1406) 
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1407)     @pytest.mark.parametrize(
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1408)         ['command_line', 'input', 'warning_message'],
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1409)         [
1410)             pytest.param(
1411)                 ['--import', '-'],
1412)                 json.dumps({
1413)                     'global': {'phrase': 'Du\u0308sseldorf'},
1414)                     'services': {},
1415)                 }),
Marco Ricci Signal and list falsy value...

Marco Ricci authored 2 months ago

1416)                 'the $.global passphrase is not NFC-normalized',
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1417)                 id='global-NFC',
1418)             ),
1419)             pytest.param(
1420)                 ['--import', '-'],
1421)                 json.dumps({
1422)                     'services': {
1423)                         DUMMY_SERVICE: DUMMY_CONFIG_SETTINGS.copy(),
1424)                         'weird entry name': {'phrase': 'Du\u0308sseldorf'},
1425)                     }
1426)                 }),
1427)                 (
Marco Ricci Signal and list falsy value...

Marco Ricci authored 2 months ago

1428)                     'the $.services["weird entry name"] passphrase '
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1429)                     'is not NFC-normalized'
1430)                 ),
1431)                 id='service-weird-name-NFC',
1432)             ),
1433)             pytest.param(
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

1434)                 ['--config', '-p', '--', DUMMY_SERVICE],
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1435)                 'Du\u0308sseldorf',
1436)                 (
Marco Ricci Signal and list falsy value...

Marco Ricci authored 2 months ago

1437)                     f'the $.services.{DUMMY_SERVICE} passphrase '
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1438)                     f'is not NFC-normalized'
1439)                 ),
1440)                 id='config-NFC',
1441)             ),
1442)             pytest.param(
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

1443)                 ['-p', '--', DUMMY_SERVICE],
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1444)                 'Du\u0308sseldorf',
1445)                 'the interactive passphrase is not NFC-normalized',
1446)                 id='direct-input-NFC',
1447)             ),
1448)             pytest.param(
1449)                 ['--import', '-'],
1450)                 json.dumps({
1451)                     'global': {
1452)                         'unicode_normalization_form': 'NFD',
1453)                         'phrase': 'D\u00fcsseldorf',
1454)                     },
1455)                     'services': {},
1456)                 }),
Marco Ricci Signal and list falsy value...

Marco Ricci authored 2 months ago

1457)                 'the $.global passphrase is not NFD-normalized',
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1458)                 id='global-NFD',
1459)             ),
1460)             pytest.param(
1461)                 ['--import', '-'],
1462)                 json.dumps({
1463)                     'global': {
1464)                         'unicode_normalization_form': 'NFD',
1465)                     },
1466)                     'services': {
1467)                         DUMMY_SERVICE: DUMMY_CONFIG_SETTINGS.copy(),
1468)                         'weird entry name': {'phrase': 'D\u00fcsseldorf'},
Marco Ricci Apply new ruff ruleset to c...

Marco Ricci authored 3 months ago

1469)                     },
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1470)                 }),
1471)                 (
Marco Ricci Signal and list falsy value...

Marco Ricci authored 2 months ago

1472)                     'the $.services["weird entry name"] passphrase '
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1473)                     'is not NFD-normalized'
1474)                 ),
1475)                 id='service-weird-name-NFD',
1476)             ),
1477)         ],
1478)     )
1479)     def test_300_unicode_normalization_form_warning(
1480)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1481)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1482)         command_line: list[str],
1483)         input: str | None,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1484)         warning_message: str,
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1485)     ) -> None:
1486)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

1487)         with tests.isolated_vault_config(
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1488)             monkeypatch=monkeypatch,
1489)             runner=runner,
1490)             config={'services': {DUMMY_SERVICE: DUMMY_CONFIG_SETTINGS.copy()}},
1491)         ):
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1493)                 cli.derivepassphrase_vault,
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1494)                 command_line,
1495)                 catch_exceptions=False,
1496)                 input=input,
1497)             )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1498)         result = tests.ReadableResult.parse(_result)
1499)         assert result.clean_exit(), 'expected clean exit'
1500)         assert (
1501)             warning_message in result.stderr
1502)         ), 'expected known warning message in stderr'
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

1503) 
Marco Ricci Fail gracefully if UNIX dom...

Marco Ricci authored 2 months ago

1504)     def test_400_missing_af_unix_support(
1505)         self,
1506)         monkeypatch: pytest.MonkeyPatch,
1507)     ) -> None:
1508)         runner = click.testing.CliRunner(mix_stderr=False)
1509)         with tests.isolated_vault_config(
1510)             monkeypatch=monkeypatch,
1511)             runner=runner,
1512)             config={'global': {'phrase': 'abc'}, 'services': {}},
1513)         ):
1514)             monkeypatch.setenv(
1515)                 'SSH_AUTH_SOCK', "the value doesn't even matter"
1516)             )
1517)             monkeypatch.delattr(socket, 'AF_UNIX', raising=False)
1518)             _result = runner.invoke(
1519)                 cli.derivepassphrase_vault,
1520)                 ['--key', '--config'],
1521)                 catch_exceptions=False,
1522)             )
1523)         result = tests.ReadableResult.parse(_result)
1524)         assert result.error_exit(
1525)             error='does not support UNIX domain sockets'
1526)         ), 'expected error exit and known error message'
1527) 
Marco Ricci Add finished command-line i...

Marco Ricci authored 6 months ago

1528) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1529) class TestCLIUtils:
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

1530)     @pytest.mark.parametrize(
1531)         'config',
1532)         [
1533)             {'global': {'phrase': 'my passphrase'}, 'services': {}},
1534)             {'global': {'key': DUMMY_KEY1_B64}, 'services': {}},
1535)             {
1536)                 'global': {'phrase': 'abc'},
1537)                 'services': {'sv': {'phrase': 'my passphrase'}},
1538)             },
1539)             {
1540)                 'global': {'phrase': 'abc'},
1541)                 'services': {'sv': {'key': DUMMY_KEY1_B64}},
1542)             },
1543)             {
1544)                 'global': {'phrase': 'abc'},
1545)                 'services': {'sv': {'key': DUMMY_KEY1_B64, 'length': 15}},
1546)             },
1547)         ],
1548)     )
1549)     def test_100_load_config(
1550)         self, monkeypatch: pytest.MonkeyPatch, config: Any
1551)     ) -> None:
1552)         runner = click.testing.CliRunner()
1553)         with tests.isolated_vault_config(
1554)             monkeypatch=monkeypatch, runner=runner, config=config
1555)         ):
1556)             config_filename = cli._config_filename(subsystem='vault')
1557)             with open(config_filename, encoding='UTF-8') as fileobj:
1558)                 assert json.load(fileobj) == config
1559)             assert cli._load_config() == config
1560) 
1561)     def test_110_save_bad_config(
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1562)         self, monkeypatch: pytest.MonkeyPatch
1563)     ) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1564)         runner = click.testing.CliRunner()
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

1565)         with (
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

1566)             tests.isolated_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1567)                 monkeypatch=monkeypatch, runner=runner, config={}
1568)             ),
1569)             pytest.raises(ValueError, match='Invalid vault config'),
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

1570)         ):
Marco Ricci Apply new ruff ruleset to c...

Marco Ricci authored 3 months ago

1571)             cli._save_config(None)  # type: ignore[arg-type]
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1572) 
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

1573)     def test_111_prompt_for_selection_multiple(self) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1574)         @click.command()
1575)         @click.option('--heading', default='Our menu:')
1576)         @click.argument('items', nargs=-1)
Marco Ricci Fix typing issues in mypy s...

Marco Ricci authored 5 months ago

1577)         def driver(heading: str, items: list[str]) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1578)             # from https://montypython.fandom.com/wiki/Spam#The_menu
1579)             items = items or [
1580)                 'Egg and bacon',
1581)                 'Egg, sausage and bacon',
1582)                 'Egg and spam',
1583)                 'Egg, bacon and spam',
1584)                 'Egg, bacon, sausage and spam',
1585)                 'Spam, bacon, sausage and spam',
1586)                 'Spam, egg, spam, spam, bacon and spam',
1587)                 'Spam, spam, spam, egg and spam',
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1588)                 (
1589)                     'Spam, spam, spam, spam, spam, spam, baked beans, '
1590)                     'spam, spam, spam and spam'
1591)                 ),
1592)                 (
1593)                     'Lobster thermidor aux crevettes with a mornay sauce '
1594)                     'garnished with truffle paté, brandy '
1595)                     'and a fried egg on top and spam'
1596)                 ),
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1597)             ]
1598)             index = cli._prompt_for_selection(items, heading=heading)
1599)             click.echo('A fine choice: ', nl=False)
1600)             click.echo(items[index])
1601)             click.echo('(Note: Vikings strictly optional.)')
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1602) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1603)         runner = click.testing.CliRunner(mix_stderr=True)
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1604)         _result = runner.invoke(driver, [], input='9')
1605)         result = tests.ReadableResult.parse(_result)
1606)         assert result.clean_exit(
1607)             output="""\
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1608) Our menu:
1609) [1] Egg and bacon
1610) [2] Egg, sausage and bacon
1611) [3] Egg and spam
1612) [4] Egg, bacon and spam
1613) [5] Egg, bacon, sausage and spam
1614) [6] Spam, bacon, sausage and spam
1615) [7] Spam, egg, spam, spam, bacon and spam
1616) [8] Spam, spam, spam, egg and spam
1617) [9] Spam, spam, spam, spam, spam, spam, baked beans, spam, spam, spam and spam
1618) [10] Lobster thermidor aux crevettes with a mornay sauce garnished with truffle paté, brandy and a fried egg on top and spam
1619) Your selection? (1-10, leave empty to abort): 9
1620) A fine choice: Spam, spam, spam, spam, spam, spam, baked beans, spam, spam, spam and spam
1621) (Note: Vikings strictly optional.)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1622) """  # noqa: E501
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1623)         ), 'expected clean exit'
1624)         _result = runner.invoke(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1625)             driver, ['--heading='], input='', catch_exceptions=True
1626)         )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1627)         result = tests.ReadableResult.parse(_result)
1628)         assert result.error_exit(
1629)             error=IndexError
1630)         ), 'expected error exit and known error type'
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1631)         assert (
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1632)             result.output
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1633)             == """\
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1634) [1] Egg and bacon
1635) [2] Egg, sausage and bacon
1636) [3] Egg and spam
1637) [4] Egg, bacon and spam
1638) [5] Egg, bacon, sausage and spam
1639) [6] Spam, bacon, sausage and spam
1640) [7] Spam, egg, spam, spam, bacon and spam
1641) [8] Spam, spam, spam, egg and spam
1642) [9] Spam, spam, spam, spam, spam, spam, baked beans, spam, spam, spam and spam
1643) [10] Lobster thermidor aux crevettes with a mornay sauce garnished with truffle paté, brandy and a fried egg on top and spam
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1644) Your selection? (1-10, leave empty to abort):\x20
1645) """  # noqa: E501
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1646)         ), 'expected known output'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1647) 
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

1648)     def test_112_prompt_for_selection_single(self) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1649)         @click.command()
1650)         @click.option('--item', default='baked beans')
1651)         @click.argument('prompt')
Marco Ricci Fix typing issues in mypy s...

Marco Ricci authored 5 months ago

1652)         def driver(item: str, prompt: str) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1653)             try:
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1654)                 cli._prompt_for_selection(
1655)                     [item], heading='', single_choice_prompt=prompt
1656)                 )
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

1657)             except IndexError:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1658)                 click.echo('Boo.')
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

1659)                 raise
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1660)             else:
1661)                 click.echo('Great!')
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1662) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1663)         runner = click.testing.CliRunner(mix_stderr=True)
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1664)         _result = runner.invoke(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1665)             driver, ['Will replace with spam. Confirm, y/n?'], input='y'
1666)         )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1667)         result = tests.ReadableResult.parse(_result)
1668)         assert result.clean_exit(
1669)             output="""\
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1670) [1] baked beans
1671) Will replace with spam. Confirm, y/n? y
1672) Great!
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1673) """
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1674)         ), 'expected clean exit'
1675)         _result = runner.invoke(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1676)             driver,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

1677)             ['Will replace with spam, okay? (Please say "y" or "n".)'],
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1678)             input='',
1679)         )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1680)         result = tests.ReadableResult.parse(_result)
1681)         assert result.error_exit(
1682)             error=IndexError
1683)         ), 'expected error exit and known error type'
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1684)         assert (
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1685)             result.output
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1686)             == """\
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1687) [1] baked beans
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

1688) Will replace with spam, okay? (Please say "y" or "n".):\x20
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1689) Boo.
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1690) """
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1691)         ), 'expected known output'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1692) 
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

1693)     def test_113_prompt_for_passphrase(
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1694)         self, monkeypatch: pytest.MonkeyPatch
1695)     ) -> None:
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1696)         monkeypatch.setattr(
1697)             click,
1698)             'prompt',
1699)             lambda *a, **kw: json.dumps({'args': a, 'kwargs': kw}),
1700)         )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1701)         res = json.loads(cli._prompt_for_passphrase())
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

1702)         err_msg = 'missing arguments to passphrase prompt'
1703)         assert 'args' in res, err_msg
1704)         assert 'kwargs' in res, err_msg
1705)         assert res['args'][:1] == ['Passphrase'], err_msg
1706)         assert res['kwargs'].get('default') == '', err_msg
1707)         assert not res['kwargs'].get('show_default', True), err_msg
1708)         assert res['kwargs'].get('err'), err_msg
1709)         assert res['kwargs'].get('hide_input'), err_msg
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1710) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1711)     @pytest.mark.parametrize(
1712)         ['command_line', 'config', 'result_config'],
1713)         [
1714)             (
1715)                 ['--delete-globals'],
1716)                 {'global': {'phrase': 'abc'}, 'services': {}},
1717)                 {'services': {}},
1718)             ),
1719)             (
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

1720)                 ['--delete', '--', DUMMY_SERVICE],
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1721)                 {
1722)                     'global': {'phrase': 'abc'},
1723)                     'services': {DUMMY_SERVICE: {'notes': '...'}},
1724)                 },
1725)                 {'global': {'phrase': 'abc'}, 'services': {}},
1726)             ),
1727)             (
1728)                 ['--clear'],
1729)                 {
1730)                     'global': {'phrase': 'abc'},
1731)                     'services': {DUMMY_SERVICE: {'notes': '...'}},
1732)                 },
1733)                 {'services': {}},
1734)             ),
1735)         ],
1736)     )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1737)     def test_203_repeated_config_deletion(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1738)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1739)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1740)         command_line: list[str],
Marco Ricci Consolidate `types` submodu...

Marco Ricci authored 4 months ago

1741)         config: _types.VaultConfig,
1742)         result_config: _types.VaultConfig,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1743)     ) -> None:
1744)         runner = click.testing.CliRunner(mix_stderr=False)
1745)         for start_config in [config, result_config]:
Marco Ricci Modularize test helpers to...

Marco Ricci authored 3 months ago

1746)             with tests.isolated_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1747)                 monkeypatch=monkeypatch, runner=runner, config=start_config
1748)             ):
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

1750)                     cli.derivepassphrase_vault,
1751)                     command_line,
1752)                     catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1753)                 )
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1754)                 result = tests.ReadableResult.parse(_result)
1755)                 assert result.clean_exit(
1756)                     empty_stderr=True
1757)                 ), 'expected clean exit'
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

1758)                 with open(
1759)                     cli._config_filename(subsystem='vault'), encoding='UTF-8'
1760)                 ) as infile:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1761)                     config_readback = json.load(infile)
1762)                 assert config_readback == result_config
1763) 
1764)     def test_204_phrase_from_key_manually(self) -> None:
1765)         assert (
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

1766)             vault.Vault(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1767)                 phrase=DUMMY_PHRASE_FROM_KEY1, **DUMMY_CONFIG_SETTINGS
1768)             ).generate(DUMMY_SERVICE)
1769)             == DUMMY_RESULT_KEY1
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1770)         )
1771) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1772)     @pytest.mark.parametrize(
1773)         ['vfunc', 'input'],
1774)         [
1775)             (cli._validate_occurrence_constraint, 20),
1776)             (cli._validate_length, 20),
1777)         ],
1778)     )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1779)     def test_210a_validate_constraints_manually(
1780)         self,
1781)         vfunc: Callable[[click.Context, click.Parameter, Any], int | None],
1782)         input: int,
1783)     ) -> None:
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

1784)         ctx = cli.derivepassphrase_vault.make_context(cli.PROG_NAME, [])
1785)         param = cli.derivepassphrase_vault.params[0]
Marco Ricci Rename and regroup all test...

Marco Ricci authored 5 months ago

1786)         assert vfunc(ctx, param, input) == input
1787) 
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 5 months ago

1788)     @pytest.mark.parametrize('conn_hint', ['none', 'socket', 'client'])
Marco Ricci Fix miscellaneous type chec...

Marco Ricci authored 5 months ago

1789)     def test_227_get_suitable_ssh_keys(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1790)         self,
Marco Ricci Clean up testing machinery...

Marco Ricci authored 3 months ago

1791)         monkeypatch: pytest.MonkeyPatch,
Marco Ricci Add test fixture for manual...

Marco Ricci authored 3 months ago

1792)         running_ssh_agent: str,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 5 months ago

1793)         conn_hint: str,
Marco Ricci Fix miscellaneous type chec...

Marco Ricci authored 5 months ago

1794)     ) -> None:
Marco Ricci Add test fixture for manual...

Marco Ricci authored 3 months ago

1795)         with monkeypatch.context():
1796)             monkeypatch.setenv('SSH_AUTH_SOCK', running_ssh_agent)
1797)             monkeypatch.setattr(
1798)                 ssh_agent.SSHAgentClient, 'list_keys', tests.list_keys
1799)             )
1800)             hint: ssh_agent.SSHAgentClient | socket.socket | None
Marco Ricci Add support for Python 3.9

Marco Ricci authored 2 months ago

1801)             # Use match/case here once Python 3.9 becomes unsupported.
1802)             if conn_hint == 'client':
1803)                 hint = ssh_agent.SSHAgentClient()
1804)             elif conn_hint == 'socket':
1805)                 hint = socket.socket(family=socket.AF_UNIX)
1806)                 hint.connect(running_ssh_agent)
1807)             else:
1808)                 assert conn_hint == 'none'
1809)                 hint = None
Marco Ricci Add test fixture for manual...

Marco Ricci authored 3 months ago

1810)             exception: Exception | None = None
1811)             try:
1812)                 list(cli._get_suitable_ssh_keys(hint))
1813)             except RuntimeError:  # pragma: no cover
1814)                 pass
1815)             except Exception as e:  # noqa: BLE001 # pragma: no cover
1816)                 exception = e
1817)             finally:
1818)                 assert (
1819)                     exception is None
1820)                 ), 'exception querying suitable SSH keys'
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

1821) 
1822) 
1823) class TestCLITransition:
1824)     def test_100_help_output(self, monkeypatch: pytest.MonkeyPatch) -> None:
1825)         runner = click.testing.CliRunner(mix_stderr=False)
1826)         with tests.isolated_config(
1827)             monkeypatch=monkeypatch,
1828)             runner=runner,
1829)         ):
1830)             _result = runner.invoke(
1831)                 cli.derivepassphrase, ['--help'], catch_exceptions=False
1832)             )
1833)             result = tests.ReadableResult.parse(_result)
1834)         assert result.clean_exit(
1835)             empty_stderr=True, output='currently implemented subcommands'
1836)         ), 'expected clean exit, and known help text'
1837) 
1838)     def test_101_help_output_export(
1839)         self, monkeypatch: pytest.MonkeyPatch
1840)     ) -> None:
1841)         runner = click.testing.CliRunner(mix_stderr=False)
1842)         with tests.isolated_config(
1843)             monkeypatch=monkeypatch,
1844)             runner=runner,
1845)         ):
1846)             _result = runner.invoke(
1847)                 cli.derivepassphrase,
1848)                 ['export', '--help'],
1849)                 catch_exceptions=False,
1850)             )
1851)             result = tests.ReadableResult.parse(_result)
1852)         assert result.clean_exit(
1853)             empty_stderr=True, output='only available subcommand'
1854)         ), 'expected clean exit, and known help text'
1855) 
1856)     def test_102_help_output_export_vault(
1857)         self, monkeypatch: pytest.MonkeyPatch
1858)     ) -> None:
1859)         runner = click.testing.CliRunner(mix_stderr=False)
1860)         with tests.isolated_config(
1861)             monkeypatch=monkeypatch,
1862)             runner=runner,
1863)         ):
1864)             _result = runner.invoke(
1865)                 cli.derivepassphrase,
1866)                 ['export', 'vault', '--help'],
1867)                 catch_exceptions=False,
1868)             )
1869)             result = tests.ReadableResult.parse(_result)
1870)         assert result.clean_exit(
1871)             empty_stderr=True, output='Read the vault-native configuration'
1872)         ), 'expected clean exit, and known help text'
1873) 
1874)     def test_103_help_output_vault(
1875)         self, monkeypatch: pytest.MonkeyPatch
1876)     ) -> None:
1877)         runner = click.testing.CliRunner(mix_stderr=False)
1878)         with tests.isolated_config(
1879)             monkeypatch=monkeypatch,
1880)             runner=runner,
1881)         ):
1882)             _result = runner.invoke(
1883)                 cli.derivepassphrase,
1884)                 ['vault', '--help'],
1885)                 catch_exceptions=False,
1886)             )
1887)             result = tests.ReadableResult.parse(_result)
1888)         assert result.clean_exit(
1889)             empty_stderr=True, output='Password generation:\n'
1890)         ), 'expected clean exit, and option groups in help text'
1891)         assert result.clean_exit(
1892)             empty_stderr=True, output='Use NUMBER=0, e.g. "--symbol 0"'
1893)         ), 'expected clean exit, and option group epilog in help text'
1894) 
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

1895)     @pytest.mark.parametrize(
1896)         'config',
1897)         [
1898)             {'global': {'phrase': 'my passphrase'}, 'services': {}},
1899)             {'global': {'key': DUMMY_KEY1_B64}, 'services': {}},
1900)             {
1901)                 'global': {'phrase': 'abc'},
1902)                 'services': {'sv': {'phrase': 'my passphrase'}},
1903)             },
1904)             {
1905)                 'global': {'phrase': 'abc'},
1906)                 'services': {'sv': {'key': DUMMY_KEY1_B64}},
1907)             },
1908)             {
1909)                 'global': {'phrase': 'abc'},
1910)                 'services': {'sv': {'key': DUMMY_KEY1_B64, 'length': 15}},
1911)             },
1912)         ],
1913)     )
1914)     def test_110_load_config_backup(
1915)         self, monkeypatch: pytest.MonkeyPatch, config: Any
1916)     ) -> None:
1917)         runner = click.testing.CliRunner()
1918)         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
1919)             config_filename = cli._config_filename()
1920)             with open(config_filename, 'w', encoding='UTF-8') as fileobj:
1921)                 print(json.dumps(config, indent=2), file=fileobj)
1922)             assert cli._migrate_and_load_old_config()[0] == config
1923) 
1924)     @pytest.mark.parametrize(
1925)         'config',
1926)         [
1927)             {'global': {'phrase': 'my passphrase'}, 'services': {}},
1928)             {'global': {'key': DUMMY_KEY1_B64}, 'services': {}},
1929)             {
1930)                 'global': {'phrase': 'abc'},
1931)                 'services': {'sv': {'phrase': 'my passphrase'}},
1932)             },
1933)             {
1934)                 'global': {'phrase': 'abc'},
1935)                 'services': {'sv': {'key': DUMMY_KEY1_B64}},
1936)             },
1937)             {
1938)                 'global': {'phrase': 'abc'},
1939)                 'services': {'sv': {'key': DUMMY_KEY1_B64, 'length': 15}},
1940)             },
1941)         ],
1942)     )
1943)     def test_111_migrate_config(
1944)         self, monkeypatch: pytest.MonkeyPatch, config: Any
1945)     ) -> None:
1946)         runner = click.testing.CliRunner()
1947)         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
1948)             config_filename = cli._config_filename()
1949)             with open(config_filename, 'w', encoding='UTF-8') as fileobj:
1950)                 print(json.dumps(config, indent=2), file=fileobj)
1951)             assert cli._migrate_and_load_old_config() == (config, None)
1952) 
1953)     @pytest.mark.parametrize(
1954)         'config',
1955)         [
1956)             {'global': {'phrase': 'my passphrase'}, 'services': {}},
1957)             {'global': {'key': DUMMY_KEY1_B64}, 'services': {}},
1958)             {
1959)                 'global': {'phrase': 'abc'},
1960)                 'services': {'sv': {'phrase': 'my passphrase'}},
1961)             },
1962)             {
1963)                 'global': {'phrase': 'abc'},
1964)                 'services': {'sv': {'key': DUMMY_KEY1_B64}},
1965)             },
1966)             {
1967)                 'global': {'phrase': 'abc'},
1968)                 'services': {'sv': {'key': DUMMY_KEY1_B64, 'length': 15}},
1969)             },
1970)         ],
1971)     )
1972)     def test_112_migrate_config_error(
1973)         self, monkeypatch: pytest.MonkeyPatch, config: Any
1974)     ) -> None:
1975)         runner = click.testing.CliRunner()
1976)         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
1977)             config_filename = cli._config_filename()
1978)             with open(config_filename, 'w', encoding='UTF-8') as fileobj:
1979)                 print(json.dumps(config, indent=2), file=fileobj)
1980)             os.mkdir(cli._config_filename(subsystem='vault'))
1981)             config2, err = cli._migrate_and_load_old_config()
1982)             assert config2 == config
1983)             assert isinstance(err, OSError)
1984)             assert err.errno == errno.EISDIR
1985) 
1986)     @pytest.mark.parametrize(
1987)         'config',
1988)         [
1989)             {'global': '', 'services': {}},
1990)             {'global': 0, 'services': {}},
1991)             {
1992)                 'global': {'phrase': 'abc'},
1993)                 'services': False,
1994)             },
1995)             {
1996)                 'global': {'phrase': 'abc'},
1997)                 'services': True,
1998)             },
1999)             {
2000)                 'global': {'phrase': 'abc'},
2001)                 'services': None,
2002)             },
2003)         ],
2004)     )
2005)     def test_113_migrate_config_error_bad_config_value(
2006)         self, monkeypatch: pytest.MonkeyPatch, config: Any
2007)     ) -> None:
2008)         runner = click.testing.CliRunner()
2009)         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
2010)             config_filename = cli._config_filename()
2011)             with open(config_filename, 'w', encoding='UTF-8') as fileobj:
2012)                 print(json.dumps(config, indent=2), file=fileobj)
2013)             with pytest.raises(ValueError, match=cli._INVALID_VAULT_CONFIG):
2014)                 cli._migrate_and_load_old_config()
2015) 
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

2016)     def test_200_forward_export_vault_path_parameter(
2017)         self, monkeypatch: pytest.MonkeyPatch
2018)     ) -> None:
2019)         pytest.importorskip('cryptography', minversion='38.0')
2020)         runner = click.testing.CliRunner(mix_stderr=False)
2021)         with tests.isolated_vault_exporter_config(
2022)             monkeypatch=monkeypatch,
2023)             runner=runner,
2024)             vault_config=tests.VAULT_V03_CONFIG,
2025)             vault_key=tests.VAULT_MASTER_KEY,
2026)         ):
2027)             monkeypatch.setenv('VAULT_KEY', tests.VAULT_MASTER_KEY)
2028)             _result = runner.invoke(
2029)                 cli.derivepassphrase,
2030)                 ['export', 'VAULT_PATH'],
2031)             )
2032)         result = tests.ReadableResult.parse(_result)
2033)         assert result.clean_exit(empty_stderr=False), 'expected clean exit'
Marco Ricci Fix minor typo, formatting...

Marco Ricci authored 3 months ago

2034)         assert (
2035)             result.stderr
2036)             == f"""\
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

2037) {cli.PROG_NAME}: Deprecation warning: A subcommand will be required in v1.0. See --help for available subcommands.
2038) {cli.PROG_NAME}: Warning: Defaulting to subcommand "vault".
2039) """  # noqa: E501
Marco Ricci Fix minor typo, formatting...

Marco Ricci authored 3 months ago

2040)         )
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

2041)         assert json.loads(result.output) == tests.VAULT_V03_CONFIG_DATA
2042) 
2043)     @pytest.mark.parametrize(
2044)         'charset_name', ['lower', 'upper', 'number', 'space', 'dash', 'symbol']
2045)     )
2046)     def test_210_forward_vault_disable_character_set(
2047)         self, monkeypatch: pytest.MonkeyPatch, charset_name: str
2048)     ) -> None:
2049)         monkeypatch.setattr(cli, '_prompt_for_passphrase', tests.auto_prompt)
2050)         option = f'--{charset_name}'
Marco Ricci Align behavior with vault c...

Marco Ricci authored 2 months ago

2051)         charset = vault.Vault._CHARSETS[charset_name].decode('ascii')
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

2052)         runner = click.testing.CliRunner(mix_stderr=False)
2053)         with tests.isolated_config(
2054)             monkeypatch=monkeypatch,
2055)             runner=runner,
2056)         ):
2057)             _result = runner.invoke(
2058)                 cli.derivepassphrase,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

2059)                 [option, '0', '-p', '--', DUMMY_SERVICE],
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

2060)                 input=DUMMY_PASSPHRASE,
2061)                 catch_exceptions=False,
2062)             )
2063)             result = tests.ReadableResult.parse(_result)
2064)         assert result.clean_exit(empty_stderr=False), 'expected clean exit'
Marco Ricci Fix minor typo, formatting...

Marco Ricci authored 3 months ago

2065)         assert (
2066)             result.stderr
2067)             == f"""\
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

2068) {cli.PROG_NAME}: Deprecation warning: A subcommand will be required in v1.0. See --help for available subcommands.
2069) {cli.PROG_NAME}: Warning: Defaulting to subcommand "vault".
2070) """  # noqa: E501
Marco Ricci Fix minor typo, formatting...

Marco Ricci authored 3 months ago

2071)         )
Marco Ricci Reintegrate all functionali...

Marco Ricci authored 3 months ago

2072)         for c in charset:
2073)             assert (
2074)                 c not in result.output
2075)             ), f'derived password contains forbidden character {c!r}'
Marco Ricci Rename the configuration fi...

Marco Ricci authored 3 months ago

2076) 
2077)     def test_300_export_using_old_config_file(
2078)         self,
2079)         monkeypatch: pytest.MonkeyPatch,
2080)     ) -> None:
2081)         runner = click.testing.CliRunner(mix_stderr=False)
2082)         with tests.isolated_config(
2083)             monkeypatch=monkeypatch,
2084)             runner=runner,
2085)         ):
2086)             with open(
2087)                 cli._config_filename(), 'w', encoding='UTF-8'
2088)             ) as fileobj:
2089)                 print(
2090)                     json.dumps(
2091)                         {'services': {DUMMY_SERVICE: DUMMY_CONFIG_SETTINGS}},
2092)                         indent=2,
2093)                     ),
2094)                     file=fileobj,
2095)                 )
2096)             _result = runner.invoke(
2097)                 cli.derivepassphrase_vault,
2098)                 ['--export', '-'],
2099)                 catch_exceptions=False,
2100)             )
2101)         result = tests.ReadableResult.parse(_result)
2102)         assert result.clean_exit(), 'expected clean exit'
2103)         assert (
2104)             'v0.1-style config file' in result.stderr
2105)         ), 'expected known warning message in stderr'
2106)         assert (
2107)             'Successfully migrated to ' in result.stderr
2108)         ), 'expected known warning message in stderr'
2109) 
2110)     def test_300a_export_using_old_config_file_migration_error(
2111)         self,
2112)         monkeypatch: pytest.MonkeyPatch,
2113)     ) -> None:
2114)         runner = click.testing.CliRunner(mix_stderr=False)
2115)         with tests.isolated_config(
2116)             monkeypatch=monkeypatch,
2117)             runner=runner,
2118)         ):
2119)             with open(
2120)                 cli._config_filename(), 'w', encoding='UTF-8'
2121)             ) as fileobj:
2122)                 print(
2123)                     json.dumps(
2124)                         {'services': {DUMMY_SERVICE: DUMMY_CONFIG_SETTINGS}},
2125)                         indent=2,
2126)                     ),
2127)                     file=fileobj,
2128)                 )
2129) 
2130)             def raiser(*_args: Any, **_kwargs: Any) -> None:
2131)                 raise OSError(
2132)                     errno.EACCES,
2133)                     os.strerror(errno.EACCES),
2134)                     cli._config_filename(subsystem='vault'),
2135)                 )
2136) 
2137)             monkeypatch.setattr(os, 'replace', raiser)
2138)             _result = runner.invoke(
2139)                 cli.derivepassphrase_vault,
2140)                 ['--export', '-'],
2141)                 catch_exceptions=False,
2142)             )
2143)         result = tests.ReadableResult.parse(_result)
2144)         assert result.clean_exit(), 'expected clean exit'
2145)         assert (
2146)             'v0.1-style config file' in result.stderr
2147)         ), 'expected known warning message in stderr'
2148)         assert (
2149)             'Warning: Failed to migrate to ' in result.stderr
2150)         ), 'expected known warning message in stderr'
Marco Ricci Add a stateful hypothesis t...

Marco Ricci authored 2 months ago

2151) 
2152) 
2153) class ConfigMergingStateMachine(stateful.RuleBasedStateMachine):
2154)     def __init__(self) -> None:
2155)         super().__init__()
2156)         self.runner = click.testing.CliRunner(mix_stderr=False)
2157)         self.exit_stack = contextlib.ExitStack().__enter__()
2158)         self.monkeypatch = self.exit_stack.enter_context(
2159)             pytest.MonkeyPatch().context()
2160)         )
2161)         self.isolated_config = self.exit_stack.enter_context(
2162)             tests.isolated_vault_config(
2163)                 monkeypatch=self.monkeypatch,
2164)                 runner=self.runner,
2165)                 config={'services': {}},
2166)             )
2167)         )
2168)         self.current_config = cli._load_config()
2169) 
2170)     known_services = stateful.Bundle('known_services')
2171)     settings = stateful.Bundle('settings')
2172)     configurations = stateful.Bundle('configurations')
2173) 
2174)     @stateful.initialize(target=configurations)
2175)     def init_empty_configuration(self) -> _types.VaultConfig:
2176)         return copy.deepcopy(self.current_config)
2177) 
2178)     @stateful.initialize(target=configurations)
2179)     def init_standard_testing_configuration(self) -> _types.VaultConfig:
2180)         return cast(
2181)             _types.VaultConfig,
2182)             {
2183)                 'services': {
2184)                     DUMMY_SERVICE: copy.deepcopy(DUMMY_CONFIG_SETTINGS)
2185)                 }
2186)             },
2187)         )
2188) 
2189)     @stateful.initialize(
2190)         target=known_services,
2191)         service_names=strategies.lists(
2192)             strategies.text(
2193)                 strategies.characters(min_codepoint=32, max_codepoint=126),
2194)                 min_size=1,
2195)                 max_size=50,
2196)             ),
2197)             min_size=10,
2198)             max_size=10,
2199)             unique=True,
2200)         ),
2201)     )
2202)     def init_random_service_names(
2203)         self, service_names: list[str]
2204)     ) -> Iterable[str]:
2205)         return stateful.multiple(*service_names)
2206) 
2207)     # Don't include key or phrase settings here.  While easy to
2208)     # implement when manipulating the stored config directly, the
2209)     # command-line interface for changing the passphrase and key values
2210)     # is not straight-forward, and key values require a running SSH
2211)     # agent and the key to be loaded.
2212)     @stateful.initialize(
2213)         target=settings,
2214)         settings_list=strategies.lists(
2215)             tests.vault_full_service_config(),
2216)             min_size=10,
2217)             max_size=10,
2218)             unique_by=lambda obj: json.dumps(obj, sort_keys=True),
2219)         ),
2220)     )
2221)     def init_random_settings(
2222)         self, settings_list: list[_types.VaultConfigGlobalSettings]
2223)     ) -> Iterable[_types.VaultConfigGlobalSettings]:
2224)         return stateful.multiple(*settings_list)
2225) 
2226)     @stateful.invariant()
2227)     def check_consistency_of_configs(self) -> None:
2228)         _types.clean_up_falsy_vault_config_values(self.current_config)
2229)         assert self.current_config == cli._load_config()
2230) 
2231)     @stateful.rule(
2232)         target=settings,
2233)         settings_obj=tests.vault_full_service_config(),
2234)     )
2235)     def prepare_settings(
2236)         self, settings_obj: dict[str, int]
2237)     ) -> _types.VaultConfigGlobalSettings:
2238)         return cast(_types.VaultConfigGlobalSettings, settings_obj.copy())
2239) 
2240)     @stateful.rule(
2241)         target=known_services,
2242)         name=strategies.text(
2243)             strategies.characters(min_codepoint=32, max_codepoint=126),
2244)             min_size=1,
2245)             max_size=50,
2246)         ),
2247)     )
2248)     def prepare_service_name(self, name: str) -> str:
2249)         return name
2250) 
2251)     @stateful.rule(
2252)         target=configurations,
2253)         settings_obj=stateful.consumes(settings),
2254)     )
2255)     def prepare_global_config(
2256)         self,
2257)         settings_obj: dict[str, int],
2258)     ) -> _types.VaultConfig:
2259)         return {
2260)             'global': cast(_types.VaultConfigGlobalSettings, settings_obj),
2261)             'services': {},
2262)         }
2263) 
2264)     @stateful.rule(
2265)         target=configurations,
2266)         service=known_services,
2267)         settings_obj=stateful.consumes(settings),
2268)     )
2269)     def prepare_service_config(
2270)         self,
2271)         service: str,
2272)         settings_obj: dict[str, int],
2273)     ) -> _types.VaultConfig:
2274)         return {
2275)             'services': {
2276)                 service: cast(
2277)                     _types.VaultConfigServicesSettings, settings_obj
2278)                 ),
2279)             },
2280)         }
2281) 
2282)     @staticmethod
2283)     def fold_configs(
2284)         c1: _types.VaultConfig, c2: _types.VaultConfig
2285)     ) -> _types.VaultConfig:
2286)         new_global_dict = c1.get('global', c2.get('global'))
2287)         if new_global_dict is not None:
2288)             return {
2289)                 'global': new_global_dict,
2290)                 'services': {**c2['services'], **c1['services']},
2291)             }
2292)         return {
2293)             'services': {**c2['services'], **c1['services']},
2294)         }
2295) 
2296)     @stateful.rule(
2297)         target=configurations,
2298)         config_base=stateful.consumes(configurations),
2299)         config_folded=stateful.consumes(configurations),
2300)     )
2301)     def fold_configuration_into(
2302)         self,
2303)         config_base: _types.VaultConfig,
2304)         config_folded: _types.VaultConfig,
2305)     ) -> _types.VaultConfig:
2306)         return self.fold_configs(config_folded, config_base)
2307) 
2308)     @stateful.rule(
2309)         settings_obj=stateful.consumes(settings),
2310)     )
2311)     def set_globals(
2312)         self,
2313)         settings_obj: _types.VaultConfigGlobalSettings,
2314)     ) -> None:
2315)         self.current_config['global'] = settings_obj
2316)         assert _types.is_vault_config(self.current_config)
2317)         # NOTE: This relies on settings_obj containing only the keys
2318)         # "length", "repeat", "upper", "lower", "number", "space",
2319)         # "dash" and "symbol".
2320)         _result = self.runner.invoke(
2321)             cli.derivepassphrase_vault,
2322)             ['--config']
2323)             + [f'--{key}={value}' for key, value in settings_obj.items()],
2324)             catch_exceptions=False,
2325)         )
2326)         result = tests.ReadableResult.parse(_result)
2327)         assert result.clean_exit(empty_stderr=False)
2328) 
2329)     # No check for whether the service settings currently exist.  This
2330)     # may therefore actually "create" the settings, not merely "modify"
2331)     # them.
2332)     #
2333)     # (There is no check because this appears to be hard or impossible
2334)     # to express as a hypothesis strategy: it would depend on the
2335)     # current state of the state machine instance.  This could be
2336)     # circumvented with `hypothesis.assume`, but that may likely trigger
2337)     # health check errors.)
2338)     @stateful.rule(
2339)         service=known_services,
2340)         settings_obj=stateful.consumes(settings),
2341)     )
2342)     def set_service(
2343)         self,
2344)         service: str,
2345)         settings_obj: _types.VaultConfigServicesSettings,
2346)     ) -> None:
2347)         self.current_config['services'][service] = settings_obj
2348)         assert _types.is_vault_config(self.current_config)
2349)         # NOTE: This relies on settings_obj containing only the keys
2350)         # "length", "repeat", "upper", "lower", "number", "space",
2351)         # "dash" and "symbol".
2352)         _result = self.runner.invoke(
2353)             cli.derivepassphrase_vault,
2354)             ['--config']
2355)             + [f'--{key}={value}' for key, value in settings_obj.items()]
2356)             + ['--', service],
2357)             catch_exceptions=False,
2358)         )
2359)         result = tests.ReadableResult.parse(_result)
2360)         assert result.clean_exit(empty_stderr=False)
2361) 
2362)     @stateful.precondition(lambda self: 'global' in self.current_config)
2363)     @stateful.rule()
2364)     def purge_global(self) -> None:
2365)         self.current_config.pop('global')
2366)         _result = self.runner.invoke(
2367)             cli.derivepassphrase_vault,
2368)             ['--delete-globals'],
2369)             input='y',
2370)             catch_exceptions=False,
2371)         )
2372)         result = tests.ReadableResult.parse(_result)
2373)         assert result.clean_exit(empty_stderr=False)
2374) 
2375)     # No check for whether the service settings currently exist.  This
2376)     # may therefore actually be almost a no-op, purging settings that
2377)     # aren't set in the first place.
2378)     #
2379)     # (There is no check because this appears to be hard or impossible
2380)     # to express as a hypothesis strategy: it would depend on the
2381)     # current state of the state machine instance.  This could be
2382)     # circumvented with `hypothesis.assume`, but that may likely trigger
2383)     # health check errors.)
2384)     @stateful.precondition(lambda self: bool(self.current_config['services']))
2385)     @stateful.rule(service=stateful.consumes(known_services))
2386)     def purge_service(self, service: str) -> None:
2387)         ret = self.current_config['services'].pop(service, None)
2388)         if ret is not None:
2389)             _result = self.runner.invoke(
2390)                 cli.derivepassphrase_vault,
Marco Ricci Fix missing escaping in tes...

Marco Ricci authored 1 month ago

2391)                 ['--delete', '--', service],