1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import java .lang .annotation .Retention ;
20
20
import java .lang .annotation .RetentionPolicy ;
21
+ import java .util .Collection ;
22
+ import java .util .Set ;
21
23
import java .util .function .Supplier ;
22
24
23
25
import jakarta .annotation .security .DenyAll ;
30
32
import org .springframework .security .authentication .TestAuthentication ;
31
33
import org .springframework .security .authentication .TestingAuthenticationToken ;
32
34
import org .springframework .security .authorization .AuthorizationDecision ;
35
+ import org .springframework .security .authorization .AuthorizationManager ;
33
36
import org .springframework .security .core .Authentication ;
34
37
35
38
import static org .assertj .core .api .Assertions .assertThat ;
36
39
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
37
40
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
41
+ import static org .mockito .Mockito .mock ;
42
+ import static org .mockito .Mockito .verify ;
38
43
39
44
/**
40
45
* Tests for {@link Jsr250AuthorizationManager}.
@@ -63,6 +68,27 @@ public void setRolePrefixWhenNotNullThenSets() {
63
68
assertThat (manager ).extracting ("rolePrefix" ).isEqualTo ("CUSTOM_" );
64
69
}
65
70
71
+ @ Test
72
+ public void setAuthoritiesAuthorizationManagerWhenNullThenException () {
73
+ Jsr250AuthorizationManager manager = new Jsr250AuthorizationManager ();
74
+ assertThatIllegalArgumentException ().isThrownBy (() -> manager .setAuthoritiesAuthorizationManager (null ))
75
+ .withMessage ("authoritiesAuthorizationManager cannot be null" );
76
+ }
77
+
78
+ @ Test
79
+ public void setAuthoritiesAuthorizationManagerWhenNotNullThenVerifyUsage () throws Exception {
80
+ AuthorizationManager <Collection <String >> authoritiesAuthorizationManager = mock (AuthorizationManager .class );
81
+ Jsr250AuthorizationManager manager = new Jsr250AuthorizationManager ();
82
+ manager .setAuthoritiesAuthorizationManager (authoritiesAuthorizationManager );
83
+ MockMethodInvocation methodInvocation = new MockMethodInvocation (new ClassLevelAnnotations (),
84
+ ClassLevelAnnotations .class , "rolesAllowedAdmin" );
85
+ Supplier <Authentication > authentication = () -> new TestingAuthenticationToken ("user" , "password" ,
86
+ "ROLE_ADMIN" );
87
+ AuthorizationDecision decision = manager .check (authentication , methodInvocation );
88
+ assertThat (decision ).isNull ();
89
+ verify (authoritiesAuthorizationManager ).check (authentication , Set .of ("ROLE_ADMIN" ));
90
+ }
91
+
66
92
@ Test
67
93
public void checkDoSomethingWhenNoJsr250AnnotationsThenNullDecision () throws Exception {
68
94
MockMethodInvocation methodInvocation = new MockMethodInvocation (new TestClass (), TestClass .class ,
@@ -123,7 +149,7 @@ public void checkRolesAllowedUserOrAdminWhenRoleAnonymousThenDeniedDecision() th
123
149
}
124
150
125
151
@ Test
126
- public void checkMultipleAnnotationsWhenInvokedThenAnnotationConfigurationException () throws Exception {
152
+ public void checkMultipleMethodAnnotationsWhenInvokedThenAnnotationConfigurationException () throws Exception {
127
153
Supplier <Authentication > authentication = () -> new TestingAuthenticationToken ("user" , "password" ,
128
154
"ROLE_ANONYMOUS" );
129
155
MockMethodInvocation methodInvocation = new MockMethodInvocation (new TestClass (), TestClass .class ,
@@ -133,6 +159,16 @@ public void checkMultipleAnnotationsWhenInvokedThenAnnotationConfigurationExcept
133
159
.isThrownBy (() -> manager .check (authentication , methodInvocation ));
134
160
}
135
161
162
+ @ Test
163
+ public void checkMultipleClassAnnotationsWhenInvokedThenAnnotationConfigurationException () throws Exception {
164
+ Supplier <Authentication > authentication = () -> new TestingAuthenticationToken ("user" , "password" , "ROLE_USER" );
165
+ MockMethodInvocation methodInvocation = new MockMethodInvocation (new ClassLevelIllegalAnnotations (),
166
+ ClassLevelIllegalAnnotations .class , "inheritedAnnotations" );
167
+ Jsr250AuthorizationManager manager = new Jsr250AuthorizationManager ();
168
+ assertThatExceptionOfType (AnnotationConfigurationException .class )
169
+ .isThrownBy (() -> manager .check (authentication , methodInvocation ));
170
+ }
171
+
136
172
@ Test
137
173
public void checkRequiresAdminWhenClassAnnotationsThenMethodAnnotationsTakePrecedence () throws Exception {
138
174
Supplier <Authentication > authentication = () -> new TestingAuthenticationToken ("user" , "password" , "ROLE_USER" );
@@ -247,6 +283,15 @@ public void inheritedAnnotations() {
247
283
248
284
}
249
285
286
+ @ MyIllegalRolesAllowed
287
+ public static class ClassLevelIllegalAnnotations {
288
+
289
+ public void inheritedAnnotations () {
290
+
291
+ }
292
+
293
+ }
294
+
250
295
public interface InterfaceAnnotationsOne {
251
296
252
297
@ RolesAllowed ("ADMIN" )
@@ -274,4 +319,11 @@ public interface InterfaceAnnotationsThree {
274
319
275
320
}
276
321
322
+ @ DenyAll
323
+ @ RolesAllowed ("USER" )
324
+ @ Retention (RetentionPolicy .RUNTIME )
325
+ public @interface MyIllegalRolesAllowed {
326
+
327
+ }
328
+
277
329
}
0 commit comments