Skip to content

Commit 9595d37

Browse files
committed
Integration Test for DefaultLoginPageGeneratingFilterTests
Add a minimal test to ensure that DelegatingMissingAuthorityAccessDeniedHandler and DefaultLoginPageGeneratingFilterTests work together properly. Issue gh-18002
1 parent 2473378 commit 9595d37

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

web/src/test/java/org/springframework/security/web/authentication/DefaultLoginPageGeneratingFilterTests.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package org.springframework.security.web.authentication;
1818

1919
import java.io.IOException;
20+
import java.util.ArrayList;
2021
import java.util.Collections;
22+
import java.util.List;
2123

2224
import jakarta.servlet.FilterChain;
2325
import jakarta.servlet.ServletException;
@@ -27,9 +29,15 @@
2729
import org.springframework.mock.web.MockHttpServletResponse;
2830
import org.springframework.security.authentication.BadCredentialsException;
2931
import org.springframework.security.authentication.TestAuthentication;
32+
import org.springframework.security.authorization.AuthorizationDeniedException;
33+
import org.springframework.security.authorization.FactorAuthorizationDecision;
34+
import org.springframework.security.authorization.RequiredFactor;
35+
import org.springframework.security.authorization.RequiredFactorError;
36+
import org.springframework.security.core.GrantedAuthorities;
3037
import org.springframework.security.core.context.SecurityContextHolderStrategy;
3138
import org.springframework.security.core.context.SecurityContextImpl;
3239
import org.springframework.security.web.WebAttributes;
40+
import org.springframework.security.web.access.DelegatingMissingAuthorityAccessDeniedHandler;
3341
import org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter;
3442
import org.springframework.security.web.servlet.TestMockHttpServletRequests;
3543

@@ -204,8 +212,9 @@ public void generateWhenOneTimeTokenRequestedThenOttForm() throws Exception {
204212
filter.setOneTimeTokenEnabled(true);
205213
filter.setOneTimeTokenGenerationUrl("/ott/authenticate");
206214
MockHttpServletResponse response = new MockHttpServletResponse();
207-
filter.doFilter(TestMockHttpServletRequests.get("/login?factor.type=ott&factor.reason=missing").build(),
208-
response, this.chain);
215+
MockHttpServletRequest loginRequest = createLoginRequestFromMissingAuthority(
216+
GrantedAuthorities.FACTOR_OTT_AUTHORITY);
217+
filter.doFilter(loginRequest, response, this.chain);
209218
assertThat(response.getContentAsString()).contains("Request a One-Time Token");
210219
assertThat(response.getContentAsString()).contains("""
211220
<form id="ott-form" class="login-form" method="post" action="/ott/authenticate">
@@ -251,6 +260,24 @@ public void generateWhenTwoAuthoritiesRequestedThenBothForms() throws Exception
251260
assertThat(response.getContentAsString()).contains("Password");
252261
}
253262

263+
private MockHttpServletRequest createLoginRequestFromMissingAuthority(String factorAuthority)
264+
throws ServletException, IOException {
265+
LoginUrlAuthenticationEntryPoint entryPoint = new LoginUrlAuthenticationEntryPoint("/login");
266+
List<RequiredFactorError> factorErrors = new ArrayList<>();
267+
DelegatingMissingAuthorityAccessDeniedHandler.Builder handlerBldr = DelegatingMissingAuthorityAccessDeniedHandler
268+
.builder();
269+
handlerBldr.addEntryPointFor(entryPoint, factorAuthority);
270+
RequiredFactor requiredFactor = RequiredFactor.withAuthority(factorAuthority).build();
271+
RequiredFactorError factorError = RequiredFactorError.createMissing(requiredFactor);
272+
factorErrors.add(factorError);
273+
DelegatingMissingAuthorityAccessDeniedHandler handler = handlerBldr.build();
274+
MockHttpServletRequest request = new MockHttpServletRequest();
275+
MockHttpServletResponse response = new MockHttpServletResponse();
276+
FactorAuthorizationDecision decision = new FactorAuthorizationDecision(factorErrors);
277+
handler.handle(request, response, new AuthorizationDeniedException("", decision));
278+
return TestMockHttpServletRequests.get(response.getRedirectedUrl()).build();
279+
}
280+
254281
@Test
255282
public void generateWhenAuthenticatedThenReadOnlyUsername() throws Exception {
256283
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);

0 commit comments

Comments
 (0)