204af5e376b662d18fb20f00351e30386c309c30
Marco Ricci Add prototype command-line...

Marco Ricci authored 4 months ago

1) # SPDX-FileCopyrightText: 2024 Marco Ricci <m@the13thletter.info>
2) #
3) # SPDX-License-Identifier: MIT
4) 
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

7) import contextlib
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

8) import json
9) import os
Marco Ricci Create the configuration di...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

11) import socket
Marco Ricci Fix typing issues in mypy s...

Marco Ricci authored 3 months ago

12) from typing import TYPE_CHECKING
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

14) import click.testing
15) import pytest
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

16) from typing_extensions import NamedTuple
17) 
18) import derivepassphrase as dpp
19) import ssh_agent_client
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

20) import tests
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

21) from derivepassphrase import cli
22) 
23) if TYPE_CHECKING:
24)     from collections.abc import Callable
25) 
26)     from typing_extensions import Any
Marco Ricci Add prototype command-line...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 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 Add finished command-line i...

Marco Ricci authored 4 months ago

39) 
40) 
41) class IncompatibleConfiguration(NamedTuple):
42)     other_options: list[tuple[str, ...]]
43)     needs_service: bool | None
44)     input: bytes | None
45) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

47) class SingleConfiguration(NamedTuple):
48)     needs_service: bool | None
49)     input: bytes | None
50)     check_success: bool
51) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

53) class OptionCombination(NamedTuple):
54)     options: list[str]
55)     incompatible: bool
56)     needs_service: bool | None
57)     input: bytes | None
58)     check_success: bool
59) 
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

62)     ('--phrase',),
63)     ('--key',),
64)     ('--length', '20'),
65)     ('--repeat', '20'),
66)     ('--lower', '1'),
67)     ('--upper', '1'),
68)     ('--number', '1'),
69)     ('--space', '1'),
70)     ('--dash', '1'),
71)     ('--symbol', '1'),
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

74)     ('--notes',),
75)     ('--config',),
76)     ('--delete',),
77)     ('--delete-globals',),
78)     ('--clear',),
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

85) ]
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

90)         True,
91)         DUMMY_PASSPHRASE,
92)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

121)         [
122)             ('--config',),
123)             ('--delete',),
124)             ('--delete-globals',),
125)             ('--clear',),
126)             *STORAGE_OPTIONS,
127)         ],
128)         True,
129)         None,
130)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

132)         [('--delete',), ('--delete-globals',), ('--clear',), *STORAGE_OPTIONS],
133)         None,
134)         DUMMY_PASSPHRASE,
135)     ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

144)         [('--import', '-')], False, None
145)     ),
146)     ('--import', '-'): IncompatibleConfiguration([], False, None),
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

147) }
148) SINGLES: dict[tuple[str, ...], SingleConfiguration] = {
149)     ('--phrase',): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
150)     ('--key',): SingleConfiguration(True, None, False),
151)     ('--length', '20'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
152)     ('--repeat', '20'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
153)     ('--lower', '1'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
154)     ('--upper', '1'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
155)     ('--number', '1'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
156)     ('--space', '1'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
157)     ('--dash', '1'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
158)     ('--symbol', '1'): SingleConfiguration(True, DUMMY_PASSPHRASE, True),
159)     ('--notes',): SingleConfiguration(True, None, False),
160)     ('--config', '-p'): SingleConfiguration(None, DUMMY_PASSPHRASE, False),
161)     ('--delete',): SingleConfiguration(True, None, False),
162)     ('--delete-globals',): SingleConfiguration(False, None, True),
163)     ('--clear',): SingleConfiguration(False, None, True),
164)     ('--export', '-'): SingleConfiguration(False, None, True),
165)     ('--import', '-'): SingleConfiguration(False, b'{"services": {}}', True),
166) }
167) INTERESTING_OPTION_COMBINATIONS: list[OptionCombination] = []
Marco Ricci Fix miscellaneous type chec...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

169) for opt, config in INCOMPATIBLE.items():
170)     for opt2 in config.other_options:
171)         INTERESTING_OPTION_COMBINATIONS.extend([
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

172)             OptionCombination(
173)                 options=list(opt + opt2),
174)                 incompatible=True,
175)                 needs_service=config.needs_service,
176)                 input=config.input,
177)                 check_success=False,
178)             ),
179)             OptionCombination(
180)                 options=list(opt2 + opt),
181)                 incompatible=True,
182)                 needs_service=config.needs_service,
183)                 input=config.input,
184)                 check_success=False,
185)             ),
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

186)         ])
187) for opt, config in SINGLES.items():
188)     INTERESTING_OPTION_COMBINATIONS.append(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

189)         OptionCombination(
190)             options=list(opt),
191)             incompatible=False,
192)             needs_service=config.needs_service,
193)             input=config.input,
194)             check_success=config.check_success,
195)         )
196)     )
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

197) 
198) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

199) class TestCLI:
Marco Ricci Isolate tests properly and...

Marco Ricci authored 3 months ago

200)     def test_200_help_output(self, monkeypatch: Any) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

202)         with tests.isolated_config(
203)             monkeypatch=monkeypatch,
204)             runner=runner,
205)             config={'services': {}},
206)         ):
207)             result = runner.invoke(
208)                 cli.derivepassphrase, ['--help'], catch_exceptions=False
209)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

210)         assert result.exit_code == 0
211)         assert (
212)             'Password generation:\n' in result.output
213)         ), 'Option groups not respected in help text.'
214)         assert (
215)             'Use NUMBER=0, e.g. "--symbol 0"' in result.output
216)         ), 'Option group epilog not printed.'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

217) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

221)     def test_201_disable_character_set(
222)         self, monkeypatch: Any, charset_name: str
223)     ) -> None:
224)         monkeypatch.setattr(cli, '_prompt_for_passphrase', tests.auto_prompt)
225)         option = f'--{charset_name}'
226)         charset = dpp.Vault._CHARSETS[charset_name].decode('ascii')
227)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Isolate tests properly and...

