27
27
import jakarta .servlet .ServletException ;
28
28
import jakarta .servlet .http .HttpServletRequest ;
29
29
import jakarta .servlet .http .HttpServletResponse ;
30
+ import org .apache .commons .lang3 .tuple .Pair ;
30
31
import org .junit .jupiter .api .Test ;
31
32
import org .junit .jupiter .params .ParameterizedTest ;
33
+ import org .junit .jupiter .params .provider .MethodSource ;
32
34
import org .junit .jupiter .params .provider .ValueSource ;
33
35
import org .opengrok .indexer .configuration .Project ;
34
36
import org .opengrok .indexer .configuration .RuntimeEnvironment ;
37
39
import org .opengrok .web .api .v1 .RestApp ;
38
40
39
41
import java .io .IOException ;
42
+ import java .util .stream .Stream ;
40
43
41
44
import static org .mockito .ArgumentMatchers .anyInt ;
42
45
import static org .mockito .ArgumentMatchers .anyString ;
@@ -74,14 +77,21 @@ public String getServletPath() {
74
77
verify (response , never ()).sendError (anyInt (), anyString ());
75
78
}
76
79
80
+ private static Stream <Pair <Boolean , String >> getParamsForTestIsAllowed () {
81
+ return Stream .of (Pair .of (true , null ),
82
+ Pair .of (true , "user" ),
83
+ Pair .of (false , null ),
84
+ Pair .of (false , "user" ));
85
+ }
86
+
77
87
@ ParameterizedTest
78
- @ ValueSource ( booleans = { true , false } )
79
- void testIsAllowed (boolean isAllowed ) throws ServletException , IOException {
88
+ @ MethodSource ( "getParamsForTestIsAllowed" )
89
+ void testIsAllowed (Pair < Boolean , String > pair ) throws ServletException , IOException {
80
90
AuthorizationFilter filter = new AuthorizationFilter ();
81
91
PageConfig pageConfig = mock (PageConfig .class );
82
92
Project project = mock (Project .class );
83
93
when (pageConfig .getProject ()).thenReturn (project );
84
- when (pageConfig .isAllowed (project )).thenReturn (isAllowed );
94
+ when (pageConfig .isAllowed (project )).thenReturn (pair . getLeft () );
85
95
when (pageConfig .getEnv ()).thenReturn (RuntimeEnvironment .getInstance ());
86
96
HttpServletRequest request = new DummyHttpServletRequest () {
87
97
@ Override
@@ -91,7 +101,7 @@ public String getServletPath() {
91
101
92
102
@ Override
93
103
public String getRemoteUser () {
94
- return "user" ;
104
+ return pair . getRight () ;
95
105
}
96
106
97
107
@ Override
@@ -116,7 +126,7 @@ public Object getAttribute(String s) {
116
126
filter .init (filterConfig );
117
127
filter .doFilter (request , response , chain );
118
128
119
- if (isAllowed ) {
129
+ if (pair . getLeft () ) {
120
130
verify (chain ).doFilter (request , response );
121
131
verify (response , never ()).sendError (anyInt ());
122
132
verify (response , never ()).sendError (anyInt (), anyString ());
0 commit comments