def test_repr(): # type: ignore cfg = AzureKeyVaultConfiguration( "fake_id", "fake_secret", "fake_tenant", "fake_vault", cache_expiration=0 ) d = DICT.copy() cfg._kv_client = FakeSecretClient(d) assert repr(cfg) == "<AzureKeyVaultConfiguration: 'vault URL'>"
def test_load_dict(): # type: ignore cfg = AzureKeyVaultConfiguration( "fake_id", "fake_secret", "fake_tenant", "fake_vault" ) cfg._kv_client = FakeSecretClient(DICT) assert cfg["foo"] == "foo_val" assert cfg["with_underscore"] == "works" assert cfg.get("foo", "default") == "foo_val"
def test_get_attr(): # type: ignore cfg = AzureKeyVaultConfiguration( "fake_id", "fake_secret", "fake_tenant", "fake_vault", cache_expiration=0 ) d = DICT.copy() cfg._kv_client = FakeSecretClient(d) assert cfg.foo == "foo_val" with raises(KeyError): assert cfg.foo_missing is KeyError
def test_missing_key(): # type: ignore cfg = AzureKeyVaultConfiguration( "fake_id", "fake_secret", "fake_tenant", "fake_vault", cache_expiration=0 ) d = DICT.copy() cfg._kv_client = FakeSecretClient(d) with raises(KeyError): assert cfg["foo-missing"] is KeyError assert cfg.get("foo-missing", "default") == "default"
def test_deletion(): # type: ignore cfg = AzureKeyVaultConfiguration( "fake_id", "fake_secret", "fake_tenant", "fake_vault", cache_expiration=0 ) d = DICT.copy() cfg._kv_client = FakeSecretClient(d) assert cfg["foo"] == "foo_val" assert "foo" in cfg._cache del d["foo"] with raises(KeyError): assert cfg["foo"] is KeyError
def test_str(): # type: ignore cfg = AzureKeyVaultConfiguration( "fake_id", "fake_secret", "fake_tenant", "fake_vault", cache_expiration=0 ) d = DICT.copy() cfg._kv_client = FakeSecretClient(d) # str assert ( str(cfg) == "{'bar': 'bar_val', 'foo': 'foo_val', 'password': '******', 'with-underscore': 'works'}" ) assert cfg["password"] == "some passwd"
def test_reload(): # type: ignore cfg = AzureKeyVaultConfiguration( "fake_id", "fake_secret", "fake_tenant", "fake_vault" ) cfg._kv_client = FakeSecretClient(DICT) assert cfg == config_from_dict(DICT) cfg._kv_client = FakeSecretClient(DICT2) cfg.reload() assert cfg == config_from_dict(DICT2)
def test_dict(): # type: ignore cfg = AzureKeyVaultConfiguration( "fake_id", "fake_secret", "fake_tenant", "fake_vault", cache_expiration=0 ) d = DICT.copy() cfg._kv_client = FakeSecretClient(d) assert sorted(cfg.keys()) == sorted(d.keys()) assert sorted(cfg.values()) == sorted(d.values()) assert sorted(cfg.items()) == sorted(d.items())
def test_expiration(mocker): # type: ignore # with cache cfg = AzureKeyVaultConfiguration( "fake_id", "fake_secret", "fake_tenant", "fake_vault" ) cfg._kv_client = FakeSecretClient(DICT) spy = mocker.spy(cfg._kv_client, "get_secret") assert cfg["foo"] == "foo_val" assert cfg["foo"] == "foo_val" assert spy.call_count == 1 # without cache cfg = AzureKeyVaultConfiguration( "fake_id", "fake_secret", "fake_tenant", "fake_vault", cache_expiration=0 ) cfg._kv_client = FakeSecretClient(DICT) spy = mocker.spy(cfg._kv_client, "get_secret") assert cfg["foo"] == "foo_val" assert cfg["foo"] == "foo_val" # this will ignore the cache assert spy.call_count == 2