Marco Ricci authored 3 months ago

228)         with tests.isolated_config(
229)             monkeypatch=monkeypatch,
230)             runner=runner,
231)             config={'services': {}},
232)         ):
233)             result = runner.invoke(
234)                 cli.derivepassphrase,
235)                 [option, '0', '-p', DUMMY_SERVICE],
236)                 input=DUMMY_PASSPHRASE,
237)                 catch_exceptions=False,
238)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

239)         assert (
240)             result.exit_code == 0
241)         ), f'program died unexpectedly with exit code {result.exit_code}'
242)         assert (
243)             not result.stderr_bytes
244)         ), f'program barfed on stderr: {result.stderr_bytes!r}'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

245)         for c in charset:
246)             assert c not in result.stdout, (
247)                 f'derived password contains forbidden character {c!r}: '
248)                 f'{result.stdout!r}'
249)             )
Marco Ricci Add prototype command-line...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

251)     def test_202_disable_repetition(self, monkeypatch: Any) -> None:
252)         monkeypatch.setattr(cli, '_prompt_for_passphrase', tests.auto_prompt)
253)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Isolate tests properly and...

Marco Ricci authored 3 months ago

254)         with tests.isolated_config(
255)             monkeypatch=monkeypatch,
256)             runner=runner,
257)             config={'services': {}},
258)         ):
259)             result = runner.invoke(
260)                 cli.derivepassphrase,
261)                 ['--repeat', '0', '-p', DUMMY_SERVICE],
262)                 input=DUMMY_PASSPHRASE,
263)                 catch_exceptions=False,
264)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

265)         assert (
266)             result.exit_code == 0
267)         ), f'program died unexpectedly with exit code {result.exit_code}'
268)         assert (
269)             not result.stderr_bytes
270)         ), f'program barfed on stderr: {result.stderr_bytes!r}'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

271)         passphrase = result.stdout.rstrip('\r\n')
272)         for i in range(len(passphrase) - 1):
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

274)                 f'derived password contains repeated character '
275)                 f'at position {i}: {result.stdout!r}'
276)             )
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

277) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

278)     @pytest.mark.parametrize(
279)         'config',
280)         [
281)             pytest.param(
282)                 {
283)                     'global': {'key': DUMMY_KEY1_B64},
284)                     'services': {DUMMY_SERVICE: DUMMY_CONFIG_SETTINGS},
285)                 },
286)                 id='global',
287)             ),
288)             pytest.param(
289)                 {
290)                     'global': {
291)                         'phrase': DUMMY_PASSPHRASE.rstrip(b'\n').decode(
292)                             'ASCII'
293)                         )
294)                     },
295)                     'services': {
296)                         DUMMY_SERVICE: {
297)                             'key': DUMMY_KEY1_B64,
298)                             **DUMMY_CONFIG_SETTINGS,
299)                         }
300)                     },
301)                 },
302)                 id='service',
303)             ),
304)         ],
305)     )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

306)     def test_204a_key_from_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

307)         self,
308)         monkeypatch: Any,
309)         config: dpp.types.VaultConfig,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

310)     ) -> None:
311)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

312)         with tests.isolated_config(
313)             monkeypatch=monkeypatch, runner=runner, config=config
314)         ):
315)             monkeypatch.setattr(
316)                 dpp.Vault, 'phrase_from_key', tests.phrase_from_key
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

317)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

318)             result = runner.invoke(
319)                 cli.derivepassphrase, [DUMMY_SERVICE], catch_exceptions=False
320)             )
321)             assert (result.exit_code, result.stderr_bytes) == (
322)                 0,
323)                 b'',
324)             ), 'program exited with failure'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

325)             assert (
326)                 result.stdout_bytes.rstrip(b'\n') != DUMMY_RESULT_PASSPHRASE
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

327)             ), 'program generated unexpected result (phrase instead of key)'
328)             assert (
329)                 result.stdout_bytes.rstrip(b'\n') == DUMMY_RESULT_KEY1
330)             ), 'program generated unexpected result (wrong settings?)'
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

332)     def test_204b_key_from_command_line(self, monkeypatch: Any) -> None:
333)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

334)         with tests.isolated_config(
335)             monkeypatch=monkeypatch,
336)             runner=runner,
337)             config={'services': {DUMMY_SERVICE: DUMMY_CONFIG_SETTINGS}},
338)         ):
339)             monkeypatch.setattr(
340)                 cli, '_get_suitable_ssh_keys', tests.suitable_ssh_keys
341)             )
342)             monkeypatch.setattr(
343)                 dpp.Vault, 'phrase_from_key', tests.phrase_from_key
344)             )
345)             result = runner.invoke(
346)                 cli.derivepassphrase,
347)                 ['-k', DUMMY_SERVICE],
348)                 input=b'1\n',
349)                 catch_exceptions=False,
350)             )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

351)             assert result.exit_code == 0, 'program exited with failure'
352)             assert result.stdout_bytes, 'program output expected'
353)             last_line = result.stdout_bytes.splitlines(True)[-1]
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

354)             assert (
355)                 last_line.rstrip(b'\n') != DUMMY_RESULT_PASSPHRASE
356)             ), 'program generated unexpected result (phrase instead of key)'
357)             assert (
358)                 last_line.rstrip(b'\n') == DUMMY_RESULT_KEY1
359)             ), 'program generated unexpected result (wrong settings?)'
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

361)     def test_205_service_phrase_if_key_in_global_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

362)         self,
363)         monkeypatch: Any,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

364)     ) -> None:
365)         runner = click.testing.CliRunner(mix_stderr=False)
366)         with tests.isolated_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

367)             monkeypatch=monkeypatch,
368)             runner=runner,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

