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 4d5df7ca9..fc53ba6d6 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vp/PresentationTest.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vp/PresentationTest.java @@ -42,6 +42,10 @@ import org.eclipse.tractusx.ssi.lib.exception.DidDocumentResolverNotRegisteredException; import org.eclipse.tractusx.ssi.lib.exception.JwtException; import org.eclipse.tractusx.ssi.lib.jwt.SignedJwtVerifier; +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.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -53,11 +57,10 @@ import org.springframework.http.*; import org.springframework.test.context.ContextConfiguration; +import java.net.URI; import java.text.ParseException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.time.Instant; +import java.util.*; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = {ManagedIdentityWalletsApplication.class}) @ContextConfiguration(initializers = {TestContextInitializer.class}) @@ -143,7 +146,7 @@ void validateVPAsJwtWithInvalidSignatureAndInValidAudienceAndExpiryDateValidatio } @Test - void validateVPAsJwtWithValidAudienceAndDateValidation() throws JsonProcessingException{ + void validateVPAsJwtWithValidAudienceAndDateValidation() throws JsonProcessingException { //create VP String bpn = UUID.randomUUID().toString(); String audience = "companyA"; @@ -159,6 +162,24 @@ void validateVPAsJwtWithValidAudienceAndDateValidation() throws JsonProcessingEx Assertions.assertTrue(Boolean.parseBoolean(map.get(StringPool.VALIDATE_JWT_EXPIRY_DATE).toString())); } + @Test + void validateVPAsJwtWithInValidVCDateValidation() throws JsonProcessingException { + //create VP + String bpn = UUID.randomUUID().toString(); + String audience = "companyA"; + + ResponseEntity vpResponse = getIssueVPRequestWithShortExpiry(bpn, audience); + Map body = vpResponse.getBody(); + + ResponseEntity> mapResponseEntity = presentationController.validatePresentation(body, audience, true, true); + + Map map = mapResponseEntity.getBody(); + Assertions.assertFalse(Boolean.parseBoolean(map.get(StringPool.VALID).toString())); + Assertions.assertTrue(Boolean.parseBoolean(map.get(StringPool.VALIDATE_AUDIENCE).toString())); + Assertions.assertFalse(Boolean.parseBoolean(map.get(StringPool.VALIDATE_EXPIRY_DATE).toString())); + Assertions.assertTrue(Boolean.parseBoolean(map.get(StringPool.VALIDATE_JWT_EXPIRY_DATE).toString())); + } + @Test void createPresentationAsJWT201() throws JsonProcessingException, ParseException { String bpn = UUID.randomUUID().toString(); @@ -176,8 +197,6 @@ void createPresentationAsJWT201() throws JsonProcessingException, ParseException } private ResponseEntity createBpnVCAsJwt(String bpn, String audience) throws JsonProcessingException { - String didWeb = DidWebFactory.fromHostnameAndPath(miwSettings.host(), bpn).toString(); - Map request = getIssueVPRequest(bpn); HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders(bpn); @@ -269,4 +288,61 @@ private Map getIssueVPRequest(String bpn) throws JsonProcessingE request.put(StringPool.VERIFIABLE_CREDENTIALS, List.of(map)); return request; } + + @NotNull + private ResponseEntity getIssueVPRequestWithShortExpiry(String bpn, String audience) throws JsonProcessingException { + ResponseEntity response = TestUtils.createWallet(bpn, bpn, restTemplate); + Assertions.assertEquals(response.getStatusCode().value(), HttpStatus.CREATED.value()); + Wallet wallet = TestUtils.getWalletFromString(response.getBody()); + + //create VC + HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()); + String type = VerifiableCredentialType.MEMBERSHIP_CREDENTIAL; + Instant vcExpiry = Instant.now().minusSeconds(60); + ResponseEntity vcResponse = issueVC(wallet.getBpn(), wallet.getDid(), miwSettings.authorityWalletDid(), type, headers, miwSettings.vcContexts(), vcExpiry); + + + Map map = objectMapper.readValue(vcResponse.getBody(), Map.class); + + //create request + Map request = new HashMap<>(); + request.put(StringPool.HOLDER_IDENTIFIER, wallet.getDid()); + request.put(StringPool.VERIFIABLE_CREDENTIALS, List.of(map)); + + headers = AuthenticationUtils.getValidUserHttpHeaders(bpn); + headers.put(HttpHeaders.CONTENT_TYPE, List.of(MediaType.APPLICATION_JSON_VALUE)); + + HttpEntity entity = new HttpEntity<>(objectMapper.writeValueAsString(request), headers); + + ResponseEntity vpResponse = restTemplate.exchange(RestURI.API_PRESENTATIONS + "?asJwt={asJwt}&audience={audience}", HttpMethod.POST, entity, Map.class, true, audience); + return vpResponse; + } + + private ResponseEntity issueVC(String bpn, String holderDid, String issuerDid, String type, HttpHeaders headers, List contexts, Instant expiry) throws JsonProcessingException { + // Create VC without proof + //VC Bulider + VerifiableCredentialBuilder verifiableCredentialBuilder = + new VerifiableCredentialBuilder(); + + //VC Subject + VerifiableCredentialSubject verifiableCredentialSubject = new VerifiableCredentialSubject(Map.of(StringPool.TYPE, MIWVerifiableCredentialType.BPN_CREDENTIAL, + StringPool.ID, holderDid, + StringPool.BPN, bpn)); + + //Using Builder + VerifiableCredential credentialWithoutProof = + verifiableCredentialBuilder + .id(URI.create(issuerDid + "#" + UUID.randomUUID())) + .context(contexts) + .type(List.of(VerifiableCredentialType.VERIFIABLE_CREDENTIAL, type)) + .issuer(URI.create(issuerDid)) //issuer must be base wallet + .expirationDate(expiry) + .issuanceDate(Instant.now()) + .credentialSubject(verifiableCredentialSubject) + .build(); + + Map map = objectMapper.readValue(credentialWithoutProof.toJson(), Map.class); + HttpEntity entity = new HttpEntity<>(map, headers); + return restTemplate.exchange(RestURI.ISSUERS_CREDENTIALS + "?holderDid={did}", HttpMethod.POST, entity, String.class, holderDid); + } } \ No newline at end of file