Skip to content

Commit

Permalink
fix: save empty IAM policy bindings (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
busunkim96 committed Mar 23, 2021
1 parent b4860fe commit 536c2ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
5 changes: 4 additions & 1 deletion google/api_core/iam.py
Expand Up @@ -136,7 +136,10 @@ def __getitem__(self, key):
for b in self._bindings:
if b["role"] == key:
return b["members"]
return set()
# binding does not yet exist, create one
new_binding = {"role": key, "members": set()}
self._bindings.append(new_binding)
return new_binding["members"]

def __setitem__(self, key, value):
self.__check_version__()
Expand Down
18 changes: 13 additions & 5 deletions tests/unit/test_iam.py
Expand Up @@ -32,11 +32,11 @@ def test_ctor_defaults(self):
policy = self._make_one()
assert policy.etag is None
assert policy.version is None
assert len(policy) == 0
assert dict(policy) == {}
assert policy.owners == empty
assert policy.editors == empty
assert policy.viewers == empty
assert len(policy) == 0
assert dict(policy) == {}

def test_ctor_explicit(self):
VERSION = 1
Expand All @@ -45,16 +45,24 @@ def test_ctor_explicit(self):
policy = self._make_one(ETAG, VERSION)
assert policy.etag == ETAG
assert policy.version == VERSION
assert len(policy) == 0
assert dict(policy) == {}
assert policy.owners == empty
assert policy.editors == empty
assert policy.viewers == empty
assert len(policy) == 0
assert dict(policy) == {}

def test___getitem___miss(self):
policy = self._make_one()
assert policy["nonesuch"] == set()

def test__getitem___and_set(self):
from google.api_core.iam import OWNER_ROLE
policy = self._make_one()

# get the policy using the getter and then modify it
policy[OWNER_ROLE].add("user:phred@example.com")
assert dict(policy) == {OWNER_ROLE: {"user:phred@example.com"}}

def test___getitem___version3(self):
policy = self._make_one("DEADBEEF", 3)
with pytest.raises(InvalidOperationException, match=_DICT_ACCESS_MSG):
Expand Down Expand Up @@ -293,10 +301,10 @@ def test_from_api_repr_only_etag(self):
policy = klass.from_api_repr(RESOURCE)
assert policy.etag == "ACAB"
assert policy.version is None
assert dict(policy) == {}
assert policy.owners == empty
assert policy.editors == empty
assert policy.viewers == empty
assert dict(policy) == {}

def test_from_api_repr_complete(self):
from google.api_core.iam import OWNER_ROLE, EDITOR_ROLE, VIEWER_ROLE
Expand Down

0 comments on commit 536c2ca

Please sign in to comment.