|
@@ -1,11 +1,11 @@
|
|
/*
|
|
/*
|
|
- * Copyright 2002-2018 the original author or authors.
|
|
|
|
|
|
+ * Copyright 2002-2019 the original author or authors.
|
|
*
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
* You may obtain a copy of the License at
|
|
*
|
|
*
|
|
- * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
+ * https://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
@@ -13,15 +13,12 @@
|
|
* See the License for the specific language governing permissions and
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
* limitations under the License.
|
|
*/
|
|
*/
|
|
-
|
|
|
|
package org.springframework.security.oauth2.client;
|
|
package org.springframework.security.oauth2.client;
|
|
|
|
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
|
|
|
|
-import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
|
|
|
-import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
|
|
|
-
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Tests for {@link OAuth2AuthorizedClientId}.
|
|
* Tests for {@link OAuth2AuthorizedClientId}.
|
|
@@ -30,65 +27,59 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|
*/
|
|
*/
|
|
public class OAuth2AuthorizedClientIdTests {
|
|
public class OAuth2AuthorizedClientIdTests {
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void constructorWhenRegistrationIdNullThenThrowIllegalArgumentException() {
|
|
|
|
+ assertThatThrownBy(() -> new OAuth2AuthorizedClientId(null, "test-principal"))
|
|
|
|
+ .isInstanceOf(IllegalArgumentException.class)
|
|
|
|
+ .hasMessage("clientRegistrationId cannot be empty");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void constructorWhenPrincipalNameNullThenThrowIllegalArgumentException() {
|
|
|
|
+ assertThatThrownBy(() -> new OAuth2AuthorizedClientId("test-client", null))
|
|
|
|
+ .isInstanceOf(IllegalArgumentException.class)
|
|
|
|
+ .hasMessage("principalName cannot be empty");
|
|
|
|
+ }
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void equalsWhenSameRegistrationIdAndPrincipalThenShouldReturnTrue() {
|
|
public void equalsWhenSameRegistrationIdAndPrincipalThenShouldReturnTrue() {
|
|
- OAuth2AuthorizedClientId id1 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"),
|
|
|
|
- "test-principal");
|
|
|
|
- OAuth2AuthorizedClientId id2 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"),
|
|
|
|
- "test-principal");
|
|
|
|
|
|
+ OAuth2AuthorizedClientId id1 = new OAuth2AuthorizedClientId("test-client", "test-principal");
|
|
|
|
+ OAuth2AuthorizedClientId id2 = new OAuth2AuthorizedClientId("test-client", "test-principal");
|
|
assertThat(id1.equals(id2)).isTrue();
|
|
assertThat(id1.equals(id2)).isTrue();
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void equalsWhenDifferentRegistrationIdAndSamePrincipalThenShouldReturnFalse() {
|
|
public void equalsWhenDifferentRegistrationIdAndSamePrincipalThenShouldReturnFalse() {
|
|
- OAuth2AuthorizedClientId id1 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client1"),
|
|
|
|
- "test-principal");
|
|
|
|
- OAuth2AuthorizedClientId id2 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client2"),
|
|
|
|
- "test-principal");
|
|
|
|
|
|
+ OAuth2AuthorizedClientId id1 = new OAuth2AuthorizedClientId("test-client1", "test-principal");
|
|
|
|
+ OAuth2AuthorizedClientId id2 = new OAuth2AuthorizedClientId("test-client2", "test-principal");
|
|
assertThat(id1.equals(id2)).isFalse();
|
|
assertThat(id1.equals(id2)).isFalse();
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void equalsWhenSameRegistrationIdAndDifferentPrincipalThenShouldReturnFalse() {
|
|
public void equalsWhenSameRegistrationIdAndDifferentPrincipalThenShouldReturnFalse() {
|
|
- OAuth2AuthorizedClientId id1 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"),
|
|
|
|
- "test-principal1");
|
|
|
|
- OAuth2AuthorizedClientId id2 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"),
|
|
|
|
- "test-principal2");
|
|
|
|
|
|
+ OAuth2AuthorizedClientId id1 = new OAuth2AuthorizedClientId("test-client", "test-principal1");
|
|
|
|
+ OAuth2AuthorizedClientId id2 = new OAuth2AuthorizedClientId("test-client", "test-principal2");
|
|
assertThat(id1.equals(id2)).isFalse();
|
|
assertThat(id1.equals(id2)).isFalse();
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void hashCodeWhenSameRegistrationIdAndPrincipalThenShouldReturnSame() {
|
|
public void hashCodeWhenSameRegistrationIdAndPrincipalThenShouldReturnSame() {
|
|
- OAuth2AuthorizedClientId id1 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"),
|
|
|
|
- "test-principal");
|
|
|
|
- OAuth2AuthorizedClientId id2 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"),
|
|
|
|
- "test-principal");
|
|
|
|
|
|
+ OAuth2AuthorizedClientId id1 = new OAuth2AuthorizedClientId("test-client", "test-principal");
|
|
|
|
+ OAuth2AuthorizedClientId id2 = new OAuth2AuthorizedClientId("test-client", "test-principal");
|
|
assertThat(id1.hashCode()).isEqualTo(id2.hashCode());
|
|
assertThat(id1.hashCode()).isEqualTo(id2.hashCode());
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void hashCodeWhenDifferentRegistrationIdAndSamePrincipalThenShouldNotReturnSame() {
|
|
public void hashCodeWhenDifferentRegistrationIdAndSamePrincipalThenShouldNotReturnSame() {
|
|
- OAuth2AuthorizedClientId id1 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client1"),
|
|
|
|
- "test-principal");
|
|
|
|
- OAuth2AuthorizedClientId id2 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client2"),
|
|
|
|
- "test-principal");
|
|
|
|
|
|
+ OAuth2AuthorizedClientId id1 = new OAuth2AuthorizedClientId("test-client1", "test-principal");
|
|
|
|
+ OAuth2AuthorizedClientId id2 = new OAuth2AuthorizedClientId("test-client2", "test-principal");
|
|
assertThat(id1.hashCode()).isNotEqualTo(id2.hashCode());
|
|
assertThat(id1.hashCode()).isNotEqualTo(id2.hashCode());
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void hashCodeWhenSameRegistrationIdAndDifferentPrincipalThenShouldNotReturnSame() {
|
|
public void hashCodeWhenSameRegistrationIdAndDifferentPrincipalThenShouldNotReturnSame() {
|
|
- OAuth2AuthorizedClientId id1 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"),
|
|
|
|
- "test-principal1");
|
|
|
|
- OAuth2AuthorizedClientId id2 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"),
|
|
|
|
- "test-principal2");
|
|
|
|
|
|
+ OAuth2AuthorizedClientId id1 = new OAuth2AuthorizedClientId("test-client", "test-principal1");
|
|
|
|
+ OAuth2AuthorizedClientId id2 = new OAuth2AuthorizedClientId("test-client", "test-principal2");
|
|
assertThat(id1.hashCode()).isNotEqualTo(id2.hashCode());
|
|
assertThat(id1.hashCode()).isNotEqualTo(id2.hashCode());
|
|
}
|
|
}
|
|
-
|
|
|
|
- private static ClientRegistration testClientRegistration(String registrationId) {
|
|
|
|
- return ClientRegistration.withRegistrationId(registrationId).clientId("id").clientSecret("secret")
|
|
|
|
- .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
|
|
|
|
- .redirectUriTemplate("{baseUrl}/{action}/oauth2/code/{registrationId}")
|
|
|
|
- .authorizationUri("http://example.com/authorize").tokenUri("http://example.com/token").build();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
}
|
|
}
|