test_passcommand.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.hooks.credential import passcommand as module
  4. def test_run_passcommand_with_passphrase_configured_bails():
  5. flexmock(module.borgmatic.execute).should_receive('execute_command_and_capture_output').never()
  6. assert (
  7. module.run_passcommand('passcommand', passphrase_configured=True, working_directory=None)
  8. is None
  9. )
  10. def test_run_passcommand_without_passphrase_configured_executes_passcommand():
  11. flexmock(module.borgmatic.execute).should_receive(
  12. 'execute_command_and_capture_output'
  13. ).and_return('passphrase').once()
  14. assert (
  15. module.run_passcommand('passcommand', passphrase_configured=False, working_directory=None)
  16. == 'passphrase'
  17. )
  18. def test_load_credential_with_unknown_credential_name_errors():
  19. with pytest.raises(ValueError):
  20. module.load_credential(hook_config={}, config={}, credential_name='wtf')
  21. def test_load_credential_with_configured_passcommand_runs_it():
  22. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  23. '/working'
  24. )
  25. flexmock(module).should_receive('run_passcommand').with_args(
  26. 'command', False, '/working'
  27. ).and_return('passphrase').once()
  28. assert (
  29. module.load_credential(
  30. hook_config={},
  31. config={'encryption_passcommand': 'command'},
  32. credential_name='encryption_passphrase',
  33. )
  34. == 'passphrase'
  35. )
  36. def test_load_credential_with_configured_passphrase_and_passcommand_detects_passphrase():
  37. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  38. '/working'
  39. )
  40. flexmock(module).should_receive('run_passcommand').with_args(
  41. 'command', True, '/working'
  42. ).and_return(None).once()
  43. assert (
  44. module.load_credential(
  45. hook_config={},
  46. config={'encryption_passphrase': 'passphrase', 'encryption_passcommand': 'command'},
  47. credential_name='encryption_passphrase',
  48. )
  49. is None
  50. )