test_passcommand.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from flexmock import flexmock
  2. from borgmatic.borg import passcommand as module
  3. def test_run_passcommand_with_passphrase_configured_bails():
  4. module.run_passcommand.cache_clear()
  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. module.run_passcommand.cache_clear()
  12. flexmock(module.borgmatic.execute).should_receive(
  13. 'execute_command_and_capture_output'
  14. ).and_return('passphrase').once()
  15. assert (
  16. module.run_passcommand('passcommand', passphrase_configured=False, working_directory=None)
  17. == 'passphrase'
  18. )
  19. def test_get_passphrase_from_passcommand_with_configured_passcommand_runs_it():
  20. module.run_passcommand.cache_clear()
  21. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  22. '/working'
  23. )
  24. flexmock(module).should_receive('run_passcommand').with_args(
  25. 'command', False, '/working'
  26. ).and_return('passphrase').once()
  27. assert (
  28. module.get_passphrase_from_passcommand(
  29. {'encryption_passcommand': 'command'},
  30. )
  31. == 'passphrase'
  32. )
  33. def test_get_passphrase_from_passcommand_with_configured_passphrase_and_passcommand_detects_passphrase():
  34. module.run_passcommand.cache_clear()
  35. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  36. '/working'
  37. )
  38. flexmock(module).should_receive('run_passcommand').with_args(
  39. 'command', True, '/working'
  40. ).and_return(None).once()
  41. assert (
  42. module.get_passphrase_from_passcommand(
  43. {'encryption_passphrase': 'passphrase', 'encryption_passcommand': 'command'},
  44. )
  45. is None
  46. )
  47. def test_get_passphrase_from_passcommand_with_configured_blank_passphrase_and_passcommand_detects_passphrase():
  48. module.run_passcommand.cache_clear()
  49. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  50. '/working'
  51. )
  52. flexmock(module).should_receive('run_passcommand').with_args(
  53. 'command', True, '/working'
  54. ).and_return(None).once()
  55. assert (
  56. module.get_passphrase_from_passcommand(
  57. {'encryption_passphrase': '', 'encryption_passcommand': 'command'},
  58. )
  59. is None
  60. )
  61. def test_run_passcommand_caches_passcommand_after_first_call():
  62. module.run_passcommand.cache_clear()
  63. flexmock(module.borgmatic.execute).should_receive(
  64. 'execute_command_and_capture_output'
  65. ).and_return('passphrase').once()
  66. assert (
  67. module.run_passcommand('passcommand', passphrase_configured=False, working_directory=None)
  68. == 'passphrase'
  69. )
  70. assert (
  71. module.run_passcommand('passcommand', passphrase_configured=False, working_directory=None)
  72. == 'passphrase'
  73. )