SES-1: added some unit tests

This commit is contained in:
Mike Wiesner
2009-08-26 15:30:50 +00:00
parent 0cf070a457
commit 8131173a96
3 changed files with 214 additions and 0 deletions

View File

@@ -87,6 +87,12 @@
<version>4.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>

View File

@@ -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<GrantedAuthority> 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);
}
}

View File

@@ -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();
}
}