Skip to content

Commit

Permalink
chore: reduce visibility of test fields (#14)
Browse files Browse the repository at this point in the history
* Identity Hub CLI

* Add test for buildSignedJwt

* Rename test util class for CLI

* Consolidate test

* Remove unused import

* Checkstyle

* Make test fields private

* Reduce visibility of util class

* Checkstyle

* Make static variables final
  • Loading branch information
marcgs authored Jul 28, 2022
1 parent f6ec368 commit 7c6908f
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@
import static org.eclipse.dataspaceconnector.identityhub.credentials.CryptoUtils.readPrivateEcKey;
import static org.eclipse.dataspaceconnector.identityhub.credentials.CryptoUtils.readPublicEcKey;


public class CliTestUtils {
static final Faker FAKER = new Faker();
class CliTestUtils {
public static final String PUBLIC_KEY_PATH = "src/test/resources/test-public-key.pem";
public static final String PRIVATE_KEY_PATH = "src/test/resources/test-private-key.pem";
public static final PublicKeyWrapper PUBLIC_KEY;
public static final PrivateKeyWrapper PRIVATE_KEY;
private static final Faker FAKER = new Faker();
private static final VerifiableCredentialsJwtService VC_JWT_SERVICE = new VerifiableCredentialsJwtServiceImpl(new ObjectMapper());

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,24 @@

class VerifiableCredentialsCommandTest {

static final Faker FAKER = new Faker();
static final ObjectMapper MAPPER = new ObjectMapper();

static final VerifiableCredential VC1 = createVerifiableCredential();
static final SignedJWT SIGNED_VC1 = signVerifiableCredential(VC1);

static final VerifiableCredential VC2 = createVerifiableCredential();
static final SignedJWT SIGNED_VC2 = signVerifiableCredential(VC2);

String hubUrl = FAKER.internet().url();

IdentityHubCli app = new IdentityHubCli();
CommandLine cmd = new CommandLine(app);
StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
private static final Faker FAKER = new Faker();
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final VerifiableCredential VC1 = createVerifiableCredential();
private static final SignedJWT SIGNED_VC1 = signVerifiableCredential(VC1);
private static final VerifiableCredential VC2 = createVerifiableCredential();
private static final SignedJWT SIGNED_VC2 = signVerifiableCredential(VC2);
private static final String HUB_URL = FAKER.internet().url();

private IdentityHubCli app = new IdentityHubCli();
private CommandLine cmd = new CommandLine(app);
private StringWriter out = new StringWriter();
private StringWriter err = new StringWriter();

@BeforeEach
void setUp() {
app.identityHubClient = mock(IdentityHubClient.class);
app.verifiableCredentialsJwtService = new VerifiableCredentialsJwtServiceImpl(new ObjectMapper());
app.hubUrl = hubUrl;
app.hubUrl = HUB_URL;
cmd.setOut(new PrintWriter(out));
cmd.setErr(new PrintWriter(err));
}
Expand Down Expand Up @@ -160,10 +157,10 @@ void add_invalidPrivateKey_fails() throws JsonProcessingException {
}

private int executeList() {
return cmd.execute("-s", hubUrl, "vc", "list");
return cmd.execute("-s", HUB_URL, "vc", "list");
}

private int executeAdd(String json, String privateKey) {
return cmd.execute("-s", hubUrl, "vc", "add", "-c", json, "-i", "identity-hub-test-issuer", "-b", "identity-hub-test-subject", "-k", privateKey);
return cmd.execute("-s", HUB_URL, "vc", "add", "-c", json, "-i", "identity-hub-test-issuer", "-b", "identity-hub-test-subject", "-k", privateKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,54 +42,53 @@ public class DidJwtCredentialsVerifierTest {

private static final Faker FAKER = new Faker();
private static final Monitor MONITOR = new ConsoleMonitor();

DidPublicKeyResolver didPublicKeyResolver = mock(DidPublicKeyResolver.class);
DidJwtCredentialsVerifier didJwtCredentialsVerifier = new DidJwtCredentialsVerifier(didPublicKeyResolver, MONITOR);
ECKey jwk = generateEcKey();
ECKey anotherJwk = generateEcKey();
String issuer = FAKER.internet().url();
String subject = FAKER.internet().url();
String otherSubject = FAKER.internet().url() + "other";
SignedJWT jwt = buildSignedJwt(generateVerifiableCredential(), issuer, subject, jwk);
private static final ECKey JWK = generateEcKey();
private static final ECKey ANOTHER_JWK = generateEcKey();
private static final String ISSUER = FAKER.internet().url();
private static final String SUBJECT = FAKER.internet().url();
private static final String OTHER_SUBJECT = FAKER.internet().url() + "other";
private static final SignedJWT JWT = buildSignedJwt(generateVerifiableCredential(), ISSUER, SUBJECT, JWK);
private DidPublicKeyResolver didPublicKeyResolver = mock(DidPublicKeyResolver.class);
private DidJwtCredentialsVerifier didJwtCredentialsVerifier = new DidJwtCredentialsVerifier(didPublicKeyResolver, MONITOR);

@Test
public void isSignedByIssuer_jwtSignedByIssuer() {

// Arrange
when(didPublicKeyResolver.resolvePublicKey(issuer)).thenReturn(Result.success(toPublicKeyWrapper(jwk)));
when(didPublicKeyResolver.resolvePublicKey(ISSUER)).thenReturn(Result.success(toPublicKeyWrapper(JWK)));

// Assert
assertThat(didJwtCredentialsVerifier.isSignedByIssuer(jwt)).isTrue();
assertThat(didJwtCredentialsVerifier.isSignedByIssuer(JWT)).isTrue();
}

@Test
public void isSignedByIssuer_jwtSignedByWrongIssuer() {

// Arrange
when(didPublicKeyResolver.resolvePublicKey(issuer)).thenReturn(Result.success(toPublicKeyWrapper(anotherJwk)));
when(didPublicKeyResolver.resolvePublicKey(ISSUER)).thenReturn(Result.success(toPublicKeyWrapper(ANOTHER_JWK)));

// Assert
assertThat(didJwtCredentialsVerifier.isSignedByIssuer(jwt)).isFalse();
assertThat(didJwtCredentialsVerifier.isSignedByIssuer(JWT)).isFalse();
}

@Test
public void isSignedByIssuer_PublicKeyCantBeResolved() {

// Arrange
when(didPublicKeyResolver.resolvePublicKey(issuer)).thenReturn(Result.failure("Failed resolving public key"));
when(didPublicKeyResolver.resolvePublicKey(ISSUER)).thenReturn(Result.failure("Failed resolving public key"));

// Assert
assertThat(didJwtCredentialsVerifier.isSignedByIssuer(jwt)).isFalse();
assertThat(didJwtCredentialsVerifier.isSignedByIssuer(JWT)).isFalse();
}

@Test
public void isSignedByIssuer_issuerDidCantBeResolved() throws ParseException {

// Arrange
when(didPublicKeyResolver.resolvePublicKey(jwt.getJWTClaimsSet().getIssuer())).thenReturn(Result.failure(FAKER.lorem().sentence()));
when(didPublicKeyResolver.resolvePublicKey(JWT.getJWTClaimsSet().getIssuer())).thenReturn(Result.failure(FAKER.lorem().sentence()));

// Assert
assertThat(didJwtCredentialsVerifier.isSignedByIssuer(jwt)).isFalse();
assertThat(didJwtCredentialsVerifier.isSignedByIssuer(JWT)).isFalse();
}

@Test
Expand All @@ -105,24 +104,24 @@ public void isSignedByIssuer_cantParsePayload() throws Exception {

@Test
void verifyClaims_success() {
assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, subject)).isTrue();
assertThat(didJwtCredentialsVerifier.verifyClaims(JWT, SUBJECT)).isTrue();
}

@Test
void verifyClaims_OnInvalidSubject() {
assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, otherSubject)).isFalse();
assertThat(didJwtCredentialsVerifier.verifyClaims(JWT, OTHER_SUBJECT)).isFalse();
}

@Test
void verifyClaims_OnEmptySubject() {
var jwt = buildSignedJwt(generateVerifiableCredential(), issuer, null, jwk);
assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, otherSubject)).isFalse();
var jwt = buildSignedJwt(generateVerifiableCredential(), ISSUER, null, JWK);
assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, OTHER_SUBJECT)).isFalse();
}

