Exemple #1
0
def test_secret_loader_pass_loader_as_tuple_requires_four_elements():
    with pytest.raises(ConstructLoaderError):
        secret = SecretLoader([
            (helpers.DummyLoader, ),
        ])

    with pytest.raises(ConstructLoaderError):
        secret = SecretLoader([
            (
                helpers.DummyLoader,
                0,
            ),
        ])

    with pytest.raises(ConstructLoaderError):
        secret = SecretLoader([
            (helpers.DummyLoader, 0, ()),
        ])

    with pytest.raises(ConstructLoaderError):
        secret = SecretLoader([
            (helpers.DummyLoader, 0, (), {}, "other"),
        ])

    secret = SecretLoader([
        (helpers.DummyLoader, 0, (), {}),
    ])

    assert secret is not None
Exemple #2
0
def test_secret_loader_pass_parser_to_parse():
    value = "secret_value"
    parser = lambda x: value
    secret = SecretLoader()

    assert helpers.ENV_VAR_VALUE == secret.parse(helpers.ENV_VAR_VALUE)
    assert value == secret.parse(helpers.ENV_VAR_NAME, parser=parser)
Exemple #3
0
def test_secret_loader_construct_loader_set_priority():
    loader_class = helpers.DummyLoader

    secret = SecretLoader(loaders=[])
    loader = secret._construct_loader(loader=loader_class, priority=3)

    assert loader.priority == 3
Exemple #4
0
def test_secret_loader_use_parser_passed_to_call():
    secret = SecretLoader([
        (helpers.DummyLoader, 3, (), {}),
    ])
    value = "secret_value"

    assert secret(helpers.ENV_VAR_NAME, parser=lambda x: value) == value
Exemple #5
0
def test_secret_loader_construct_loader_default_priority_is_zero():
    loader_class = helpers.DummyLoader

    secret = SecretLoader(loaders=[])
    loader = secret._construct_loader(loader=loader_class)

    assert loader.priority == 0
Exemple #6
0
def test_secret_loader_construct_loader_returns_loader_container_instance():
    loader_class = helpers.DummyLoader

    secret = SecretLoader(loaders=[])
    loader = secret._construct_loader(loader=loader_class)

    assert isinstance(loader, LoaderContainer)
Exemple #7
0
def test_secret_loader_parser_on_call_must_be_keyword():
    secret = SecretLoader([
        (helpers.DummyLoader, 3, (), {}),
    ])
    value = "secret_value"

    with pytest.raises(TypeError):
        secret(helpers.ENV_VAR_NAME, lambda x: value)
Exemple #8
0
def test_secret_laoder_register_loader():
    loaders = []
    secret = SecretLoader(loaders=loaders)
    loader = helpers.DummyLoader
    secret.register(loader=loader, priority=2)

    assert isinstance(secret.loaders[0].loader, helpers.DummyLoader)
    assert len(secret.loaders) == 1
Exemple #9
0
def test_secret_laoder_register_loader_with_kwargs():
    loaders = []
    secret = SecretLoader(loaders=loaders)
    loader = helpers.DummyLoader
    secret.register(loader=loader, raise_not_found=True)

    assert isinstance(secret.loaders[0].loader, helpers.DummyLoader)
    assert len(secret.loaders) == 1
    assert secret.loaders[0].loader.raise_not_found == True
Exemple #10
0
def test_secret_laoder_priority_for_registered_loader():
    loaders = []
    priority = 3

    secret = SecretLoader(loaders=loaders)
    loader = helpers.DummyLoader
    secret.register(loader=loader, priority=priority)

    assert secret.loaders[0].priority == priority
Exemple #11
0
def test_secret_loader_pass_kwargs_to_call():
    secret = SecretLoader([
        (helpers.DummyLoader, 3, (), {}),
    ])

    assert (secret(helpers.ENV_VAR_NAME,
                   some_dummy_var="abc",
                   some_other_dummy="efg",
                   some_int=1) == helpers.ENV_VAR_NAME)
Exemple #12
0
def test_secret_loader_last_parser_beats_init_parser():
    value_1 = "secret_value_1"
    value_2 = "secret_value_2"
    parser_1 = lambda x: value_1
    parser_2 = lambda x: value_2
    secret = SecretLoader(parser=parser_1)

    assert value_1 == secret.parse(helpers.ENV_VAR_NAME)
    assert value_2 == secret.parse(helpers.ENV_VAR_NAME, parser=parser_2)
Exemple #13
0
def test_secret_loader_construct_loader_container_contains_expected_loader():
    loader_class = helpers.DummyLoader
    kwargs = {"raise_not_found": True}

    secret = SecretLoader(loaders=[])
    loader = secret._construct_loader(loader=loader_class, **kwargs)

    assert isinstance(loader.loader, helpers.DummyLoader)
    assert loader.loader.raise_not_found == True
Exemple #14
0
def test_secret_laoder_register_loader_order():
    default_loaders = [EnvLoader]
    secret = SecretLoader(default_loaders)

    assert len(secret.loaders) == len(default_loaders)

    secret.register(loader=helpers.DummyLoader, priority=1)

    assert len(secret.loaders) == len(default_loaders) + 1
    assert isinstance(secret.loaders[0].loader, helpers.DummyLoader)
