test_passcommand.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from flexmock import flexmock
  2. from borgmatic.borg import passcommand as module
  3. def test_run_passcommand_with_passphrase_configured_bails():
  4. flexmock(module.borgmatic.execute).should_receive('execute_command_and_capture_output').never()
  5. assert (
  6. module.run_passcommand('passcommand', passphrase_configured=True, working_directory=None)
  7. is None
  8. )
  9. def test_run_passcommand_without_passphrase_configured_executes_passcommand():
  10. flexmock(module.borgmatic.execute).should_receive(
  11. 'execute_command_and_capture_output'
  12. ).and_return('passphrase').once()
  13. assert (
  14. module.run_passcommand('passcommand', passphrase_configured=False, working_directory=None)
  15. == 'passphrase'
  16. )
  17. def test_get_passphrase_from_passcommand_with_configured_passcommand_runs_it():
  18. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  19. '/working'
  20. )
  21. flexmock(module).should_receive('run_passcommand').with_args(
  22. 'command', False, '/working'
  23. ).and_return('passphrase').once()
  24. assert (
  25. module.get_passphrase_from_passcommand(
  26. {'encryption_passcommand': 'command'},
  27. )
  28. == 'passphrase'
  29. )
  30. def test_get_passphrase_from_passcommand_with_configured_passphrase_and_passcommand_detects_passphrase():
  31. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  32. '/working'
  33. )
  34. flexmock(module).should_receive('run_passcommand').with_args(
  35. 'command', True, '/working'
  36. ).and_return(None).once()
  37. assert (
  38. module.get_passphrase_from_passcommand(
  39. {'encryption_passphrase': 'passphrase', 'encryption_passcommand': 'command'},
  40. )
  41. is None
  42. )