17
17
import static java .util .Collections .emptyList ;
18
18
import static java .util .Collections .emptySet ;
19
19
import static javax .ws .rs .core .Response .Status .FORBIDDEN ;
20
+ import static javax .ws .rs .core .Response .Status .NOT_FOUND ;
20
21
import static javax .ws .rs .core .Response .Status .OK ;
21
22
import static org .junit .jupiter .api .Assertions .*;
22
23
import static org .mockito .ArgumentMatchers .*;
30
31
31
32
import javax .ws .rs .ForbiddenException ;
32
33
import javax .ws .rs .NotAuthorizedException ;
34
+ import javax .ws .rs .NotFoundException ;
33
35
import javax .ws .rs .container .ContainerRequestContext ;
34
36
import javax .ws .rs .container .ContainerResponseContext ;
35
37
import javax .ws .rs .core .Link ;
@@ -126,13 +128,32 @@ public void testFilterRead() throws Exception {
126
128
127
129
modes .clear ();
128
130
assertThrows (NotAuthorizedException .class , () -> filter .filter (mockContext ),
129
- "No expception thrown when not authorized!" );
131
+ "No exception thrown when not authorized!" );
130
132
131
133
when (mockContext .getProperty (SESSION_PROPERTY )).thenReturn (session );
132
134
assertThrows (ForbiddenException .class , () -> filter .filter (mockContext ),
133
135
"No exception thrown!" );
134
136
}
135
137
138
+ @ Test
139
+ public void testFilterReadHidden () throws Exception {
140
+ final Set <IRI > modes = new HashSet <>();
141
+ when (mockContext .getMethod ()).thenReturn ("GET" );
142
+ when (mockWebAcService .getAccessModes (any (IRI .class ), any (Session .class ), any ())).thenReturn (modes );
143
+
144
+ final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ("Bearer" , "Basic" ), "trellis" , true , null );
145
+ modes .add (ACL .Read );
146
+ assertDoesNotThrow (() -> filter .filter (mockContext ), "Unexpected exception after adding Read ability!" );
147
+
148
+ modes .clear ();
149
+ assertThrows (NotAuthorizedException .class , () -> filter .filter (mockContext ),
150
+ "No exception thrown when not authorized!" );
151
+
152
+ when (mockContext .getProperty (SESSION_PROPERTY )).thenReturn (session );
153
+ assertThrows (NotFoundException .class , () -> filter .filter (mockContext ),
154
+ "No exception thrown!" );
155
+ }
156
+
136
157
@ Test
137
158
public void testFilterCustomRead () throws Exception {
138
159
final Set <IRI > modes = new HashSet <>();
@@ -145,7 +166,7 @@ public void testFilterCustomRead() throws Exception {
145
166
146
167
modes .clear ();
147
168
assertThrows (NotAuthorizedException .class , () -> filter .filter (mockContext ),
148
- "No expception thrown when not authorized!" );
169
+ "No exception thrown when not authorized!" );
149
170
150
171
when (mockContext .getProperty (SESSION_PROPERTY )).thenReturn (session );
151
172
assertThrows (ForbiddenException .class , () -> filter .filter (mockContext ),
@@ -165,13 +186,32 @@ public void testFilterWrite() throws Exception {
165
186
166
187
modes .clear ();
167
188
assertThrows (NotAuthorizedException .class , () -> filter .filter (mockContext ),
168
- "No expception thrown when not authorized!" );
189
+ "No exception thrown when not authorized!" );
169
190
170
191
when (mockContext .getProperty (SESSION_PROPERTY )).thenReturn (session );
171
192
assertThrows (ForbiddenException .class , () -> filter .filter (mockContext ),
172
193
"No exception thrown!" );
173
194
}
174
195
196
+ @ Test
197
+ public void testFilterWriteHidden () throws Exception {
198
+ final Set <IRI > modes = new HashSet <>();
199
+ when (mockContext .getMethod ()).thenReturn ("PUT" );
200
+ when (mockWebAcService .getAccessModes (any (IRI .class ), any (Session .class ), any ())).thenReturn (modes );
201
+
202
+ final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ("Bearer" , "Basic" ), "trellis" , true , null );
203
+ modes .add (ACL .Write );
204
+ assertDoesNotThrow (() -> filter .filter (mockContext ), "Unexpected exception after adding Write ability!" );
205
+
206
+ modes .clear ();
207
+ assertThrows (NotAuthorizedException .class , () -> filter .filter (mockContext ),
208
+ "No exception thrown when not authorized!" );
209
+
210
+ when (mockContext .getProperty (SESSION_PROPERTY )).thenReturn (session );
211
+ assertThrows (NotFoundException .class , () -> filter .filter (mockContext ),
212
+ "No exception thrown!" );
213
+ }
214
+
175
215
@ Test
176
216
public void testFilterCustomWrite () throws Exception {
177
217
final Set <IRI > modes = new HashSet <>();
@@ -184,7 +224,7 @@ public void testFilterCustomWrite() throws Exception {
184
224
185
225
modes .clear ();
186
226
assertThrows (NotAuthorizedException .class , () -> filter .filter (mockContext ),
187
- "No expception thrown when not authorized!" );
227
+ "No exception thrown when not authorized!" );
188
228
189
229
when (mockContext .getProperty (SESSION_PROPERTY )).thenReturn (session );
190
230
assertThrows (ForbiddenException .class , () -> filter .filter (mockContext ),
@@ -209,13 +249,32 @@ public void testFilterAppend() throws Exception {
209
249
210
250
modes .clear ();
211
251
assertThrows (NotAuthorizedException .class , () -> filter .filter (mockContext ),
212
- "No expception thrown when not authorized!" );
252
+ "No exception thrown when not authorized!" );
213
253
214
254
when (mockContext .getProperty (SESSION_PROPERTY )).thenReturn (session );
215
255
assertThrows (ForbiddenException .class , () -> filter .filter (mockContext ),
216
256
"No exception thrown!" );
217
257
}
218
258
259
+ @ Test
260
+ public void testFilterAppendHide () throws Exception {
261
+ final Set <IRI > modes = new HashSet <>();
262
+ when (mockContext .getMethod ()).thenReturn ("POST" );
263
+ when (mockWebAcService .getAccessModes (any (IRI .class ), any (Session .class ), any ())).thenReturn (modes );
264
+
265
+ final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ("Bearer" , "Basic" ), "trellis" , true , null );
266
+ modes .add (ACL .Append );
267
+ assertDoesNotThrow (() -> filter .filter (mockContext ), "Unexpected exception after adding Append ability!" );
268
+
269
+ modes .clear ();
270
+ assertThrows (NotAuthorizedException .class , () -> filter .filter (mockContext ),
271
+ "No exception thrown when not authorized!" );
272
+
273
+ when (mockContext .getProperty (SESSION_PROPERTY )).thenReturn (session );
274
+ assertThrows (NotFoundException .class , () -> filter .filter (mockContext ),
275
+ "No exception thrown!" );
276
+ }
277
+
219
278
@ Test
220
279
public void testFilterCustomAppend () throws Exception {
221
280
final Set <IRI > modes = new HashSet <>();
@@ -234,7 +293,7 @@ public void testFilterCustomAppend() throws Exception {
234
293
235
294
modes .clear ();
236
295
assertThrows (NotAuthorizedException .class , () -> filter .filter (mockContext ),
237
- "No expception thrown when not authorized!" );
296
+ "No exception thrown when not authorized!" );
238
297
239
298
when (mockContext .getProperty (SESSION_PROPERTY )).thenReturn (session );
240
299
assertThrows (ForbiddenException .class , () -> filter .filter (mockContext ),
@@ -255,7 +314,7 @@ public void testFilterControl() throws Exception {
255
314
.thenReturn ("return=representation; include=\" " + Trellis .PreferAudit .getIRIString () + "\" " );
256
315
257
316
assertThrows (NotAuthorizedException .class , () -> filter .filter (mockContext ),
258
- "No expception thrown when not authorized!" );
317
+ "No exception thrown when not authorized!" );
259
318
260
319
modes .add (ACL .Control );
261
320
assertDoesNotThrow (() -> filter .filter (mockContext ), "Unexpected exception after adding Control ability!" );
@@ -272,21 +331,21 @@ public void testFilterControl2() throws Exception {
272
331
when (mockContext .getMethod ()).thenReturn ("GET" );
273
332
when (mockWebAcService .getAccessModes (any (IRI .class ), any (Session .class ), any ())).thenReturn (modes );
274
333
275
- final WebAcFilter filter = new WebAcFilter (mockWebAcService );
334
+ final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ( "Bearer" , "Basic" ), "trellis" , true , null );
276
335
modes .add (ACL .Read );
277
336
assertDoesNotThrow (() -> filter .filter (mockContext ), "Unexpected exception after adding Read ability!" );
278
337
279
338
when (mockQueryParams .getOrDefault (eq ("ext" ), eq (emptyList ()))).thenReturn (asList ("acl" ));
280
339
281
340
assertThrows (NotAuthorizedException .class , () -> filter .filter (mockContext ),
282
- "No expception thrown when not authorized!" );
341
+ "No exception thrown when not authorized!" );
283
342
284
343
modes .add (ACL .Control );
285
344
assertDoesNotThrow (() -> filter .filter (mockContext ), "Unexpected exception after adding Control ability!" );
286
345
287
346
modes .clear ();
288
347
when (mockContext .getProperty (SESSION_PROPERTY )).thenReturn (session );
289
- assertThrows (ForbiddenException .class , () -> filter .filter (mockContext ),
348
+ assertThrows (NotFoundException .class , () -> filter .filter (mockContext ),
290
349
"No exception thrown!" );
291
350
}
292
351
@@ -295,7 +354,7 @@ public void testFilterChallenges() throws Exception {
295
354
when (mockContext .getMethod ()).thenReturn ("POST" );
296
355
when (mockWebAcService .getAccessModes (any (IRI .class ), any (Session .class ), any ())).thenReturn (emptySet ());
297
356
298
- final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ("Foo" , "Bar" ), "my-realm" ,
357
+ final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ("Foo" , "Bar" ), "my-realm" , false ,
299
358
"http://example.com/" );
300
359
301
360
final List <Object > challenges = assertThrows (NotAuthorizedException .class , () -> filter .filter (mockContext ),
@@ -312,7 +371,7 @@ public void testFilterResponse() throws Exception {
312
371
when (mockResponseContext .getHeaders ()).thenReturn (headers );
313
372
when (mockUriInfo .getAbsolutePathBuilder ()).thenReturn (UriBuilder .fromUri ("http://localhost/" ));
314
373
315
- final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ("Foo" , "Bar" ), "my-realm" , null );
374
+ final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ("Foo" , "Bar" ), "my-realm" , false , null );
316
375
317
376
assertTrue (headers .isEmpty ());
318
377
filter .filter (mockContext , mockResponseContext );
@@ -330,7 +389,7 @@ public void testFilterResponseBaseUrl() throws Exception {
330
389
when (mockResponseContext .getStatusInfo ()).thenReturn (OK );
331
390
when (mockResponseContext .getHeaders ()).thenReturn (headers );
332
391
333
- final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ("Foo" , "Bar" ), "my-realm" ,
392
+ final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ("Foo" , "Bar" ), "my-realm" , false ,
334
393
"http://example.com/" );
335
394
336
395
assertTrue (headers .isEmpty ());
@@ -354,7 +413,7 @@ public void testFilterResponseWebac2() throws Exception {
354
413
when (mockUriInfo .getQueryParameters ()).thenReturn (params );
355
414
when (mockUriInfo .getAbsolutePathBuilder ()).thenReturn (UriBuilder .fromUri ("http://localhost/" ));
356
415
357
- final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ("Foo" , "Bar" ), "my-realm" , null );
416
+ final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ("Foo" , "Bar" ), "my-realm" , false , null );
358
417
359
418
assertTrue (headers .isEmpty ());
360
419
filter .filter (mockContext , mockResponseContext );
@@ -367,7 +426,20 @@ public void testFilterResponseForbidden() throws Exception {
367
426
when (mockResponseContext .getStatusInfo ()).thenReturn (FORBIDDEN );
368
427
when (mockResponseContext .getHeaders ()).thenReturn (headers );
369
428
370
- final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ("Foo" , "Bar" ), "my-realm" , null );
429
+ final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ("Foo" , "Bar" ), "my-realm" , false , null );
430
+
431
+ assertTrue (headers .isEmpty ());
432
+ filter .filter (mockContext , mockResponseContext );
433
+ assertTrue (headers .isEmpty ());
434
+ }
435
+
436
+ @ Test
437
+ public void testFilterResponseHidden () throws Exception {
438
+ final MultivaluedMap <String , Object > headers = new MultivaluedHashMap <>();
439
+ when (mockResponseContext .getStatusInfo ()).thenReturn (NOT_FOUND );
440
+ when (mockResponseContext .getHeaders ()).thenReturn (headers );
441
+
442
+ final WebAcFilter filter = new WebAcFilter (mockWebAcService , asList ("Foo" , "Bar" ), "my-realm" , true , null );
371
443
372
444
assertTrue (headers .isEmpty ());
373
445
filter .filter (mockContext , mockResponseContext );
0 commit comments