@@ -34,17 +34,18 @@ class IamCommandTest extends TestCase
3434 protected static $ storage ;
3535 protected static $ user ;
3636 protected static $ bucket ;
37+ private static $ role = 'roles/storage.objectViewer ' ;
3738 private static $ commandFile = __DIR__ . '/../storage.php ' ;
3839
3940 public static function setUpBeforeClass ()
4041 {
4142 self ::$ storage = new StorageClient ();
4243 self ::$ user = self ::requireEnv ('GOOGLE_IAM_USER ' );
4344 self ::$ bucket = self ::requireEnv ('GOOGLE_STORAGE_BUCKET ' );
44- self ::cleanUpIam ();
45+ self ::setUpIam ();
4546 }
4647
47- private static function cleanUpIam ()
48+ private static function setUpIam ()
4849 {
4950 $ bucket = self ::$ storage ->bucket (self ::$ bucket );
5051
@@ -59,11 +60,10 @@ private static function cleanUpIam()
5960 $ iam = $ bucket ->iam ();
6061
6162 $ policy = $ iam ->policy (['requestedPolicyVersion ' => 3 ]);
62- $ roles = ['roles/storage.objectViewer ' , 'roles/storage.objectCreator ' ];
6363
6464 foreach ($ policy ['bindings ' ] as $ i => $ binding ) {
6565 if (
66- in_array ( $ binding ['role ' ], $ roles ) &&
66+ $ binding ['role ' ] == self :: $ role &&
6767 in_array (self ::$ user , $ binding ['members ' ])
6868 ) {
6969 unset($ policy ['bindings ' ][$ i ]);
@@ -75,16 +75,15 @@ private static function cleanUpIam()
7575
7676 public function testAddBucketIamMember ()
7777 {
78- $ role = 'roles/storage.objectViewer ' ;
7978 $ output = $ this ->runCommand ('iam ' , [
8079 'bucket ' => self ::$ bucket ,
81- '--role ' => $ role ,
80+ '--role ' => self :: $ role ,
8281 '--add-member ' => [self ::$ user ],
8382 ]);
8483
8584 $ outputString = sprintf (
8685 'Added the following member(s) to role %s for bucket %s
87- %s ' , $ role , self ::$ bucket , self ::$ user );
86+ %s ' , self :: $ role , self ::$ bucket , self ::$ user );
8887
8988 $ this ->assertStringContainsString ($ outputString , $ output );
9089
@@ -93,7 +92,7 @@ public function testAddBucketIamMember()
9392 'requestedPolicyVersion ' => 3
9493 ]);
9594 foreach ($ policy ['bindings ' ] as $ binding ) {
96- if ($ binding ['role ' ] == $ role ) {
95+ if ($ binding ['role ' ] == self :: $ role ) {
9796 $ foundRoleMember = in_array (self ::$ user , $ binding ['members ' ]);
9897 break ;
9998 }
@@ -103,14 +102,13 @@ public function testAddBucketIamMember()
103102
104103 public function testAddBucketConditionalIamBinding ()
105104 {
106- $ role = 'roles/storage.objectCreator ' ;
107105 $ title = 'always true ' ;
108106 $ description = 'this condition is always true ' ;
109107 $ expression = '1 < 2 ' ;
110108
111109 $ output = $ this ->runCommand ('iam ' , [
112110 'bucket ' => self ::$ bucket ,
113- '--role ' => $ role ,
111+ '--role ' => self :: $ role ,
114112 '--add-member ' => [self ::$ user ],
115113 '--title ' => $ title ,
116114 '--description ' => $ description ,
@@ -124,7 +122,7 @@ public function testAddBucketConditionalIamBinding()
124122 Title: %s
125123 Description: %s
126124 Expression: %s
127- ' , $ role , self ::$ bucket , self ::$ user , $ title , $ description , $ expression );
125+ ' , self :: $ role , self ::$ bucket , self ::$ user , $ title , $ description , $ expression );
128126
129127 $ this ->assertEquals ($ outputString , $ output );
130128
@@ -133,14 +131,16 @@ public function testAddBucketConditionalIamBinding()
133131 'requestedPolicyVersion ' => 3
134132 ]);
135133 foreach ($ policy ['bindings ' ] as $ binding ) {
136- if ($ binding ['role ' ] == $ role ) {
137- $ foundBinding =
138- in_array (self ::$ user , $ binding ['members ' ]) &&
134+ if ($ binding ['role ' ] == self ::$ role ) {
135+ if (in_array (self ::$ user , $ binding ['members ' ]) &&
139136 isset ($ binding ['condition ' ]) &&
140137 $ binding ['condition ' ]['title ' ] == $ title &&
141138 $ binding ['condition ' ]['description ' ] == $ description &&
142- $ binding ['condition ' ]['expression ' ] == $ expression ;
143- break ;
139+ $ binding ['condition ' ]['expression ' ] == $ expression
140+ ) {
141+ $ foundBinding = true ;
142+ break ;
143+ }
144144 }
145145 }
146146 $ this ->assertTrue ($ foundBinding );
@@ -168,7 +168,7 @@ public function testListIamMembers()
168168 $ this ->assertRegexp ($ binding , $ output );
169169
170170 $ bindingWithCondition = sprintf (
171- 'Role: roles/storage.objectCreator
171+ 'Role: roles/storage.objectViewer
172172Members:
173173 %s
174174 with condition:
@@ -186,28 +186,30 @@ public function testListIamMembers()
186186 */
187187 public function testRemoveBucketIamMember ()
188188 {
189- $ role = 'roles/storage.objectViewer ' ;
190189 $ output = $ this ->runCommand ('iam ' , [
191190 'bucket ' => self ::$ bucket ,
192- '--role ' => ' roles/storage.objectViewer ' ,
191+ '--role ' => self :: $ role ,
193192 '--remove-member ' => self ::$ user ,
194193 ]);
195194
196- $ outputString = sprintf (
195+ $ expected = sprintf (
197196 'User %s removed from role %s for bucket %s ' ,
198197 self ::$ user ,
199- $ role ,
198+ self :: $ role ,
200199 self ::$ bucket
201200 );
202201
203- $ this ->assertStringContainsString ($ outputString , $ output );
202+ $ this ->assertStringContainsString ($ expected , $ output );
204203
205204 $ foundRoleMember = false ;
206205 $ policy = self ::$ storage ->bucket (self ::$ bucket )->iam ()->policy ([
207206 'requestedPolicyVersion ' => 3
208207 ]);
209208 foreach ($ policy ['bindings ' ] as $ binding ) {
210- if ($ binding ['role ' ] == $ role ) {
209+ if (
210+ $ binding ['role ' ] == self ::$ role
211+ && empty ($ binding ['condition ' ])
212+ ) {
211213 $ foundRoleMember = in_array (self ::$ user , $ binding ['members ' ]);
212214 break ;
213215 }
@@ -221,29 +223,32 @@ public function testRemoveBucketIamMember()
221223 */
222224 public function testRemoveBucketConditionalIamBinding ()
223225 {
224- $ role = 'roles/storage.objectViewer ' ;
225226 $ title = 'always true ' ;
226227 $ description = 'this condition is always true ' ;
227228 $ expression = '1 < 2 ' ;
228229 $ output = $ this ->runCommand ('iam ' , [
229230 'bucket ' => self ::$ bucket ,
230- '--role ' => ' roles/storage.objectViewer ' ,
231+ '--role ' => self :: $ role ,
231232 '--remove-binding ' => true ,
232233 '--title ' => $ title ,
233234 '--description ' => $ description ,
234235 '--expression ' => $ expression
235236 ]);
236237
237- $ outputString = sprintf ('Conditional Binding was removed. ' );
238-
239- $ this ->assertStringContainsString ($ outputString , $ output );
238+ $ this ->assertStringContainsString (
239+ 'Conditional Binding was removed. ' ,
240+ $ output
241+ );
240242
241243 $ foundBinding = false ;
242244 $ policy = self ::$ storage ->bucket (self ::$ bucket )->iam ()->policy ([
243245 'requestedPolicyVersion ' => 3
244246 ]);
245247 foreach ($ policy ['bindings ' ] as $ binding ) {
246- if ($ binding ['role ' ] == $ role && $ binding ['condition ' ] != null ) {
248+ if (
249+ $ binding ['role ' ] == self ::$ role
250+ && isset ($ binding ['condition ' ])
251+ ) {
247252 $ condition = $ binding ['condition ' ];
248253 if ($ condition ['title ' ] == $ title
249254 && $ condition ['description ' ] == $ description
0 commit comments