From 8131173a96f5632fa359ebf62da3aaecc7189ba4 Mon Sep 17 00:00:00 2001 From: Mike Wiesner Date: Wed, 26 Aug 2009 15:30:50 +0000 Subject: [PATCH] SES-1: added some unit tests --- spring-security-kerberos-core/pom.xml | 6 + ...erosServiceAuthenticationProviderTest.java | 97 +++++++++++++++ ...egoAuthenticationProcessingFilterTest.java | 111 ++++++++++++++++++ 3 files changed, 214 insertions(+) create mode 100644 spring-security-kerberos-core/src/test/java/org/springframework/security/extensions/kerberos/KerberosServiceAuthenticationProviderTest.java create mode 100644 spring-security-kerberos-core/src/test/java/org/springframework/security/extensions/kerberos/web/SpnegoAuthenticationProcessingFilterTest.java diff --git a/spring-security-kerberos-core/pom.xml b/spring-security-kerberos-core/pom.xml index 4298d01..be5fe49 100644 --- a/spring-security-kerberos-core/pom.xml +++ b/spring-security-kerberos-core/pom.xml @@ -87,6 +87,12 @@ 4.6 test + + org.mockito + mockito-core + 1.7 + test + org.springframework.security spring-security-core diff --git a/spring-security-kerberos-core/src/test/java/org/springframework/security/extensions/kerberos/KerberosServiceAuthenticationProviderTest.java b/spring-security-kerberos-core/src/test/java/org/springframework/security/extensions/kerberos/KerberosServiceAuthenticationProviderTest.java new file mode 100644 index 0000000..dda053c --- /dev/null +++ b/spring-security-kerberos-core/src/test/java/org/springframework/security/extensions/kerberos/KerberosServiceAuthenticationProviderTest.java @@ -0,0 +1,97 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * 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.springframework.security.extensions.kerberos; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.AuthorityUtils; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; + +/** + * + * @author Mike Wiesner + * @since 1.0 + * @version $Id$ + */ +public class KerberosServiceAuthenticationProviderTest { + + private KerberosServiceAuthenticationProvider provider; + private KerberosTicketValidator ticketValidator; + private UserDetailsService userDetailsService; + + // data + private static final byte[] testToken = "TestToken".getBytes(); + private static final String testuser = "Testuser@SPRINGSOURCE.ORG"; + private static final List authorityList = AuthorityUtils.createAuthorityList("ROLE_ADMIN"); + private static final UserDetails userDetails = new User(testuser, "empty", true, true, true,true, authorityList); + private static final KerberosServiceRequestToken input = new KerberosServiceRequestToken(testToken); + + @Before + public void before() { + // mocking + this.ticketValidator = mock(KerberosTicketValidator.class); + this.userDetailsService = mock(UserDetailsService.class); + this.provider = new KerberosServiceAuthenticationProvider(); + this.provider.setTicketValidator(this.ticketValidator); + this.provider.setUserDetailsService(this.userDetailsService); + } + + @Test + public void testEverythingWorks() throws Exception { + // stubbing + when(ticketValidator.validateTicket(testToken)).thenReturn(testuser); + when(userDetailsService.loadUserByUsername(testuser)).thenReturn(userDetails); + + // testing + Authentication output = provider.authenticate(input); + assertNotNull(output); + assertEquals(testuser, output.getName()); + assertEquals(authorityList, output.getAuthorities()); + assertEquals(userDetails, output.getPrincipal()); + } + + @Test(expected=UsernameNotFoundException.class) + public void testUsernameNotFound() throws Exception { + // stubbing + when(ticketValidator.validateTicket(testToken)).thenReturn(testuser); + when(userDetailsService.loadUserByUsername(testuser)).thenThrow(new UsernameNotFoundException("")); + + // testing + provider.authenticate(input); + } + + @Test(expected=BadCredentialsException.class) + public void testTicketValidationWrong() throws Exception { + // stubbing + when(ticketValidator.validateTicket(testToken)).thenThrow(new BadCredentialsException("")); + + // testing + provider.authenticate(input); + } + +} diff --git a/spring-security-kerberos-core/src/test/java/org/springframework/security/extensions/kerberos/web/SpnegoAuthenticationProcessingFilterTest.java b/spring-security-kerberos-core/src/test/java/org/springframework/security/extensions/kerberos/web/SpnegoAuthenticationProcessingFilterTest.java new file mode 100644 index 0000000..6d9107e --- /dev/null +++ b/spring-security-kerberos-core/src/test/java/org/springframework/security/extensions/kerberos/web/SpnegoAuthenticationProcessingFilterTest.java @@ -0,0 +1,111 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * 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.springframework.security.extensions.kerberos.web; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import javax.servlet.FilterChain; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.authority.AuthorityUtils; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.extensions.kerberos.KerberosServiceRequestToken; + +/** + * + * @author Mike Wiesner + * @since 1.0 + * @version $Id$ + */ +public class SpnegoAuthenticationProcessingFilterTest { + + private SpnegoAuthenticationProcessingFilter filter; + private AuthenticationManager authenticationManager; + private HttpServletRequest request; + private HttpServletResponse response; + private FilterChain chain; + + // data + private static final byte[] testToken = "TestToken".getBytes(); + private static final String testTokenBase64 = "VGVzdFRva2Vu"; + private static final Authentication authentication = new KerberosServiceRequestToken("test", + AuthorityUtils.createAuthorityList("ROLE_ADMIN"), testToken); + + + @Before + public void before() { + // mocking + authenticationManager = mock(AuthenticationManager.class); + filter = new SpnegoAuthenticationProcessingFilter(); + filter.setAuthenticationManager(authenticationManager); + request = mock(HttpServletRequest.class); + response = mock(HttpServletResponse.class); + chain = mock(FilterChain.class); + } + + @Test + public void testEverythingWorks() throws Exception { + // stubbing + when(request.getHeader("Authorization")).thenReturn("Negotiate "+testTokenBase64); + when(authenticationManager.authenticate(new KerberosServiceRequestToken(testToken))).thenReturn(authentication); + + // testing + filter.doFilter(request, response, chain); + verify(chain).doFilter(request, response); + assertEquals(authentication, SecurityContextHolder.getContext().getAuthentication()); + } + + @Test + public void testNoHeader() throws Exception { + filter.doFilter(request, response, chain); + // If the header is not present, the filter is not allowed to call authenticate() + verify(authenticationManager, never()).authenticate(any(Authentication.class)); + // chain should go on + verify(chain).doFilter(request, response); + assertEquals(null, SecurityContextHolder.getContext().getAuthentication()); + } + + @Test + public void testAuthenticationFails() throws Exception { + // stubbing + when(request.getHeader("Authorization")).thenReturn("Negotiate "+testTokenBase64); + when(authenticationManager.authenticate(any(Authentication.class))).thenThrow(new BadCredentialsException("")); + + // testing + filter.doFilter(request, response, chain); + // chain should stop here and it should send back a 500 + // future version should call some error handler + verify(chain, never()).doFilter(any(ServletRequest.class), any(ServletResponse.class)); + verify(response).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } + + @After + public void after() { + SecurityContextHolder.clearContext(); + } + + +}