test_passcommand.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. )
  43. def test_get_passphrase_from_passcommand_with_configured_blank_passphrase_and_passcommand_detects_passphrase():
  44. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  45. '/working'
  46. )
  47. flexmock(module).should_receive('run_passcommand').with_args(
  48. 'command', True, '/working'
  49. ).and_return(None).once()
  50. assert (
  51. module.get_passphrase_from_passcommand(
  52. {'encryption_passphrase': '', 'encryption_passcommand': 'command'},
  53. )
  54. is None
  55. )