24
24
25
25
import org .junit .Test ;
26
26
import org .mockito .ArgumentCaptor ;
27
+ import org .opengrok .indexer .configuration .RuntimeEnvironment ;
27
28
28
29
import javax .servlet .http .HttpServletRequest ;
29
30
import javax .ws .rs .container .ContainerRequestContext ;
31
+ import javax .ws .rs .core .HttpHeaders ;
30
32
import javax .ws .rs .core .Response ;
31
33
import javax .ws .rs .core .UriInfo ;
32
34
33
35
import java .lang .reflect .Field ;
36
+ import java .util .HashSet ;
37
+ import java .util .Map ;
38
+ import java .util .Set ;
39
+ import java .util .TreeMap ;
34
40
35
41
import static org .junit .Assert .assertEquals ;
36
42
import static org .mockito .Mockito .mock ;
37
43
import static org .mockito .Mockito .never ;
38
44
import static org .mockito .Mockito .verify ;
39
45
import static org .mockito .Mockito .when ;
40
46
41
- public class LocalhostFilterTest {
47
+ public class IncomingFilterTest {
48
+ @ Test
49
+ public void nonLocalhostTestWithValidToken () throws Exception {
50
+ nonLocalhostTestWithToken (true );
51
+ }
52
+
53
+ @ Test
54
+ public void nonLocalhostTestWithInvalidToken () throws Exception {
55
+ nonLocalhostTestWithToken (false );
56
+ }
57
+
58
+ private void nonLocalhostTestWithToken (boolean allowed ) throws Exception {
59
+ String allowedToken = "foo" ;
60
+
61
+ Set <String > tokens = new HashSet <>();
62
+ tokens .add (allowedToken );
63
+ RuntimeEnvironment .getInstance ().setAuthenticationTokens (tokens );
64
+
65
+ Map <String , String > headers = new TreeMap <>();
66
+ headers .put (HttpHeaders .AUTHORIZATION , allowed ? allowedToken : allowedToken + "_" );
67
+ IncomingFilter filter = mockWithRemoteAddress ("192.168.1.1" , headers , true );
68
+
69
+ ContainerRequestContext context = mockContainerRequestContext ("test" );
70
+
71
+ ArgumentCaptor <Response > captor = ArgumentCaptor .forClass (Response .class );
72
+
73
+ filter .filter (context );
74
+
75
+ if (allowed ) {
76
+ verify (context , never ()).abortWith (captor .capture ());
77
+ } else {
78
+ verify (context ).abortWith (captor .capture ());
79
+ }
80
+ }
42
81
43
82
@ Test
44
- public void nonLocalhostTest () throws Exception {
83
+ public void nonLocalhostTestWithoutToken () throws Exception {
45
84
IncomingFilter filter = mockWithRemoteAddress ("192.168.1.1" );
46
85
47
86
ContainerRequestContext context = mockContainerRequestContext ("test" );
@@ -55,18 +94,27 @@ public void nonLocalhostTest() throws Exception {
55
94
assertEquals (Response .Status .UNAUTHORIZED .getStatusCode (), captor .getValue ().getStatus ());
56
95
}
57
96
58
- private IncomingFilter mockWithRemoteAddress (final String remoteAddr ) throws Exception {
97
+ private IncomingFilter mockWithRemoteAddress (final String remoteAddr , Map <String , String > headers , boolean secure )
98
+ throws Exception {
59
99
IncomingFilter filter = new IncomingFilter ();
60
100
filter .init ();
61
101
62
102
HttpServletRequest request = mock (HttpServletRequest .class );
103
+ for (String name : headers .keySet ()) {
104
+ when (request .getHeader (name )).thenReturn (headers .get (name ));
105
+ }
106
+ when (request .isSecure ()).thenReturn (secure );
63
107
when (request .getRemoteAddr ()).thenReturn (remoteAddr );
64
108
65
109
setHttpRequest (filter , request );
66
110
67
111
return filter ;
68
112
}
69
113
114
+ private IncomingFilter mockWithRemoteAddress (final String remoteAddr ) throws Exception {
115
+ return mockWithRemoteAddress (remoteAddr , new TreeMap <>(), false );
116
+ }
117
+
70
118
private void setHttpRequest (final IncomingFilter filter , final HttpServletRequest request ) throws Exception {
71
119
Field f = IncomingFilter .class .getDeclaredField ("request" );
72
120
f .setAccessible (true );
0 commit comments