@@ -866,6 +866,135 @@ def test_disable_website(self):
866866 bucket .disable_website ()
867867 self .assertEqual (bucket ._properties , UNSET )
868868
869+ def test_get_iam_policy (self ):
870+ from google .cloud .storage .iam import STORAGE_OWNER_ROLE
871+ from google .cloud .storage .iam import STORAGE_EDITOR_ROLE
872+ from google .cloud .storage .iam import STORAGE_VIEWER_ROLE
873+ from google .cloud .iam import Policy
874+
875+ NAME = 'name'
876+ PATH = '/b/%s' % (NAME ,)
877+ ETAG = 'DEADBEEF'
878+ VERSION = 17
879+ OWNER1 = 'user:phred@example.com'
880+ OWNER2 = 'group:cloud-logs@google.com'
881+ EDITOR1 = 'domain:google.com'
882+ EDITOR2 = 'user:phred@example.com'
883+ VIEWER1 = 'serviceAccount:1234-abcdef@service.example.com'
884+ VIEWER2 = 'user:phred@example.com'
885+ RETURNED = {
886+ 'resourceId' : PATH ,
887+ 'etag' : ETAG ,
888+ 'version' : VERSION ,
889+ 'bindings' : [
890+ {'role' : STORAGE_OWNER_ROLE , 'members' : [OWNER1 , OWNER2 ]},
891+ {'role' : STORAGE_EDITOR_ROLE , 'members' : [EDITOR1 , EDITOR2 ]},
892+ {'role' : STORAGE_VIEWER_ROLE , 'members' : [VIEWER1 , VIEWER2 ]},
893+ ],
894+ }
895+ EXPECTED = {
896+ binding ['role' ]: set (binding ['members' ])
897+ for binding in RETURNED ['bindings' ]}
898+ connection = _Connection (RETURNED )
899+ client = _Client (connection , None )
900+ bucket = self ._make_one (client = client , name = NAME )
901+
902+ policy = bucket .get_iam_policy ()
903+
904+ self .assertIsInstance (policy , Policy )
905+ self .assertEqual (policy .etag , RETURNED ['etag' ])
906+ self .assertEqual (policy .version , RETURNED ['version' ])
907+ self .assertEqual (dict (policy ), EXPECTED )
908+
909+ kw = connection ._requested
910+ self .assertEqual (len (kw ), 1 )
911+ self .assertEqual (kw [0 ]['method' ], 'GET' )
912+ self .assertEqual (kw [0 ]['path' ], '%s/iam' % (PATH ,))
913+
914+ def test_set_iam_policy (self ):
915+ import operator
916+ from google .cloud .storage .iam import STORAGE_OWNER_ROLE
917+ from google .cloud .storage .iam import STORAGE_EDITOR_ROLE
918+ from google .cloud .storage .iam import STORAGE_VIEWER_ROLE
919+ from google .cloud .iam import Policy
920+
921+ NAME = 'name'
922+ PATH = '/b/%s' % (NAME ,)
923+ ETAG = 'DEADBEEF'
924+ VERSION = 17
925+ OWNER1 = 'user:phred@example.com'
926+ OWNER2 = 'group:cloud-logs@google.com'
927+ EDITOR1 = 'domain:google.com'
928+ EDITOR2 = 'user:phred@example.com'
929+ VIEWER1 = 'serviceAccount:1234-abcdef@service.example.com'
930+ VIEWER2 = 'user:phred@example.com'
931+ BINDINGS = [
932+ {'role' : STORAGE_OWNER_ROLE , 'members' : [OWNER1 , OWNER2 ]},
933+ {'role' : STORAGE_EDITOR_ROLE , 'members' : [EDITOR1 , EDITOR2 ]},
934+ {'role' : STORAGE_VIEWER_ROLE , 'members' : [VIEWER1 , VIEWER2 ]},
935+ ]
936+ RETURNED = {
937+ 'etag' : ETAG ,
938+ 'version' : VERSION ,
939+ 'bindings' : BINDINGS ,
940+ }
941+ policy = Policy ()
942+ for binding in BINDINGS :
943+ policy [binding ['role' ]] = binding ['members' ]
944+
945+ connection = _Connection (RETURNED )
946+ client = _Client (connection , None )
947+ bucket = self ._make_one (client = client , name = NAME )
948+
949+ returned = bucket .set_iam_policy (policy )
950+
951+ self .assertEqual (returned .etag , ETAG )
952+ self .assertEqual (returned .version , VERSION )
953+ self .assertEqual (dict (returned ), dict (policy ))
954+
955+ kw = connection ._requested
956+ self .assertEqual (len (kw ), 1 )
957+ self .assertEqual (kw [0 ]['method' ], 'PUT' )
958+ self .assertEqual (kw [0 ]['path' ], '%s/iam' % (PATH ,))
959+ sent = kw [0 ]['data' ]
960+ self .assertEqual (sent ['resourceId' ], PATH )
961+ self .assertEqual (len (sent ['bindings' ]), len (BINDINGS ))
962+ key = operator .itemgetter ('role' )
963+ for found , expected in zip (
964+ sorted (sent ['bindings' ], key = key ),
965+ sorted (BINDINGS , key = key )):
966+ self .assertEqual (found ['role' ], expected ['role' ])
967+ self .assertEqual (
968+ sorted (found ['members' ]), sorted (expected ['members' ]))
969+
970+ def test_test_iam_permissions (self ):
971+ from google .cloud .storage .iam import STORAGE_OBJECTS_LIST
972+ from google .cloud .storage .iam import STORAGE_BUCKETS_GET
973+ from google .cloud .storage .iam import STORAGE_BUCKETS_UPDATE
974+
975+ NAME = 'name'
976+ PATH = '/b/%s' % (NAME ,)
977+ PERMISSIONS = [
978+ STORAGE_OBJECTS_LIST ,
979+ STORAGE_BUCKETS_GET ,
980+ STORAGE_BUCKETS_UPDATE ,
981+ ]
982+ ALLOWED = PERMISSIONS [1 :]
983+ RETURNED = {'permissions' : ALLOWED }
984+ connection = _Connection (RETURNED )
985+ client = _Client (connection , None )
986+ bucket = self ._make_one (client = client , name = NAME )
987+
988+ allowed = bucket .test_iam_permissions (PERMISSIONS )
989+
990+ self .assertEqual (allowed , ALLOWED )
991+
992+ kw = connection ._requested
993+ self .assertEqual (len (kw ), 1 )
994+ self .assertEqual (kw [0 ]['method' ], 'GET' )
995+ self .assertEqual (kw [0 ]['path' ], '%s/iam/testPermissions' % (PATH ,))
996+ self .assertEqual (kw [0 ]['query_params' ], {'permissions' : PERMISSIONS })
997+
869998 def test_make_public_defaults (self ):
870999 from google .cloud .storage .acl import _ACLEntity
8711000
0 commit comments