|
@@ -40,6 +40,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
* @author Joe Grandja
|
|
* @author Joe Grandja
|
|
*/
|
|
*/
|
|
public class InMemoryOAuth2AuthorizationServiceTests {
|
|
public class InMemoryOAuth2AuthorizationServiceTests {
|
|
|
|
+ private static final String ID = "id";
|
|
private static final RegisteredClient REGISTERED_CLIENT = TestRegisteredClients.registeredClient().build();
|
|
private static final RegisteredClient REGISTERED_CLIENT = TestRegisteredClients.registeredClient().build();
|
|
private static final String PRINCIPAL_NAME = "principal";
|
|
private static final String PRINCIPAL_NAME = "principal";
|
|
private static final AuthorizationGrantType AUTHORIZATION_GRANT_TYPE = AuthorizationGrantType.AUTHORIZATION_CODE;
|
|
private static final AuthorizationGrantType AUTHORIZATION_GRANT_TYPE = AuthorizationGrantType.AUTHORIZATION_CODE;
|
|
@@ -64,6 +65,7 @@ public class InMemoryOAuth2AuthorizationServiceTests {
|
|
@Test
|
|
@Test
|
|
public void saveWhenAuthorizationProvidedThenSaved() {
|
|
public void saveWhenAuthorizationProvidedThenSaved() {
|
|
OAuth2Authorization expectedAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
OAuth2Authorization expectedAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
|
|
+ .id(ID)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
.token(AUTHORIZATION_CODE)
|
|
.token(AUTHORIZATION_CODE)
|
|
@@ -75,6 +77,25 @@ public class InMemoryOAuth2AuthorizationServiceTests {
|
|
assertThat(authorization).isEqualTo(expectedAuthorization);
|
|
assertThat(authorization).isEqualTo(expectedAuthorization);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void saveWhenAuthorizationNotUniqueThenThrowIllegalArgumentException() {
|
|
|
|
+ OAuth2Authorization expectedAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
|
|
+ .id(ID)
|
|
|
|
+ .principalName(PRINCIPAL_NAME)
|
|
|
|
+ .authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
|
|
+ .token(AUTHORIZATION_CODE)
|
|
|
|
+ .build();
|
|
|
|
+ this.authorizationService.save(expectedAuthorization);
|
|
|
|
+
|
|
|
|
+ OAuth2Authorization authorization = this.authorizationService.findById(
|
|
|
|
+ expectedAuthorization.getId());
|
|
|
|
+ assertThat(authorization).isEqualTo(expectedAuthorization);
|
|
|
|
+
|
|
|
|
+ assertThatThrownBy(() -> this.authorizationService.save(authorization))
|
|
|
|
+ .isInstanceOf(IllegalArgumentException.class)
|
|
|
|
+ .hasMessage("The authorization must be unique. Found duplicate identifier: " + ID);
|
|
|
|
+ }
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void removeWhenAuthorizationNullThenThrowIllegalArgumentException() {
|
|
public void removeWhenAuthorizationNullThenThrowIllegalArgumentException() {
|
|
assertThatThrownBy(() -> this.authorizationService.remove(null))
|
|
assertThatThrownBy(() -> this.authorizationService.remove(null))
|
|
@@ -85,6 +106,7 @@ public class InMemoryOAuth2AuthorizationServiceTests {
|
|
@Test
|
|
@Test
|
|
public void removeWhenAuthorizationProvidedThenRemoved() {
|
|
public void removeWhenAuthorizationProvidedThenRemoved() {
|
|
OAuth2Authorization expectedAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
OAuth2Authorization expectedAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
|
|
+ .id(ID)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
.token(AUTHORIZATION_CODE)
|
|
.token(AUTHORIZATION_CODE)
|
|
@@ -101,6 +123,13 @@ public class InMemoryOAuth2AuthorizationServiceTests {
|
|
assertThat(authorization).isNull();
|
|
assertThat(authorization).isNull();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void findByIdWhenIdNullThenThrowIllegalArgumentException() {
|
|
|
|
+ assertThatThrownBy(() -> this.authorizationService.findById(null))
|
|
|
|
+ .isInstanceOf(IllegalArgumentException.class)
|
|
|
|
+ .hasMessage("id cannot be empty");
|
|
|
|
+ }
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void findByTokenWhenTokenNullThenThrowIllegalArgumentException() {
|
|
public void findByTokenWhenTokenNullThenThrowIllegalArgumentException() {
|
|
assertThatThrownBy(() -> this.authorizationService.findByToken(null, AUTHORIZATION_CODE_TOKEN_TYPE))
|
|
assertThatThrownBy(() -> this.authorizationService.findByToken(null, AUTHORIZATION_CODE_TOKEN_TYPE))
|
|
@@ -112,6 +141,7 @@ public class InMemoryOAuth2AuthorizationServiceTests {
|
|
public void findByTokenWhenStateExistsThenFound() {
|
|
public void findByTokenWhenStateExistsThenFound() {
|
|
String state = "state";
|
|
String state = "state";
|
|
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
|
|
+ .id(ID)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
.attribute(OAuth2ParameterNames.STATE, state)
|
|
.attribute(OAuth2ParameterNames.STATE, state)
|
|
@@ -128,6 +158,7 @@ public class InMemoryOAuth2AuthorizationServiceTests {
|
|
@Test
|
|
@Test
|
|
public void findByTokenWhenAuthorizationCodeExistsThenFound() {
|
|
public void findByTokenWhenAuthorizationCodeExistsThenFound() {
|
|
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
|
|
+ .id(ID)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
.token(AUTHORIZATION_CODE)
|
|
.token(AUTHORIZATION_CODE)
|
|
@@ -146,6 +177,7 @@ public class InMemoryOAuth2AuthorizationServiceTests {
|
|
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
|
|
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
|
|
"access-token", Instant.now().minusSeconds(60), Instant.now());
|
|
"access-token", Instant.now().minusSeconds(60), Instant.now());
|
|
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
|
|
+ .id(ID)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
.token(AUTHORIZATION_CODE)
|
|
.token(AUTHORIZATION_CODE)
|
|
@@ -164,6 +196,7 @@ public class InMemoryOAuth2AuthorizationServiceTests {
|
|
public void findByTokenWhenRefreshTokenExistsThenFound() {
|
|
public void findByTokenWhenRefreshTokenExistsThenFound() {
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", Instant.now());
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", Instant.now());
|
|
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
|
|
+ .id(ID)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.principalName(PRINCIPAL_NAME)
|
|
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
|
|
.refreshToken(refreshToken)
|
|
.refreshToken(refreshToken)
|