From bafaab91d67ef1d85229f786500243dc22ef4d33 Mon Sep 17 00:00:00 2001 From: Nitin Vavdiya Date: Wed, 7 Jun 2023 11:46:10 +0530 Subject: [PATCH] feat: Issuer get credential API testcases, test case modification for self_issued and is_stored --- .../service/IssuersCredentialService.java | 70 ++++++++++------ src/main/resources/application.yaml | 2 +- .../utils/TestUtils.java | 16 ++++ .../vc/DismantlerHoldersCredentialTest.java | 16 +++- .../vc/FrameworkHoldersCredentialTest.java | 26 ++++++ .../vc/HoldersCredentialTest.java | 23 +---- .../vc/IssuersCredentialTest.java | 83 +++++++++++++++++-- .../vc/MembershipHoldersCredentialTest.java | 54 ++++++++++-- .../vp/PresentationTest.java | 6 +- 9 files changed, 234 insertions(+), 62 deletions(-) diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialService.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialService.java index 176b8b3cc..9c3dfc854 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialService.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialService.java @@ -195,19 +195,20 @@ public VerifiableCredential issueFrameworkCredential(IssueFrameworkCredentialReq Wallet baseWallet = walletService.getWalletByIdentifier(miwSettings.authorityWalletBpn()); - //validate BPN access - Validate.isFalse(callerBPN.equals(baseWallet.getBpn())).launch(new ForbiddenException(BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN)); - + validateAccess(callerBPN, baseWallet); // get Key byte[] privateKeyBytes = walletKeyService.getPrivateKeyByWalletIdentifierAsBytes(baseWallet.getId()); + //if base wallet issue credentials to itself + boolean isSelfIssued = isSelfIssued(request.getBpn()); + Map subject = Map.of("type", request.getType(), "id", holderWallet.getDid(), "value", request.getValue(), "contract-template", request.getContractTemplate(), "contract-version", request.getContractVersion()); List types = List.of(VerifiableCredentialType.VERIFIABLE_CREDENTIAL, MIWVerifiableCredentialType.USE_CASE_FRAMEWORK_CONDITION_CX); - HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(subject, types, baseWallet.getDidDocument(), privateKeyBytes, holderWallet.getDid(), miwSettings.vcContexts(), miwSettings.vcExpiryDate(), false); + HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(subject, types, baseWallet.getDidDocument(), privateKeyBytes, holderWallet.getDid(), miwSettings.vcContexts(), miwSettings.vcExpiryDate(), isSelfIssued); //save in holder wallet holdersCredential = holdersCredentialRepository.save(holdersCredential); @@ -233,15 +234,17 @@ public VerifiableCredential issueDismantlerCredential(IssueDismantlerCredentialR Wallet holderWallet = walletService.getWalletByIdentifier(request.getBpn()); // Fetch Issuer Wallet - Wallet baseWallet = walletService.getWalletByIdentifier(miwSettings.authorityWalletBpn()); + Wallet issuerWallet = walletService.getWalletByIdentifier(miwSettings.authorityWalletBpn()); - //check BPN access - Validate.isFalse(callerBPN.equals(baseWallet.getBpn())).launch(new ForbiddenException(BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN)); + validateAccess(callerBPN, issuerWallet); //check duplicate isCredentialExit(holderWallet.getDid(), MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX); - byte[] privateKeyBytes = walletKeyService.getPrivateKeyByWalletIdentifierAsBytes(baseWallet.getId()); + byte[] privateKeyBytes = walletKeyService.getPrivateKeyByWalletIdentifierAsBytes(issuerWallet.getId()); + + //if base wallet issue credentials to itself + boolean isSelfIssued = isSelfIssued(request.getBpn()); Map subject = Map.of("type", MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL, "id", holderWallet.getDid(), @@ -249,7 +252,7 @@ public VerifiableCredential issueDismantlerCredential(IssueDismantlerCredentialR "activityType", request.getActivityType(), "allowedVehicleBrands", request.getAllowedVehicleBrands()); List types = List.of(VerifiableCredentialType.VERIFIABLE_CREDENTIAL, MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX); - HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(subject, types, baseWallet.getDidDocument(), privateKeyBytes, holderWallet.getDid(), miwSettings.vcContexts(), miwSettings.vcExpiryDate(), false); + HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(subject, types, issuerWallet.getDidDocument(), privateKeyBytes, holderWallet.getDid(), miwSettings.vcContexts(), miwSettings.vcExpiryDate(), isSelfIssued); //save in holder wallet @@ -280,20 +283,23 @@ public VerifiableCredential issueMembershipCredential(IssueMembershipCredentialR isCredentialExit(holderWallet.getDid(), MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX); // Fetch Issuer Wallet - Wallet baseWallet = walletService.getWalletByIdentifier(miwSettings.authorityWalletBpn()); + Wallet issuerWallet = walletService.getWalletByIdentifier(miwSettings.authorityWalletBpn()); - //validate BPN access - Validate.isFalse(callerBPN.equals(baseWallet.getBpn())).launch(new ForbiddenException(BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN)); + validateAccess(callerBPN, issuerWallet); - byte[] privateKeyBytes = walletKeyService.getPrivateKeyByWalletIdentifierAsBytes(baseWallet.getId()); + byte[] privateKeyBytes = walletKeyService.getPrivateKeyByWalletIdentifierAsBytes(issuerWallet.getId()); List types = List.of(VerifiableCredentialType.VERIFIABLE_CREDENTIAL, MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX); + + //if base wallet issue credentials to itself + boolean isSelfIssued = isSelfIssued(issueMembershipCredentialRequest.getBpn()); + //VC Subject HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(Map.of("type", VerifiableCredentialType.MEMBERSHIP_CREDENTIAL, "id", holderWallet.getDid(), "holderIdentifier", holderWallet.getBpn(), - "memberOf", baseWallet.getName(), + "memberOf", issuerWallet.getName(), "status", "Active", - "startTime", Instant.now().toString()), types, baseWallet.getDidDocument(), privateKeyBytes, holderWallet.getDid(), miwSettings.vcContexts(), miwSettings.vcExpiryDate(), false); + "startTime", Instant.now().toString()), types, issuerWallet.getDidDocument(), privateKeyBytes, holderWallet.getDid(), miwSettings.vcContexts(), miwSettings.vcExpiryDate(), isSelfIssued); //save in holder wallet @@ -321,21 +327,19 @@ public VerifiableCredential issueCredentialUsingBaseWallet(Map d Wallet issuerWallet = walletService.getWalletByIdentifier(verifiableCredential.getIssuer().toString()); - //validate BPN access, VC must be issued by base wallet - Validate.isFalse(callerBpn.equals(issuerWallet.getBpn())).launch(new ForbiddenException(BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN)); - - //issuer must be base wallet - Validate.isFalse(issuerWallet.getBpn().equals(miwSettings.authorityWalletBpn())).launch(new ForbiddenException(BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN)); + validateAccess(callerBpn, issuerWallet); // get Key byte[] privateKeyBytes = walletKeyService.getPrivateKeyByWalletIdentifierAsBytes(issuerWallet.getId()); + boolean isSelfIssued = isSelfIssued(issuerWallet.getBpn()); //TODO need to pass holder bpn + // Create Credential HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(verifiableCredential.getCredentialSubject().get(0), verifiableCredential.getTypes(), issuerWallet.getDidDocument(), privateKeyBytes, issuerWallet.getDid(), //TODO need to check, how we can identify holder of VC, need to m - verifiableCredential.getContext(), Date.from(verifiableCredential.getExpirationDate()), false); + verifiableCredential.getContext(), Date.from(verifiableCredential.getExpirationDate()), isSelfIssued); //save in holder wallet @@ -349,11 +353,6 @@ public VerifiableCredential issueCredentialUsingBaseWallet(Map d return issuersCredential.getData(); } - - private void isCredentialExit(String holderDid, String credentialType) { - Validate.isTrue(holdersCredentialRepository.existsByHolderDidAndType(holderDid, credentialType)).launch(new DuplicateCredentialProblem("Credential of type " + credentialType + " is already exists ")); - } - /** * Credentials validation map. * @@ -376,4 +375,23 @@ public Map credentialsValidation(Map data) { return response; } + + + private void validateAccess(String callerBpn, Wallet issuerWallet) { + //validate BPN access, VC must be issued by base wallet + Validate.isFalse(callerBpn.equals(issuerWallet.getBpn())).launch(new ForbiddenException(BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN)); + + //issuer must be base wallet + Validate.isFalse(issuerWallet.getBpn().equals(miwSettings.authorityWalletBpn())).launch(new ForbiddenException(BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN)); + } + + + private void isCredentialExit(String holderDid, String credentialType) { + Validate.isTrue(holdersCredentialRepository.existsByHolderDidAndType(holderDid, credentialType)).launch(new DuplicateCredentialProblem("Credential of type " + credentialType + " is already exists ")); + } + + private boolean isSelfIssued(String holderBpn) { + return holderBpn.equals(miwSettings.authorityWalletBpn()); + } + } diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index aa27ffa17..77bbf099f 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -64,7 +64,7 @@ miw: encryptionKey: ${ENCRYPTION_KEY:Woh9waid4Ei5eez0aitieghoow9so4oe} authorityWalletBpn: ${AUTHORITY_WALLET_BPN:BPNL000000000000} authorityWalletName: ${AUTHORITY_WALLET_NAME:Catena-X} - authorityWalletDid: ${AUTHORITY_WALLET_DID:localhost:BPNL000000000000} + authorityWalletDid: ${AUTHORITY_WALLET_DID:did:web:localhost:BPNL000000000000} vcContexts: ${VC_SCHEMA_LINK:https://www.w3.org/2018/credentials/v1, https://raw.githubusercontent.com/catenax-ng/product-core-schemas/main/businessPartnerData} vcExpiryDate: ${VC_EXPIRY_DATE:01-01-2025} #dd-MM-yyyy ie. 01-01-2025 expiry date will be 2024-12-31T18:30:00Z in VC supportedFrameworkVCTypes: ${SUPPORTED_FRAMEWORK_VC_TYPES:cx-behavior-twin=Behavior Twin,cx-pcf=PCF,cx-quality=Quality,cx-resiliency=Resiliency,cx-sustainability=Sustainability,cx-traceability=ID_3.0_Trace} diff --git a/src/test/java/org/eclipse/tractusx/managedidentitywallets/utils/TestUtils.java b/src/test/java/org/eclipse/tractusx/managedidentitywallets/utils/TestUtils.java index f331a2193..28a658aef 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/utils/TestUtils.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/utils/TestUtils.java @@ -144,4 +144,20 @@ public static Wallet getWalletFromString(String body) throws JsonProcessingExcep System.out.println("wallet -- >" + wallet1.getBpn()); return wallet1; } + + public static List getCredentialsFromString(String body) throws com.fasterxml.jackson.core.JsonProcessingException { + List credentialList = new ArrayList<>(); + + JSONArray array = new JSONArray(body); + if (array.length() == 0) { + return credentialList; + } + + for (int i = 0; i < array.length(); i++) { + JSONObject jsonObject = array.getJSONObject(i); + ObjectMapper objectMapper = new ObjectMapper(); + credentialList.add(new VerifiableCredential(objectMapper.readValue(jsonObject.toString(), Map.class))); + } + return credentialList; + } } diff --git a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/DismantlerHoldersCredentialTest.java b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/DismantlerHoldersCredentialTest.java index 5d928f06b..8166fc7ab 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/DismantlerHoldersCredentialTest.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/DismantlerHoldersCredentialTest.java @@ -93,11 +93,22 @@ void issueDismantlerCredentialTest403() { } + @Test + void issueDismantlerCredentialToBaseWalletTest201() throws JsonProcessingException, JSONException { + ResponseEntity response = issueDismantlerCredential(miwSettings.authorityWalletBpn(), miwSettings.authorityWalletBpn()); + Assertions.assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value()); + List credentials = holdersCredentialRepository.getByHolderDidAndType(miwSettings.authorityWalletDid(), MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX); + Assertions.assertFalse(credentials.isEmpty()); + Assertions.assertTrue(credentials.get(0).isSelfIssued()); //self issued must be false + Assertions.assertFalse(credentials.get(0).isStored()); //stored must be false + } + + @Test void issueDismantlerCredentialTest201() throws JsonProcessingException, JSONException { String bpn = UUID.randomUUID().toString(); - String did = "did:web:localhost:"+bpn; + String did = "did:web:localhost:" + bpn; Wallet wallet = TestUtils.createWallet(bpn, did, walletRepository); ResponseEntity response = issueDismantlerCredential(bpn, did); @@ -116,7 +127,8 @@ void issueDismantlerCredentialTest201() throws JsonProcessingException, JSONExce List credentials = holdersCredentialRepository.getByHolderDidAndType(wallet.getDid(), MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX); Assertions.assertFalse(credentials.isEmpty()); TestUtils.checkVC(credentials.get(0).getData(), miwSettings); - + Assertions.assertFalse(credentials.get(0).isSelfIssued()); //self issued must be false + Assertions.assertFalse(credentials.get(0).isStored()); //stored must be false VerifiableCredential data = credentials.get(0).getData(); diff --git a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/FrameworkHoldersCredentialTest.java b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/FrameworkHoldersCredentialTest.java index 12ddd5c98..d39ea3e1f 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/FrameworkHoldersCredentialTest.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/FrameworkHoldersCredentialTest.java @@ -115,6 +115,30 @@ void issueFrameworkCredentialWithInvalidBpnAccessTest403() throws JsonProcessing Assertions.assertEquals(HttpStatus.FORBIDDEN.value(), response.getStatusCode().value()); } + @Test + void issueFrameWorkVCToBaseWalletTest201() throws JSONException { + String bpn = miwSettings.authorityWalletBpn(); + String type = "cx-pcf"; + String value = "PCF"; + HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()); + + IssueFrameworkCredentialRequest twinRequest = TestUtils.getIssueFrameworkCredentialRequest(bpn, type, value); + + HttpEntity entity = new HttpEntity<>(twinRequest, headers); + + ResponseEntity response = restTemplate.exchange(RestURI.API_CREDENTIALS_ISSUER_FRAMEWORK, HttpMethod.POST, entity, String.class); + Assertions.assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value()); + + List credentials = holdersCredentialRepository.getByHolderDidAndType(miwSettings.authorityWalletDid(), MIWVerifiableCredentialType.USE_CASE_FRAMEWORK_CONDITION_CX); + Assertions.assertFalse(credentials.isEmpty()); + + VerifiableCredential vcFromDB = credentials.get(0).getData(); + TestUtils.checkVC(vcFromDB, miwSettings); + + Assertions.assertFalse(credentials.get(0).isStored()); //stored must be false + Assertions.assertTrue(credentials.get(0).isSelfIssued()); //self issue must be false + } + @ParameterizedTest @MethodSource("getTypes") void issueFrameWorkVCTest201(IssueFrameworkCredentialRequest request) throws JsonProcessingException, JSONException { @@ -196,6 +220,8 @@ private void validate(Wallet wallet, String type, String value, ResponseEntity response = restTemplate.exchange(RestURI.CREDENTIALS + "?issuerIdentifier={did}" , HttpMethod.GET, entity, String.class, baseDID); - List credentialList = getCredentialsFromString(response.getBody()); + List credentialList = TestUtils.getCredentialsFromString(response.getBody()); Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); Assertions.assertEquals(6, Objects.requireNonNull(credentialList).size()); response = restTemplate.exchange(RestURI.CREDENTIALS + "?credentialId={id}" , HttpMethod.GET, entity, String.class, credentialList.get(0).getId()); - credentialList = getCredentialsFromString(response.getBody()); + credentialList = TestUtils.getCredentialsFromString(response.getBody()); Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); Assertions.assertEquals(1, Objects.requireNonNull(credentialList).size()); @@ -179,7 +180,7 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti list.add(MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX); response = restTemplate.exchange(RestURI.CREDENTIALS + "?type={list}" , HttpMethod.GET, entity, String.class, String.join(",", list)); - credentialList = getCredentialsFromString(response.getBody()); + credentialList = TestUtils.getCredentialsFromString(response.getBody()); Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); Assertions.assertEquals(1, Objects.requireNonNull(credentialList).size()); } @@ -229,22 +230,6 @@ void validateCredentials() throws com.fasterxml.jackson.core.JsonProcessingExcep } - private List getCredentialsFromString(String body) throws com.fasterxml.jackson.core.JsonProcessingException { - List credentialList = new ArrayList<>(); - - JSONArray array = new JSONArray(body); - if (array.length() == 0) { - return credentialList; - } - - for (int i = 0; i < array.length(); i++) { - JSONObject jsonObject = array.getJSONObject(i); - ObjectMapper objectMapper = new ObjectMapper(); - credentialList.add(new VerifiableCredential(objectMapper.readValue(jsonObject.toString(), Map.class))); - } - return credentialList; - } - private Map issueVC() throws JsonProcessingException { String bpn = UUID.randomUUID().toString(); HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders(bpn); diff --git a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/IssuersCredentialTest.java b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/IssuersCredentialTest.java index 956b33b32..f6d9ee46b 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/IssuersCredentialTest.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/IssuersCredentialTest.java @@ -26,18 +26,22 @@ import org.eclipse.tractusx.managedidentitywallets.ManagedIdentityWalletsApplication; import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings; import org.eclipse.tractusx.managedidentitywallets.config.TestContextInitializer; +import org.eclipse.tractusx.managedidentitywallets.constant.MIWVerifiableCredentialType; import org.eclipse.tractusx.managedidentitywallets.constant.RestURI; import org.eclipse.tractusx.managedidentitywallets.dao.entity.HoldersCredential; import org.eclipse.tractusx.managedidentitywallets.dao.repository.HoldersCredentialRepository; import org.eclipse.tractusx.managedidentitywallets.dao.repository.IssuersCredentialRepository; import org.eclipse.tractusx.managedidentitywallets.dao.repository.WalletRepository; import org.eclipse.tractusx.managedidentitywallets.dto.CreateWalletRequest; +import org.eclipse.tractusx.managedidentitywallets.dto.IssueFrameworkCredentialRequest; import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils; import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialBuilder; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialSubject; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialType; +import org.json.JSONArray; +import org.json.JSONObject; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -49,10 +53,7 @@ import java.net.URI; import java.time.Instant; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.UUID; +import java.util.*; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {ManagedIdentityWalletsApplication.class}) @ActiveProfiles("test") @@ -76,7 +77,59 @@ class IssuersCredentialTest { @Test void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingException { - //TODO added by Ronak + String baseBPN = miwSettings.authorityWalletBpn(); + String holderBpn = UUID.randomUUID().toString(); + String holderDID = "did:web:localhost:" + holderBpn; + HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders(baseBPN); + //save wallet + TestUtils.createWallet(holderBpn, holderDID, walletRepository); + TestUtils.issueMembershipVC(restTemplate, holderBpn, baseBPN); + String vcList = """ + [ + {"type":"cx-traceability","value":"ID_3.0_Trace"}, + {"type":"cx-sustainability","value":"Sustainability"}, + {"type":"cx-resiliency","value":"Resiliency"}, + {"type":"cx-quality","value":"Quality"}, + {"type":"cx-pcf","value":"PCF"} + ] + """; + JSONArray jsonArray = new JSONArray(vcList); + + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject jsonObject = jsonArray.getJSONObject(i); + IssueFrameworkCredentialRequest request = TestUtils.getIssueFrameworkCredentialRequest(holderBpn, jsonObject.get("type").toString(), jsonObject.get("value").toString()); + HttpEntity entity = new HttpEntity<>(request, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn())); //ony base wallet can issue VC + ResponseEntity exchange = restTemplate.exchange(RestURI.API_CREDENTIALS_ISSUER_FRAMEWORK, HttpMethod.POST, entity, String.class); + Assertions.assertEquals(exchange.getStatusCode().value(), HttpStatus.CREATED.value()); + } + + + HttpEntity entity = new HttpEntity<>(headers); + + ResponseEntity response = restTemplate.exchange(RestURI.ISSUERS_CREDENTIALS + "?holderIdentifier={did}" + , HttpMethod.GET, entity, String.class, holderDID); + List credentialList = TestUtils.getCredentialsFromString(response.getBody()); + Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); + Assertions.assertEquals(6, Objects.requireNonNull(credentialList).size()); //5 framework CV + 1 membership + + + response = restTemplate.exchange(RestURI.ISSUERS_CREDENTIALS + "?credentialId={id}" + , HttpMethod.GET, entity, String.class, credentialList.get(0).getId()); + credentialList = TestUtils.getCredentialsFromString(response.getBody()); + Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); + Assertions.assertEquals(1, Objects.requireNonNull(credentialList).size()); + + List list = new ArrayList<>(); + list.add(MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX); + response = restTemplate.exchange(RestURI.ISSUERS_CREDENTIALS + "?type={list}" + , HttpMethod.GET, entity, String.class, String.join(",", list)); + credentialList = TestUtils.getCredentialsFromString(response.getBody()); + Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); + + //all VC must be type of MEMBERSHIP_CREDENTIAL_CX + credentialList.forEach(vc -> { + Assertions.assertTrue(vc.getTypes().contains(MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX)); + }); } @Test @@ -103,6 +156,22 @@ void issueCredentialsWithoutBaseWalletBPN403() throws JsonProcessingException { Assertions.assertEquals(HttpStatus.FORBIDDEN.value(), response.getStatusCode().value()); } + @Test + void issueCredentialsToBaseWallet200() throws JsonProcessingException { + String type = "TestCredential"; + HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()); + + ResponseEntity response = issueVC(miwSettings.authorityWalletBpn(), miwSettings.authorityWalletDid(), miwSettings.authorityWalletDid(), type, headers); + + Assertions.assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value()); + VerifiableCredential verifiableCredential = new VerifiableCredential(new ObjectMapper().readValue(response.getBody(), Map.class)); + Assertions.assertNotNull(verifiableCredential.getProof()); + + List credentials = holdersCredentialRepository.getByHolderDidAndType(miwSettings.authorityWalletDid(), type); + Assertions.assertFalse(credentials.isEmpty()); + Assertions.assertFalse(credentials.get(0).isStored()); //stored must be false + Assertions.assertTrue(credentials.get(0).isSelfIssued()); //stored must be true + } @Test void issueCredentials200() throws com.fasterxml.jackson.core.JsonProcessingException { @@ -121,7 +190,9 @@ void issueCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcep List credentials = holdersCredentialRepository.getByHolderDidAndType(miwSettings.authorityWalletDid(), type); Assertions.assertFalse(credentials.isEmpty()); TestUtils.checkVC(credentials.get(0).getData(), miwSettings); - + /* Assertions.assertFalse(credentials.get(0).isStored()); //stored must be false + Assertions.assertFalse(credentials.get(0).isSelfIssued()); //stored must be false +*/ //check is it is stored in issuer wallet //TODO need to change once we have solutions to identify VC holder /*List issuersCredentials = issuersCredentialRepository.getByIssuerDidAndHolderDidAndType(miwSettings.authorityWalletDid(), did, type); diff --git a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/MembershipHoldersCredentialTest.java b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/MembershipHoldersCredentialTest.java index 41c68f128..7c538e231 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/MembershipHoldersCredentialTest.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/MembershipHoldersCredentialTest.java @@ -39,6 +39,7 @@ import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils; import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; +import org.jetbrains.annotations.NotNull; import org.json.JSONException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -73,6 +74,9 @@ class MembershipHoldersCredentialTest { @Autowired private IssuersCredentialRepository issuersCredentialRepository; + @Autowired + private ObjectMapper objectMapper; + @Test void issueMembershipCredentialTest403() { @@ -91,6 +95,34 @@ void issueMembershipCredentialTest403() { } + @Test + void issueMembershipCredentialToBaseWalletTest201() throws JsonProcessingException, JSONException { + + Wallet wallet = walletRepository.getByBpn(miwSettings.authorityWalletBpn()); + + ResponseEntity response = TestUtils.issueMembershipVC(restTemplate, miwSettings.authorityWalletBpn(), miwSettings.authorityWalletBpn()); + Assertions.assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value()); + + VerifiableCredential verifiableCredential = getVerifiableCredential(response); + + TestUtils.checkVC(verifiableCredential, miwSettings); + + validateTypes(verifiableCredential, miwSettings.authorityWalletBpn()); + + List holderVCs = holdersCredentialRepository.getByHolderDidAndType(wallet.getDid(), MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX); + Assertions.assertFalse(holderVCs.isEmpty()); + + TestUtils.checkVC(holderVCs.get(0).getData(), miwSettings); + Assertions.assertTrue(holderVCs.get(0).isSelfIssued()); //must be self issued true + Assertions.assertFalse(holderVCs.get(0).isStored()); //store must be false + + //check in issuer tables + List issuerVCs = issuersCredentialRepository.getByIssuerDidAndHolderDidAndType(miwSettings.authorityWalletDid(), wallet.getDid(), MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX); + Assertions.assertEquals(1, issuerVCs.size()); + TestUtils.checkVC(issuerVCs.get(0).getData(), miwSettings); + } + + @Test void issueMembershipCredentialTest201() throws JsonProcessingException, JSONException { @@ -104,17 +136,17 @@ void issueMembershipCredentialTest201() throws JsonProcessingException, JSONExce ResponseEntity response = TestUtils.issueMembershipVC(restTemplate, bpn, miwSettings.authorityWalletBpn()); Assertions.assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value()); - ObjectMapper objectMapper = new ObjectMapper(); - Map map = objectMapper.readValue(response.getBody(), Map.class); - VerifiableCredential verifiableCredential = new VerifiableCredential(map); + VerifiableCredential verifiableCredential = getVerifiableCredential(response); TestUtils.checkVC(verifiableCredential, miwSettings); - Assertions.assertTrue(verifiableCredential.getTypes().contains(MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX)); - Assertions.assertEquals(verifiableCredential.getCredentialSubject().get(0).get("holderIdentifier"), bpn); + validateTypes(verifiableCredential, bpn); List holderVCs = holdersCredentialRepository.getByHolderDidAndType(wallet.getDid(), MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX); Assertions.assertFalse(holderVCs.isEmpty()); + Assertions.assertFalse(holderVCs.get(0).isSelfIssued()); //must be self issued false + Assertions.assertFalse(holderVCs.get(0).isStored()); //store must be false + TestUtils.checkVC(holderVCs.get(0).getData(), miwSettings); @@ -160,4 +192,16 @@ void issueMembershipCredentialWithDuplicateBpn409() { Assertions.assertEquals(HttpStatus.CONFLICT.value(), duplicateResponse.getStatusCode().value()); } + + @NotNull + private VerifiableCredential getVerifiableCredential(ResponseEntity response) throws JsonProcessingException { + Map map = objectMapper.readValue(response.getBody(), Map.class); + return new VerifiableCredential(map); + } + + private void validateTypes(VerifiableCredential verifiableCredential, String holderBpn) { + Assertions.assertTrue(verifiableCredential.getTypes().contains(MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX)); + Assertions.assertEquals(verifiableCredential.getCredentialSubject().get(0).get("holderIdentifier"), holderBpn); + } + } diff --git a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vp/PresentationTest.java b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vp/PresentationTest.java index 8250bcb62..46a3093b6 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vp/PresentationTest.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vp/PresentationTest.java @@ -129,14 +129,14 @@ void validateVPAsJwtWithInvalidSignatureAndInValidAudienceAndExpiryDateValidatio ResponseEntity vpResponse = createBpnVCAsJwt(bpn, audience); Map body = vpResponse.getBody(); - try (MockedConstruction mocked = Mockito.mockConstruction(SignedJwtVerifier.class)) { + try (MockedConstruction mocked = Mockito.mockConstruction(SignedJwtVerifier.class)) { DidDocumentResolverRegistry didDocumentResolverRegistry = Mockito.mock(DidDocumentResolverRegistry.class); SignedJwtVerifier signedJwtVerifier = new SignedJwtVerifier(didDocumentResolverRegistry); Mockito.doThrow(new JwtException("invalid")).when(signedJwtVerifier).verify(Mockito.any(SignedJWT.class)); - Thread.sleep(62000L); // need to remove this??? + Thread.sleep(62000L); // need to remove this??? Can not mock 2 object creation using new ResponseEntity> mapResponseEntity = presentationController.validatePresentation(body, "no valid", true, true); @@ -157,7 +157,7 @@ void validateVPAsJwtWithValidAudienceAndDateValidation() throws JsonProcessingEx ResponseEntity vpResponse = createBpnVCAsJwt(bpn, audience); Map body = vpResponse.getBody(); - try (MockedConstruction mocked = Mockito.mockConstruction(SignedJwtVerifier.class)) { + try (MockedConstruction mocked = Mockito.mockConstruction(SignedJwtVerifier.class)) { DidDocumentResolverRegistry didDocumentResolverRegistry = Mockito.mock(DidDocumentResolverRegistry.class); SignedJwtVerifier signedJwtVerifier = new SignedJwtVerifier(didDocumentResolverRegistry);