369)             config={
370)                 'global': {'key': DUMMY_KEY1_B64},
371)                 'services': {
372)                     DUMMY_SERVICE: {
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

373)                         'phrase': DUMMY_PASSPHRASE.rstrip(b'\n').decode(
374)                             'ASCII'
375)                         ),
376)                         **DUMMY_CONFIG_SETTINGS,
377)                     }
378)                 },
379)             },
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

380)         ):
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

381)             result = runner.invoke(
382)                 cli.derivepassphrase, [DUMMY_SERVICE], catch_exceptions=False
383)             )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

384)             assert result.exit_code == 0, 'program exited with failure'
385)             assert result.stdout_bytes, 'program output expected'
386)             last_line = result.stdout_bytes.splitlines(True)[-1]
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

387)             assert (
388)                 last_line.rstrip(b'\n') != DUMMY_RESULT_KEY1
389)             ), 'program generated unexpected result (key instead of phrase)'
390)             assert (
391)                 last_line.rstrip(b'\n') == DUMMY_RESULT_PASSPHRASE
392)             ), 'program generated unexpected result (wrong settings?)'
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

393) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

394)     @pytest.mark.parametrize(
395)         'option',
396)         [
397)             '--lower',
398)             '--upper',
399)             '--number',
400)             '--space',
401)             '--dash',
402)             '--symbol',
403)             '--repeat',
404)             '--length',
405)         ],
406)     )
Marco Ricci Isolate tests properly and...

Marco Ricci authored 3 months ago

407)     def test_210_invalid_argument_range(
408)         self, monkeypatch: Any, option: str
409)     ) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

411)         with tests.isolated_config(
412)             monkeypatch=monkeypatch,
413)             runner=runner,
414)             config={'services': {}},
415)         ):
416)             for value in '-42', 'invalid':
417)                 result = runner.invoke(
418)                     cli.derivepassphrase,
419)                     [option, value, '-p', DUMMY_SERVICE],
420)                     input=DUMMY_PASSPHRASE,
421)                     catch_exceptions=False,
422)                 )
423)                 assert result.exit_code > 0, 'program unexpectedly succeeded'
424)                 assert (
425)                     result.stderr_bytes
426)                 ), 'program did not print any error message'
427)                 assert (
428)                     b'Error: Invalid value' in result.stderr_bytes
429)                 ), 'program did not print the expected error message'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

430) 
431)     @pytest.mark.parametrize(
432)         ['options', 'service', 'input', 'check_success'],
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

433)         [
434)             (o.options, o.needs_service, o.input, o.check_success)
435)             for o in INTERESTING_OPTION_COMBINATIONS
436)             if not o.incompatible
437)         ],
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

438)     )
439)     def test_211_service_needed(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

440)         self,
441)         monkeypatch: Any,
442)         options: list[str],
443)         service: bool | None,
444)         input: bytes | None,
445)         check_success: bool,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

446)     ) -> None:
447)         monkeypatch.setattr(cli, '_prompt_for_passphrase', tests.auto_prompt)
448)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

449)         with tests.isolated_config(
450)             monkeypatch=monkeypatch,
451)             runner=runner,
452)             config={'global': {'phrase': 'abc'}, 'services': {}},
453)         ):
454)             result = runner.invoke(
455)                 cli.derivepassphrase,
456)                 options if service else [*options, DUMMY_SERVICE],
457)                 input=input,
458)                 catch_exceptions=False,
459)             )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

461)                 assert result.exit_code > 0, 'program unexpectedly succeeded'
462)                 assert (
463)                     result.stderr_bytes
464)                 ), 'program did not print any error message'
465)                 err_msg = (
466)                     b' requires a SERVICE'
467)                     if service
468)                     else b' does not take a SERVICE argument'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

469)                 )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

470)                 assert (
471)                     err_msg in result.stderr_bytes
472)                 ), 'program did not print the expected error message'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

473)             else:
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

474)                 assert (result.exit_code, result.stderr_bytes) == (
475)                     0,
476)                     b'',
477)                 ), 'program unexpectedly failed'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

478)         if check_success:
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

479)             with tests.isolated_config(
480)                 monkeypatch=monkeypatch,
481)                 runner=runner,
482)                 config={'global': {'phrase': 'abc'}, 'services': {}},
483)             ):
484)                 monkeypatch.setattr(
485)                     cli, '_prompt_for_passphrase', tests.auto_prompt
486)                 )
487)                 result = runner.invoke(
488)                     cli.derivepassphrase,
489)                     [*options, DUMMY_SERVICE] if service else options,
490)                     input=input,
491)                     catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

492)                 )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

493)                 assert (result.exit_code, result.stderr_bytes) == (
494)                     0,
495)                     b'',
496)                 ), 'program unexpectedly failed'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

500)         [
501)             (o.options, o.needs_service)
502)             for o in INTERESTING_OPTION_COMBINATIONS
503)             if o.incompatible
504)         ],
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

506)     def test_212_incompatible_options(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

507)         self,
Marco Ricci Isolate tests properly and...

Marco Ricci authored 3 months ago

508)         monkeypatch: Any,
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

513)         with tests.isolated_config(
514)             monkeypatch=monkeypatch,
515)             runner=runner,
516)             config={'services': {}},
517)         ):
518)             result = runner.invoke(
519)                 cli.derivepassphrase,
520)                 [*options, DUMMY_SERVICE] if service else options,
521)                 input=DUMMY_PASSPHRASE,
522)                 catch_exceptions=False,
523)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

524)         assert result.exit_code > 0, 'program unexpectedly succeeded'
525)         assert result.stderr_bytes, 'program did not print any error message'
526)         assert (
527)             b'mutually exclusive with ' in result.stderr_bytes
528)         ), 'program did not print the expected error message'
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

530)     def test_213_import_bad_config_not_vault_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

531)         self,
532)         monkeypatch: Any,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

533)     ) -> None:
534)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

535)         with tests.isolated_config(
536)             monkeypatch=monkeypatch, runner=runner, config={'services': {}}
537)         ):
538)             result = runner.invoke(
539)                 cli.derivepassphrase,
540)                 ['--import', '-'],
541)                 input=b'null',
542)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

