📄 switchuserprocessingfiltertests.java
字号:
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.acegisecurity.ui.switchuser;import junit.framework.TestCase;import org.acegisecurity.AccountExpiredException;import org.acegisecurity.Authentication;import org.acegisecurity.AuthenticationException;import org.acegisecurity.CredentialsExpiredException;import org.acegisecurity.DisabledException;import org.acegisecurity.GrantedAuthority;import org.acegisecurity.GrantedAuthorityImpl;import org.acegisecurity.context.SecurityContextHolder;import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;import org.acegisecurity.userdetails.User;import org.acegisecurity.userdetails.UserDetails;import org.acegisecurity.userdetails.UserDetailsService;import org.acegisecurity.userdetails.UsernameNotFoundException;import org.acegisecurity.util.MockFilterChain;import org.springframework.dao.DataAccessException;import org.springframework.mock.web.MockHttpServletRequest;import org.springframework.mock.web.MockHttpServletResponse;import java.util.List;/** * Tests {@link org.acegisecurity.ui.switchuser.SwitchUserProcessingFilter}. * * @author Mark St.Godard * @version $Id: SwitchUserProcessingFilterTests.java 2540 2008-01-28 19:24:45Z luke_t $ */public class SwitchUserProcessingFilterTests extends TestCase { //~ Constructors =================================================================================================== public SwitchUserProcessingFilterTests() { super(); } public SwitchUserProcessingFilterTests(String arg0) { super(arg0); } //~ Methods ======================================================================================================== private MockHttpServletRequest createMockSwitchRequest() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setScheme("http"); request.setServerName("localhost"); request.setRequestURI("/j_acegi_switch_user"); return request; } public static void main(String[] args) { junit.textui.TestRunner.run(SwitchUserProcessingFilterTests.class); } public final void setUp() throws Exception { super.setUp(); } public void testAttemptSwitchToUnknownUser() throws Exception { // set current user UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("dano", "hawaii50"); SecurityContextHolder.getContext().setAuthentication(auth); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter(SwitchUserProcessingFilter.ACEGI_SECURITY_SWITCH_USERNAME_KEY, "user-that-doesnt-exist"); SwitchUserProcessingFilter filter = new SwitchUserProcessingFilter(); filter.setUserDetailsService(new MockAuthenticationDaoUserJackLord()); try { Authentication result = filter.attemptSwitchUser(request); fail("Should not be able to switch to unknown user"); } catch (UsernameNotFoundException expected) {} } public void testAttemptSwitchToUserThatIsDisabled() throws Exception { // set current user UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("dano", "hawaii50"); SecurityContextHolder.getContext().setAuthentication(auth); MockHttpServletRequest request = new MockHttpServletRequest(); // this user is disabled request.addParameter(SwitchUserProcessingFilter.ACEGI_SECURITY_SWITCH_USERNAME_KEY, "mcgarrett"); SwitchUserProcessingFilter filter = new SwitchUserProcessingFilter(); filter.setUserDetailsService(new MockAuthenticationDaoUserJackLord()); try { Authentication result = filter.attemptSwitchUser(request); fail("Should not be able to switch to disabled user"); } catch (DisabledException expected) { // user should be disabled } } public void testAttemptSwitchToUserWithAccountExpired() throws Exception { // set current user UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("dano", "hawaii50"); SecurityContextHolder.getContext().setAuthentication(auth); MockHttpServletRequest request = new MockHttpServletRequest(); // this user is disabled request.addParameter(SwitchUserProcessingFilter.ACEGI_SECURITY_SWITCH_USERNAME_KEY, "wofat"); SwitchUserProcessingFilter filter = new SwitchUserProcessingFilter(); filter.setUserDetailsService(new MockAuthenticationDaoUserJackLord()); try { Authentication result = filter.attemptSwitchUser(request); fail("Should not be able to switch to user with expired account"); } catch (AccountExpiredException expected) { // expected user account expired } } public void testAttemptSwitchToUserWithExpiredCredentials() throws Exception { // set current user UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("dano", "hawaii50"); SecurityContextHolder.getContext().setAuthentication(auth); MockHttpServletRequest request = new MockHttpServletRequest(); // this user is disabled request.addParameter(SwitchUserProcessingFilter.ACEGI_SECURITY_SWITCH_USERNAME_KEY, "steve"); SwitchUserProcessingFilter filter = new SwitchUserProcessingFilter(); filter.setUserDetailsService(new MockAuthenticationDaoUserJackLord()); try { Authentication result = filter.attemptSwitchUser(request); fail("Should not be able to switch to user with expired account"); } catch (CredentialsExpiredException expected) { // user credentials expired } } public void testAttemptSwitchUser() throws Exception { // set current user UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("dano", "hawaii50"); SecurityContextHolder.getContext().setAuthentication(auth); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter(SwitchUserProcessingFilter.ACEGI_SECURITY_SWITCH_USERNAME_KEY, "jacklord"); SwitchUserProcessingFilter filter = new SwitchUserProcessingFilter(); filter.setUserDetailsService(new MockAuthenticationDaoUserJackLord()); Authentication result = filter.attemptSwitchUser(request); assertTrue(result != null); } public void testIfSwitchUserWithNullUsernameThrowsException() throws Exception { // set current user UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("dano", "hawaii50"); SecurityContextHolder.getContext().setAuthentication(auth); MockHttpServletRequest request = new MockHttpServletRequest(); String username = null; request.addParameter(SwitchUserProcessingFilter.ACEGI_SECURITY_SWITCH_USERNAME_KEY, username); SwitchUserProcessingFilter filter = new SwitchUserProcessingFilter(); filter.setUserDetailsService(new MockAuthenticationDaoUserJackLord()); Authentication result = null ; try { result = filter.attemptSwitchUser(request); fail("UsernameNotFoundException should have been thrown"); } catch (UsernameNotFoundException e) { } assertFalse(result != null); } public void testBadConfigMissingAuthenticationDao() { SwitchUserProcessingFilter filter = new SwitchUserProcessingFilter(); filter.setSwitchUserUrl("/j_acegi_switch_user"); filter.setExitUserUrl("/j_acegi_exit_user"); filter.setTargetUrl("/main.jsp"); try { filter.afterPropertiesSet(); fail("Expect to fail due to missing 'authenticationDao'"); } catch (Exception expected) { // expected exception } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -