33
33
import com .google .cloud .RetryHelper .RetryHelperException ;
34
34
import com .google .cloud .http .BaseHttpServiceException ;
35
35
import com .google .cloud .storage .Acl ;
36
+ import com .google .cloud .storage .Acl .Entity ;
37
+ import com .google .cloud .storage .Acl .Project .ProjectRole ;
36
38
import com .google .cloud .storage .Acl .Role ;
37
39
import com .google .cloud .storage .Acl .User ;
38
40
import com .google .cloud .storage .Blob ;
70
72
import java .util .Map ;
71
73
import java .util .Set ;
72
74
import java .util .concurrent .Callable ;
75
+ import java .util .function .Predicate ;
73
76
import java .util .stream .Collector ;
74
77
import java .util .stream .Collectors ;
75
78
import org .junit .Ignore ;
@@ -207,6 +210,7 @@ public void bucket_defaultAcl_create() throws Exception {
207
210
List <Acl > expectedAcls = dropEtags (bucket .getDefaultAcl ());
208
211
List <Acl > actualAcls = dropEtags (bucketUpdated .getDefaultAcl ());
209
212
assertThat (actualAcls ).containsAtLeastElementsIn (expectedAcls );
213
+ assertThat (actualAcls ).contains (readAll );
210
214
}
211
215
}
212
216
@@ -230,12 +234,47 @@ public void bucket_defaultAcl_update() throws Exception {
230
234
TemporaryBucket .newBuilder ().setBucketInfo (bucketInfo ).setStorage (storage ).build ()) {
231
235
BucketInfo bucket = tempB .getBucket ();
232
236
233
- Acl readAll = Acl .of (User .ofAllAuthenticatedUsers (), Role .READER );
234
- Acl actual = retry429s (() -> storage .updateDefaultAcl (bucket .getName (), readAll ), storage );
237
+ List <Acl > defaultAcls = bucket .getDefaultAcl ();
238
+ System .out .println ("defaultAcls = " + defaultAcls );
239
+ assertThat (defaultAcls ).isNotEmpty ();
235
240
236
- assertThat (actual .getEntity ()).isEqualTo (readAll .getEntity ());
237
- assertThat (actual .getRole ()).isEqualTo (readAll .getRole ());
241
+ Predicate <Acl > isProjectEditor = hasProjectRole (ProjectRole .EDITORS );
242
+
243
+ //noinspection OptionalGetWithoutIsPresent
244
+ Acl projectEditorAsOwner =
245
+ defaultAcls .stream ().filter (hasRole (Role .OWNER ).and (isProjectEditor )).findFirst ().get ();
246
+ System .out .println ("projectEditorAsOwner = " + projectEditorAsOwner );
247
+
248
+ // lower the privileges of project editors to writer from owner
249
+ Entity entity = projectEditorAsOwner .getEntity ();
250
+ System .out .println ("entity = " + entity );
251
+ Acl projectEditorAsReader = Acl .of (entity , Role .READER );
252
+ System .out .println ("projectEditorAsReader = " + projectEditorAsReader );
253
+
254
+ Acl actual =
255
+ retry429s (
256
+ () -> storage .updateDefaultAcl (bucket .getName (), projectEditorAsReader ), storage );
257
+
258
+ assertThat (actual .getEntity ()).isEqualTo (projectEditorAsReader .getEntity ());
259
+ assertThat (actual .getRole ()).isEqualTo (projectEditorAsReader .getRole ());
238
260
assertThat (actual .getEtag ()).isNotEmpty ();
261
+
262
+ Bucket bucketUpdated =
263
+ storage .get (bucket .getName (), BucketGetOption .fields (BucketField .values ()));
264
+ assertThat (bucketUpdated .getMetageneration ()).isNotEqualTo (bucket .getMetageneration ());
265
+
266
+ // etags change when updates happen, drop before our comparison
267
+ List <Acl > expectedAcls =
268
+ dropEtags (
269
+ bucket .getDefaultAcl ().stream ()
270
+ .filter (isProjectEditor .negate ())
271
+ .collect (Collectors .toList ()));
272
+ System .out .println ("expectedAcls = " + expectedAcls );
273
+ List <Acl > actualAcls = dropEtags (bucketUpdated .getDefaultAcl ());
274
+ System .out .println ("actualAcls = " + actualAcls );
275
+ assertThat (actualAcls ).containsAtLeastElementsIn (expectedAcls );
276
+ assertThat (actualAcls ).doesNotContain (projectEditorAsOwner );
277
+ assertThat (actualAcls ).contains (projectEditorAsReader );
239
278
}
240
279
}
241
280
@@ -1098,4 +1137,18 @@ private static ImmutableList<Acl> dropEtags(List<Acl> defaultAcls) {
1098
1137
.map (acl -> Acl .of (acl .getEntity (), acl .getRole ()))
1099
1138
.collect (ImmutableList .toImmutableList ());
1100
1139
}
1140
+
1141
+ private static Predicate <Acl > hasRole (Acl .Role expected ) {
1142
+ return acl -> acl .getRole ().equals (expected );
1143
+ }
1144
+
1145
+ private static Predicate <Acl > hasProjectRole (Acl .Project .ProjectRole expected ) {
1146
+ return acl -> {
1147
+ Entity entity = acl .getEntity ();
1148
+ if (entity .getType ().equals (Entity .Type .PROJECT )) {
1149
+ return ((Acl .Project ) entity ).getProjectRole ().equals (expected );
1150
+ }
1151
+ return false ;
1152
+ };
1153
+ }
1101
1154
}
0 commit comments