2121import static org .junit .Assert .assertNull ;
2222import static org .junit .Assert .assertTrue ;
2323
24+ import com .google .cloud .Identity ;
25+ import com .google .cloud .Policy ;
2426import com .google .cloud .storage .Acl ;
2527import com .google .cloud .storage .Acl .Role ;
2628import com .google .cloud .storage .Blob ;
2729import com .google .cloud .storage .Bucket ;
2830import com .google .cloud .storage .BucketInfo ;
2931import com .google .cloud .storage .Storage ;
3032import com .google .cloud .storage .StorageException ;
33+ import com .google .cloud .storage .StorageRoles ;
3134import com .google .cloud .storage .testing .RemoteStorageHelper ;
3235import com .google .common .collect .Sets ;
3336
@@ -49,13 +52,15 @@ public class ITBucketSnippets {
4952
5053 private static final Logger log = Logger .getLogger (ITBucketSnippets .class .getName ());
5154 private static final String BUCKET = RemoteStorageHelper .generateBucketName ();
55+ private static final String USER_EMAIL =
"[email protected] " ;
5256 private static final String BLOB1 = "blob1" ;
5357 private static final String BLOB2 = "blob2" ;
5458 private static final String BLOB3 = "blob3" ;
5559 private static final String BLOB4 = "blob4" ;
5660
5761 private static Storage storage ;
5862 private static BucketSnippets bucketSnippets ;
63+ private static BucketIamSnippets bucketIamSnippets ;
5964
6065 @ Rule
6166 public ExpectedException thrown = ExpectedException .none ();
@@ -68,6 +73,7 @@ public static void beforeClass() {
6873 RemoteStorageHelper helper = RemoteStorageHelper .create ();
6974 storage = helper .getOptions ().getService ();
7075 bucketSnippets = new BucketSnippets (storage .create (BucketInfo .of (BUCKET )));
76+ bucketIamSnippets = new BucketIamSnippets (storage );
7177 }
7278
7379 @ AfterClass
@@ -133,4 +139,50 @@ public void testBucket() throws InterruptedException {
133139 thrown .expect (StorageException .class );
134140 assertTrue (bucketSnippets .delete ());
135141 }
142+
143+ @ Test
144+ public void testListBucketIamMembers () {
145+ // Test an added Bucket-level IAM member is listed
146+ Policy policy = storage .getIamPolicy (BUCKET );
147+ storage .setIamPolicy (BUCKET , policy .toBuilder ().removeRole (StorageRoles .admin ()).build ());
148+ policy = storage .getIamPolicy (BUCKET );
149+ assertNull (policy .getBindings ().get (StorageRoles .admin ()));
150+ storage .setIamPolicy (BUCKET , policy .toBuilder ().addIdentity (StorageRoles .admin (),
151+ Identity .user (USER_EMAIL )).build ());
152+ policy = storage .getIamPolicy (BUCKET );
153+ assertTrue (policy .getBindings ().get (StorageRoles .admin ()).contains (Identity .user (USER_EMAIL )));
154+ Policy snippetPolicy = bucketIamSnippets .listBucketIamMembers (BUCKET );
155+ assertTrue (snippetPolicy .getBindings ().get (StorageRoles .admin ()).
156+ contains (Identity .user (USER_EMAIL )));
157+ }
158+
159+ @ Test
160+ public void testAddBucketIamMemeber () {
161+ // Test a member is added to Bucket-level IAM
162+ Policy policy = storage .getIamPolicy (BUCKET );
163+ storage .setIamPolicy (BUCKET , policy .toBuilder ().removeRole (StorageRoles .admin ()).build ());
164+ policy = storage .getIamPolicy (BUCKET );
165+ assertNull (policy .getBindings ().get (StorageRoles .admin ()));
166+ bucketIamSnippets .addBucketIamMember (BUCKET , StorageRoles .admin (), Identity .user (USER_EMAIL ));
167+ policy = storage .getIamPolicy (BUCKET );
168+ assertTrue (policy .getBindings ().get (StorageRoles .admin ()).contains (Identity .user (USER_EMAIL )));
169+ }
170+
171+ @ Test
172+ public void testRemoveBucketIamMember () {
173+ // Test a member is removed from Bucket-level IAM
174+ Policy policy = storage .getIamPolicy (BUCKET );
175+ storage .setIamPolicy (BUCKET , policy .toBuilder ().removeRole (StorageRoles .admin ()).build ());
176+ policy = storage .getIamPolicy (BUCKET );
177+ assertNull (policy .getBindings ().get (StorageRoles .admin ()));
178+ policy = policy .toBuilder ().addIdentity (StorageRoles .admin (),
179+ Identity .user (USER_EMAIL )).build ();
180+ storage .setIamPolicy (BUCKET , policy );
181+ policy = storage .getIamPolicy (BUCKET );
182+ assertTrue (policy .getBindings ().get (StorageRoles .admin ()).contains (Identity .user (USER_EMAIL )));
183+ bucketIamSnippets .removeBucketIamMember (BUCKET , StorageRoles .admin (),
184+ Identity .user (USER_EMAIL ));
185+ policy = storage .getIamPolicy (BUCKET );
186+ assertNull (policy .getBindings ().get (StorageRoles .admin ()));
187+ }
136188}
0 commit comments