@@ -1173,6 +1173,144 @@ def test_create_resumable_upload_session_args(self):
1173
1173
self .assertEqual (
1174
1174
headers ['Origin' ], ORIGIN )
1175
1175
1176
+ def test_get_iam_policy (self ):
1177
+ from six .moves .http_client import OK
1178
+ from google .cloud .storage .iam import STORAGE_OWNER_ROLE
1179
+ from google .cloud .storage .iam import STORAGE_EDITOR_ROLE
1180
+ from google .cloud .storage .iam import STORAGE_VIEWER_ROLE
1181
+ from google .cloud .iam import Policy
1182
+
1183
+ BLOB_NAME = 'blob-name'
1184
+ PATH = '/b/name/o/%s' % (BLOB_NAME ,)
1185
+ ETAG = 'DEADBEEF'
1186
+ VERSION = 17
1187
+ OWNER1 = 'user:phred@example.com'
1188
+ OWNER2 = 'group:cloud-logs@google.com'
1189
+ EDITOR1 = 'domain:google.com'
1190
+ EDITOR2 = 'user:phred@example.com'
1191
+ VIEWER1 = 'serviceAccount:1234-abcdef@service.example.com'
1192
+ VIEWER2 = 'user:phred@example.com'
1193
+ RETURNED = {
1194
+ 'resourceId' : PATH ,
1195
+ 'etag' : ETAG ,
1196
+ 'version' : VERSION ,
1197
+ 'bindings' : [
1198
+ {'role' : STORAGE_OWNER_ROLE , 'members' : [OWNER1 , OWNER2 ]},
1199
+ {'role' : STORAGE_EDITOR_ROLE , 'members' : [EDITOR1 , EDITOR2 ]},
1200
+ {'role' : STORAGE_VIEWER_ROLE , 'members' : [VIEWER1 , VIEWER2 ]},
1201
+ ],
1202
+ }
1203
+ after = ({'status' : OK }, RETURNED )
1204
+ EXPECTED = {
1205
+ binding ['role' ]: set (binding ['members' ])
1206
+ for binding in RETURNED ['bindings' ]}
1207
+ connection = _Connection (after )
1208
+ client = _Client (connection )
1209
+ bucket = _Bucket (client = client )
1210
+ blob = self ._make_one (BLOB_NAME , bucket = bucket )
1211
+
1212
+ policy = blob .get_iam_policy ()
1213
+
1214
+ self .assertIsInstance (policy , Policy )
1215
+ self .assertEqual (policy .etag , RETURNED ['etag' ])
1216
+ self .assertEqual (policy .version , RETURNED ['version' ])
1217
+ self .assertEqual (dict (policy ), EXPECTED )
1218
+
1219
+ kw = connection ._requested
1220
+ self .assertEqual (len (kw ), 1 )
1221
+ self .assertEqual (kw [0 ]['method' ], 'GET' )
1222
+ self .assertEqual (kw [0 ]['path' ], '%s/iam' % (PATH ,))
1223
+
1224
+ def test_set_iam_policy (self ):
1225
+ import operator
1226
+ from six .moves .http_client import OK
1227
+ from google .cloud .storage .iam import STORAGE_OWNER_ROLE
1228
+ from google .cloud .storage .iam import STORAGE_EDITOR_ROLE
1229
+ from google .cloud .storage .iam import STORAGE_VIEWER_ROLE
1230
+ from google .cloud .iam import Policy
1231
+
1232
+ BLOB_NAME = 'blob-name'
1233
+ PATH = '/b/name/o/%s' % (BLOB_NAME ,)
1234
+ ETAG = 'DEADBEEF'
1235
+ VERSION = 17
1236
+ OWNER1 = 'user:phred@example.com'
1237
+ OWNER2 = 'group:cloud-logs@google.com'
1238
+ EDITOR1 = 'domain:google.com'
1239
+ EDITOR2 = 'user:phred@example.com'
1240
+ VIEWER1 = 'serviceAccount:1234-abcdef@service.example.com'
1241
+ VIEWER2 = 'user:phred@example.com'
1242
+ BINDINGS = [
1243
+ {'role' : STORAGE_OWNER_ROLE , 'members' : [OWNER1 , OWNER2 ]},
1244
+ {'role' : STORAGE_EDITOR_ROLE , 'members' : [EDITOR1 , EDITOR2 ]},
1245
+ {'role' : STORAGE_VIEWER_ROLE , 'members' : [VIEWER1 , VIEWER2 ]},
1246
+ ]
1247
+ RETURNED = {
1248
+ 'etag' : ETAG ,
1249
+ 'version' : VERSION ,
1250
+ 'bindings' : BINDINGS ,
1251
+ }
1252
+ after = ({'status' : OK }, RETURNED )
1253
+ policy = Policy ()
1254
+ for binding in BINDINGS :
1255
+ policy [binding ['role' ]] = binding ['members' ]
1256
+
1257
+ connection = _Connection (after )
1258
+ client = _Client (connection )
1259
+ bucket = _Bucket (client = client )
1260
+ blob = self ._make_one (BLOB_NAME , bucket = bucket )
1261
+
1262
+ returned = blob .set_iam_policy (policy )
1263
+
1264
+ self .assertEqual (returned .etag , ETAG )
1265
+ self .assertEqual (returned .version , VERSION )
1266
+ self .assertEqual (dict (returned ), dict (policy ))
1267
+
1268
+ kw = connection ._requested
1269
+ self .assertEqual (len (kw ), 1 )
1270
+ self .assertEqual (kw [0 ]['method' ], 'PUT' )
1271
+ self .assertEqual (kw [0 ]['path' ], '%s/iam' % (PATH ,))
1272
+ sent = kw [0 ]['data' ]
1273
+ self .assertEqual (sent ['resourceId' ], PATH )
1274
+ self .assertEqual (len (sent ['bindings' ]), len (BINDINGS ))
1275
+ key = operator .itemgetter ('role' )
1276
+ for found , expected in zip (
1277
+ sorted (sent ['bindings' ], key = key ),
1278
+ sorted (BINDINGS , key = key )):
1279
+ self .assertEqual (found ['role' ], expected ['role' ])
1280
+ self .assertEqual (
1281
+ sorted (found ['members' ]), sorted (expected ['members' ]))
1282
+
1283
+ def test_test_iam_permissions (self ):
1284
+ from six .moves .http_client import OK
1285
+ from google .cloud .storage .iam import STORAGE_OBJECTS_LIST
1286
+ from google .cloud .storage .iam import STORAGE_BUCKETS_GET
1287
+ from google .cloud .storage .iam import STORAGE_BUCKETS_UPDATE
1288
+
1289
+ BLOB_NAME = 'blob-name'
1290
+ PATH = '/b/name/o/%s' % (BLOB_NAME ,)
1291
+ PERMISSIONS = [
1292
+ STORAGE_OBJECTS_LIST ,
1293
+ STORAGE_BUCKETS_GET ,
1294
+ STORAGE_BUCKETS_UPDATE ,
1295
+ ]
1296
+ ALLOWED = PERMISSIONS [1 :]
1297
+ RETURNED = {'permissions' : ALLOWED }
1298
+ after = ({'status' : OK }, RETURNED )
1299
+ connection = _Connection (after )
1300
+ client = _Client (connection )
1301
+ bucket = _Bucket (client = client )
1302
+ blob = self ._make_one (BLOB_NAME , bucket = bucket )
1303
+
1304
+ allowed = blob .test_iam_permissions (PERMISSIONS )
1305
+
1306
+ self .assertEqual (allowed , ALLOWED )
1307
+
1308
+ kw = connection ._requested
1309
+ self .assertEqual (len (kw ), 1 )
1310
+ self .assertEqual (kw [0 ]['method' ], 'GET' )
1311
+ self .assertEqual (kw [0 ]['path' ], '%s/iam/testPermissions' % (PATH ,))
1312
+ self .assertEqual (kw [0 ]['query_params' ], {'permissions' : PERMISSIONS })
1313
+
1176
1314
def test_make_public (self ):
1177
1315
from six .moves .http_client import OK
1178
1316
from google .cloud .storage .acl import _ACLEntity
0 commit comments