test_file.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import io
  2. import sys
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic.hooks.credential import file as module
  6. @pytest.mark.parametrize('credential_parameters', ((), ('foo', 'bar')))
  7. def test_load_credential_with_invalid_credential_parameters_raises(credential_parameters):
  8. with pytest.raises(ValueError):
  9. module.load_credential(
  10. hook_config={},
  11. config={},
  12. credential_parameters=credential_parameters,
  13. )
  14. def test_load_credential_with_invalid_credential_name_raises():
  15. with pytest.raises(ValueError):
  16. module.load_credential(
  17. hook_config={},
  18. config={},
  19. credential_parameters=('this is invalid',),
  20. )
  21. def test_load_credential_reads_named_credential_from_file():
  22. credential_stream = io.StringIO('password')
  23. credential_stream.name = '/credentials/mycredential'
  24. builtins = flexmock(sys.modules['builtins'])
  25. flexmock(module.os.path).should_receive('expanduser').with_args(
  26. '/credentials/mycredential',
  27. ).and_return('/credentials/mycredential')
  28. builtins.should_receive('open').with_args(
  29. '/credentials/mycredential', encoding='utf-8'
  30. ).and_return(
  31. credential_stream,
  32. )
  33. assert (
  34. module.load_credential(
  35. hook_config={},
  36. config={},
  37. credential_parameters=('/credentials/mycredential',),
  38. )
  39. == 'password'
  40. )
  41. def test_load_credential_reads_named_credential_from_file_using_working_directory():
  42. credential_stream = io.StringIO('password')
  43. credential_stream.name = '/working/credentials/mycredential'
  44. builtins = flexmock(sys.modules['builtins'])
  45. flexmock(module.os.path).should_receive('expanduser').with_args(
  46. 'credentials/mycredential',
  47. ).and_return('credentials/mycredential')
  48. builtins.should_receive('open').with_args(
  49. '/working/credentials/mycredential', encoding='utf-8'
  50. ).and_return(
  51. credential_stream,
  52. )
  53. assert (
  54. module.load_credential(
  55. hook_config={},
  56. config={'working_directory': '/working'},
  57. credential_parameters=('credentials/mycredential',),
  58. )
  59. == 'password'
  60. )
  61. def test_load_credential_with_file_not_found_error_raises():
  62. builtins = flexmock(sys.modules['builtins'])
  63. flexmock(module.os.path).should_receive('expanduser').with_args(
  64. '/credentials/mycredential',
  65. ).and_return('/credentials/mycredential')
  66. builtins.should_receive('open').with_args(
  67. '/credentials/mycredential', encoding='utf-8'
  68. ).and_raise(
  69. FileNotFoundError,
  70. )
  71. with pytest.raises(ValueError):
  72. module.load_credential(
  73. hook_config={},
  74. config={},
  75. credential_parameters=('/credentials/mycredential',),
  76. )
  77. def test_load_credential_reads_named_credential_from_expanded_directory():
  78. credential_stream = io.StringIO('password')
  79. credential_stream.name = '/root/credentials/mycredential'
  80. builtins = flexmock(sys.modules['builtins'])
  81. flexmock(module.os.path).should_receive('expanduser').with_args(
  82. '~/credentials/mycredential',
  83. ).and_return('/root/credentials/mycredential')
  84. builtins.should_receive('open').with_args(
  85. '/root/credentials/mycredential', encoding='utf-8'
  86. ).and_return(
  87. credential_stream,
  88. )
  89. assert (
  90. module.load_credential(
  91. hook_config={},
  92. config={},
  93. credential_parameters=('~/credentials/mycredential',),
  94. )
  95. == 'password'
  96. )