5
5
import ch .sbb .polarion .extension .generic .auth .ValidatorType ;
6
6
import com .polarion .core .config .Configuration ;
7
7
import com .polarion .core .config .IConfiguration ;
8
- import com .polarion .core .config .IRestConfiguration ;
9
8
import com .polarion .platform .security .AuthenticationFailedException ;
10
9
import com .polarion .platform .security .ISecurityService ;
11
- import com .polarion .platform .security .login .AccessToken ;
12
10
import com .polarion .platform .security .login .ILogin ;
13
11
import com .polarion .platform .security .login .IToken ;
14
12
import org .junit .jupiter .api .Test ;
15
13
import org .junit .jupiter .api .extension .ExtendWith ;
14
+ import org .mockito .Answers ;
16
15
import org .mockito .Mock ;
17
16
import org .mockito .MockedStatic ;
18
17
import org .mockito .junit .jupiter .MockitoExtension ;
@@ -34,22 +33,14 @@ class AuthenticationFilterTest {
34
33
35
34
@ Mock
36
35
private ContainerRequestContext requestContext ;
37
- @ Mock
36
+ @ Mock ( answer = Answers . RETURNS_DEEP_STUBS )
38
37
private ISecurityService securityService ;
39
38
@ Mock
40
- private ILogin login ;
41
- @ Mock
42
- private ILogin .IBase base ;
43
- @ Mock
44
- private ILogin .IUsingAuthenticator authenticator ;
45
- @ Mock
46
39
private ILogin .IFinal loginFinal ;
47
- @ Mock
40
+ @ Mock ( answer = Answers . RETURNS_DEEP_STUBS )
48
41
private HttpServletRequest httpServletRequest ;
49
- @ Mock
42
+ @ Mock ( answer = Answers . RETURNS_DEEP_STUBS )
50
43
private IConfiguration configuration ;
51
- @ Mock
52
- private IRestConfiguration restConfiguration ;
53
44
54
45
@ Test
55
46
void filterRequestWithoutAuthorizationHeaderAndXsrfHeader () {
@@ -76,14 +67,15 @@ void filterRequestWithoutBearerInAuthorizationHeader() {
76
67
}
77
68
78
69
@ Test
70
+ @ SuppressWarnings ("unchecked" )
79
71
void filterRequestWithValidBearerToken () throws IOException , AuthenticationFailedException {
80
72
when (requestContext .getHeaderString (HttpHeaders .AUTHORIZATION )).thenReturn ("Bearer token" );
81
73
when (requestContext .getHeaderString (AuthenticationFilter .X_POLARION_REST_TOKEN_HEADER )).thenReturn (null );
82
74
83
- when (securityService .login ()). thenReturn ( login );
84
- when ( login .from ("REST" )). thenReturn ( base );
85
- when ( base .authenticator (any ())). thenReturn ( authenticator );
86
- when ( authenticator .with (( IToken < AccessToken >) any ())).thenReturn (loginFinal );
75
+ when (securityService .login ()
76
+ .from ("REST" )
77
+ .authenticator (any ())
78
+ .with (any (IToken . class ))).thenReturn (loginFinal );
87
79
88
80
Subject subject = new Subject ();
89
81
when (loginFinal .perform ()).thenReturn (subject );
@@ -95,14 +87,15 @@ void filterRequestWithValidBearerToken() throws IOException, AuthenticationFaile
95
87
96
88
97
89
@ Test
90
+ @ SuppressWarnings ("unchecked" )
98
91
void filterRequestWithFailedAuthentication () throws AuthenticationFailedException {
99
92
when (requestContext .getHeaderString (HttpHeaders .AUTHORIZATION )).thenReturn ("Bearer failed_token" );
100
93
when (requestContext .getHeaderString (AuthenticationFilter .X_POLARION_REST_TOKEN_HEADER )).thenReturn (null );
101
94
102
- when (securityService .login ()). thenReturn ( login );
103
- when ( login .from ("REST" )). thenReturn ( base );
104
- when ( base .authenticator (any ())). thenReturn ( authenticator );
105
- when ( authenticator .with (( IToken < AccessToken >) any ())).thenReturn (loginFinal );
95
+ when (securityService .login ()
96
+ .from ("REST" )
97
+ .authenticator (any ())
98
+ .with (any (IToken . class ))).thenReturn (loginFinal );
106
99
107
100
when (loginFinal .perform ()).thenThrow (new AuthenticationFailedException ("Something went wrong" ));
108
101
@@ -114,6 +107,7 @@ void filterRequestWithFailedAuthentication() throws AuthenticationFailedExceptio
114
107
}
115
108
116
109
@ Test
110
+ @ SuppressWarnings ("unused" )
117
111
void filterRequestWithValidXsrfToken () throws IOException , AuthenticationFailedException {
118
112
when (requestContext .getHeaderString (HttpHeaders .AUTHORIZATION )).thenReturn (null );
119
113
when (requestContext .getHeaderString (AuthenticationFilter .X_POLARION_REST_TOKEN_HEADER )).thenReturn ("validXsrfToken" );
@@ -144,12 +138,13 @@ void filterRequestWithInvalidXsrfToken() {
144
138
145
139
try (MockedStatic <Configuration > configurationMockedStatic = mockStatic (Configuration .class )) {
146
140
configurationMockedStatic .when (Configuration ::getInstance ).thenReturn (configuration );
147
- when (configuration .rest ()).thenReturn (restConfiguration );
148
- when (restConfiguration .restApiTokenEnabled ()).thenReturn (true );
141
+ when (configuration
142
+ .rest ()
143
+ .restApiTokenEnabled ()).thenReturn (true );
149
144
150
- Principal userPrincipal = mock ( Principal . class );
151
- when ( httpServletRequest .getUserPrincipal ()). thenReturn ( userPrincipal );
152
- when ( userPrincipal .getName ()).thenReturn ("user" );
145
+ when ( httpServletRequest
146
+ .getUserPrincipal ()
147
+ .getName ()).thenReturn ("user" );
153
148
154
149
AuthenticationFilter filter = new AuthenticationFilter (securityService , httpServletRequest );
155
150
@@ -165,12 +160,13 @@ void filterRequestWithXsrfTokenButConfigurationIsDisabled() {
165
160
when (requestContext .getHeaderString (AuthenticationFilter .X_POLARION_REST_TOKEN_HEADER )).thenReturn ("xsrf_token" );
166
161
try (MockedStatic <Configuration > configurationMockedStatic = mockStatic (Configuration .class )) {
167
162
configurationMockedStatic .when (Configuration ::getInstance ).thenReturn (configuration );
168
- when (configuration .rest ()).thenReturn (restConfiguration );
169
- when (restConfiguration .restApiTokenEnabled ()).thenReturn (false );
163
+ when (configuration
164
+ .rest ()
165
+ .restApiTokenEnabled ()).thenReturn (false );
170
166
171
- Principal userPrincipal = mock ( Principal . class );
172
- when ( httpServletRequest .getUserPrincipal ()). thenReturn ( userPrincipal );
173
- when ( userPrincipal .getName ()).thenReturn ("user" );
167
+ when ( httpServletRequest
168
+ .getUserPrincipal ()
169
+ .getName ()).thenReturn ("user" );
174
170
175
171
AuthenticationFilter filter = new AuthenticationFilter (securityService , httpServletRequest );
176
172
@@ -186,12 +182,13 @@ void filterRequestWithXsrfTokenForDifferentUser() {
186
182
when (requestContext .getHeaderString (AuthenticationFilter .X_POLARION_REST_TOKEN_HEADER )).thenReturn ("xsrf_token_for_different_user" );
187
183
try (MockedStatic <Configuration > configurationMockedStatic = mockStatic (Configuration .class )) {
188
184
configurationMockedStatic .when (Configuration ::getInstance ).thenReturn (configuration );
189
- when (configuration .rest ()).thenReturn (restConfiguration );
190
- when (restConfiguration .restApiTokenEnabled ()).thenReturn (true );
185
+ when (configuration
186
+ .rest ()
187
+ .restApiTokenEnabled ()).thenReturn (true );
191
188
192
- Principal userPrincipal = mock ( Principal . class );
193
- when ( httpServletRequest .getUserPrincipal ()). thenReturn ( userPrincipal );
194
- when ( userPrincipal .getName ()).thenReturn ("different_user" );
189
+ when ( httpServletRequest
190
+ .getUserPrincipal ()
191
+ .getName ()).thenReturn ("different_user" );
195
192
196
193
AuthenticationFilter filter = new AuthenticationFilter (securityService , httpServletRequest );
197
194
0 commit comments