|
| 1 | +package org.springframework.social.security; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertSame; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | +import org.junit.Test; |
| 8 | +import org.junit.runner.RunWith; |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.context.annotation.Bean; |
| 12 | +import org.springframework.context.annotation.Configuration; |
| 13 | +import org.springframework.security.authentication.ProviderManager; |
| 14 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 15 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 16 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 17 | +import org.springframework.security.web.FilterChainProxy; |
| 18 | +import org.springframework.security.web.SecurityFilterChain; |
| 19 | +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; |
| 20 | +import org.springframework.social.connect.UsersConnectionRepository; |
| 21 | +import org.springframework.test.context.junit4.SpringRunner; |
| 22 | +import org.springframework.test.util.ReflectionTestUtils; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Florian Lopes |
| 26 | + */ |
| 27 | +@RunWith(SpringRunner.class) |
| 28 | +public class SpringSocialConfigurerTest { |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private FilterChainProxy springSecurityFilterChain; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private UsersConnectionRepository usersConnectionRepository; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private SocialAuthenticationServiceLocator socialAuthenticationServiceLocator; |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testConfigure() { |
| 41 | + final SecurityFilterChain defaultFilterChain = this.springSecurityFilterChain.getFilterChains().get(0); |
| 42 | + assertTrue(defaultFilterChain.getFilters().stream().anyMatch(filter -> filter instanceof SocialAuthenticationFilter)); |
| 43 | + |
| 44 | + final SocialAuthenticationFilter socialAuthenticationFilter = |
| 45 | + (SocialAuthenticationFilter) defaultFilterChain.getFilters() |
| 46 | + .stream().filter(filter -> filter instanceof SocialAuthenticationFilter).findFirst().orElse(null); |
| 47 | + assertNotNull(socialAuthenticationFilter); |
| 48 | + |
| 49 | + assertTrue(ReflectionTestUtils.getField(socialAuthenticationFilter, "userIdSource") instanceof AuthenticationNameUserIdSource); |
| 50 | + |
| 51 | + final ProviderManager providerManager = |
| 52 | + (ProviderManager) ReflectionTestUtils.getField(socialAuthenticationFilter, "authenticationManager"); |
| 53 | + assertTrue(providerManager.getProviders().stream().anyMatch(authenticationProvider -> authenticationProvider instanceof SocialAuthenticationProvider)); |
| 54 | + |
| 55 | + assertNotNull(ReflectionTestUtils.getField(socialAuthenticationFilter, "rememberMeServices")); |
| 56 | + final SocialAuthenticationFailureHandler failureHandler = |
| 57 | + (SocialAuthenticationFailureHandler) ReflectionTestUtils.getField(socialAuthenticationFilter, "failureHandler"); |
| 58 | + |
| 59 | + assertEquals("/postFailure", ReflectionTestUtils.getField(failureHandler.getDelegate(), "defaultFailureUrl")); |
| 60 | + final AuthenticationSuccessHandler successHandler = |
| 61 | + (AuthenticationSuccessHandler) ReflectionTestUtils.getField(socialAuthenticationFilter, "successHandler"); |
| 62 | + assertEquals("/postLogin", ReflectionTestUtils.getField(successHandler, "defaultTargetUrl")); |
| 63 | + assertEquals("/social-login", ReflectionTestUtils.getField(socialAuthenticationFilter, "filterProcessesUrl")); |
| 64 | + assertEquals("/connectionAdded", ReflectionTestUtils.getField(socialAuthenticationFilter, "connectionAddedRedirectUrl")); |
| 65 | + assertEquals("/signup", ReflectionTestUtils.getField(socialAuthenticationFilter, "signupUrl")); |
| 66 | + |
| 67 | + assertSame(this.usersConnectionRepository, socialAuthenticationFilter.getUsersConnectionRepository()); |
| 68 | + assertSame(this.socialAuthenticationServiceLocator, socialAuthenticationFilter.getAuthServiceLocator()); |
| 69 | + } |
| 70 | + |
| 71 | + @EnableWebSecurity |
| 72 | + @Configuration |
| 73 | + static class SpringSocialSecurityConfig extends WebSecurityConfigurerAdapter { |
| 74 | + |
| 75 | + // @formatter:off |
| 76 | + @Override |
| 77 | + protected void configure(HttpSecurity http) throws Exception { |
| 78 | + http |
| 79 | + .rememberMe() |
| 80 | + .and() |
| 81 | + .apply(new SpringSocialConfigurer() |
| 82 | + .userIdSource(new AuthenticationNameUserIdSource()) |
| 83 | + .postLoginUrl("/postLogin") |
| 84 | + .postFailureUrl("/postFailure") |
| 85 | + .signupUrl("/signup") |
| 86 | + .connectionAddedRedirectUrl("/connectionAdded") |
| 87 | + .filterProcessesUrl("/social-login")); |
| 88 | + } |
| 89 | + // @formatter:on |
| 90 | + |
| 91 | + @Bean |
| 92 | + public UsersConnectionRepository usersConnectionRepository() { |
| 93 | + return mock(UsersConnectionRepository.class); |
| 94 | + } |
| 95 | + |
| 96 | + @Bean |
| 97 | + public SocialUserDetailsService socialUserDetailsService() { |
| 98 | + return mock(SocialUserDetailsService.class); |
| 99 | + } |
| 100 | + |
| 101 | + @Bean |
| 102 | + public SocialAuthenticationServiceLocator socialAuthenticationServiceLocator() { |
| 103 | + return mock(SocialAuthenticationServiceLocator.class); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments