Skip to content

Commit 0895fe7

Browse files
authored
JCL-382: Additional tests for the query builder feature (#533)
1 parent 2265c44 commit 0895fe7

4 files changed

Lines changed: 88 additions & 32 deletions

File tree

access-grant/src/main/java/com/inrupt/client/accessgrant/AccessCredentialQuery.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public class AccessCredentialQuery<T extends AccessCredential> {
3636
private static final URI SOLID_ACCESS_REQUEST = URI.create("SolidAccessRequest");
3737
private static final URI SOLID_ACCESS_DENIAL = URI.create("SolidAccessDenial");
3838

39-
private final URI type;
4039
private final Set<URI> purposes;
4140
private final Set<String> modes;
4241
private final URI resource;
@@ -57,33 +56,13 @@ public class AccessCredentialQuery<T extends AccessCredential> {
5756
AccessCredentialQuery(final URI resource, final URI creator, final URI recipient,
5857
final Set<URI> purposes, final Set<String> modes, final Class<T> clazz) {
5958
this.clazz = Objects.requireNonNull(clazz, "The clazz parameter must not be null!");
60-
61-
if (AccessGrant.class.isAssignableFrom(clazz)) {
62-
this.type = SOLID_ACCESS_GRANT;
63-
} else if (AccessRequest.class.isAssignableFrom(clazz)) {
64-
this.type = SOLID_ACCESS_REQUEST;
65-
} else if (AccessDenial.class.isAssignableFrom(clazz)) {
66-
this.type = SOLID_ACCESS_DENIAL;
67-
} else {
68-
throw new AccessGrantException("Unsupported type " + clazz + " in query request");
69-
}
70-
7159
this.resource = resource;
7260
this.creator = creator;
7361
this.recipient = recipient;
7462
this.purposes = purposes;
7563
this.modes = modes;
7664
}
7765

78-
/**
79-
* Get the access credential type value.
80-
*
81-
* @return the type, never {@code null}
82-
*/
83-
public URI getType() {
84-
return type;
85-
}
86-
8766
/**
8867
* Get the requested resource.
8968
*

access-grant/src/test/java/com/inrupt/client/accessgrant/AccessGrantClientTest.java

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,48 @@ void testQueryGrantAgentBuilder() {
562562
assertEquals(1, grants.size());
563563
}
564564

565+
@Test
566+
void testQueryGrantModesPurposesBuilder() {
567+
final Map<String, Object> claims = new HashMap<>();
568+
claims.put("webid", WEBID);
569+
claims.put("sub", SUB);
570+
claims.put("iss", ISS);
571+
claims.put("azp", AZP);
572+
final String token = generateIdToken(claims);
573+
final AccessGrantClient client = agClient.session(OpenIdSession.ofIdToken(token));
574+
575+
final AccessCredentialQuery<AccessGrant> query = AccessCredentialQuery.newBuilder()
576+
.resource(URI.create("https://storage.example/"))
577+
.mode("Read").mode("Write").purpose(URI.create("https://id.example/Purpose8"))
578+
.purpose(URI.create("https://id.example/Purpose9")).build(AccessGrant.class);
579+
final List<AccessGrant> grants = client.query(query).toCompletableFuture().join();
580+
assertEquals(1, grants.size());
581+
}
582+
583+
@Test
584+
void testQueryGrantModesPurposesNoMatchBuilder() {
585+
final Map<String, Object> claims = new HashMap<>();
586+
claims.put("webid", WEBID);
587+
claims.put("sub", SUB);
588+
claims.put("iss", ISS);
589+
claims.put("azp", AZP);
590+
final String token = generateIdToken(claims);
591+
final AccessGrantClient client = agClient.session(OpenIdSession.ofIdToken(token));
592+
593+
final AccessCredentialQuery<AccessGrant> query1 = AccessCredentialQuery.newBuilder()
594+
.resource(URI.create("https://storage.example/"))
595+
.mode("Read").mode(null).purpose(URI.create("https://id.example/Purpose8"))
596+
.purpose(URI.create("https://id.example/Purpose9")).purpose(null).build(AccessGrant.class);
597+
final List<AccessGrant> grants1 = client.query(query1).toCompletableFuture().join();
598+
assertEquals(0, grants1.size());
599+
600+
final AccessCredentialQuery<AccessGrant> query2 = AccessCredentialQuery.newBuilder()
601+
.resource(URI.create("https://storage.example/")).mode("Read").mode("Write")
602+
.purpose(URI.create("https://id.example/Purpose9")).build(AccessGrant.class);
603+
final List<AccessGrant> grants2 = client.query(query2).toCompletableFuture().join();
604+
assertEquals(0, grants2.size());
605+
}
606+
565607
@Test
566608
void testQueryRequestAgent() {
567609
final Map<String, Object> claims = new HashMap<>();
@@ -659,16 +701,6 @@ void testQueryDenialBuilder() {
659701
assertEquals(1, grants.size());
660702
}
661703

662-
@Test
663-
void testQueryInvalidTypeBuilder() {
664-
final URI uri = URI.create("https://storage.example/f1759e6d-4dda-4401-be61-d90d070a5474/a/b/c");
665-
final AccessCredentialQuery.Builder builder = AccessCredentialQuery.newBuilder()
666-
.resource(uri).mode("Read");
667-
668-
assertThrows(AccessGrantException.class, () -> builder.build(AccessCredential.class));
669-
}
670-
671-
672704
@Test
673705
void testQueryInvalidType() {
674706
final Map<String, Object> claims = new HashMap<>();
@@ -684,7 +716,6 @@ void testQueryInvalidType() {
684716
client.query(uri, null, null, null, "Read", AccessCredential.class));
685717
}
686718

687-
688719
@Test
689720
void testQueryInvalidAuth() {
690721
final CompletionException err = assertThrows(CompletionException.class,

access-grant/src/test/java/com/inrupt/client/accessgrant/MockAccessGrantServer.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,19 @@ private void setupMocks() {
274274
.withStatus(401)
275275
.withHeader("WWW-Authenticate", "Bearer,DPoP algs=\"ES256\"")));
276276

277+
wireMockServer.stubFor(post(urlEqualTo("/derive"))
278+
.atPriority(1)
279+
.withHeader("Authorization", containing("Bearer eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9."))
280+
.withRequestBody(containing("SolidAccessGrant"))
281+
.withRequestBody(containing("\"https://id.example/Purpose8\""))
282+
.withRequestBody(containing("\"https://id.example/Purpose9\""))
283+
.withRequestBody(containing("\"Read\""))
284+
.withRequestBody(containing("\"Write\""))
285+
.willReturn(aResponse()
286+
.withStatus(200)
287+
.withHeader("Content-Type", "application/json")
288+
.withBody(getResource("/query_response7.json", wireMockServer.baseUrl()))));
289+
277290
wireMockServer.stubFor(post(urlEqualTo("/derive"))
278291
.atPriority(1)
279292
.withHeader("Authorization", containing("Bearer eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9."))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"verifiableCredential": [{
3+
"@context":[
4+
"https://www.w3.org/2018/credentials/v1",
5+
"https://w3id.org/security/suites/ed25519-2020/v1",
6+
"https://w3id.org/vc-revocation-list-2020/v1",
7+
"https://schema.inrupt.com/credentials/v1.jsonld"],
8+
"id":"{{baseUrl}}/access-grant-1",
9+
"type":["VerifiableCredential","SolidAccessGrant"],
10+
"issuer":"{{baseUrl}}",
11+
"expirationDate":"2022-08-27T12:00:00Z",
12+
"issuanceDate":"2022-08-25T20:34:05.153Z",
13+
"credentialStatus":{
14+
"id":"https://accessgrant.example/status/CVAM#2832",
15+
"revocationListCredential":"https://accessgrant.example/status/CVAM",
16+
"revocationListIndex":"2832",
17+
"type":"RevocationList2020Status"},
18+
"credentialSubject":{
19+
"id":"https://id.example/grantor",
20+
"providedConsent":{
21+
"mode":["Read","Write"],
22+
"hasStatus":"https://w3id.org/GConsent#ConsentStatusExplicitlyGiven",
23+
"isProvidedTo":"https://id.example/grantee",
24+
"forPurpose":["https://purpose.example/Purpose8","https://purpose.example/Purpose9"],
25+
"forPersonalData":["https://storage.example/ef9c4b90-0459-408d-bfa9-1c61d46e1eaf/"]}},
26+
"proof":{
27+
"created":"2022-08-25T20:34:05.236Z",
28+
"proofPurpose":"assertionMethod",
29+
"proofValue":"nIeQF44XVik7onnAbdkbp8xxJ2C8JoTw6-VtCkAzxuWYRFsSfYpft5MuAJaivyeKDmaK82Lj_YsME2xgL2WIBQ",
30+
"type":"Ed25519Signature2020",
31+
"verificationMethod":"https://accessgrant.example/key/1e332728-4af5-46e4-a5db-4f7b89e3f378"}
32+
}]
33+
}

0 commit comments

Comments
 (0)