Skip to content

Commit

Permalink
fix: only pass base URL for the CredentialService (#3666)
Browse files Browse the repository at this point in the history
* fix: only pass base URL for the CredentialService

* run ci
  • Loading branch information
paullatzelsperger authored Nov 28, 2023
1 parent ad100a6 commit cd9dbec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import static org.eclipse.edc.spi.result.Result.success;

public class DefaultCredentialServiceClient implements CredentialServiceClient {
public static final String PRESENTATION_ENDPOINT = "/presentation/query";
private final EdcHttpClient httpClient;
private final JsonBuilderFactory jsonFactory;
private final ObjectMapper objectMapper;
Expand All @@ -63,14 +64,16 @@ public DefaultCredentialServiceClient(EdcHttpClient httpClient, JsonBuilderFacto
}

@Override
public Result<List<VerifiablePresentationContainer>> requestPresentation(String credentialServiceUrl, String selfIssuedTokenJwt, List<String> scopes) {
public Result<List<VerifiablePresentationContainer>> requestPresentation(String credentialServiceBaseUrl, String selfIssuedTokenJwt, List<String> scopes) {
var query = createPresentationQuery(scopes);

var url = credentialServiceBaseUrl + PRESENTATION_ENDPOINT;

try {
var requestJson = objectMapper.writeValueAsString(query);
var request = new Request.Builder()
.post(RequestBody.create(requestJson, MediaType.parse("application/json")))
.url(credentialServiceUrl)
.url(url)
.addHeader("Authorization", "%s".formatted(selfIssuedTokenJwt))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
import static org.eclipse.edc.junit.testfixtures.TestUtils.getResourceFileContentAsString;
import static org.eclipse.edc.spi.result.Result.success;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class DefaultCredentialServiceClientTest {
Expand Down Expand Up @@ -73,6 +75,7 @@ void requestPresentation_singleLdpVp() throws IOException {
var result = client.requestPresentation(CS_URL, "foo", List.of());
assertThat(result.succeeded()).isTrue();
assertThat(result.getContent()).hasSize(1).allMatch(vpc -> vpc.format() == CredentialFormat.JSON_LD);
verify(httpClientMock).execute(argThat(rq -> rq.url().toString().endsWith("/presentation/query")));
}

@Test
Expand All @@ -84,6 +87,7 @@ void requestPresentation_singleJwtVp() throws IOException {
var result = client.requestPresentation(CS_URL, "foo", List.of());
assertThat(result.succeeded()).isTrue();
assertThat(result.getContent()).hasSize(1).allMatch(vpc -> vpc.format() == CredentialFormat.JWT);
verify(httpClientMock).execute(argThat(rq -> rq.url().toString().endsWith("/presentation/query")));
}

@Test
Expand All @@ -97,6 +101,7 @@ void requestPresentationLdp_multipleVp_mixed() throws IOException {
assertThat(result.getContent()).hasSize(2)
.anySatisfy(vp -> assertThat(vp.format()).isEqualTo(CredentialFormat.JSON_LD))
.anySatisfy(vp -> assertThat(vp.format()).isEqualTo(CredentialFormat.JWT));
verify(httpClientMock).execute(argThat(rq -> rq.url().toString().endsWith("/presentation/query")));
}

@Test
Expand All @@ -109,6 +114,7 @@ void requestPresentation_mulipleVp_onlyLdp() throws IOException {
assertThat(result.succeeded()).isTrue();
assertThat(result.getContent()).hasSize(2)
.allSatisfy(vp -> assertThat(vp.format()).isEqualTo(CredentialFormat.JSON_LD));
verify(httpClientMock).execute(argThat(rq -> rq.url().toString().endsWith("/presentation/query")));
}

@Test
Expand All @@ -121,6 +127,7 @@ void requestPresentation_mulipleVp_onlyJwt() throws IOException {
assertThat(result.succeeded()).isTrue();
assertThat(result.getContent()).hasSize(2)
.allSatisfy(vp -> assertThat(vp.format()).isEqualTo(CredentialFormat.JWT));
verify(httpClientMock).execute(argThat(rq -> rq.url().toString().endsWith("/presentation/query")));
}

@ParameterizedTest(name = "CS returns HTTP error code {0}")
Expand All @@ -132,6 +139,7 @@ void requestPresentation_csReturnsError(int httpCode) throws IOException {
var res = client.requestPresentation(CS_URL, "foo", List.of());
assertThat(res.failed()).isTrue();
assertThat(res.getFailureDetail()).isEqualTo("Presentation Query failed: HTTP %s, message: Test failure".formatted(httpCode));
verify(httpClientMock).execute(argThat(rq -> rq.url().toString().endsWith("/presentation/query")));
}

@DisplayName("CS returns an empty array, because no VC was found")
Expand All @@ -143,6 +151,7 @@ void requestPresentation_emptyArray() throws IOException {
var res = client.requestPresentation(CS_URL, "foo", List.of());
assertThat(res.succeeded()).isTrue();
assertThat(res.getContent()).isNotNull().doesNotContainNull().isEmpty();
verify(httpClientMock).execute(argThat(rq -> rq.url().toString().endsWith("/presentation/query")));
}

private VerifiablePresentation createPresentation() {
Expand Down

0 comments on commit cd9dbec

Please sign in to comment.