test_credential.py 4.3 KB

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