test_credential.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.config import credential as module
  4. def test_resolve_credentials_ignores_string_without_credential_tag():
  5. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').never()
  6. module.resolve_credentials(config=flexmock(), item='!no credentials here')
  7. def test_resolve_credentials_with_invalid_credential_tag_raises():
  8. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').never()
  9. with pytest.raises(ValueError):
  10. module.resolve_credentials(config=flexmock(), item='!credential systemd')
  11. def test_resolve_credentials_with_valid_credential_tag_loads_credential():
  12. config = flexmock()
  13. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  14. 'load_credential',
  15. config,
  16. 'systemd',
  17. 'mycredential',
  18. ).and_return('result').once()
  19. assert (
  20. module.resolve_credentials(config=config, item='!credential systemd mycredential')
  21. == 'result'
  22. )
  23. def test_resolve_credentials_with_list_recurses_and_loads_credentials():
  24. config = flexmock()
  25. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  26. 'load_credential',
  27. config,
  28. 'systemd',
  29. 'mycredential',
  30. ).and_return('result1').once()
  31. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  32. 'load_credential',
  33. config,
  34. 'systemd',
  35. 'othercredential',
  36. ).and_return('result2').once()
  37. assert module.resolve_credentials(
  38. config=config,
  39. item=['!credential systemd mycredential', 'nope', '!credential systemd othercredential'],
  40. ) == ['result1', 'nope', 'result2']
  41. def test_resolve_credentials_with_dict_recurses_and_loads_credentials():
  42. config = flexmock()
  43. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  44. 'load_credential',
  45. config,
  46. 'systemd',
  47. 'mycredential',
  48. ).and_return('result1').once()
  49. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  50. 'load_credential',
  51. config,
  52. 'systemd',
  53. 'othercredential',
  54. ).and_return('result2').once()
  55. assert module.resolve_credentials(
  56. config=config,
  57. item={
  58. 'a': '!credential systemd mycredential',
  59. 'b': 'nope',
  60. 'c': '!credential systemd othercredential',
  61. },
  62. ) == {'a': 'result1', 'b': 'nope', 'c': 'result2'}
  63. def test_resolve_credentials_with_list_of_dicts_recurses_and_loads_credentials():
  64. config = flexmock()
  65. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  66. 'load_credential',
  67. config,
  68. 'systemd',
  69. 'mycredential',
  70. ).and_return('result1').once()
  71. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  72. 'load_credential',
  73. config,
  74. 'systemd',
  75. 'othercredential',
  76. ).and_return('result2').once()
  77. assert module.resolve_credentials(
  78. config=config,
  79. item=[
  80. {'a': '!credential systemd mycredential', 'b': 'nope'},
  81. {'c': '!credential systemd othercredential'},
  82. ],
  83. ) == [{'a': 'result1', 'b': 'nope'}, {'c': 'result2'}]
  84. def test_resolve_credentials_with_dict_of_lists_recurses_and_loads_credentials():
  85. config = flexmock()
  86. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  87. 'load_credential',
  88. config,
  89. 'systemd',
  90. 'mycredential',
  91. ).and_return('result1').once()
  92. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  93. 'load_credential',
  94. config,
  95. 'systemd',
  96. 'othercredential',
  97. ).and_return('result2').once()
  98. assert module.resolve_credentials(
  99. config=config,
  100. item={
  101. 'a': ['!credential systemd mycredential', 'nope'],
  102. 'b': ['!credential systemd othercredential'],
  103. },
  104. ) == {'a': ['result1', 'nope'], 'b': ['result2']}