Exemple #15
0
def test_secret_loader_pass_dummy_loader_as_tuple():
    loaders = [
        (helpers.DummyLoader, 1, (), {
            "raise_not_found": True
        }),
    ]
    secret = SecretLoader(loaders=loaders)

    assert isinstance(secret.loaders[0].loader, helpers.DummyLoader)
    assert secret.loaders[0].loader.raise_not_found == True
Exemple #16
0
def test_secret_loader_construct_loader_list():
    loaders = [
        (helpers.DummyLoader, 0, (), {
            "raise_not_found": True
        }),
    ]
    secret = SecretLoader(loaders=[])
    loader_list = secret._construct_loader_list(loaders)

    assert isinstance(loader_list[0].loader, helpers.DummyLoader)
    assert loader_list[0].loader.raise_not_found == True
Exemple #17
0
def test_secret_loader_highest_priority_loader_comes_first():
    high_priority = 3
    medium_priority = 2
    low_priority = 0
    secret = SecretLoader([
        (helpers.DummyLoader, medium_priority, (), {}),
        (helpers.DummyLoader, low_priority, (), {}),
        (helpers.DummyLoader, high_priority, (), {}),
    ])

    secret_2 = SecretLoader([
        (helpers.DummyLoader, high_priority, (), {}),
        (helpers.DummyLoader, low_priority, (), {}),
        (helpers.DummyLoader, medium_priority, (), {}),
    ])

    assert secret.loaders[0].priority == high_priority
    assert secret.loaders[-1].priority == low_priority

    assert secret_2.loaders[0].priority == high_priority
    assert secret_2.loaders[-1].priority == low_priority
Exemple #18
0
def test_secret_loader_raises_not_found_after_last_loader_failed():
    loaders = [
        (helpers.DummyLoader, 3, (), {
            "raise_not_found": True
        }),
    ]
    secret = SecretLoader(loaders=loaders)
    secret_name = "SOME_DUMMY_SECRET"

    with pytest.raises(SecretNotFoundError) as excption_info:
        secret(secret_name)

    assert f"Could not load '{secret_name}' using loaders: [" in str(
        excption_info.value)
Exemple #19
0
def test_secret_loader_pass_dummy_loader_as_dict():
    loaders = [
        {
            "loader": helpers.DummyLoader,
            "priority": 0,
            "args": (),
            "kwargs": {
                "raise_not_found": True
            },
        },
    ]
    secret = SecretLoader(loaders=loaders)

    assert isinstance(secret.loaders[0].loader, helpers.DummyLoader)
    assert secret.loaders[0].loader.raise_not_found == True
Exemple #20
0
def get_secret_loader(args):
    if args.remove_loaders:
        logger.debug(f"Get new secret_loader without any registered loader")
        secret_ = SecretLoader()
    else:
        logger.debug(f"Use default secret_loader with loaders pre-registered")
        secret_ = secret

    # It would be possible to allow both custom_loader and loader args to be added.
    # However this creates an issue with the priority flag as it is implemented now.
    # Additionally it might not be interesting in the long run, as custom_loader and
    # loader functionallity might be merged into one flag.
    if args.custom_loader:
        loader = get_custom_loader(args.custom_loader)
        secret_.register(loader, args.priority)

    elif args.loader:
        loader = available_loaders[args.loader]
        secret_.register(loader, args.priority)

    return secret_
Exemple #21
0
def test_secret_loader_pass_empty_loaders():
    loaders = []
    secret = SecretLoader(loaders=loaders)

    assert secret.loaders == loaders
Exemple #22
0
def test_secret_loader_construct_loader_list_fail_for_wrong_type():
    loaders = [helpers.DummyLoader()]
    secret = SecretLoader(loaders=[])

    with pytest.raises(ConstructLoaderError):
        loader_list = secret._construct_loader_list(loaders)
Exemple #23
0
def test_secret_loader_is_callable():
    secret = SecretLoader()
    assert callable(secret)
Exemple #24
0
def test_secret_loader_with_dummy_loader():
    loaders = [helpers.DummyLoader]
    secret = SecretLoader(loaders=loaders)
    dummy_secret = "some_secret"

    assert dummy_secret == secret(dummy_secret)
Exemple #25
0
def test_secret_loader_empty_loaders_raises_no_loaders():
    loaders = []
    secret = SecretLoader(loaders=loaders)

    with pytest.raises(NoLoaderConfiguredError):
        secret("SOME_DUMMY_SECRET")
Exemple #26
0
def test_secret_loader_pass_dummy_loader_as_callable():
    loaders = [helpers.DummyLoader]
    secret = SecretLoader(loaders=loaders)

    assert isinstance(secret.loaders[0].loader, helpers.DummyLoader)
    assert secret.loaders[0].loader.raise_not_found == False
Exemple #27
0
def test_secret_loader_pass_parser():
    value = "secret_value"
    secret = SecretLoader(parser=lambda x: value)

    assert value == secret.parse(helpers.ENV_VAR_NAME)
    assert value == secret.parse(value)
Exemple #28
0
def test_secret_loader_parser_must_be_keyword():

    with pytest.raises(TypeError):
        secret = SecretLoader([
            (helpers.DummyLoader, 4, (), {}),
        ], lambda x: x)