From 5d7da8f3c27c07a46a51be1b11535a1afe342f73 Mon Sep 17 00:00:00 2001 From: Mustafa Alsalfiti Date: Fri, 19 Apr 2024 13:30:56 +0200 Subject: [PATCH] fix: test for jwt --- .../utils/CommonUtils.java | 6 ++---- .../service/IssuersCredentialServiceTest.java | 17 +++++++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/utils/CommonUtils.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/utils/CommonUtils.java index 360f2ae7b..15fe3cd1e 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/utils/CommonUtils.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/utils/CommonUtils.java @@ -31,7 +31,6 @@ import org.eclipse.tractusx.managedidentitywallets.dao.entity.HoldersCredential; import org.eclipse.tractusx.managedidentitywallets.dto.SecureTokenRequest; import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet; -import org.eclipse.tractusx.managedidentitywallets.dao.entity.WalletKey; import org.eclipse.tractusx.managedidentitywallets.exception.BadDataException; import org.eclipse.tractusx.managedidentitywallets.service.WalletKeyService; import org.eclipse.tractusx.ssi.lib.crypt.octet.OctetKeyPairFactory; @@ -64,7 +63,6 @@ import java.util.Date; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.UUID; import java.util.regex.Pattern; @@ -158,8 +156,8 @@ private static VerifiableCredential createVerifiableCredential(DidDocument issue LinkedDataProofGenerator generator = LinkedDataProofGenerator.newInstance(SignatureType.JWS); URI verificationMethod = issuerDoc.getVerificationMethods().get(0).getId(); - JWSSignature2020 proof = (JWSSignature2020) generator.createProof(builder.build(), verificationMethod, - new x25519PrivateKey(privateKey)); + JWSSignature2020 proof = new JWSSignature2020(generator.createProof(builder.build(), verificationMethod, + new x25519PrivateKey(privateKey))); // Adding Proof to VC builder.proof(proof); diff --git a/src/test/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialServiceTest.java b/src/test/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialServiceTest.java index 623fb8c10..4d896529a 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialServiceTest.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialServiceTest.java @@ -175,11 +175,12 @@ void shouldIssueCredentialAsJwt() WalletKey walletKey = mock(WalletKey.class); when(walletKey.getKeyId()).thenReturn(KEY_ID); when(walletKey.getId()).thenReturn(42L); - + when(baseWallet.getAlgorithm()).thenReturn("ED25519"); when(walletKeyService.getPrivateKeyByWalletIdAndAlgorithm(baseWallet.getId() ,SupportedAlgorithms.valueOf(baseWallet.getAlgorithm()))) .thenReturn(new x25519PrivateKey(keyPair.getPrivateKey().asStringForStoring(), true)); when(walletKeyService.getWalletKeyIdByWalletId(baseWallet.getId())).thenReturn(walletKeyId); - + when(walletKeyService.getPrivateKeyByWalletIdAsBytes(baseWallet.getId() , "ED25519")).thenReturn(keyPair.getPrivateKey() + .asByte()); CredentialsResponse credentialsResponse = assertDoesNotThrow( () -> issuersCredentialService.issueMembershipCredential( issueMembershipCredentialRequest, @@ -225,7 +226,9 @@ void shouldIssueCredentialAsJwt() WalletKey walletKey = mock(WalletKey.class); when(walletKey.getKeyId()).thenReturn(KEY_ID); when(walletKey.getId()).thenReturn(42L); - + when(baseWallet.getAlgorithm()).thenReturn("ED25519"); + when(walletKeyService.getPrivateKeyByWalletIdAsBytes(baseWallet.getId() , "ED25519")).thenReturn(keyPair.getPrivateKey() + .asByte()); when(walletKeyService.getPrivateKeyByWalletIdAndAlgorithm(baseWallet.getId() ,SupportedAlgorithms.valueOf(baseWallet.getAlgorithm()))) .thenReturn(new x25519PrivateKey(keyPair.getPrivateKey().asStringForStoring(), true)); when(walletKeyService.getWalletKeyIdByWalletId(baseWallet.getId())).thenReturn(walletKeyId); @@ -264,9 +267,11 @@ void shouldIssueCredentialAsJwt() throws IOException, InvalidPrivateKeyFormatExc WalletKey walletKey = mock(WalletKey.class); when(walletKey.getKeyId()).thenReturn(KEY_ID); when(walletKey.getId()).thenReturn(42L); - + when(baseWallet.getAlgorithm()).thenReturn("ED25519"); when(walletKeyService.getPrivateKeyByWalletIdAndAlgorithm(baseWallet.getId() ,SupportedAlgorithms.valueOf(baseWallet.getAlgorithm()))) .thenReturn(new x25519PrivateKey(keyPair.getPrivateKey().asStringForStoring(), true)); + when(walletKeyService.getPrivateKeyByWalletIdAsBytes(baseWallet.getId() , "ED25519")).thenReturn(keyPair.getPrivateKey() + .asByte()); when(walletKeyService.getWalletKeyIdByWalletId(baseWallet.getId())).thenReturn(walletKeyId); CredentialsResponse credentialsResponse = assertDoesNotThrow( @@ -286,7 +291,6 @@ void shouldIssueCredentialAsJwt() throws IOException, ParseException, InvalidPri Map wallets = mockBaseAndHolderWallet(); Wallet baseWallet = (Wallet) wallets.get("base"); String baseWalletBpn = baseWallet.getBpn(); - String baseWalletDid = baseWallet.getDid(); Wallet holderWallet = (Wallet) wallets.get("holder"); String holderWalletBpn = holderWallet.getBpn(); String walletKeyId = "key-1"; @@ -299,7 +303,7 @@ void shouldIssueCredentialAsJwt() throws IOException, ParseException, InvalidPri MockUtil.generateDid("basewallet")).build(); MockUtil.makeCreateWorkForIssuer(issuersCredentialRepository); - when(walletKeyService.getPrivateKeyByWalletIdAsBytes(any(Long.class) , SupportedAlgorithms.ED25519.toString())).thenReturn(keyPair.getPrivateKey() + when(walletKeyService.getPrivateKeyByWalletIdAsBytes(baseWallet.getId() , "ED25519")).thenReturn(keyPair.getPrivateKey() .asByte()); when(commonService.getWalletByIdentifier(holderWalletBpn)).thenReturn(holderWallet); when(commonService.getWalletByIdentifier(verifiableCredential.getIssuer() @@ -317,6 +321,7 @@ public HoldersCredential answer(InvocationOnMock invocation) throws Throwable { WalletKey walletKey = mock(WalletKey.class); when(walletKey.getKeyId()).thenReturn(KEY_ID); + when(baseWallet.getAlgorithm()).thenReturn("ED25519"); when(walletKey.getId()).thenReturn(42L); when(walletKeyService.getPrivateKeyByWalletIdAndAlgorithm(baseWallet.getId() ,SupportedAlgorithms.valueOf(baseWallet.getAlgorithm()))) .thenReturn(new x25519PrivateKey(keyPair.getPrivateKey().asStringForStoring(), true));