@Test
void verifyClaims_OnEmptyIssuer() {
var jwt = buildSignedJwt(generateVerifiableCredential(), null, subject, jwk);
assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, subject)).isFalse();
var jwt = buildSignedJwt(generateVerifiableCredential(), null, SUBJECT, JWK);
assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, SUBJECT)).isFalse();
}

@Test
Expand All @@ -133,58 +132,58 @@ public void verifyClaims_OnInvalidJwt() throws Exception {
when(jwt.getJWTClaimsSet()).thenThrow(new ParseException(message, 0));

// Act
assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, subject)).isFalse();
assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, SUBJECT)).isFalse();
}

@Test
void verifyClaims_OnValidExp() {
var claims = new JWTClaimsSet.Builder()
.issuer(issuer)
.subject(subject)
.issuer(ISSUER)
.subject(SUBJECT)
.expirationTime(from(now().plus(1, DAYS)))
.build();

SignedJWT jwt = VerifiableCredentialTestUtil.buildSignedJwt(claims, jwk);
SignedJWT jwt = VerifiableCredentialTestUtil.buildSignedJwt(claims, JWK);

assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, subject)).isTrue();
assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, SUBJECT)).isTrue();
}

@Test
void verifyClaims_OnInvalidExp() {
var claims = new JWTClaimsSet.Builder()
.issuer(issuer)
.subject(subject)
.issuer(ISSUER)
.subject(SUBJECT)
.expirationTime(from(now().minus(1, DAYS)))
.build();

SignedJWT jwt = VerifiableCredentialTestUtil.buildSignedJwt(claims, jwk);
SignedJWT jwt = VerifiableCredentialTestUtil.buildSignedJwt(claims, JWK);

assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, subject)).isFalse();
assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, SUBJECT)).isFalse();
}

