2
0

test_parse.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.hooks.credential import parse as module
  4. def test_resolve_credential_passes_through_string_without_credential_tag():
  5. module.resolve_credential.cache_clear()
  6. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').never()
  7. assert module.resolve_credential('{no credentials here}') == '{no credentials here}'
  8. def test_resolve_credential_passes_through_none():
  9. module.resolve_credential.cache_clear()
  10. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').never()
  11. assert module.resolve_credential(None) is None
  12. @pytest.mark.parametrize('invalid_value', ('{credential}', '{credential }', '{credential systemd}'))
  13. def test_resolve_credential_with_invalid_credential_tag_raises(invalid_value):
  14. module.resolve_credential.cache_clear()
  15. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').never()
  16. with pytest.raises(ValueError):
  17. module.resolve_credential(invalid_value)
  18. def test_resolve_credential_with_valid_credential_tag_loads_credential():
  19. module.resolve_credential.cache_clear()
  20. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  21. 'load_credential',
  22. {},
  23. 'systemd',
  24. 'mycredential',
  25. ).and_return('result').once()
  26. assert module.resolve_credential('{credential systemd mycredential}') == 'result'
  27. def test_resolve_credential_caches_credential_after_first_call():
  28. module.resolve_credential.cache_clear()
  29. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  30. 'load_credential',
  31. {},
  32. 'systemd',
  33. 'mycredential',
  34. ).and_return('result').once()
  35. assert module.resolve_credential('{credential systemd mycredential}') == 'result'
  36. assert module.resolve_credential('{credential systemd mycredential}') == 'result'