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