|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 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 | + * https://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 | +package org.springframework.security.oauth2.client; |
| 17 | + |
| 18 | +import org.junit.Test; |
| 19 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
| 20 | +import org.springframework.security.core.Authentication; |
| 21 | +import org.springframework.security.oauth2.client.registration.TestClientRegistrations; |
| 22 | +import org.springframework.security.oauth2.core.TestOAuth2AccessTokens; |
| 23 | + |
| 24 | +import java.util.Collections; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 28 | +import static org.mockito.ArgumentMatchers.any; |
| 29 | +import static org.mockito.Mockito.mock; |
| 30 | +import static org.mockito.Mockito.when; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests for {@link DelegatingOAuth2AuthorizedClientProvider}. |
| 34 | + * |
| 35 | + * @author Joe Grandja |
| 36 | + */ |
| 37 | +public class DelegatingOAuth2AuthorizedClientProviderTests { |
| 38 | + |
| 39 | + @Test |
| 40 | + public void constructorWhenProvidersIsEmptyThenThrowIllegalArgumentException() { |
| 41 | + assertThatThrownBy(() -> new DelegatingOAuth2AuthorizedClientProvider(new OAuth2AuthorizedClientProvider[0])) |
| 42 | + .isInstanceOf(IllegalArgumentException.class); |
| 43 | + assertThatThrownBy(() -> new DelegatingOAuth2AuthorizedClientProvider(Collections.emptyList())) |
| 44 | + .isInstanceOf(IllegalArgumentException.class); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void authorizeWhenContextIsNullThenThrowIllegalArgumentException() { |
| 49 | + DelegatingOAuth2AuthorizedClientProvider delegate = new DelegatingOAuth2AuthorizedClientProvider( |
| 50 | + mock(OAuth2AuthorizedClientProvider.class)); |
| 51 | + assertThatThrownBy(() -> delegate.authorize(null)) |
| 52 | + .isInstanceOf(IllegalArgumentException.class) |
| 53 | + .hasMessage("context cannot be null"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void authorizeWhenProviderCanAuthorizeThenReturnAuthorizedClient() { |
| 58 | + Authentication principal = new TestingAuthenticationToken("principal", "password"); |
| 59 | + OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient( |
| 60 | + TestClientRegistrations.clientRegistration().build(), principal.getName(), TestOAuth2AccessTokens.noScopes()); |
| 61 | + |
| 62 | + OAuth2AuthorizedClientProvider authorizedClientProvider = mock(OAuth2AuthorizedClientProvider.class); |
| 63 | + when(authorizedClientProvider.authorize(any())).thenReturn(authorizedClient); |
| 64 | + |
| 65 | + DelegatingOAuth2AuthorizedClientProvider delegate = new DelegatingOAuth2AuthorizedClientProvider( |
| 66 | + mock(OAuth2AuthorizedClientProvider.class), mock(OAuth2AuthorizedClientProvider.class), authorizedClientProvider); |
| 67 | + OAuth2AuthorizationContext context = OAuth2AuthorizationContext.reauthorize(authorizedClient).principal(principal).build(); |
| 68 | + OAuth2AuthorizedClient reauthorizedClient = delegate.authorize(context); |
| 69 | + assertThat(reauthorizedClient).isSameAs(authorizedClient); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void authorizeWhenProviderCantAuthorizeThenReturnNull() { |
| 74 | + OAuth2AuthorizationContext context = OAuth2AuthorizationContext |
| 75 | + .authorize(TestClientRegistrations.clientRegistration().build()) |
| 76 | + .principal(new TestingAuthenticationToken("principal", "password")) |
| 77 | + .build(); |
| 78 | + |
| 79 | + DelegatingOAuth2AuthorizedClientProvider delegate = new DelegatingOAuth2AuthorizedClientProvider( |
| 80 | + mock(OAuth2AuthorizedClientProvider.class), mock(OAuth2AuthorizedClientProvider.class)); |
| 81 | + assertThat(delegate.authorize(context)).isNull(); |
| 82 | + } |
| 83 | +} |
0 commit comments