543)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

544)             assert result.exit_code > 0, 'program unexpectedly succeeded'
545)             assert (
546)                 result.stderr_bytes
547)             ), 'program did not print any error message'
548)             assert (
549)                 b'not a valid config' in result.stderr_bytes
550)             ), 'program did not print the expected error message'
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

552)     def test_213a_import_bad_config_not_json_data(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

553)         self,
554)         monkeypatch: Any,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

555)     ) -> None:
556)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

557)         with tests.isolated_config(
558)             monkeypatch=monkeypatch, runner=runner, config={'services': {}}
559)         ):
560)             result = runner.invoke(
561)                 cli.derivepassphrase,
562)                 ['--import', '-'],
563)                 input=b'This string is not valid JSON.',
564)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

565)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

566)             assert result.exit_code > 0, 'program unexpectedly succeeded'
567)             assert (
568)                 result.stderr_bytes
569)             ), 'program did not print any error message'
570)             assert (
571)                 b'cannot decode JSON' in result.stderr_bytes
572)             ), 'program did not print the expected error message'
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

574)     def test_213b_import_bad_config_not_a_file(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

575)         self,
576)         monkeypatch: Any,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

577)     ) -> None:
578)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Fix miscellaneous type chec...

Marco Ricci authored 4 months ago

579)         # `isolated_config` validates the configuration.  So, to pass an
580)         # actual broken configuration, we must open the configuration file
581)         # ourselves afterwards, inside the context.
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

582)         with tests.isolated_config(
583)             monkeypatch=monkeypatch, runner=runner, config={'services': {}}
584)         ):
585)             with open(
586)                 cli._config_filename(), 'w', encoding='UTF-8'
587)             ) as outfile:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

588)                 print('This string is not valid JSON.', file=outfile)
Marco Ricci Fix miscellaneous type chec...

Marco Ricci authored 4 months ago

589)             dname = os.path.dirname(cli._config_filename())
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

590)             result = runner.invoke(
591)                 cli.derivepassphrase,
Marco Ricci Fix miscellaneous type chec...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

594)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

595)             assert result.exit_code > 0, 'program unexpectedly succeeded'
596)             assert (
597)                 result.stderr_bytes
598)             ), 'program did not print any error message'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

599)             # Don't test the actual error message, because it is subject to
600)             # locale settings.  TODO: find a way anyway.
601) 
602)     def test_214_export_settings_no_stored_settings(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

603)         self,
604)         monkeypatch: Any,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

605)     ) -> None:
606)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

607)         with tests.isolated_config(
608)             monkeypatch=monkeypatch, runner=runner, config={'services': {}}
609)         ):
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

610)             with contextlib.suppress(FileNotFoundError):
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

611)                 os.remove(cli._config_filename())
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

612)             result = runner.invoke(
613)                 cli.derivepassphrase, ['--export', '-'], catch_exceptions=False
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

614)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

615)             assert (result.exit_code, result.stderr_bytes) == (
616)                 0,
617)                 b'',
618)             ), 'program exited with failure'
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

620)     def test_214a_export_settings_bad_stored_config(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

621)         self,
622)         monkeypatch: Any,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

623)     ) -> None:
624)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

625)         with tests.isolated_config(
626)             monkeypatch=monkeypatch, runner=runner, config={}
627)         ):
628)             result = runner.invoke(
629)                 cli.derivepassphrase,
630)                 ['--export', '-'],
631)                 input=b'null',
632)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

633)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

634)             assert result.exit_code > 0, 'program unexpectedly succeeded'
635)             assert (
636)                 result.stderr_bytes
637)             ), 'program did not print any error message'
638)             assert (
639)                 b'cannot load config' in result.stderr_bytes
640)             ), 'program did not print the expected error message'
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

642)     def test_214b_export_settings_not_a_file(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

643)         self,
644)         monkeypatch: Any,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

645)     ) -> None:
646)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

647)         with tests.isolated_config(
648)             monkeypatch=monkeypatch, runner=runner, config={'services': {}}
649)         ):
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

650)             with contextlib.suppress(FileNotFoundError):
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

651)                 os.remove(cli._config_filename())
652)             os.makedirs(cli._config_filename())
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

653)             result = runner.invoke(
654)                 cli.derivepassphrase,
655)                 ['--export', '-'],
656)                 input=b'null',
657)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

658)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

659)             assert result.exit_code > 0, 'program unexpectedly succeeded'
660)             assert (
661)                 result.stderr_bytes
662)             ), 'program did not print any error message'
663)             assert (
664)                 b'cannot load config' in result.stderr_bytes
665)             ), 'program did not print the expected error message'
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

667)     def test_214c_export_settings_target_not_a_file(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

668)         self,
669)         monkeypatch: Any,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

670)     ) -> None:
671)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

672)         with tests.isolated_config(
673)             monkeypatch=monkeypatch, runner=runner, config={'services': {}}
674)         ):
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

675)             dname = os.path.dirname(cli._config_filename())
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

676)             result = runner.invoke(
677)                 cli.derivepassphrase,
678)                 ['--export', os.fsdecode(dname)],
679)                 input=b'null',
680)                 catch_exceptions=False,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

681)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

682)             assert result.exit_code > 0, 'program unexpectedly succeeded'
683)             assert (
684)                 result.stderr_bytes
685)             ), 'program did not print any error message'
686)             assert (
687)                 b'cannot write config' in result.stderr_bytes
688)             ), 'program did not print the expected error message'
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

689) 
Marco Ricci Create the configuration di...

Marco Ricci authored 3 months ago

