def test_set_iam_policy(self):
    import operator
    from google.cloud.storage.iam import STORAGE_OWNER_ROLE
    from google.cloud.storage.iam import STORAGE_EDITOR_ROLE
    from google.cloud.storage.iam import STORAGE_VIEWER_ROLE
    from google.cloud.iam import Policy
    BLOB_NAME = 'blob-name'
    PATH = ('/b/name/o/%s' % (BLOB_NAME,))
    ETAG = 'DEADBEEF'
    VERSION = 17
    OWNER1 = 'user:example@example.com'
    OWNER2 = 'group:example@example.com'
    EDITOR1 = 'domain:google.com'
    EDITOR2 = 'user:example@example.com'
    VIEWER1 = 'serviceAccount:example@example.com'
    VIEWER2 = 'user:example@example.com'
    BINDINGS = [{'role': STORAGE_OWNER_ROLE, 'members': [OWNER1, OWNER2]}, {'role': STORAGE_EDITOR_ROLE, 'members': [EDITOR1, EDITOR2]}, {'role': STORAGE_VIEWER_ROLE, 'members': [VIEWER1, VIEWER2]}]
    RETURNED = {'etag': ETAG, 'version': VERSION, 'bindings': BINDINGS}
    after = ({'status': http_client.OK}, RETURNED)
    policy = Policy()
    for binding in BINDINGS:
        policy[binding['role']] = binding['members']
    connection = _Connection(after)
    client = _Client(connection)
    bucket = _Bucket(client=client)
    blob = self._make_one(BLOB_NAME, bucket=bucket)
    returned = blob.set_iam_policy(policy)
    self.assertEqual(returned.etag, ETAG)
    self.assertEqual(returned.version, VERSION)
    self.assertEqual(dict(returned), dict(policy))
    kw = connection._requested
    self.assertEqual(len(kw), 1)
    self.assertEqual(kw[0]['method'], 'PUT')
    self.assertEqual(kw[0]['path'], ('%s/iam' % (PATH,)))
    sent = kw[0]['data']
    self.assertEqual(sent['resourceId'], PATH)
    self.assertEqual(len(sent['bindings']), len(BINDINGS))
    key = operator.itemgetter('role')
    for (found, expected) in zip(sorted(sent['bindings'], key=key), sorted(BINDINGS, key=key)):
        self.assertEqual(found['role'], expected['role'])
        self.assertEqual(sorted(found['members']), sorted(expected['members']))
def test_set_iam_policy(self):
    import operator
    from google.cloud.storage.iam import STORAGE_OWNER_ROLE
    from google.cloud.storage.iam import STORAGE_EDITOR_ROLE
    from google.cloud.storage.iam import STORAGE_VIEWER_ROLE
    from google.cloud.iam import Policy
    BLOB_NAME = 'blob-name'
    PATH = ('/b/name/o/%s' % (BLOB_NAME,))
    ETAG = 'DEADBEEF'
    VERSION = 17
    OWNER1 = 'user:example@example.com'
    OWNER2 = 'group:example@example.com'
    EDITOR1 = 'domain:google.com'
    EDITOR2 = 'user:example@example.com'
    VIEWER1 = 'serviceAccount:example@example.com'
    VIEWER2 = 'user:example@example.com'
    BINDINGS = [{'role': STORAGE_OWNER_ROLE, 'members': [OWNER1, OWNER2]}, {'role': STORAGE_EDITOR_ROLE, 'members': [EDITOR1, EDITOR2]}, {'role': STORAGE_VIEWER_ROLE, 'members': [VIEWER1, VIEWER2]}]
    RETURNED = {'etag': ETAG, 'version': VERSION, 'bindings': BINDINGS}
    after = ({'status': http_client.OK}, RETURNED)
    policy = Policy()
    for binding in BINDINGS:
        policy[binding['role']] = binding['members']
    connection = _Connection(after)
    client = _Client(connection)
    bucket = _Bucket(client=client)
    blob = self._make_one(BLOB_NAME, bucket=bucket)
    returned = blob.set_iam_policy(policy)
    self.assertEqual(returned.etag, ETAG)
    self.assertEqual(returned.version, VERSION)
    self.assertEqual(dict(returned), dict(policy))
    kw = connection._requested
    self.assertEqual(len(kw), 1)
    self.assertEqual(kw[0]['method'], 'PUT')
    self.assertEqual(kw[0]['path'], ('%s/iam' % (PATH,)))
    sent = kw[0]['data']
    self.assertEqual(sent['resourceId'], PATH)
    self.assertEqual(len(sent['bindings']), len(BINDINGS))
    key = operator.itemgetter('role')
    for (found, expected) in zip(sorted(sent['bindings'], key=key), sorted(BINDINGS, key=key)):
        self.assertEqual(found['role'], expected['role'])
        self.assertEqual(sorted(found['members']), sorted(expected['members']))