Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: save empty IAM policy bindings #155

Merged
merged 2 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion google/api_core/iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ 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
Original file line number Diff line number Diff line change
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) == {}
Comment on lines +35 to +36
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accessing policy.owners, policy.editors results in __getitem__ being called, which adds empty entries to the dictionary. I moved these empty dict asserts up as a result.

An alternative is to keep them in their previous location and check that the three roles with empty bindings were added

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