@Test
void verifyClaims_OnValidNotBefore() {
var claims = new JWTClaimsSet.Builder()
.issuer(issuer)
.subject(subject)
.issuer(ISSUER)
.subject(SUBJECT)
.notBeforeTime(from(now().minus(1, DAYS)))
.build();

SignedJWT jwt = VerifiableCredentialTestUtil.buildSignedJwt(claims, jwk);
SignedJWT jwt = VerifiableCredentialTestUtil.buildSignedJwt(claims, JWK);

assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, subject)).isTrue();
assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, SUBJECT)).isTrue();
}

@Test
void verifyClaims_OnInvalidNotBefore() {
var claims = new JWTClaimsSet.Builder()
.issuer(issuer)
.subject(subject)
.issuer(ISSUER)
.subject(SUBJECT)
.notBeforeTime(from(now().plus(1, DAYS)))
.build();

SignedJWT jwt = VerifiableCredentialTestUtil.buildSignedJwt(claims, jwk);
SignedJWT jwt = VerifiableCredentialTestUtil.buildSignedJwt(claims, JWK);

assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, subject)).isFalse();
assertThat(didJwtCredentialsVerifier.verifyClaims(jwt, SUBJECT)).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,38 +50,37 @@ public class IdentityHubCredentialsVerifierTest {

private static final Faker FAKER = new Faker();
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final String HUB_BASE_URL = "https://" + FAKER.internet().url();
private static final DidDocument DID_DOCUMENT = DidDocument.Builder.newInstance()
.service(List.of(new Service("IdentityHub", "IdentityHub", HUB_BASE_URL))).build();
private static final String ISSUER = FAKER.internet().url();
private static final String SUBJECT = FAKER.internet().url();
private Monitor monitorMock = mock(Monitor.class);
private IdentityHubClient identityHubClientMock = mock(IdentityHubClient.class);
private JwtCredentialsVerifier jwtCredentialsVerifierMock = mock(JwtCredentialsVerifier.class);

private VerifiableCredentialsJwtServiceImpl verifiableCredentialsJwtService = new VerifiableCredentialsJwtServiceImpl(OBJECT_MAPPER);
private CredentialsVerifier credentialsVerifier = new IdentityHubCredentialsVerifier(identityHubClientMock, monitorMock, jwtCredentialsVerifierMock, verifiableCredentialsJwtService);
private String hubBaseUrl = "https://" + FAKER.internet().url();
DidDocument didDocument = DidDocument.Builder.newInstance()
.service(List.of(new Service("IdentityHub", "IdentityHub", hubBaseUrl))).build();
String issuer = FAKER.internet().url();
String subject = FAKER.internet().url();

@Test
public void getVerifiedClaims_getValidClaims() throws Exception {

// Arrange
var credential = generateVerifiableCredential();
var jws = buildSignedJwt(credential, issuer, subject, generateEcKey());
var jws = buildSignedJwt(credential, ISSUER, SUBJECT, generateEcKey());
setUpMocks(jws, true, true);

// Act
var credentials = credentialsVerifier.getVerifiedCredentials(didDocument);
var credentials = credentialsVerifier.getVerifiedCredentials(DID_DOCUMENT);

// Assert
assertThat(credentials.succeeded()).isTrue();
assertThat(credentials.getContent())
.usingRecursiveComparison()
.isEqualTo(toMap(credential, issuer, subject));
.isEqualTo(toMap(credential, ISSUER, SUBJECT));
}

private void setUpMocks(SignedJWT jws, boolean isSigned, boolean claimsValid) {
when(identityHubClientMock.getVerifiableCredentials(hubBaseUrl)).thenReturn(StatusResult.success(List.of(jws)));
when(identityHubClientMock.getVerifiableCredentials(HUB_BASE_URL)).thenReturn(StatusResult.success(List.of(jws)));
when(jwtCredentialsVerifierMock.isSignedByIssuer(jws)).thenReturn(isSigned);
when(jwtCredentialsVerifierMock.verifyClaims(eq(jws), any())).thenReturn(claimsValid);
}
Expand All @@ -91,11 +90,11 @@ public void getVerifiedClaims_filtersSignedByWrongIssuer() throws Exception {

// Arrange
var credential = generateVerifiableCredential();
var jws = buildSignedJwt(credential, issuer, subject, generateEcKey());
var jws = buildSignedJwt(credential, ISSUER, SUBJECT, generateEcKey());
setUpMocks(jws, true, false);

// Act
var credentials = credentialsVerifier.getVerifiedCredentials(didDocument);
var credentials = credentialsVerifier.getVerifiedCredentials(DID_DOCUMENT);

// Assert
assertThat(credentials.succeeded()).isTrue();
Expand All @@ -119,10 +118,10 @@ public void getVerifiedClaims_hubUrlNotResolved() {
public void getVerifiedClaims_idHubCallFails() {

// Arrange
when(identityHubClientMock.getVerifiableCredentials(hubBaseUrl)).thenReturn(StatusResult.failure(ResponseStatus.FATAL_ERROR));
when(identityHubClientMock.getVerifiableCredentials(HUB_BASE_URL)).thenReturn(StatusResult.failure(ResponseStatus.FATAL_ERROR));

// Act
var credentials = credentialsVerifier.getVerifiedCredentials(didDocument);
var credentials = credentialsVerifier.getVerifiedCredentials(DID_DOCUMENT);

// Assert
assertThat(credentials.failed()).isTrue();
Expand All @@ -136,7 +135,7 @@ public void getVerifiedClaims_verifiableCredentialsWithWrongFormat() {
setUpMocks(jws, true, true);

// Act
var credentials = credentialsVerifier.getVerifiedCredentials(didDocument);
var credentials = credentialsVerifier.getVerifiedCredentials(DID_DOCUMENT);

// Assert
assertThat(credentials.succeeded()).isTrue();
Expand All @@ -153,13 +152,13 @@ public void getVerifiedClaims_verifiableCredentialsWithMissingId() {
var jwtClaims = new JWTClaimsSet.Builder()
.claim("vc", Map.of(FAKER.lorem().word(), FAKER.lorem().word()))
.issuer(FAKER.lorem().word())
.subject(subject)
.subject(SUBJECT)
.build();
var jws = new SignedJWT(jwsHeader, jwtClaims);
setUpMocks(jws, true, true);

// Act
var credentials = credentialsVerifier.getVerifiedCredentials(didDocument);
var credentials = credentialsVerifier.getVerifiedCredentials(DID_DOCUMENT);

// Assert
assertThat(credentials.succeeded()).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package org.eclipse.dataspaceconnector.identityhub.processor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.javafaker.Faker;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
Expand All @@ -41,7 +40,6 @@
public class CollectionsWriteProcessorTest {

private static final Faker FAKER = new Faker();
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

private IdentityHubStore identityHubStore;
private CollectionsWriteProcessor writeProcessor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class IdentityHubClientImplIntegrationTest {

private static final String API_URL = "http://localhost:8181/api/identity-hub";
private static final Faker FAKER = new Faker();
public static final VerifiableCredential VERIFIABLE_CREDENTIAL = VerifiableCredential.Builder.newInstance().id(FAKER.internet().uuid()).build();
private static final VerifiableCredential VERIFIABLE_CREDENTIAL = VerifiableCredential.Builder.newInstance().id(FAKER.internet().uuid()).build();
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private IdentityHubClient client;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@

public class VerifiableCredentialsJwtServiceTest {

static final Faker FAKER = new Faker();
static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
static final VerifiableCredential VERIFIABLE_CREDENTIAL = generateVerifiableCredential();
static final JWSHeader JWS_HEADER = new JWSHeader.Builder(JWSAlgorithm.ES256).build();
EcPrivateKeyWrapper privateKey;
EcPublicKeyWrapper publicKey;
VerifiableCredentialsJwtService service;
private static final Faker FAKER = new Faker();
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final VerifiableCredential VERIFIABLE_CREDENTIAL = generateVerifiableCredential();
private static final JWSHeader JWS_HEADER = new JWSHeader.Builder(JWSAlgorithm.ES256).build();
private EcPrivateKeyWrapper privateKey;
private EcPublicKeyWrapper publicKey;
private VerifiableCredentialsJwtService service;

@BeforeEach
public void setUp() {
Expand Down
Loading

0 comments on commit 7c6908f

Please sign in to comment.