test_keepassxc.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.hooks.credential import keepassxc as module
  4. @pytest.mark.parametrize('credential_parameters', ((), ('foo',), ('foo', 'bar', 'baz')))
  5. def test_load_credential_with_invalid_credential_parameters_raises(credential_parameters):
  6. flexmock(module.borgmatic.execute).should_receive('execute_command_and_capture_output').never()
  7. with pytest.raises(ValueError):
  8. module.load_credential(
  9. hook_config={}, config={}, credential_parameters=credential_parameters
  10. )
  11. def test_load_credential_with_missing_database_raises():
  12. flexmock(module.os.path).should_receive('exists').and_return(False)
  13. flexmock(module.borgmatic.execute).should_receive('execute_command_and_capture_output').never()
  14. with pytest.raises(ValueError):
  15. module.load_credential(
  16. hook_config={}, config={}, credential_parameters=('database.kdbx', 'mypassword')
  17. )
  18. def test_load_credential_with_present_database_fetches_password_from_keepassxc():
  19. flexmock(module.os.path).should_receive('exists').and_return(True)
  20. flexmock(module.borgmatic.execute).should_receive(
  21. 'execute_command_and_capture_output'
  22. ).with_args(
  23. (
  24. 'keepassxc-cli',
  25. 'show',
  26. '--show-protected',
  27. '--attributes',
  28. 'Password',
  29. 'database.kdbx',
  30. 'mypassword',
  31. )
  32. ).and_return(
  33. 'password'
  34. ).once()
  35. assert (
  36. module.load_credential(
  37. hook_config={}, config={}, credential_parameters=('database.kdbx', 'mypassword')
  38. )
  39. == 'password'
  40. )
  41. def test_load_credential_with_custom_keepassxc_cli_command_calls_it():
  42. config = {'keepassxc': {'keepassxc_cli_command': '/usr/local/bin/keepassxc-cli --some-option'}}
  43. flexmock(module.os.path).should_receive('exists').and_return(True)
  44. flexmock(module.borgmatic.execute).should_receive(
  45. 'execute_command_and_capture_output'
  46. ).with_args(
  47. (
  48. '/usr/local/bin/keepassxc-cli',
  49. '--some-option',
  50. 'show',
  51. '--show-protected',
  52. '--attributes',
  53. 'Password',
  54. 'database.kdbx',
  55. 'mypassword',
  56. )
  57. ).and_return(
  58. 'password'
  59. ).once()
  60. assert (
  61. module.load_credential(
  62. hook_config=config['keepassxc'],
  63. config=config,
  64. credential_parameters=('database.kdbx', 'mypassword'),
  65. )
  66. == 'password'
  67. )