Skip to content

Commit

Permalink
fix: failing test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-vavdiya committed Jun 12, 2024
1 parent 5b404c9 commit e3a48bf
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,6 @@ private StringPool() {
public static final String VC_JWT_KEY = "jwt";

public static final String AS_JWT = "asJwt";

public static final String BPN_CREDENTIAL = "BpnCredential";
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.eclipse.tractusx.managedidentitywallets.dto.CreateWalletRequest;
import org.eclipse.tractusx.managedidentitywallets.exception.ForbiddenException;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocument;
import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable;
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;
Expand All @@ -64,6 +65,7 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import static org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames.ACCESS_TOKEN;
Expand Down Expand Up @@ -222,10 +224,10 @@ public static Wallet buildWallet(String bpn, String did, String didJson) {
}

@SneakyThrows
public static VerifiableCredential issueCustomVCUsingBaseWallet(String holderDid, String issuerDid, String type, HttpHeaders headers,
public static VerifiableCredential issueCustomVCUsingBaseWallet(String holderBPn, String holderDid, String issuerDid, String type, HttpHeaders headers,
MIWSettings miwSettings, ObjectMapper objectMapper, TestRestTemplate restTemplate) {

Map<String, Object> map = getCredentialAsMap(issuerDid, type, miwSettings, objectMapper);
Map<String, Object> map = getCredentialAsMap(holderBPn, holderDid, issuerDid, type, miwSettings, objectMapper);
HttpEntity<Map> entity = new HttpEntity<>(map, headers);
ResponseEntity<String> response = restTemplate.exchange(RestURI.ISSUERS_CREDENTIALS + "?holderDid={did}", HttpMethod.POST, entity, String.class, holderDid);
if (response.getStatusCode().value() == HttpStatus.FORBIDDEN.value()) {
Expand All @@ -235,15 +237,21 @@ public static VerifiableCredential issueCustomVCUsingBaseWallet(String holderDid
return new VerifiableCredential(new ObjectMapper().readValue(response.getBody(), Map.class));
}

public static Map<String, Object> getCredentialAsMap(String issuerDid, String type, MIWSettings miwSettings, ObjectMapper objectMapper) throws JsonProcessingException {
public static Map<String, Object> getCredentialAsMap(String holderBpn, String holderDid, String issuerDid, String type, MIWSettings miwSettings, ObjectMapper objectMapper) throws JsonProcessingException {
// Create VC without proof
//VC Builder
VerifiableCredentialBuilder verifiableCredentialBuilder =
new VerifiableCredentialBuilder();

Map<String, Object> subjectData;
if (Objects.equals(type, StringPool.BPN_CREDENTIAL)) {
subjectData = Map.of(Verifiable.ID, holderDid, StringPool.BPN, holderBpn);
} else {
subjectData = Map.of(Verifiable.ID, "test");
}
//VC Subject
VerifiableCredentialSubject verifiableCredentialSubject =
new VerifiableCredentialSubject(Map.of("id", "test"));
new VerifiableCredentialSubject(subjectData);

//Using Builder
VerifiableCredential credentialWithoutProof =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti
List<String> typesOfVcs = List.of("Type1", "Type2", "Type3");

typesOfVcs.forEach(type -> {
VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(did, miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);
VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(bpn, did, miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);
vcs.add(verifiableCredential);
});

Expand Down Expand Up @@ -215,7 +215,7 @@ void getCredentialsAsJWT200() throws JSONException {
List<String> typesOfVcs = List.of("Type1", "Type2", "Type3");

typesOfVcs.forEach(type -> {
VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(did, miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);
VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(bpn, did, miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);
vcs.add(verifiableCredential);
});

Expand Down Expand Up @@ -344,7 +344,7 @@ private Map<String, Object> issueVC() throws JsonProcessingException {
String defaultLocation = miwSettings.host() + COLON_SEPARATOR + bpn;
ResponseEntity<String> response = TestUtils.createWallet(bpn, "Test Wallet", restTemplate, baseBpn, defaultLocation);
Wallet wallet = TestUtils.getWalletFromString(response.getBody());
VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(wallet.getDid(), miwSettings.authorityWalletDid(), "Type1", AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);
VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(bpn, wallet.getDid(), miwSettings.authorityWalletDid(), "Type1", AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);
Map<String, Object> map = objectMapper.readValue(verifiableCredential.toJson(), Map.class);
return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti
//issue some VCs
List<String> typesOfVcs = List.of("Type1", "Type2", "Type3");
typesOfVcs.forEach(type -> {
TestUtils.issueCustomVCUsingBaseWallet(wallet.getDid(), miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);
TestUtils.issueCustomVCUsingBaseWallet(holderBpn, wallet.getDid(), miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);
});


Expand Down Expand Up @@ -146,7 +146,7 @@ void getCredentialsAsJWT200() throws JSONException {
//create test data
List<String> typesOfVcs = List.of("Type1", "Type2", "Type3");
typesOfVcs.forEach(type -> {
TestUtils.issueCustomVCUsingBaseWallet(wallet.getDid(), miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);
TestUtils.issueCustomVCUsingBaseWallet(holderBpn, wallet.getDid(), miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);
});

HttpEntity<Map> entity = new HttpEntity<>(headers);
Expand Down Expand Up @@ -191,7 +191,7 @@ void issueCredentialsWithoutBaseWalletBPN403() throws JsonProcessingException {


Assertions.assertThrows(ForbiddenException.class, () -> {
TestUtils.issueCustomVCUsingBaseWallet(holderDid, holderDid, type, headers, miwSettings, objectMapper, restTemplate);
TestUtils.issueCustomVCUsingBaseWallet(bpn, holderDid, holderDid, type, headers, miwSettings, objectMapper, restTemplate);
});
}

Expand All @@ -206,7 +206,7 @@ void issueCredentialsToBaseWallet200() throws JsonProcessingException {
String defaultLocation = miwSettings.host() + COLON_SEPARATOR + bpn;
TestUtils.createWallet(bpn, bpn, restTemplate, baseBpn, defaultLocation);

VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(miwSettings.authorityWalletDid(), miwSettings.authorityWalletDid(), type, headers, miwSettings, objectMapper, restTemplate);
VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(baseBpn, miwSettings.authorityWalletDid(), miwSettings.authorityWalletDid(), type, headers, miwSettings, objectMapper, restTemplate);

Assertions.assertNotNull(verifiableCredential.getProof());

Expand All @@ -229,7 +229,7 @@ void issueCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcep
String defaultLocation = miwSettings.host() + COLON_SEPARATOR + bpn;
TestUtils.createWallet(bpn, bpn, restTemplate, baseBpn, defaultLocation);

VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(did, miwSettings.authorityWalletDid(), type, headers, miwSettings, objectMapper, restTemplate);
VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(bpn, did, miwSettings.authorityWalletDid(), type, headers, miwSettings, objectMapper, restTemplate);

Assertions.assertNotNull(verifiableCredential.getProof());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ public void setup() throws DidParseException {
tenant_2 = DidParser.parse(tenantWallet2.getDid());


Map<String, Object> type1 = TestUtils.getCredentialAsMap(miwSettings.authorityWalletDid(), "Type1", miwSettings, new com.fasterxml.jackson.databind.ObjectMapper());
Map<String, Object> type1 = TestUtils.getCredentialAsMap(miwSettings.authorityWalletBpn(), miwSettings.authorityWalletDid(), miwSettings.authorityWalletDid(), "Type1", miwSettings, new com.fasterxml.jackson.databind.ObjectMapper());

CredentialsResponse rs1 = issuersCredentialService.issueCredentialUsingBaseWallet(tenantWallet.getDid(), type1, false, bpnOperator);
vc_1 = new ObjectMapper().convertValue(rs1, VerifiableCredential.class);


Map<String, Object> type2 = TestUtils.getCredentialAsMap(miwSettings.authorityWalletDid(), "Type2", miwSettings, new com.fasterxml.jackson.databind.ObjectMapper());
Map<String, Object> type2 = TestUtils.getCredentialAsMap(miwSettings.authorityWalletBpn(), miwSettings.authorityWalletDid(), miwSettings.authorityWalletDid(), "Type2", miwSettings, new com.fasterxml.jackson.databind.ObjectMapper());

CredentialsResponse rs2 = issuersCredentialService.issueCredentialUsingBaseWallet(tenantWallet.getDid(), type2, false, bpnOperator);
vc_2 = new ObjectMapper().convertValue(rs2, VerifiableCredential.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

package org.eclipse.tractusx.managedidentitywallets.vp;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.nimbusds.jwt.JWT;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.JWTParser;
Expand All @@ -29,6 +30,7 @@
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.StringPool;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.JtiRecord;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet;
import org.eclipse.tractusx.managedidentitywallets.dao.repository.JtiRepository;
Expand All @@ -38,8 +40,10 @@
import org.eclipse.tractusx.managedidentitywallets.exception.PermissionViolationException;
import org.eclipse.tractusx.managedidentitywallets.service.IssuersCredentialService;
import org.eclipse.tractusx.managedidentitywallets.service.PresentationService;
import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.TestConstants;
import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils;
import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialSubject;
import org.eclipse.tractusx.ssi.lib.model.verifiable.presentation.VerifiablePresentation;
Expand Down Expand Up @@ -71,7 +75,7 @@
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = {
ManagedIdentityWalletsApplication.class })
@ContextConfiguration(initializers = { TestContextInitializer.class })
public class PresentationServiceTest {
class PresentationServiceTest {

@Autowired
private MIWSettings miwSettings;
Expand All @@ -88,6 +92,9 @@ public class PresentationServiceTest {
@Autowired
private IssuersCredentialService issuersCredentialService;

@Autowired
private ObjectMapper objectMapper;

@Autowired
private WalletRepository walletRepository;

Expand All @@ -102,6 +109,10 @@ void createPresentation200ResponseAsJWT() {
JtiRecord jtiRecord = buildJti(jtiValue, false);
jtiRepository.save(jtiRecord);

//issue BPN vc
VerifiableCredential bpnVc = TestUtils.issueCustomVCUsingBaseWallet(bpn, did, miwSettings.authorityWalletDid(), StringPool.BPN_CREDENTIAL,
AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);

Map<String, Object> presentation = presentationService.createVpWithRequiredScopes(SignedJWT.parse(accessToken), asJwt);
String vpAsJwt = String.valueOf(presentation.get(VERIFIABLE_PRESENTATION));
JWT jwt = JWTParser.parse(vpAsJwt);
Expand All @@ -122,6 +133,10 @@ void createPresentation200ResponseAsJsonLD() {
JtiRecord jtiRecord = buildJti(jtiValue, false);
jtiRepository.save(jtiRecord);

//issue BPN vc
TestUtils.issueCustomVCUsingBaseWallet(bpn, did, miwSettings.authorityWalletDid(), StringPool.BPN_CREDENTIAL,
AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);

Map<String, Object> presentation = presentationService.createVpWithRequiredScopes(SignedJWT.parse(accessToken), asJwt);
Assertions.assertNotNull(presentation);

Expand All @@ -130,8 +145,8 @@ void createPresentation200ResponseAsJsonLD() {
VerifiableCredential verifiableCredential = vp.getVerifiableCredentials().get(0);
VerifiableCredentialSubject verifiableCredentialSubject = verifiableCredential.getCredentialSubject().get(0);
Assertions.assertNotNull(verifiableCredentialSubject);
Assertions.assertEquals(bpn, verifiableCredentialSubject.get("bpn"));
Assertions.assertEquals(did, verifiableCredentialSubject.get("id"));
Assertions.assertEquals(bpn, verifiableCredentialSubject.get(StringPool.BPN));
Assertions.assertEquals(did, verifiableCredentialSubject.get(Verifiable.ID));
}

@SneakyThrows
Expand All @@ -143,6 +158,11 @@ void createPresentation200ResponseNoJtiRecord() {
String jtiValue = generateUuid();
String accessToken = generateAccessToken(did, did, did, BPN_CREDENTIAL_READ, jtiValue);

//issue BPN vc
TestUtils.issueCustomVCUsingBaseWallet(bpn, did, miwSettings.authorityWalletDid(), StringPool.BPN_CREDENTIAL,
AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);


Map<String, Object> presentation = presentationService.createVpWithRequiredScopes(SignedJWT.parse(accessToken), asJwt);
String vpAsJwt = String.valueOf(presentation.get(VERIFIABLE_PRESENTATION));
JWT jwt = JWTParser.parse(vpAsJwt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private Map<String, Object> getIssueVPRequest(String bpn) throws JsonProcessingE
List<String> typesOfVcs = List.of("Type1", "Type2", "Type3");

typesOfVcs.forEach(type -> {
VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(wallet.getDid(), miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);
VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(wallet.getBpn(), wallet.getDid(), miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate);
vcs.add(verifiableCredential);
});
List<HoldersCredential> credentials = holdersCredentialRepository.getByHolderDid(wallet.getDid());
Expand Down

0 comments on commit e3a48bf

Please sign in to comment.