690)     def test_214d_export_settings_settings_directory_not_a_directory(
691)         self,
692)         monkeypatch: Any,
693)     ) -> None:
694)         runner = click.testing.CliRunner(mix_stderr=False)
695)         with tests.isolated_config(
696)             monkeypatch=monkeypatch, runner=runner, config={'services': {}}
697)         ):
698)             with contextlib.suppress(FileNotFoundError):
699)                 shutil.rmtree('.derivepassphrase')
700)             with open('.derivepassphrase', 'w', encoding='UTF-8') as outfile:
701)                 print('Obstruction!!', file=outfile)
702)             result = runner.invoke(
703)                 cli.derivepassphrase,
704)                 ['--export', '-'],
705)                 input=b'null',
706)                 catch_exceptions=False,
707)             )
708)             assert result.exit_code > 0, 'program unexpectedly succeeded'
709)             assert (
710)                 result.stderr_bytes
711)             ), 'program did not print any error message'
712)             assert (
713)                 b'cannot load config' in result.stderr_bytes
714)             ), 'program did not print the expected error message'
715) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

716)     def test_220_edit_notes_successfully(self, monkeypatch: Any) -> None:
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

718) 
719) # - - - - - >8 - - - - - >8 - - - - - >8 - - - - - >8 - - - - -
720) contents go here
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

722)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

723)         with tests.isolated_config(
724)             monkeypatch=monkeypatch,
725)             runner=runner,
726)             config={'global': {'phrase': 'abc'}, 'services': {}},
727)         ):
728)             monkeypatch.setattr(click, 'edit', lambda *a, **kw: edit_result)  # noqa: ARG005
729)             result = runner.invoke(
730)                 cli.derivepassphrase, ['--notes', 'sv'], catch_exceptions=False
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

731)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

732)             assert (result.exit_code, result.stderr_bytes) == (
733)                 0,
734)                 b'',
735)             ), 'program exited with failure'
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

738)             assert config == {
739)                 'global': {'phrase': 'abc'},
740)                 'services': {'sv': {'notes': 'contents go here'}},
741)             }
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

742) 
743)     def test_221_edit_notes_noop(self, monkeypatch: Any) -> None:
744)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

745)         with tests.isolated_config(
746)             monkeypatch=monkeypatch,
747)             runner=runner,
748)             config={'global': {'phrase': 'abc'}, 'services': {}},
749)         ):
750)             monkeypatch.setattr(click, 'edit', lambda *a, **kw: None)  # noqa: ARG005
751)             result = runner.invoke(
752)                 cli.derivepassphrase, ['--notes', 'sv'], catch_exceptions=False
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

753)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

754)             assert (result.exit_code, result.stderr_bytes) == (
755)                 0,
756)                 b'',
757)             ), 'program exited with failure'
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

759)                 config = json.load(infile)
760)             assert config == {'global': {'phrase': 'abc'}, 'services': {}}
761) 
762)     def test_222_edit_notes_marker_removed(self, monkeypatch: Any) -> None:
763)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

764)         with tests.isolated_config(
765)             monkeypatch=monkeypatch,
766)             runner=runner,
767)             config={'global': {'phrase': 'abc'}, 'services': {}},
768)         ):
769)             monkeypatch.setattr(click, 'edit', lambda *a, **kw: 'long\ntext')  # noqa: ARG005
770)             result = runner.invoke(
771)                 cli.derivepassphrase, ['--notes', 'sv'], catch_exceptions=False
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

772)             )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

773)             assert (result.exit_code, result.stderr_bytes) == (
774)                 0,
775)                 b'',
776)             ), 'program exited with failure'
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

779)             assert config == {
780)                 'global': {'phrase': 'abc'},
781)                 'services': {'sv': {'notes': 'long\ntext'}},
782)             }
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

783) 
784)     def test_223_edit_notes_abort(self, monkeypatch: Any) -> None:
785)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

786)         with tests.isolated_config(
787)             monkeypatch=monkeypatch,
788)             runner=runner,
789)             config={'global': {'phrase': 'abc'}, 'services': {}},
790)         ):
791)             monkeypatch.setattr(click, 'edit', lambda *a, **kw: '\n\n')  # noqa: ARG005
792)             result = runner.invoke(
793)                 cli.derivepassphrase, ['--notes', 'sv'], catch_exceptions=False
794)             )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

795)             assert result.exit_code != 0, 'program unexpectedly succeeded'
Marco Ricci Fix miscellaneous type chec...

Marco Ricci authored 4 months ago

796)             assert result.stderr_bytes is not None
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

797)             assert (
798)                 b'user aborted request' in result.stderr_bytes
799)             ), 'expected error message missing'
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

804)     @pytest.mark.parametrize(
805)         ['command_line', 'input', 'result_config'],
806)         [
807)             (
808)                 ['--phrase'],
809)                 b'my passphrase\n',
810)                 {'global': {'phrase': 'my passphrase'}, 'services': {}},
811)             ),
812)             (
813)                 ['--key'],
814)                 b'1\n',
815)                 {'global': {'key': DUMMY_KEY1_B64}, 'services': {}},
816)             ),
817)             (
818)                 ['--phrase', 'sv'],
819)                 b'my passphrase\n',
820)                 {
821)                     'global': {'phrase': 'abc'},
822)                     'services': {'sv': {'phrase': 'my passphrase'}},
823)                 },
824)             ),
825)             (
826)                 ['--key', 'sv'],
827)                 b'1\n',
828)                 {
829)                     'global': {'phrase': 'abc'},
830)                     'services': {'sv': {'key': DUMMY_KEY1_B64}},
831)                 },
832)             ),
833)             (
834)                 ['--key', '--length', '15', 'sv'],
835)                 b'1\n',
836)                 {
837)                     'global': {'phrase': 'abc'},
838)                     'services': {'sv': {'key': DUMMY_KEY1_B64, 'length': 15}},
839)                 },
840)             ),
841)         ],
842)     )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

843)     def test_224_store_config_good(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

844)         self,
845)         monkeypatch: Any,
846)         command_line: list[str],
847)         input: bytes,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

848)         result_config: Any,
849)     ) -> None:
850)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

