def test_reload(): # type: ignore secretmanager.SecretManagerServiceClient = fake_client(DICT) cfg = GCPSecretManagerConfiguration("fake_id") assert cfg == config_from_dict(DICT) cfg._client = FakeSecretClient(DICT2) cfg.reload() assert cfg == config_from_dict(DICT2)
def test_dict(): # type: ignore d = DICT.copy() secretmanager.SecretManagerServiceClient = fake_client(d) cfg = GCPSecretManagerConfiguration("fake_id", cache_expiration=0) assert sorted(cfg.keys()) == sorted(d.keys()) assert sorted(cfg.values()) == sorted(d.values()) assert sorted(cfg.items()) == sorted(d.items())
def test_load_dict(): # type: ignore secretmanager.SecretManagerServiceClient = fake_client(DICT) cfg = GCPSecretManagerConfiguration("fake_id") print(cfg) assert cfg["foo"] == "foo_val" assert cfg["with_underscore"] == "works" assert cfg.get("foo", "default") == "foo_val"
def test_missing_key(): # type: ignore d = DICT.copy() secretmanager.SecretManagerServiceClient = fake_client(d) cfg = GCPSecretManagerConfiguration("fake_id", cache_expiration=0) with raises(KeyError): assert cfg["foo-missing"] is KeyError assert cfg.get("foo-missing", "default") == "default"
def test_expiration(mocker): # type: ignore # with cache secretmanager.SecretManagerServiceClient = fake_client(DICT) cfg = GCPSecretManagerConfiguration("fake_id") spy = mocker.spy(cfg._client, "access_secret_version") assert cfg["foo"] == "foo_val" assert cfg["foo"] == "foo_val" assert spy.call_count == 1 # without cache secretmanager.SecretManagerServiceClient = fake_client(DICT) cfg = GCPSecretManagerConfiguration("fake_id", cache_expiration=0) spy = mocker.spy(cfg._client, "access_secret_version") assert cfg["foo"] == "foo_val" assert cfg["foo"] == "foo_val" # this will ignore the cache assert spy.call_count == 2
def test_get_attr(): # type: ignore d = DICT.copy() secretmanager.SecretManagerServiceClient = fake_client(d) cfg = GCPSecretManagerConfiguration("fake_id", cache_expiration=0) assert cfg.foo == "foo_val" with raises(KeyError): assert cfg.foo_missing is KeyError
def test_deletion(): # type: ignore d = DICT.copy() secretmanager.SecretManagerServiceClient = fake_client(d) cfg = GCPSecretManagerConfiguration("fake_id", cache_expiration=0) 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 d = DICT.copy() secretmanager.SecretManagerServiceClient = fake_client(d) cfg = GCPSecretManagerConfiguration("fake_id", cache_expiration=0) # str assert ( str(cfg) == "{'bar': 'bar_val', 'foo': 'foo_val', 'password': '******', 'with_underscore': 'works'}" ) assert cfg["password"] == "some passwd"
def test_repr(): # type: ignore d = DICT.copy() secretmanager.SecretManagerServiceClient = fake_client(d) cfg = GCPSecretManagerConfiguration("fake_id", cache_expiration=0) assert repr(cfg) == "<GCPSecretManagerConfiguration: 'fake_id'>"