|
@@ -17,6 +17,7 @@ package org.springframework.security.oauth2.server.authorization;
|
|
|
|
|
|
import java.time.Instant;
|
|
import java.time.Instant;
|
|
import java.time.temporal.ChronoUnit;
|
|
import java.time.temporal.ChronoUnit;
|
|
|
|
+import java.util.List;
|
|
|
|
|
|
import org.junit.Before;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
@@ -55,6 +56,34 @@ public class InMemoryOAuth2AuthorizationServiceTests {
|
|
this.authorizationService = new InMemoryOAuth2AuthorizationService();
|
|
this.authorizationService = new InMemoryOAuth2AuthorizationService();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void constructorVarargsWhenAuthorizationNullThenThrowIllegalArgumentException() {
|
|
|
|
+ assertThatThrownBy(() -> new InMemoryOAuth2AuthorizationService((OAuth2Authorization) null))
|
|
|
|
+ .isInstanceOf(IllegalArgumentException.class)
|
|
|
|
+ .hasMessage("authorization cannot be null");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void constructorListWhenAuthorizationsNullThenThrowIllegalArgumentException() {
|
|
|
|
+ assertThatThrownBy(() -> new InMemoryOAuth2AuthorizationService((List<OAuth2Authorization>) null))
|
|
|
|
+ .isInstanceOf(IllegalArgumentException.class)
|
|
|
|
+ .hasMessage("authorizations cannot be null");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void constructorWhenDuplicateAuthorizationsThenThrowIllegalArgumentException() {
|
|
|
|
+ OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
|
|
+ .id(ID)
|
|
|
|
+ .principalName(PRINCIPAL_NAME)
|
|
|
|
+ .authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
|
|
+ .token(AUTHORIZATION_CODE)
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ assertThatThrownBy(() -> new InMemoryOAuth2AuthorizationService(authorization, authorization))
|
|
|
|
+ .isInstanceOf(IllegalArgumentException.class)
|
|
|
|
+ .hasMessage("The authorization must be unique. Found duplicate identifier: id");
|
|
|
|
+ }
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void saveWhenAuthorizationNullThenThrowIllegalArgumentException() {
|
|
public void saveWhenAuthorizationNullThenThrowIllegalArgumentException() {
|
|
assertThatThrownBy(() -> this.authorizationService.save(null))
|
|
assertThatThrownBy(() -> this.authorizationService.save(null))
|
|
@@ -63,7 +92,7 @@ public class InMemoryOAuth2AuthorizationServiceTests {
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
- public void saveWhenAuthorizationProvidedThenSaved() {
|
|
|
|
|
|
+ public void saveWhenAuthorizationNewThenSaved() {
|
|
OAuth2Authorization expectedAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
OAuth2Authorization expectedAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
.id(ID)
|
|
.id(ID)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.principalName(PRINCIPAL_NAME)
|
|
@@ -77,23 +106,30 @@ public class InMemoryOAuth2AuthorizationServiceTests {
|
|
assertThat(authorization).isEqualTo(expectedAuthorization);
|
|
assertThat(authorization).isEqualTo(expectedAuthorization);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // gh-222
|
|
@Test
|
|
@Test
|
|
- public void saveWhenAuthorizationNotUniqueThenThrowIllegalArgumentException() {
|
|
|
|
- OAuth2Authorization expectedAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
|
|
|
|
+ public void saveWhenAuthorizationExistsThenUpdated() {
|
|
|
|
+ OAuth2Authorization originalAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
.id(ID)
|
|
.id(ID)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
.token(AUTHORIZATION_CODE)
|
|
.token(AUTHORIZATION_CODE)
|
|
.build();
|
|
.build();
|
|
- this.authorizationService.save(expectedAuthorization);
|
|
|
|
|
|
+ this.authorizationService.save(originalAuthorization);
|
|
|
|
|
|
OAuth2Authorization authorization = this.authorizationService.findById(
|
|
OAuth2Authorization authorization = this.authorizationService.findById(
|
|
- expectedAuthorization.getId());
|
|
|
|
- assertThat(authorization).isEqualTo(expectedAuthorization);
|
|
|
|
|
|
+ originalAuthorization.getId());
|
|
|
|
+ assertThat(authorization).isEqualTo(originalAuthorization);
|
|
|
|
|
|
- assertThatThrownBy(() -> this.authorizationService.save(authorization))
|
|
|
|
- .isInstanceOf(IllegalArgumentException.class)
|
|
|
|
- .hasMessage("The authorization must be unique. Found duplicate identifier: " + ID);
|
|
|
|
|
|
+ OAuth2Authorization updatedAuthorization = OAuth2Authorization.from(authorization)
|
|
|
|
+ .attribute("custom-name-1", "custom-value-1")
|
|
|
|
+ .build();
|
|
|
|
+ this.authorizationService.save(updatedAuthorization);
|
|
|
|
+
|
|
|
|
+ authorization = this.authorizationService.findById(
|
|
|
|
+ updatedAuthorization.getId());
|
|
|
|
+ assertThat(authorization).isEqualTo(updatedAuthorization);
|
|
|
|
+ assertThat(authorization).isNotEqualTo(originalAuthorization);
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|