2
0

test_keepassxc.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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={},
  10. config={},
  11. credential_parameters=credential_parameters,
  12. )
  13. def test_load_credential_with_missing_database_raises():
  14. flexmock(module.os.path).should_receive('expanduser').with_args('database.kdbx').and_return(
  15. 'database.kdbx',
  16. )
  17. flexmock(module.os.path).should_receive('exists').and_return(False)
  18. flexmock(module.borgmatic.execute).should_receive('execute_command_and_capture_output').never()
  19. with pytest.raises(ValueError):
  20. module.load_credential(
  21. hook_config={},
  22. config={},
  23. credential_parameters=('database.kdbx', 'mypassword'),
  24. )
  25. def test_load_credential_with_present_database_fetches_password_from_keepassxc():
  26. flexmock(module.os.path).should_receive('expanduser').with_args('database.kdbx').and_return(
  27. 'database.kdbx',
  28. )
  29. flexmock(module.os.path).should_receive('exists').and_return(True)
  30. flexmock(module.borgmatic.execute).should_receive(
  31. 'execute_command_and_capture_output',
  32. ).with_args(
  33. (
  34. 'keepassxc-cli',
  35. 'show',
  36. '--show-protected',
  37. '--attributes',
  38. 'Password',
  39. 'database.kdbx',
  40. 'mypassword',
  41. ),
  42. ).and_return('password').once()
  43. assert (
  44. module.load_credential(
  45. hook_config={},
  46. config={},
  47. credential_parameters=('database.kdbx', 'mypassword'),
  48. )
  49. == 'password'
  50. )
  51. def test_load_credential_with_custom_keepassxc_cli_command_calls_it():
  52. flexmock(module.os.path).should_receive('expanduser').with_args('database.kdbx').and_return(
  53. 'database.kdbx',
  54. )
  55. config = {'keepassxc': {'keepassxc_cli_command': '/usr/local/bin/keepassxc-cli --some-option'}}
  56. flexmock(module.os.path).should_receive('exists').and_return(True)
  57. flexmock(module.borgmatic.execute).should_receive(
  58. 'execute_command_and_capture_output',
  59. ).with_args(
  60. (
  61. '/usr/local/bin/keepassxc-cli',
  62. '--some-option',
  63. 'show',
  64. '--show-protected',
  65. '--attributes',
  66. 'Password',
  67. 'database.kdbx',
  68. 'mypassword',
  69. ),
  70. ).and_return('password').once()
  71. assert (
  72. module.load_credential(
  73. hook_config=config['keepassxc'],
  74. config=config,
  75. credential_parameters=('database.kdbx', 'mypassword'),
  76. )
  77. == 'password'
  78. )
  79. def test_load_credential_with_expanded_directory_with_present_database_fetches_password_from_keepassxc():
  80. flexmock(module.os.path).should_receive('expanduser').with_args('~/database.kdbx').and_return(
  81. '/root/database.kdbx',
  82. )
  83. flexmock(module.os.path).should_receive('exists').and_return(True)
  84. flexmock(module.borgmatic.execute).should_receive(
  85. 'execute_command_and_capture_output',
  86. ).with_args(
  87. (
  88. 'keepassxc-cli',
  89. 'show',
  90. '--show-protected',
  91. '--attributes',
  92. 'Password',
  93. '/root/database.kdbx',
  94. 'mypassword',
  95. ),
  96. ).and_return('password').once()
  97. assert (
  98. module.load_credential(
  99. hook_config={},
  100. config={},
  101. credential_parameters=('~/database.kdbx', 'mypassword'),
  102. )
  103. == 'password'
  104. )
  105. def test_load_credential_with_key_file():
  106. flexmock(module.os.path).should_receive('expanduser').with_args('database.kdbx').and_return(
  107. 'database.kdbx',
  108. )
  109. flexmock(module.os.path).should_receive('exists').and_return(True)
  110. flexmock(module.borgmatic.execute).should_receive(
  111. 'execute_command_and_capture_output',
  112. ).with_args(
  113. (
  114. 'keepassxc-cli',
  115. 'show',
  116. '--show-protected',
  117. '--attributes',
  118. 'Password',
  119. '--key-file',
  120. '/path/to/keyfile',
  121. 'database.kdbx',
  122. 'mypassword',
  123. ),
  124. ).and_return('password').once()
  125. assert (
  126. module.load_credential(
  127. hook_config={'key_file': '/path/to/keyfile'},
  128. config={},
  129. credential_parameters=('database.kdbx', 'mypassword'),
  130. )
  131. == 'password'
  132. )
  133. def test_load_credential_with_yubikey():
  134. flexmock(module.os.path).should_receive('expanduser').with_args('database.kdbx').and_return(
  135. 'database.kdbx',
  136. )
  137. flexmock(module.os.path).should_receive('exists').and_return(True)
  138. flexmock(module.borgmatic.execute).should_receive(
  139. 'execute_command_and_capture_output',
  140. ).with_args(
  141. (
  142. 'keepassxc-cli',
  143. 'show',
  144. '--show-protected',
  145. '--attributes',
  146. 'Password',
  147. '--yubikey',
  148. '1:7370001',
  149. 'database.kdbx',
  150. 'mypassword',
  151. ),
  152. ).and_return('password').once()
  153. assert (
  154. module.load_credential(
  155. hook_config={'yubikey': '1:7370001'},
  156. config={},
  157. credential_parameters=('database.kdbx', 'mypassword'),
  158. )
  159. == 'password'
  160. )
  161. def test_load_credential_with_key_file_and_yubikey():
  162. flexmock(module.os.path).should_receive('expanduser').with_args('database.kdbx').and_return(
  163. 'database.kdbx',
  164. )
  165. flexmock(module.os.path).should_receive('exists').and_return(True)
  166. flexmock(module.borgmatic.execute).should_receive(
  167. 'execute_command_and_capture_output',
  168. ).with_args(
  169. (
  170. 'keepassxc-cli',
  171. 'show',
  172. '--show-protected',
  173. '--attributes',
  174. 'Password',
  175. '--key-file',
  176. '/path/to/keyfile',
  177. '--yubikey',
  178. '2',
  179. 'database.kdbx',
  180. 'mypassword',
  181. ),
  182. ).and_return('password').once()
  183. assert (
  184. module.load_credential(
  185. hook_config={'key_file': '/path/to/keyfile', 'yubikey': '2'},
  186. config={},
  187. credential_parameters=('database.kdbx', 'mypassword'),
  188. )
  189. == 'password'
  190. )