851)         with tests.isolated_config(
852)             monkeypatch=monkeypatch,
853)             runner=runner,
854)             config={'global': {'phrase': 'abc'}, 'services': {}},
855)         ):
856)             monkeypatch.setattr(
857)                 cli, '_get_suitable_ssh_keys', tests.suitable_ssh_keys
858)             )
859)             result = runner.invoke(
860)                 cli.derivepassphrase,
861)                 ['--config', *command_line],
862)                 catch_exceptions=False,
863)                 input=input,
864)             )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

865)             assert result.exit_code == 0, 'program exited with failure'
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

868)             assert (
869)                 config == result_config
870)             ), 'stored config does not match expectation'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

871) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

872)     @pytest.mark.parametrize(
873)         ['command_line', 'input', 'err_text'],
874)         [
875)             (
876)                 [],
877)                 b'',
878)                 b'cannot update global settings without actual settings',
879)             ),
880)             (
881)                 ['sv'],
882)                 b'',
883)                 b'cannot update service settings without actual settings',
884)             ),
885)             (['--phrase', 'sv'], b'', b'no passphrase given'),
886)             (['--key'], b'', b'no valid SSH key selected'),
887)         ],
888)     )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

889)     def test_225_store_config_fail(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

890)         self,
891)         monkeypatch: Any,
892)         command_line: list[str],
893)         input: bytes,
894)         err_text: bytes,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

895)     ) -> None:
896)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

897)         with tests.isolated_config(
898)             monkeypatch=monkeypatch,
899)             runner=runner,
900)             config={'global': {'phrase': 'abc'}, 'services': {}},
901)         ):
902)             monkeypatch.setattr(
903)                 cli, '_get_suitable_ssh_keys', tests.suitable_ssh_keys
904)             )
905)             result = runner.invoke(
906)                 cli.derivepassphrase,
907)                 ['--config', *command_line],
908)                 catch_exceptions=False,
909)                 input=input,
910)             )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

911)             assert result.exit_code != 0, 'program unexpectedly succeeded?!'
Marco Ricci Fix miscellaneous type chec...

Marco Ricci authored 4 months ago

912)             assert result.stderr_bytes is not None
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

913)             assert (
914)                 err_text in result.stderr_bytes
915)             ), 'expected error message missing'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

916) 
917)     def test_225a_store_config_fail_manual_no_ssh_key_selection(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

918)         self,
919)         monkeypatch: Any,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

920)     ) -> None:
921)         runner = click.testing.CliRunner(mix_stderr=False)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

922)         with tests.isolated_config(
923)             monkeypatch=monkeypatch,
924)             runner=runner,
925)             config={'global': {'phrase': 'abc'}, 'services': {}},
926)         ):
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

932)             monkeypatch.setattr(cli, '_select_ssh_key', raiser)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

933)             result = runner.invoke(
934)                 cli.derivepassphrase,
935)                 ['--key', '--config'],
936)                 catch_exceptions=False,
937)             )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

938)             assert result.exit_code != 0, 'program unexpectedly succeeded'
Marco Ricci Fix miscellaneous type chec...

Marco Ricci authored 4 months ago

939)             assert result.stderr_bytes is not None
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

940)             assert (
941)                 custom_error.encode() in result.stderr_bytes
942)             ), 'expected error message missing'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

943) 
Marco Ricci Isolate tests properly and...

Marco Ricci authored 3 months ago

944)     def test_226_no_arguments(self, monkeypatch: Any) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

946)         with tests.isolated_config(
947)             monkeypatch=monkeypatch,
948)             runner=runner,
949)             config={'services': {}},
950)         ):
951)             result = runner.invoke(
952)                 cli.derivepassphrase, [], catch_exceptions=False
953)             )
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

954)         assert result.exit_code != 0, 'program unexpectedly succeeded'
Marco Ricci Fix miscellaneous type chec...

Marco Ricci authored 4 months ago

955)         assert result.stderr_bytes is not None
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

956)         assert (
957)             b'SERVICE is required' in result.stderr_bytes
958)         ), 'expected error message missing'
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

959) 
Marco Ricci Isolate tests properly and...

Marco Ricci authored 3 months ago

960)     def test_226a_no_passphrase_or_key(self, monkeypatch: Any) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

962)         with tests.isolated_config(
963)             monkeypatch=monkeypatch,
964)             runner=runner,
965)             config={'services': {}},
966)         ):
967)             result = runner.invoke(
968)                 cli.derivepassphrase, [DUMMY_SERVICE], catch_exceptions=False
969)             )
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

970)         assert result.exit_code != 0, 'program unexpectedly succeeded'
Marco Ricci Fix miscellaneous type chec...

Marco Ricci authored 4 months ago

971)         assert result.stderr_bytes is not None
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

972)         assert (
973)             b'no passphrase or key given' in result.stderr_bytes
974)         ), 'expected error message missing'
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

975) 
Marco Ricci Create the configuration di...

Marco Ricci authored 3 months ago

