Skip to content

Commit

Permalink
chore: fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswr committed Nov 21, 2023
1 parent e95c2d8 commit e093652
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 56 deletions.
1 change: 1 addition & 0 deletions src/common/getters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
import type { AccessGrant, AccessRequest } from "../gConsent";
import { GC_FOR_PERSONAL_DATA } from "../gConsent/constants";
import { TYPE, cred, gc } from "./constants";

const { quad, namedNode, literal, blankNode } = DataFactory;

jest.mock("@inrupt/universal-fetch", () => ({
Expand Down
114 changes: 61 additions & 53 deletions src/gConsent/manage/getAccessGrant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,9 @@ import type * as CrossFetch from "@inrupt/universal-fetch";
import { Response } from "@inrupt/universal-fetch";
import { beforeAll, describe, expect, it, jest } from "@jest/globals";

import {
mockAccessGrantObject,
mockAccessGrantVc,
mockAccessRequestVc,
} from "../util/access.mock";
import { mockAccessGrantObject, mockAccessGrantVc } from "../util/access.mock";
import { toBeEqual } from "../util/toBeEqual.mock";
import { getAccessGrant } from "./getAccessGrant";
import { verifiableCredentialToDataset } from "@inrupt/solid-client-vc";

jest.mock("@inrupt/universal-fetch", () => {
const crossFetch = jest.requireActual(
Expand Down Expand Up @@ -63,18 +58,19 @@ describe("getAccessGrant", () => {
expect(mockedFetch).toHaveBeenCalledWith("https://some.vc.url");
});

it("throws if resolving the IRI results in an HTTP error", async () => {
await expect(
it("throws if resolving the IRI results in an HTTP error", () => {
return expect(
getAccessGrant("https://some.vc.url", {
fetch: async () => new Response("Not Found", { status: 404, statusText: "Not Found" }),
fetch: async () =>
new Response("Not Found", { status: 404, statusText: "Not Found" }),
}),
).rejects.toThrow(
/Could not resolve \[https:\/\/some.vc.url\].*404 Not Found/,
);
});

it("throws if the given IRI does not resolve to a Verifiable Credential", async () => {
await expect(
it("throws if the given IRI does not resolve to a Verifiable Credential", () => {
return expect(
getAccessGrant("https://some.vc.url", {
fetch: async () => new Response("{'someKey': 'someValue'}"),
}),
Expand All @@ -83,12 +79,13 @@ describe("getAccessGrant", () => {
);
});

it("throws if the given IRI does not resolve to a access grant Verifiable Credential", async () => {
await expect(
it("throws if the given IRI does not resolve to a access grant Verifiable Credential", () => {
return expect(
getAccessGrant("https://some.vc.url", {
fetch: async () => new Response(JSON.stringify(await mockAccessRequestVc()), {
headers: new Headers([["content-type", "application/json"]]),
}),
fetch: async () =>
new Response(JSON.stringify(mockAccessGrant), {
headers: new Headers([["content-type", "application/json"]]),
}),
}),
).rejects.toThrow(/not an Access Grant/);
});
Expand All @@ -102,9 +99,10 @@ describe("getAccessGrant", () => {
"https://w3id.org/GConsent#ConsentStatusDenied";

const accessGrant = await getAccessGrant("https://some.vc.url", {
fetch: async () => new Response(JSON.stringify(mockedAccessGrant), {
headers: new Headers([["content-type", "application/json"]]),
}),
fetch: async () =>
new Response(JSON.stringify(mockedAccessGrant), {
headers: new Headers([["content-type", "application/json"]]),
}),
});
toBeEqual(accessGrant, mockedAccessGrant);
});
Expand All @@ -114,9 +112,10 @@ describe("getAccessGrant", () => {
// eslint-disable-next-line jest/expect-expect
it("returns the access grant with the given IRI", async () => {
const accessGrant = await getAccessGrant("https://some.vc.url", {
fetch: async () => new Response(JSON.stringify(mockAccessGrantObject()), {
headers: new Headers([["content-type", "application/json"]]),
}),
fetch: async () =>
new Response(JSON.stringify(mockAccessGrantObject()), {
headers: new Headers([["content-type", "application/json"]]),
}),
});
toBeEqual(accessGrant, mockAccessGrant);
});
Expand All @@ -129,27 +128,28 @@ describe("getAccessGrant", () => {
toBeEqual(
await getAccessGrant("https://some.vc.url", {
// The server returns an equivalent JSON-LD with a different frame:
fetch: async () => new Response(
JSON.stringify({
...normalizedAccessGrant,
credentialSubject: {
...normalizedAccessGrant.credentialSubject,
providedConsent: {
...normalizedAccessGrant.credentialSubject.providedConsent,
// The 1-value array is replaced by the literal value.
forPersonalData:
normalizedAccessGrant.credentialSubject.providedConsent
.forPersonalData[0],
mode: normalizedAccessGrant.credentialSubject.providedConsent
.mode[0],
inherit: "true",
fetch: async () =>
new Response(
JSON.stringify({
...normalizedAccessGrant,
credentialSubject: {
...normalizedAccessGrant.credentialSubject,
providedConsent: {
...normalizedAccessGrant.credentialSubject.providedConsent,
// The 1-value array is replaced by the literal value.
forPersonalData:
normalizedAccessGrant.credentialSubject.providedConsent
.forPersonalData[0],
mode: normalizedAccessGrant.credentialSubject.providedConsent
.mode[0],
inherit: "true",
},
},
}),
{
headers: new Headers([["content-type", "application/json"]]),
},
}),
{
headers: new Headers([["content-type", "application/json"]]),
},
),
),
}),
mockAccessGrant,
);
Expand All @@ -171,20 +171,28 @@ describe("getAccessGrant", () => {
toBeEqual(accessGrant, mockAccessGrant);
});

it("errors if the response is not a full access grant", async () => {
expect(getAccessGrant(new URL("https://some.vc.url"), {
fetch: async () => new Response(JSON.stringify({
"@context": "https://www.w3.org/2018/credentials/v1",
id: "https://some.credential",
})),
})).rejects.toThrow('the result is not a Verifiable Credential');
it("errors if the response is not a full access grant", () => {
return expect(
getAccessGrant(new URL("https://some.vc.url"), {
fetch: async () =>
new Response(
JSON.stringify({
"@context": "https://www.w3.org/2018/credentials/v1",
id: "https://some.credential",
}),
),
}),
).rejects.toThrow("the result is not a Verifiable Credential");
});

it("errors if the response is an empty json object", async () => {
expect(getAccessGrant(new URL("https://some.vc.url"), {
fetch: async () => new Response(JSON.stringify({}), {
headers: new Headers([["content-type", "application/json"]]),
it("errors if the response is an empty json object", () => {
return expect(
getAccessGrant(new URL("https://some.vc.url"), {
fetch: async () =>
new Response(JSON.stringify({}), {
headers: new Headers([["content-type", "application/json"]]),
}),
}),
})).rejects.toThrow('the result is not a Verifiable Credential');
).rejects.toThrow("the result is not a Verifiable Credential");
});
});
9 changes: 6 additions & 3 deletions src/gConsent/manage/getAccessGrant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ export async function getAccessGrant(
const responseErrorClone = await response.text();
let data;
try {
data = await verifiableCredentialToDataset(normalizeAccessGrant(JSON.parse(responseErrorClone)), {
baseIRI: accessGrantVcUrl.toString(),
});
data = await verifiableCredentialToDataset(
normalizeAccessGrant(JSON.parse(responseErrorClone)),
{
baseIRI: accessGrantVcUrl.toString(),
},
);
} catch (e) {
throw new Error(
`Unexpected response when resolving [${vcUrl}], the result is not a Verifiable Credential: ${responseErrorClone}.\n\nError details: ${e}`,
Expand Down

0 comments on commit e093652

Please sign in to comment.