|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.security.oauth2.sso; |
| 18 | + |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | + |
| 22 | +import org.springframework.beans.factory.ObjectProvider; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration; |
| 25 | +import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; |
| 26 | +import org.springframework.boot.test.context.SpringBootTest; |
| 27 | +import org.springframework.context.ApplicationContext; |
| 28 | +import org.springframework.context.annotation.Bean; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.context.annotation.Import; |
| 31 | +import org.springframework.context.annotation.Primary; |
| 32 | +import org.springframework.test.annotation.DirtiesContext; |
| 33 | +import org.springframework.test.context.TestPropertySource; |
| 34 | +import org.springframework.test.context.junit4.SpringRunner; |
| 35 | +import org.springframework.web.client.RestTemplate; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | +import static org.mockito.Mockito.mock; |
| 39 | +import static org.mockito.Mockito.verifyZeroInteractions; |
| 40 | + |
| 41 | +/** |
| 42 | + * Test to validate that a custom {@link RestTemplate} can be defined |
| 43 | + * with OAuth2 SSO. |
| 44 | + * |
| 45 | + * @author Stephane Nicoll |
| 46 | + */ |
| 47 | +@RunWith(SpringRunner.class) |
| 48 | +@DirtiesContext |
| 49 | +@SpringBootTest |
| 50 | +@TestPropertySource(properties = {"security.oauth2.client.clientId=client", |
| 51 | + "security.oauth2.client.clientSecret=secret", |
| 52 | + "security.oauth2.client.userAuthorizationUri=http://example.com/oauth/authorize", |
| 53 | + "security.oauth2.client.accessTokenUri=http://example.com/oauth/token", |
| 54 | + "security.oauth2.resource.jwt.keyValue=SSSSHHH"}) |
| 55 | +public class CustomRestTemplateBasicOAuth2SsoConfigurationTests { |
| 56 | + |
| 57 | + @Autowired |
| 58 | + private ApplicationContext applicationContext; |
| 59 | + |
| 60 | + @Autowired |
| 61 | + private ObjectProvider<RestTemplate> restTemplateProvider; |
| 62 | + |
| 63 | + @Test |
| 64 | + public void customRestTemplateCanBePrimary() { |
| 65 | + RestTemplate restTemplate = this.restTemplateProvider.getIfAvailable(); |
| 66 | + verifyZeroInteractions(restTemplate); |
| 67 | + assertThat(this.applicationContext.getBeansOfType(RestTemplate.class)).hasSize(2); |
| 68 | + } |
| 69 | + |
| 70 | + @Configuration |
| 71 | + @Import(OAuth2AutoConfiguration.class) |
| 72 | + @EnableOAuth2Sso |
| 73 | + @MinimalSecureWebConfiguration |
| 74 | + protected static class TestConfiguration { |
| 75 | + |
| 76 | + @Bean |
| 77 | + @Primary |
| 78 | + public RestTemplate myRestTemplate() { |
| 79 | + return mock(RestTemplate.class); |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | +} |
| 85 | + |
| 86 | + |
0 commit comments