976)     def test_230_config_directory_nonexistant(self, monkeypatch: Any) -> None:
977)         """the-13th-letter/derivepassphrase#6"""
978)         runner = click.testing.CliRunner(mix_stderr=False)
979)         with tests.isolated_config(
980)             monkeypatch=monkeypatch,
981)             runner=runner,
982)             config={'services': {}},
983)         ):
984)             os.remove('.derivepassphrase/settings.json')
985)             os.rmdir('.derivepassphrase')
986)             os_makedirs_called = False
987)             real_os_makedirs = os.makedirs
988) 
989)             def makedirs(*args: Any, **kwargs: Any) -> Any:
990)                 nonlocal os_makedirs_called
991)                 os_makedirs_called = True
992)                 return real_os_makedirs(*args, **kwargs)
993) 
994)             monkeypatch.setattr(os, 'makedirs', makedirs)
995)             result = runner.invoke(
996)                 cli.derivepassphrase,
997)                 ['--config', '-p'],
998)                 catch_exceptions=False,
999)                 input='abc\n',
1000)             )
1001)             assert (
1002)                 result.stderr_bytes == b'Passphrase:'
1003)             ), 'program unexpectedly failed?!'
1004)             assert result.exit_code == 0, 'program unexpectedly failed?!'
1005)             assert os_makedirs_called, 'os.makedirs has not been called?!'
1006)             with open(cli._config_filename(), encoding='UTF-8') as infile:
1007)                 config_readback = json.load(infile)
1008)             assert config_readback == {
1009)                 'global': {'phrase': 'abc'},
1010)                 'services': {},
1011)             }, 'config mismatch'
1012) 
1013)     def test_230a_config_directory_not_a_file(self, monkeypatch: Any) -> None:
1014)         """the-13th-letter/derivepassphrase#6"""
1015)         runner = click.testing.CliRunner(mix_stderr=False)
1016)         with tests.isolated_config(
1017)             monkeypatch=monkeypatch,
1018)             runner=runner,
1019)             config={'services': {}},
1020)         ):
1021)             _save_config = cli._save_config
1022) 
1023)             def obstruct_config_saving(*args: Any, **kwargs: Any) -> Any:
1024)                 with contextlib.suppress(FileNotFoundError):
1025)                     shutil.rmtree('.derivepassphrase')
1026)                 with open(
1027)                     '.derivepassphrase', 'w', encoding='UTF-8'
1028)                 ) as outfile:
1029)                     print('Obstruction!!', file=outfile)
1030)                 monkeypatch.setattr(cli, '_save_config', _save_config)
1031)                 return _save_config(*args, **kwargs)
1032) 
1033)             monkeypatch.setattr(cli, '_save_config', obstruct_config_saving)
1034)             with pytest.raises(FileExistsError):
1035)                 runner.invoke(
1036)                     cli.derivepassphrase,
1037)                     ['--config', '-p'],
1038)                     catch_exceptions=False,
1039)                     input='abc\n',
1040)                 )
1041) 
Marco Ricci Add finished command-line i...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

1043) class TestCLIUtils:
1044)     def test_100_save_bad_config(self, monkeypatch: Any) -> None:
1045)         runner = click.testing.CliRunner()
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

1046)         with (
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1047)             tests.isolated_config(
1048)                 monkeypatch=monkeypatch, runner=runner, config={}
1049)             ),
1050)             pytest.raises(ValueError, match='Invalid vault config'),
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

1051)         ):
1052)             cli._save_config(None)  # type: ignore
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1053) 
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

1059)             # from https://montypython.fandom.com/wiki/Spam#The_menu
1060)             items = items or [
1061)                 'Egg and bacon',
1062)                 'Egg, sausage and bacon',
1063)                 'Egg and spam',
1064)                 'Egg, bacon and spam',
1065)                 'Egg, bacon, sausage and spam',
1066)                 'Spam, bacon, sausage and spam',
1067)                 'Spam, egg, spam, spam, bacon and spam',
1068)                 'Spam, spam, spam, egg and spam',
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1069)                 (
1070)                     'Spam, spam, spam, spam, spam, spam, baked beans, '
1071)                     'spam, spam, spam and spam'
1072)                 ),
1073)                 (
1074)                     'Lobster thermidor aux crevettes with a mornay sauce '
1075)                     'garnished with truffle paté, brandy '
1076)                     'and a fried egg on top and spam'
1077)                 ),
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1078)             ]
1079)             index = cli._prompt_for_selection(items, heading=heading)
1080)             click.echo('A fine choice: ', nl=False)
1081)             click.echo(items[index])
1082)             click.echo('(Note: Vikings strictly optional.)')
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

1084)         runner = click.testing.CliRunner(mix_stderr=True)
1085)         result = runner.invoke(driver, [], input='9')
1086)         assert result.exit_code == 0, 'driver program failed'
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1087)         assert (
1088)             result.stdout
1089)             == """\
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1090) Our menu:
1091) [1] Egg and bacon
1092) [2] Egg, sausage and bacon
1093) [3] Egg and spam
1094) [4] Egg, bacon and spam
1095) [5] Egg, bacon, sausage and spam
1096) [6] Spam, bacon, sausage and spam
1097) [7] Spam, egg, spam, spam, bacon and spam
1098) [8] Spam, spam, spam, egg and spam
1099) [9] Spam, spam, spam, spam, spam, spam, baked beans, spam, spam, spam and spam
1100) [10] Lobster thermidor aux crevettes with a mornay sauce garnished with truffle paté, brandy and a fried egg on top and spam
1101) Your selection? (1-10, leave empty to abort): 9
1102) A fine choice: Spam, spam, spam, spam, spam, spam, baked beans, spam, spam, spam and spam
1103) (Note: Vikings strictly optional.)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1104) """  # noqa: E501
1105)         ), 'driver program produced unexpected output'
1106)         result = runner.invoke(
1107)             driver, ['--heading='], input='', catch_exceptions=True
1108)         )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1109)         assert result.exit_code > 0, 'driver program succeeded?!'
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1110)         assert (
1111)             result.stdout
1112)             == """\
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1113) [1] Egg and bacon
1114) [2] Egg, sausage and bacon
1115) [3] Egg and spam
1116) [4] Egg, bacon and spam
1117) [5] Egg, bacon, sausage and spam
1118) [6] Spam, bacon, sausage and spam
1119) [7] Spam, egg, spam, spam, bacon and spam
1120) [8] Spam, spam, spam, egg and spam
1121) [9] Spam, spam, spam, spam, spam, spam, baked beans, spam, spam, spam and spam
1122) [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 3 months ago

1123) Your selection? (1-10, leave empty to abort):\x20
1124) """  # noqa: E501
1125)         ), 'driver program produced unexpected output'
1126)         assert isinstance(
1127)             result.exception, IndexError
1128)         ), 'driver program did not raise IndexError?!'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1129) 
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

1135)             try:
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1136)                 cli._prompt_for_selection(
1137)                     [item], heading='', single_choice_prompt=prompt
1138)                 )
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

1145)         runner = click.testing.CliRunner(mix_stderr=True)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1146)         result = runner.invoke(
1147)             driver, ['Will replace with spam. Confirm, y/n?'], input='y'
1148)         )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1149)         assert result.exit_code == 0, 'driver program failed'
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1150)         assert (
1151)             result.stdout
1152)             == """\
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

1156) """
1157)         ), 'driver program produced unexpected output'
1158)         result = runner.invoke(
1159)             driver,
1160)             ['Will replace with spam, okay? ' '(Please say "y" or "n".)'],
1161)             input='',
1162)         )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1163)         assert result.exit_code > 0, 'driver program succeeded?!'
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1164)         assert (
1165)             result.stdout
1166)             == """\
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

1169) Boo.
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1170) """
1171)         ), 'driver program produced unexpected output'
1172)         assert isinstance(
1173)             result.exception, IndexError
1174)         ), 'driver program did not raise IndexError?!'
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1175) 
1176)     def test_103_prompt_for_passphrase(self, monkeypatch: Any) -> None:
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1177)         monkeypatch.setattr(
1178)             click,
1179)             'prompt',
1180)             lambda *a, **kw: json.dumps({'args': a, 'kwargs': kw}),
1181)         )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

1183)         err_msg = 'missing arguments to passphrase prompt'
1184)         assert 'args' in res, err_msg
1185)         assert 'kwargs' in res, err_msg
1186)         assert res['args'][:1] == ['Passphrase'], err_msg
1187)         assert res['kwargs'].get('default') == '', err_msg
1188)         assert not res['kwargs'].get('show_default', True), err_msg
1189)         assert res['kwargs'].get('err'), err_msg
1190)         assert res['kwargs'].get('hide_input'), err_msg
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1191) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1192)     @pytest.mark.parametrize(
1193)         ['command_line', 'config', 'result_config'],
1194)         [
1195)             (
1196)                 ['--delete-globals'],
1197)                 {'global': {'phrase': 'abc'}, 'services': {}},
1198)                 {'services': {}},
1199)             ),
1200)             (
1201)                 ['--delete', DUMMY_SERVICE],
1202)                 {
1203)                     'global': {'phrase': 'abc'},
1204)                     'services': {DUMMY_SERVICE: {'notes': '...'}},
1205)                 },
1206)                 {'global': {'phrase': 'abc'}, 'services': {}},
1207)             ),
1208)             (
1209)                 ['--clear'],
1210)                 {
1211)                     'global': {'phrase': 'abc'},
1212)                     'services': {DUMMY_SERVICE: {'notes': '...'}},
1213)                 },
1214)                 {'services': {}},
1215)             ),
1216)         ],
1217)     )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1218)     def test_203_repeated_config_deletion(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1219)         self,
1220)         monkeypatch: Any,
1221)         command_line: list[str],
1222)         config: dpp.types.VaultConfig,
1223)         result_config: dpp.types.VaultConfig,
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1224)     ) -> None:
1225)         runner = click.testing.CliRunner(mix_stderr=False)
1226)         for start_config in [config, result_config]:
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1227)             with tests.isolated_config(
1228)                 monkeypatch=monkeypatch, runner=runner, config=start_config
1229)             ):
1230)                 result = runner.invoke(
1231)                     cli.derivepassphrase, command_line, catch_exceptions=False
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1232)                 )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1233)                 assert (result.exit_code, result.stderr_bytes) == (
1234)                     0,
1235)                     b'',
1236)                 ), 'program exited with failure'
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

1238)                     config_readback = json.load(infile)
1239)                 assert config_readback == result_config
1240) 
1241)     def test_204_phrase_from_key_manually(self) -> None:
1242)         assert (
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1243)             dpp.Vault(
1244)                 phrase=DUMMY_PHRASE_FROM_KEY1, **DUMMY_CONFIG_SETTINGS
1245)             ).generate(DUMMY_SERVICE)
1246)             == DUMMY_RESULT_KEY1
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1247)         )
1248) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1249)     @pytest.mark.parametrize(
1250)         ['vfunc', 'input'],
1251)         [
1252)             (cli._validate_occurrence_constraint, 20),
1253)             (cli._validate_length, 20),
1254)         ],
1255)     )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1256)     def test_210a_validate_constraints_manually(
1257)         self,
1258)         vfunc: Callable[[click.Context, click.Parameter, Any], int | None],
1259)         input: int,
1260)     ) -> None:
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

1261)         ctx = cli.derivepassphrase.make_context(cli.PROG_NAME, [])
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1262)         param = cli.derivepassphrase.params[0]
1263)         assert vfunc(ctx, param, input) == input
1264) 
1265)     @tests.skip_if_no_agent
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

1267)     def test_227_get_suitable_ssh_keys(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1268)         self,
1269)         monkeypatch: Any,
1270)         conn_hint: str,
Marco Ricci Fix miscellaneous type chec...

Marco Ricci authored 4 months ago

1271)     ) -> None:
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

1272)         monkeypatch.setattr(
1273)             ssh_agent_client.SSHAgentClient, 'list_keys', tests.list_keys
1274)         )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1275)         hint: ssh_agent_client.SSHAgentClient | socket.socket | None
1276)         match conn_hint:
1277)             case 'client':
1278)                 hint = ssh_agent_client.SSHAgentClient()
1279)             case 'socket':
1280)                 hint = socket.socket(family=socket.AF_UNIX)
1281)                 hint.connect(os.environ['SSH_AUTH_SOCK'])
1282)             case _:
1283)                 assert conn_hint == 'none'
1284)                 hint = None
1285)         exception: Exception | None = None
1286)         try:
1287)             list(cli._get_suitable_ssh_keys(hint))
1288)         except RuntimeError:  # pragma: no cover
1289)             pass
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

1290)         except Exception as e:  # noqa: BLE001 # pragma: no cover
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

1291)             exception = e
1292)         finally: