Skip to content

Commit

Permalink
WIP: fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswr committed Sep 25, 2023
1 parent 2f660c1 commit 54bfacd
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 158 deletions.
86 changes: 52 additions & 34 deletions src/gConsent/manage/approveAccessRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

import { jest, it, describe, expect } from "@jest/globals";
import { jest, it, describe, expect, beforeAll } from "@jest/globals";
import { Response } from "@inrupt/universal-fetch";
import type * as CrossFetch from "@inrupt/universal-fetch";

Expand Down Expand Up @@ -61,7 +61,15 @@ jest.mock("@inrupt/solid-client", () => {
return solidClientModule;
});

jest.mock("@inrupt/solid-client-vc");
jest.mock("@inrupt/solid-client-vc", () => {
const { getVerifiableCredentialFromResponse } = jest.requireActual(
"@inrupt/solid-client-vc",
) as jest.Mocked<typeof VcClient>;
return {
getVerifiableCredentialFromResponse,
issueVerifiableCredential: jest.fn(),
};
});
jest.mock("@inrupt/universal-fetch", () => {
const crossFetch = jest.requireActual(
"@inrupt/universal-fetch",
Expand Down Expand Up @@ -122,6 +130,16 @@ const spiedAcrLookup = jest.spyOn(
const spiedAcrUpdate = jest.spyOn(mockedClientModule.acp_ess_2, "setVcAccess");

describe("approveAccessRequest", () => {
let accessRequestVc: Awaited<ReturnType<typeof mockAccessRequestVc>>;
let accessGrantVc: Awaited<ReturnType<typeof mockAccessGrantVc>>;

beforeAll(async () => {
accessRequestVc = await mockAccessRequestVc();
accessGrantVc = await mockAccessGrantVc();
console.log('the mocked access request vc is', JSON.stringify(accessRequestVc, null, 2))
console.log('the mocked access grant vc is', JSON.stringify(accessGrantVc, null, 2))
});

// FIXME: This test must run before the other tests mocking the ACP client.
// This means that some mocked isn't cleared properly between tests.
it("updates the target resources' ACR appropriately", async () => {
Expand Down Expand Up @@ -177,9 +195,7 @@ describe("approveAccessRequest", () => {
it("throws if the resource's ACR cannot be accessed by the current user", async () => {
mockAcpClient({ hasAccessibleAcr: false });
mockAccessApiEndpoint();
await expect(
approveAccessRequest(await mockAccessRequestVc()),
).rejects.toThrow(
await expect(approveAccessRequest(accessRequestVc)).rejects.toThrow(
"The current user does not have access to the resource's Access Control Resource",
);
});
Expand All @@ -188,7 +204,7 @@ describe("approveAccessRequest", () => {
mockAccessApiEndpoint();
await expect(
approveAccessRequest({
...(await mockAccessRequestVc()),
...accessRequestVc,
type: ["NotASolidAccessRequest"],
}),
).rejects.toThrow(
Expand All @@ -198,7 +214,7 @@ describe("approveAccessRequest", () => {

it("throws if the provided VC isn't an access request", async () => {
mockAccessApiEndpoint();
const accessRequest = await mockAccessRequestVc();
const accessRequest = accessRequestVc;
await expect(
approveAccessRequest({
...accessRequest,
Expand All @@ -223,7 +239,7 @@ describe("approveAccessRequest", () => {
"issueVerifiableCredential",
);
spiedIssueRequest.mockResolvedValueOnce(await mockAccessGrantVc());
await approveAccessRequest(await mockAccessRequestVc(), undefined, {
await approveAccessRequest(accessRequestVc, undefined, {
accessEndpoint: "https://some.consent-endpoint.override/",
fetch: jest.fn<typeof fetch>(),
});
Expand All @@ -247,7 +263,7 @@ describe("approveAccessRequest", () => {
"issueVerifiableCredential",
);
spiedIssueRequest.mockResolvedValueOnce(await mockAccessGrantVc());
await approveAccessRequest(await mockAccessRequestVc(), undefined, {
await approveAccessRequest(accessRequestVc, undefined, {
fetch: mockedFetch,
});
expect(spiedIssueRequest).toHaveBeenCalledWith(
Expand All @@ -270,21 +286,21 @@ describe("approveAccessRequest", () => {
);
const mockedIssue = jest.spyOn(mockedVcModule, "issueVerifiableCredential");
mockedIssue.mockResolvedValueOnce(await mockAccessGrantVc());
await approveAccessRequest(await mockAccessRequestVc(), undefined, {
await approveAccessRequest(accessRequestVc, undefined, {
fetch: jest.fn(global.fetch),
});

expect(spiedIssueRequest).toHaveBeenCalledWith(
`${MOCKED_ACCESS_ISSUER}/issue`,
expect.objectContaining({
providedConsent: {
mode: (await mockAccessRequestVc()).credentialSubject.hasConsent.mode,
mode: accessRequestVc.credentialSubject.hasConsent.mode,
hasStatus: "https://w3id.org/GConsent#ConsentStatusExplicitlyGiven",
forPersonalData: (await mockAccessRequestVc()).credentialSubject
.hasConsent.forPersonalData,
isProvidedTo: (await mockAccessRequestVc()).credentialSubject.id,
forPersonalData:
accessRequestVc.credentialSubject.hasConsent.forPersonalData,
isProvidedTo: accessRequestVc.credentialSubject.id,
},
inbox: (await mockAccessRequestVc()).credentialSubject.inbox,
inbox: accessRequestVc.credentialSubject.inbox,
}),
expect.objectContaining({
type: ["SolidAccessGrant"],
Expand Down Expand Up @@ -313,15 +329,14 @@ describe("approveAccessRequest", () => {
`${MOCKED_ACCESS_ISSUER}/issue`,
expect.objectContaining({
providedConsent: {
mode: (await mockAccessRequestVc()).credentialSubject.hasConsent.mode,
mode: accessRequestVc.credentialSubject.hasConsent.mode,
hasStatus: "https://w3id.org/GConsent#ConsentStatusExplicitlyGiven",
forPersonalData: (await mockAccessRequestVc()).credentialSubject
.hasConsent.forPersonalData,
isProvidedTo: (await mockAccessRequestVc()).credentialSubject.id,
forPurpose: (await mockAccessRequestVc()).credentialSubject.hasConsent
.forPurpose,
forPersonalData:
accessRequestVc.credentialSubject.hasConsent.forPersonalData,
isProvidedTo: accessRequestVc.credentialSubject.id,
forPurpose: accessRequestVc.credentialSubject.hasConsent.forPurpose,
},
inbox: (await mockAccessRequestVc()).credentialSubject.inbox,
inbox: accessRequestVc.credentialSubject.inbox,
}),
expect.objectContaining({
type: ["SolidAccessGrant"],
Expand All @@ -341,7 +356,7 @@ describe("approveAccessRequest", () => {
"ConsentStatusExplicitlyGiven";
mockedIssue.mockResolvedValueOnce(mockedVc);
await expect(
approveAccessRequest(await mockAccessRequestVc(), undefined, {
approveAccessRequest(accessRequestVc, undefined, {
fetch: jest.fn(global.fetch),
updateAcr: false,
}),
Expand All @@ -355,7 +370,7 @@ describe("approveAccessRequest", () => {
jest.requireMock("@inrupt/solid-client-vc") as typeof VcClient,
"issueVerifiableCredential",
);
mockedIssue.mockResolvedValueOnce(await mockAccessRequestVc());
mockedIssue.mockResolvedValueOnce(accessRequestVc);
const mockedFetch = jest.fn(global.fetch);
mockedFetch.mockResolvedValueOnce(
new Response(JSON.stringify(await mockConsentRequestVc())),
Expand Down Expand Up @@ -748,7 +763,9 @@ describe("approveAccessRequest", () => {
);
});

it("issues a proper access grant from a request VC using the deprecated signature", async () => {
it.only("issues a proper access grant from a request VC using the deprecated signature", async () => {
// console.log('calling issue request with', accessRequestVc)

mockAcpClient();
mockAccessApiEndpoint();
const mockedVcModule = jest.requireMock(
Expand All @@ -761,24 +778,25 @@ describe("approveAccessRequest", () => {
spiedIssueRequest.mockResolvedValueOnce(await mockAccessGrantVc());
await approveAccessRequest(
"https://some.resource.owner",
await mockAccessRequestVc(),
accessRequestVc,
undefined,
{
fetch: jest.fn(global.fetch),
},
);

// Tests like this are failing because of the fact that
expect(spiedIssueRequest).toHaveBeenCalledWith(
`${MOCKED_ACCESS_ISSUER}/issue`,
expect.objectContaining({
providedConsent: {
mode: (await mockAccessRequestVc()).credentialSubject.hasConsent.mode,
mode: accessRequestVc.credentialSubject.hasConsent.mode,
hasStatus: "https://w3id.org/GConsent#ConsentStatusExplicitlyGiven",
forPersonalData: (await mockAccessRequestVc()).credentialSubject
.hasConsent.forPersonalData,
isProvidedTo: (await mockAccessRequestVc()).credentialSubject.id,
forPersonalData:
accessRequestVc.credentialSubject.hasConsent.forPersonalData,
isProvidedTo: accessRequestVc.credentialSubject.id,
},
inbox: (await mockAccessRequestVc()).credentialSubject.inbox,
inbox: accessRequestVc.credentialSubject.inbox,
}),
expect.objectContaining({
type: ["SolidAccessGrant"],
Expand All @@ -794,11 +812,11 @@ describe("approveAccessRequest", () => {
jest.requireMock("@inrupt/solid-client-vc") as typeof VcClient,
"issueVerifiableCredential",
);
mockedIssue.mockResolvedValueOnce(await mockAccessRequestVc());
mockedIssue.mockResolvedValueOnce(accessRequestVc);
await expect(
approveAccessRequest(
"https://some.resource.owner",
await mockAccessRequestVc(),
accessRequestVc,
undefined,
{
fetch: jest.fn(global.fetch),
Expand Down Expand Up @@ -835,7 +853,7 @@ describe("approveAccessRequest", () => {
},
});
await expect(
approveAccessRequest(await mockAccessRequestVc(), undefined, {
approveAccessRequest(accessRequestVc, undefined, {
fetch: jest.fn(global.fetch),
}),
).resolves.toStrictEqual(normalizedAccessGrant);
Expand Down
66 changes: 29 additions & 37 deletions src/gConsent/manage/denyAccessRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,17 @@ jest.mock("@inrupt/universal-fetch", () => {

// TODO: Extract the fetch VC function and related tests
describe("denyAccessRequest", () => {
let accessRequestVc: Awaited<ReturnType<typeof mockAccessRequestVc>>;

beforeAll(async () => {
accessRequestVc = await mockAccessRequestVc();
});

it("throws if the provided VC isn't a Solid access request", async () => {
mockAccessApiEndpoint();
await expect(
denyAccessRequest({
...(await mockAccessRequestVc()),
...accessRequestVc,
type: ["NotASolidAccessRequest"],
}),
).rejects.toThrow(
Expand All @@ -66,9 +72,7 @@ describe("denyAccessRequest", () => {

it("throws if there is no well known access endpoint", async () => {
mockAccessApiEndpoint(false);
await expect(
denyAccessRequest(await mockAccessRequestVc()),
).rejects.toThrow(
await expect(denyAccessRequest(accessRequestVc)).rejects.toThrow(
"No access issuer listed for property [verifiable_credential_issuer] in",
);
});
Expand All @@ -81,7 +85,7 @@ describe("denyAccessRequest", () => {
mockedVcModule,
"issueVerifiableCredential",
);
await denyAccessRequest(await mockAccessRequestVc(), {
await denyAccessRequest(accessRequestVc, {
accessEndpoint: "https://some.access-endpoint.override/",
fetch: jest.fn<typeof fetch>(),
});
Expand All @@ -103,13 +107,9 @@ describe("denyAccessRequest", () => {
mockedVcModule,
"issueVerifiableCredential",
);
await denyAccessRequest(
"https://some.resource/owner",
await mockAccessRequestVc(),
{
fetch: mockedFetch,
},
);
await denyAccessRequest("https://some.resource/owner", accessRequestVc, {
fetch: mockedFetch,
});
expect(spiedIssueRequest).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
Expand Down Expand Up @@ -168,9 +168,7 @@ describe("denyAccessRequest", () => {
);
const mockedFetch = jest
.fn(global.fetch)
.mockResolvedValueOnce(
new Response(JSON.stringify(await mockAccessRequestVc())),
);
.mockResolvedValueOnce(new Response(JSON.stringify(accessRequestVc)));
await denyAccessRequest("https://some.credential", {
fetch: mockedFetch,
});
Expand All @@ -179,12 +177,12 @@ describe("denyAccessRequest", () => {
`${MOCKED_ACCESS_ISSUER}/issue`,
expect.objectContaining({
providedConsent: expect.objectContaining({
mode: (await mockAccessRequestVc()).credentialSubject.hasConsent.mode,
mode: accessRequestVc.credentialSubject.hasConsent.mode,
hasStatus: "https://w3id.org/GConsent#ConsentStatusDenied",
forPersonalData: (await mockAccessRequestVc()).credentialSubject
.hasConsent.forPersonalData,
forPersonalData:
accessRequestVc.credentialSubject.hasConsent.forPersonalData,
}),
inbox: (await mockAccessRequestVc()).credentialSubject.inbox,
inbox: accessRequestVc.credentialSubject.inbox,
}),
expect.objectContaining({
type: ["SolidAccessDenial"],
Expand All @@ -204,9 +202,7 @@ describe("denyAccessRequest", () => {
);
const mockedFetch = jest
.fn(global.fetch)
.mockResolvedValueOnce(
new Response(JSON.stringify(await mockAccessRequestVc())),
);
.mockResolvedValueOnce(new Response(JSON.stringify(accessRequestVc)));
await denyAccessRequest(new URL("https://some.credential"), {
fetch: mockedFetch,
});
Expand All @@ -216,12 +212,12 @@ describe("denyAccessRequest", () => {
`${MOCKED_ACCESS_ISSUER}/issue`,
expect.objectContaining({
providedConsent: expect.objectContaining({
mode: (await mockAccessRequestVc()).credentialSubject.hasConsent.mode,
mode: accessRequestVc.credentialSubject.hasConsent.mode,
hasStatus: "https://w3id.org/GConsent#ConsentStatusDenied",
forPersonalData: (await mockAccessRequestVc()).credentialSubject
.hasConsent.forPersonalData,
forPersonalData:
accessRequestVc.credentialSubject.hasConsent.forPersonalData,
}),
inbox: (await mockAccessRequestVc()).credentialSubject.inbox,
inbox: accessRequestVc.credentialSubject.inbox,
}),
expect.objectContaining({
type: ["SolidAccessDenial"],
Expand All @@ -239,26 +235,22 @@ describe("denyAccessRequest", () => {
mockedVcModule,
"issueVerifiableCredential",
);
await denyAccessRequest(
"https://some.resource.owner",
await mockAccessRequestVc(),
{
fetch: jest.fn(global.fetch),
},
);
await denyAccessRequest("https://some.resource.owner", accessRequestVc, {
fetch: jest.fn(global.fetch),
});

// TODO: Should we expect "isProvidedTo": "https://some.requestor" in "providedConsent" or nest the expect.objectContaining?
expect(spiedIssueRequest).toHaveBeenCalledWith(
`${MOCKED_ACCESS_ISSUER}/issue`,
expect.objectContaining({
providedConsent: {
mode: (await mockAccessRequestVc()).credentialSubject.hasConsent.mode,
mode: accessRequestVc.credentialSubject.hasConsent.mode,
hasStatus: "https://w3id.org/GConsent#ConsentStatusDenied",
forPersonalData: (await mockAccessRequestVc()).credentialSubject
.hasConsent.forPersonalData,
forPersonalData:
accessRequestVc.credentialSubject.hasConsent.forPersonalData,
isProvidedTo: "https://some.requestor",
},
inbox: (await mockAccessRequestVc()).credentialSubject.inbox,
inbox: accessRequestVc.credentialSubject.inbox,
}),
expect.objectContaining({
type: ["SolidAccessDenial"],
Expand Down
Loading

0 comments on commit 54bfacd

Please sign in to comment.