Skip to content

Commit 3d7e22a

Browse files
twosomjzheaux
authored andcommitted
Add test to SimpleUrlAuthenticationSuccessHandlerTests
1 parent 84cca81 commit 3d7e22a

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,16 @@
1616

1717
package org.springframework.security.web.authentication;
1818

19+
import javax.servlet.http.HttpSession;
20+
1921
import org.junit.jupiter.api.Test;
2022

2123
import org.springframework.mock.web.MockHttpServletRequest;
2224
import org.springframework.mock.web.MockHttpServletResponse;
25+
import org.springframework.security.authentication.BadCredentialsException;
2326
import org.springframework.security.core.Authentication;
27+
import org.springframework.security.core.AuthenticationException;
28+
import org.springframework.security.web.WebAttributes;
2429

2530
import static org.assertj.core.api.Assertions.assertThat;
2631
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -108,4 +113,20 @@ public void setTargetUrlParameterEmptyTargetUrlParameter() {
108113
assertThatIllegalArgumentException().isThrownBy(() -> ash.setTargetUrlParameter(" "));
109114
}
110115

116+
@Test
117+
public void shouldRemoveAuthenticationAttributeWhenOnAuthenticationSuccess() throws Exception {
118+
SimpleUrlAuthenticationSuccessHandler ash = new SimpleUrlAuthenticationSuccessHandler();
119+
MockHttpServletRequest request = new MockHttpServletRequest();
120+
MockHttpServletResponse response = new MockHttpServletResponse();
121+
HttpSession session = request.getSession();
122+
assertThat(session).isNotNull();
123+
session.setAttribute(WebAttributes.AUTHENTICATION_EXCEPTION,
124+
new BadCredentialsException("Invalid credentials"));
125+
assertThat(session.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION)).isNotNull();
126+
assertThat(session.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION))
127+
.isInstanceOf(AuthenticationException.class);
128+
ash.onAuthenticationSuccess(request, response, mock(Authentication.class));
129+
assertThat(session.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION)).isNull();
130+
}
131+
111132
}

0 commit comments

Comments
 (0)