Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Aug 7, 2024
1 parent aba1b26 commit c670e3f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Result<TokenRepresentation> generate(String privateKeyId, @NotNull TokenD

var tokenSignerResult = jwsGeneratorFunction.createJwsSigner(privateKeyId);
if (tokenSignerResult.failed()) {
return Result.failure("JWSSigner cannot be generated for private key '%s'".formatted(tokenSignerResult.getFailureDetail()));
return Result.failure("JWSSigner cannot be generated for private key '%s': %s".formatted(privateKeyId, tokenSignerResult.getFailureDetail()));
}

var tokenSigner = tokenSignerResult.getContent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import com.nimbusds.jose.jwk.RSAKey;
import com.nimbusds.jose.jwk.gen.RSAKeyGenerator;
import com.nimbusds.jwt.SignedJWT;
import org.assertj.core.api.AssertionsForClassTypes;
import org.eclipse.edc.connector.dataplane.http.spi.HttpDataAddress;
import org.eclipse.edc.iam.oauth2.spi.client.PrivateKeyOauth2CredentialsRequest;
import org.eclipse.edc.iam.oauth2.spi.client.SharedSecretOauth2CredentialsRequest;
import org.eclipse.edc.jwt.signer.spi.JwsSignerProvider;
import org.eclipse.edc.spi.result.Result;
import org.eclipse.edc.spi.security.Vault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.sql.Date;
Expand All @@ -45,6 +47,7 @@
import static org.eclipse.edc.iam.oauth2.spi.Oauth2DataAddressSchema.SCOPE;
import static org.eclipse.edc.iam.oauth2.spi.Oauth2DataAddressSchema.TOKEN_URL;
import static org.eclipse.edc.iam.oauth2.spi.Oauth2DataAddressSchema.VALIDITY;
import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
Expand All @@ -55,8 +58,13 @@ class Oauth2CredentialsRequestFactoryTest {
private final Clock clock = Clock.fixed(now, UTC);
private final JwsSignerProvider privateKeyResolver = mock();
private final Vault vault = mock();
private final Oauth2CredentialsRequestFactory factory = new Oauth2CredentialsRequestFactory(privateKeyResolver, clock, vault);

private Oauth2CredentialsRequestFactory factory;

@BeforeEach
void setUp() {
factory = new Oauth2CredentialsRequestFactory(privateKeyResolver, clock, vault);
}

@Test
void shouldCreateSharedSecretRequestWithKey_whenPrivateKeyNameIsAbsent() {
when(vault.resolveSecret("clientSecretKey")).thenReturn("clientSecret");
Expand All @@ -67,7 +75,7 @@ void shouldCreateSharedSecretRequestWithKey_whenPrivateKeyNameIsAbsent() {

var result = factory.create(address);

assertThat(result).matches(Result::succeeded).extracting(Result::getContent)
AssertionsForClassTypes.assertThat(result).matches(Result::succeeded).extracting(Result::getContent)
.asInstanceOf(type(SharedSecretOauth2CredentialsRequest.class))
.satisfies(request -> {
assertThat(request.getGrantType()).isEqualTo("client_credentials");
Expand All @@ -89,7 +97,7 @@ void shouldFail_whenClientSecretCannotBeObtained() {

var result = factory.create(address);

assertThat(result).matches(Result::failed);
AssertionsForClassTypes.assertThat(result).matches(Result::failed);
}

@Test
Expand All @@ -104,7 +112,7 @@ void shouldCreatePrivateKeyRequest_whenPrivateKeyNameIsPresent() throws JOSEExce

var result = factory.create(address);

assertThat(result).matches(Result::succeeded).extracting(Result::getContent)
AssertionsForClassTypes.assertThat(result).matches(Result::succeeded).extracting(Result::getContent)
.asInstanceOf(type(PrivateKeyOauth2CredentialsRequest.class))
.satisfies(request -> {
assertThat(request.getGrantType()).isEqualTo("client_credentials");
Expand All @@ -129,7 +137,7 @@ void shouldCreatePrivateKeyRequest_whenPrivateKeyNameIsPresent() throws JOSEExce

@Test
void shouldFailIfPrivateKeySecretNotFound() {
when(privateKeyResolver.createJwsSigner("pk-test")).thenReturn(null);
when(privateKeyResolver.createJwsSigner("pk-test")).thenReturn(Result.failure("foo bar"));

var address = defaultAddress()
.property(PRIVATE_KEY_NAME, "pk-test")
Expand All @@ -138,8 +146,8 @@ void shouldFailIfPrivateKeySecretNotFound() {

var result = factory.create(address);

assertThat(result).matches(Result::failed)
.extracting(Result::getFailureDetail).asString().contains("pk-test");
assertThat(result).isFailed()
.detail().contains("foo bar");
}

private HttpDataAddress.Builder defaultAddress() {
Expand Down

0 comments on commit c670e3f

Please sign in to comment.