SuperX-Kernmodul
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

119 lines
5.1 KiB

package de.superx.saiku;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
import org.springframework.security.web.context.SecurityContextRepository;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* Test for class {@linkplain de.superx.saiku.SuperxAuthenticationProvider}
* @author hiber
*/
public class SuperxAuthenticationProviderTest {
private SuperxAuthenticationProvider superxAuthenticationProvider = new SuperxAuthenticationProvider();
@Mock
private Authentication authentication;
@Mock
private UserDetails userDetails;
@Mock
private SecurityContextRepository securityContextRepository;
@Before
public void setupMock() {
this.authentication = Mockito.mock(Authentication.class);
this.userDetails = Mockito.mock(UserDetails.class);
this.securityContextRepository = Mockito.mock(SecurityContextRepository.class);
}
/**
* Test for method {@linkplain de.superx.saiku.SuperxAuthenticationProvider#authenticate(Authentication)}
* Test without principals should throw exception.
*/
@Test
public void testAuthenticateWithoutPrincipal() {
final String EXPECTED_EXCEPTION_MESSAGE = "No pre-authenticated principal found in request.";
Exception expectedException = null;
try {
this.superxAuthenticationProvider.authenticate(this.authentication);
}catch(Exception e) {
expectedException = e;
}
assertNotNull("method throws exception", expectedException);
assertTrue("thrown exception instance of 'BadCredentialsException'", expectedException instanceof BadCredentialsException);
assertEquals("exception message eqals expected message", EXPECTED_EXCEPTION_MESSAGE, expectedException.getMessage());
}
/**
* Test for method {@linkplain de.superx.saiku.SuperxAuthenticationProvider#authenticate(Authentication)}
* Test without credentials should throw exception.
*/
@Test
public void testAuthenticateWithoutCredentials() {
Mockito.when(this.authentication.getPrincipal()).thenReturn(new Object());
final String EXPECTED_EXCEPTION_MESSAGE = "No pre-authenticated credentials found in request.";
Exception expectedException = null;
try {
this.superxAuthenticationProvider.authenticate(this.authentication);
}catch(Exception e) {
expectedException = e;
}
assertNotNull("method throws exception", expectedException);
assertEquals("exception message eqals expected message", EXPECTED_EXCEPTION_MESSAGE, expectedException.getMessage());
assertTrue("thrown exception instance of 'BadCredentialsException'", expectedException instanceof BadCredentialsException);
}
/**
* Test for method {@linkplain de.superx.saiku.SuperxAuthenticationProvider#authenticate(Authentication)}
*/
@Test
public void testAuthenticate() {
// UserDetails mock
Mockito.when(Boolean.valueOf(this.userDetails.isEnabled())).thenReturn(Boolean.valueOf(true));
Mockito.when(Boolean.valueOf(this.userDetails.isCredentialsNonExpired())).thenReturn(Boolean.valueOf(true));
Mockito.when(Boolean.valueOf(this.userDetails.isAccountNonLocked())).thenReturn(Boolean.valueOf(true));
Mockito.when(Boolean.valueOf(this.userDetails.isAccountNonExpired())).thenReturn(Boolean.valueOf(true));
Mockito.when(Boolean.valueOf(this.userDetails.isEnabled())).thenReturn(Boolean.valueOf(true));
Mockito.when(Boolean.valueOf(this.userDetails.isEnabled())).thenReturn(Boolean.valueOf(true));
// Authentication mock
Mockito.when(this.authentication.getPrincipal()).thenReturn(this.userDetails);
Mockito.when(this.authentication.getCredentials()).thenReturn(new Object());
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
MockHttpServletResponse mockResponse = new MockHttpServletResponse();
ServletRequestAttributes attrs = new ServletRequestAttributes(mockRequest, mockResponse);
RequestContextHolder.setRequestAttributes(attrs);
this.superxAuthenticationProvider.setSecurityContextRepository(securityContextRepository);
Authentication resultAuthentication = null;
Exception expectedException = null;
try {
resultAuthentication = this.superxAuthenticationProvider.authenticate(this.authentication);
}catch(Exception e) {
expectedException = e;
e.printStackTrace();
}
assertNull("method does not throw exception", expectedException);
assertTrue("result is instance of 'PreAuthenticatedAuthenticationToken'", resultAuthentication instanceof PreAuthenticatedAuthenticationToken);
assertEquals("details set correctly", resultAuthentication.getDetails(), this.userDetails);
}
}