16 changed files with 408 additions and 392 deletions
@ -1,152 +0,0 @@
@@ -1,152 +0,0 @@
|
||||
/* |
||||
* Copyright 2020-2021 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.security.oauth2.server.authorization; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority; |
||||
|
||||
import java.util.List; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
||||
|
||||
/** |
||||
* Tests for {@link InMemoryOAuth2AuthorizationConsentService}. |
||||
* |
||||
* @author Daniel Garnier-Moiroux |
||||
*/ |
||||
public class InMemoryOAuth2AuthorizationConsentServiceTest { |
||||
private InMemoryOAuth2AuthorizationConsentService consentService; |
||||
|
||||
private static final String CLIENT_ID = "client-id"; |
||||
private static final String PRINCIPAL_NAME = "principal-name"; |
||||
private static final OAuth2AuthorizationConsent CONSENT = OAuth2AuthorizationConsent |
||||
.withId(CLIENT_ID, PRINCIPAL_NAME) |
||||
.authority(new SimpleGrantedAuthority("some.authority")) |
||||
.build(); |
||||
|
||||
@Before |
||||
public void setUp() throws Exception { |
||||
this.consentService = new InMemoryOAuth2AuthorizationConsentService(); |
||||
this.consentService.save(CONSENT); |
||||
} |
||||
|
||||
@Test |
||||
public void constructorVaragsWhenAuthorizationConsentNullThenThrowIllegalArgumentException() { |
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> new InMemoryOAuth2AuthorizationConsentService((OAuth2AuthorizationConsent) null)) |
||||
.withMessage("authorizationConsent cannot be null"); |
||||
} |
||||
|
||||
@Test |
||||
public void constructorListWhenAuthorizationConsentsNullThenThrowIllegalArgumentException() { |
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> new InMemoryOAuth2AuthorizationConsentService((List<OAuth2AuthorizationConsent>) null)) |
||||
.withMessage("authorizationConsents cannot be null"); |
||||
} |
||||
|
||||
@Test |
||||
public void constructorWhenDuplicateAuthorizationConsentsThenThrowIllegalArgumentException() { |
||||
OAuth2AuthorizationConsent authorizationConsent = OAuth2AuthorizationConsent.withId("client-id", "principal-name") |
||||
.scope("thing.write") // must have at least one scope
|
||||
.build(); |
||||
|
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> new InMemoryOAuth2AuthorizationConsentService(authorizationConsent, authorizationConsent)) |
||||
.withMessage("The authorizationConsent must be unique. Found duplicate, with registered client id: [client-id] and principal name: [principal-name]"); |
||||
} |
||||
|
||||
@Test |
||||
public void saveWhenConsentNullThenThrowIllegalArgumentException() { |
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> this.consentService.save(null)) |
||||
.withMessage("authorizationConsent cannot be null"); |
||||
} |
||||
|
||||
@Test |
||||
public void saveWhenConsentNewThenSaved() { |
||||
OAuth2AuthorizationConsent expectedConsent = OAuth2AuthorizationConsent |
||||
.withId("new-client", "new-principal") |
||||
.authority(new SimpleGrantedAuthority("new.authority")) |
||||
.build(); |
||||
|
||||
this.consentService.save(expectedConsent); |
||||
|
||||
OAuth2AuthorizationConsent consent = |
||||
this.consentService.findById("new-client", "new-principal"); |
||||
assertThat(consent).isEqualTo(expectedConsent); |
||||
} |
||||
|
||||
@Test |
||||
public void saveWhenConsentExistsThenUpdated() { |
||||
OAuth2AuthorizationConsent expectedConsent = OAuth2AuthorizationConsent |
||||
.from(CONSENT) |
||||
.authority(new SimpleGrantedAuthority("new.authority")) |
||||
.build(); |
||||
|
||||
this.consentService.save(expectedConsent); |
||||
|
||||
OAuth2AuthorizationConsent consent = |
||||
this.consentService.findById(CLIENT_ID, PRINCIPAL_NAME); |
||||
assertThat(consent).isEqualTo(expectedConsent); |
||||
assertThat(consent).isNotEqualTo(CONSENT); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void removeNullThenThrowIllegalArgumentException() { |
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> this.consentService.remove(null)) |
||||
.withMessage("authorizationConsent cannot be null"); |
||||
} |
||||
|
||||
@Test |
||||
public void removeWhenConsentProvidedThenRemoved() { |
||||
this.consentService.remove(CONSENT); |
||||
|
||||
assertThat(this.consentService.findById(CLIENT_ID, PRINCIPAL_NAME)) |
||||
.isNull(); |
||||
} |
||||
|
||||
@Test |
||||
public void findWhenRegisteredClientIdNullThenThrowIllegalArgumentException() { |
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> this.consentService.findById(null, "some-user")) |
||||
.withMessage("registeredClientId cannot be empty"); |
||||
} |
||||
|
||||
@Test |
||||
public void findWhenPrincipalNameNullThenThrowIllegalArgumentException() { |
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> this.consentService.findById("some-client", null)) |
||||
.withMessage("principalName cannot be empty"); |
||||
} |
||||
|
||||
@Test |
||||
public void findWhenConsentExistsThenFound() { |
||||
assertThat(this.consentService.findById(CLIENT_ID, PRINCIPAL_NAME)) |
||||
.isEqualTo(CONSENT); |
||||
} |
||||
|
||||
@Test |
||||
public void findWhenConsentDoesNotExistThenNull() { |
||||
this.consentService.save(CONSENT); |
||||
|
||||
assertThat(this.consentService.findById("unknown-client", PRINCIPAL_NAME)).isNull(); |
||||
assertThat(this.consentService.findById(CLIENT_ID, "unkown-user")).isNull(); |
||||
} |
||||
} |
||||
@ -0,0 +1,148 @@
@@ -0,0 +1,148 @@
|
||||
/* |
||||
* Copyright 2020-2021 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.security.oauth2.server.authorization; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
||||
|
||||
/** |
||||
* Tests for {@link InMemoryOAuth2AuthorizationConsentService}. |
||||
* |
||||
* @author Daniel Garnier-Moiroux |
||||
*/ |
||||
public class InMemoryOAuth2AuthorizationConsentServiceTests { |
||||
private static final String REGISTERED_CLIENT_ID = "registered-client-id"; |
||||
private static final String PRINCIPAL_NAME = "principal-name"; |
||||
private static final OAuth2AuthorizationConsent AUTHORIZATION_CONSENT = |
||||
OAuth2AuthorizationConsent.withId(REGISTERED_CLIENT_ID, PRINCIPAL_NAME) |
||||
.authority(new SimpleGrantedAuthority("some.authority")) |
||||
.build(); |
||||
|
||||
private InMemoryOAuth2AuthorizationConsentService authorizationConsentService; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
this.authorizationConsentService = new InMemoryOAuth2AuthorizationConsentService(); |
||||
this.authorizationConsentService.save(AUTHORIZATION_CONSENT); |
||||
} |
||||
|
||||
@Test |
||||
public void constructorVarargsWhenAuthorizationConsentNullThenThrowIllegalArgumentException() { |
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> new InMemoryOAuth2AuthorizationConsentService((OAuth2AuthorizationConsent) null)) |
||||
.withMessage("authorizationConsent cannot be null"); |
||||
} |
||||
|
||||
@Test |
||||
public void constructorListWhenAuthorizationConsentsNullThenThrowIllegalArgumentException() { |
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> new InMemoryOAuth2AuthorizationConsentService((List<OAuth2AuthorizationConsent>) null)) |
||||
.withMessage("authorizationConsents cannot be null"); |
||||
} |
||||
|
||||
@Test |
||||
public void constructorWhenDuplicateAuthorizationConsentsThenThrowIllegalArgumentException() { |
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> new InMemoryOAuth2AuthorizationConsentService(AUTHORIZATION_CONSENT, AUTHORIZATION_CONSENT)) |
||||
.withMessage("The authorizationConsent must be unique. Found duplicate, with registered client id: [registered-client-id] and principal name: [principal-name]"); |
||||
} |
||||
|
||||
@Test |
||||
public void saveWhenAuthorizationConsentNullThenThrowIllegalArgumentException() { |
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> this.authorizationConsentService.save(null)) |
||||
.withMessage("authorizationConsent cannot be null"); |
||||
} |
||||
|
||||
@Test |
||||
public void saveWhenAuthorizationConsentNewThenSaved() { |
||||
OAuth2AuthorizationConsent expectedAuthorizationConsent = |
||||
OAuth2AuthorizationConsent.withId("new-client", "new-principal") |
||||
.authority(new SimpleGrantedAuthority("new.authority")) |
||||
.build(); |
||||
|
||||
this.authorizationConsentService.save(expectedAuthorizationConsent); |
||||
|
||||
OAuth2AuthorizationConsent authorizationConsent = |
||||
this.authorizationConsentService.findById("new-client", "new-principal"); |
||||
assertThat(authorizationConsent).isEqualTo(expectedAuthorizationConsent); |
||||
} |
||||
|
||||
@Test |
||||
public void saveWhenAuthorizationConsentExistsThenUpdated() { |
||||
OAuth2AuthorizationConsent expectedAuthorizationConsent = |
||||
OAuth2AuthorizationConsent.from(AUTHORIZATION_CONSENT) |
||||
.authority(new SimpleGrantedAuthority("new.authority")) |
||||
.build(); |
||||
|
||||
this.authorizationConsentService.save(expectedAuthorizationConsent); |
||||
|
||||
OAuth2AuthorizationConsent authorizationConsent = |
||||
this.authorizationConsentService.findById( |
||||
AUTHORIZATION_CONSENT.getRegisteredClientId(), AUTHORIZATION_CONSENT.getPrincipalName()); |
||||
assertThat(authorizationConsent).isEqualTo(expectedAuthorizationConsent); |
||||
assertThat(authorizationConsent).isNotEqualTo(AUTHORIZATION_CONSENT); |
||||
} |
||||
|
||||
@Test |
||||
public void removeWhenAuthorizationConsentNullThenThrowIllegalArgumentException() { |
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> this.authorizationConsentService.remove(null)) |
||||
.withMessage("authorizationConsent cannot be null"); |
||||
} |
||||
|
||||
@Test |
||||
public void removeWhenAuthorizationConsentProvidedThenRemoved() { |
||||
this.authorizationConsentService.remove(AUTHORIZATION_CONSENT); |
||||
assertThat(this.authorizationConsentService.findById( |
||||
AUTHORIZATION_CONSENT.getRegisteredClientId(), AUTHORIZATION_CONSENT.getPrincipalName())) |
||||
.isNull(); |
||||
} |
||||
|
||||
@Test |
||||
public void findByIdWhenRegisteredClientIdNullThenThrowIllegalArgumentException() { |
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> this.authorizationConsentService.findById(null, "some-user")) |
||||
.withMessage("registeredClientId cannot be empty"); |
||||
} |
||||
|
||||
@Test |
||||
public void findByIdWhenPrincipalNameNullThenThrowIllegalArgumentException() { |
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> this.authorizationConsentService.findById("some-client", null)) |
||||
.withMessage("principalName cannot be empty"); |
||||
} |
||||
|
||||
@Test |
||||
public void findByIdWhenAuthorizationConsentExistsThenFound() { |
||||
assertThat(this.authorizationConsentService.findById(REGISTERED_CLIENT_ID, PRINCIPAL_NAME)) |
||||
.isEqualTo(AUTHORIZATION_CONSENT); |
||||
} |
||||
|
||||
@Test |
||||
public void findByIdWhenAuthorizationConsentDoesNotExistThenNull() { |
||||
this.authorizationConsentService.save(AUTHORIZATION_CONSENT); |
||||
assertThat(this.authorizationConsentService.findById("unknown-client", PRINCIPAL_NAME)).isNull(); |
||||
assertThat(this.authorizationConsentService.findById(REGISTERED_CLIENT_ID, "unknown-user")).isNull(); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue