Skip to content

Commit

Permalink
chore: improve getAccessRequest testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswr committed Nov 30, 2023
1 parent 2293d5d commit de61976
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 45 deletions.
10 changes: 6 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
"@types/rdfjs__dataset": "^1.0.5",
"auth-header": "^1.0.0",
"base64url": "^3.0.1",
"n3": "^1.17.2",
"rdf-namespaces": "^1.9.2"
},
"engines": {
Expand Down
4 changes: 3 additions & 1 deletion src/gConsent/guard/isBaseAccessVcBody.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 { namedNode, quad } from "@rdfjs/dataset";
import { DataFactory } from "n3";
import type { DatasetWithId } from "@inrupt/solid-client-vc";
import { getIssuanceDate } from "@inrupt/solid-client-vc";

Check failure on line 24 in src/gConsent/guard/isBaseAccessVcBody.ts

View workflow job for this annotation

GitHub Actions / lint / lint

Unable to resolve path to module '@inrupt/solid-client-vc'
import type { NamedNode } from "n3";
Expand All @@ -31,6 +31,8 @@ import { getConsent, getId } from "../../common/getters";
import { TYPE } from "../../common/constants";
import { isRdfjsGConsentAttributes } from "./isGConsentAttributes";

const { namedNode, quad } = DataFactory;

export function isBaseAccessVcBody(x: unknown): x is BaseAccessVcBody {
return (
isUnknownObject(x) &&
Expand Down
44 changes: 27 additions & 17 deletions src/gConsent/manage/getAccessRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { getAccessRequest } from "./getAccessRequest";
import { mockAccessGrantVc, mockAccessRequestVc } from "../util/access.mock";
import type { getSessionFetch } from "../../common/util/getSessionFetch";
import { normalizeAccessRequest } from "../request/issueAccessRequest";
import { toBeEqual, withoutDataset } from "../util/toBeEqual.mock";

jest.mock("../../common/util/getSessionFetch");
jest.mock("@inrupt/solid-client-vc", () => {
Expand Down Expand Up @@ -145,26 +146,35 @@ describe("getAccessRequest", () => {
// The server returns an equivalent JSON-LD with a different frame:
(
VcModule as jest.Mocked<typeof VcModule>
).getVerifiableCredential.mockImplementation(async (_, opts) =>
opts && "normalize" in opts && opts.normalize
? opts.normalize(vc)
: (vc as any),
).getVerifiableCredential.mockImplementation(
async (_, opts) =>
VcModule.verifiableCredentialToDataset<VcModule.VerifiableCredentialBase>(
opts && "normalize" in opts && opts.normalize
? opts.normalize(withoutDataset(vc) as unknown as VcModule.VerifiableCredential)
: withoutDataset(vc as unknown as VcModule.VerifiableCredential) as VcModule.VerifiableCredential,
{ includeVcProperties: opts?.returnLegacyJsonld !== false },
// This is a lie because it will not contain VC properties when opts?.returnLegacyJsonld is false
// but we need to do it to keep typescript happy with the current overloading we have
) as Promise<VcModule.VerifiableCredential>,
);

const accessRequest = await getAccessRequest("https://some.vc");
const accessRequestNoProperties = await getAccessRequest("https://some.vc", {
returnLegacyJsonld: false,
});
expect(accessRequest).toStrictEqual(accessRequestVc);
expect(
isomorphic(
[...accessRequest],
[
...accessRequestNoProperties,
],
),
).toBe(true);
const accessRequestNoProperties = await getAccessRequest(
"https://some.vc",
{
returnLegacyJsonld: false,
},
);
toBeEqual(accessRequest, accessRequestVc);
expect(isomorphic([...accessRequest], [...accessRequestNoProperties])).toBe(
true,
);

expect(accessRequest.credentialSubject.hasConsent.forPersonalData).toEqual([
"https://some.resource",
]);

expect(accessRequest.credentialSubject.providedConsent)
// @ts-expect-error the credential subject should not be available on the new RDFJS API
expect(accessRequestNoProperties.credentialSubject).toBeUndefined();
});
});
32 changes: 11 additions & 21 deletions src/gConsent/util/access.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import {
verifiableCredentialToDataset,
type VerifiableCredential,
} from "@inrupt/solid-client-vc";
import type { DatasetCore, Quad } from "@rdfjs/types";
import type { VerifiableCredentialBase } from "@inrupt/solid-client-vc/dist/common/common";
import { gc } from "../../common/constants";
import type { ResourceAccessMode } from "../../type/ResourceAccessMode";
import {
CREDENTIAL_TYPE_ACCESS_GRANT,
Expand All @@ -36,12 +35,6 @@ import { normalizeAccessGrant } from "../manage/approveAccessRequest";
import { normalizeAccessRequest } from "../request/issueAccessRequest";
import type { AccessGrant } from "../type/AccessGrant";
import type { AccessRequest } from "../type/AccessRequest";
import type {
AccessRequestBody,
BaseGrantBody,
BaseRequestBody,
} from "../type/AccessVerifiableCredential";
import { gc } from "../../common/constants";

type RequestVcOptions = Partial<{
resources: UrlString[];
Expand Down Expand Up @@ -110,12 +103,13 @@ export const mockAccessRequestVc = async (
const asObject = mockAccessRequestVcObject(options) as VerifiableCredential;
modify?.(asObject);

return (
await verifiableCredentialToDataset(normalizeAccessRequest(asObject), {
return (await verifiableCredentialToDataset(
normalizeAccessRequest(asObject),
{
baseIRI: asObject.id,
includeVcProperties: true,
})
) as unknown as AccessRequest;
},
)) as unknown as AccessRequest;
};

export const mockAccessGrantObject = (
Expand Down Expand Up @@ -166,19 +160,15 @@ export const mockAccessGrantVc = async (
const asObject = mockAccessGrantObject(options);
modify?.(asObject);

return (
await verifiableCredentialToDataset(normalizeAccessGrant(asObject), {
baseIRI: asObject.id,
includeVcProperties: true,
})
) as unknown as AccessGrant;
return (await verifiableCredentialToDataset(normalizeAccessGrant(asObject), {
baseIRI: asObject.id,
includeVcProperties: true,
})) as unknown as AccessGrant;
};

export const mockConsentRequestVc = async (
options?: RequestVcOptions,
): Promise<
AccessRequest
> => {
): Promise<AccessRequest> => {
const requestVc = await mockAccessRequestVc(options, (object) => {
object.credentialSubject.hasConsent.forPurpose = ["https://some.purpose"];
object.expirationDate = new Date(2021, 8, 14).toISOString();
Expand Down
15 changes: 13 additions & 2 deletions src/gConsent/util/toBeEqual.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
//
import type { JsonLd } from "@inrupt/solid-client-vc";
import { expect } from "@jest/globals";
import DatasetCore from "@rdfjs/dataset/DatasetCore";

/**
* Remove the properties of an RDF.DatasetCore from an object so that
Expand All @@ -29,8 +30,8 @@ import { expect } from "@jest/globals";
function viaJson(data: JsonLd) {
return JSON.parse(JSON.stringify(data));
}
function withoutDataset(data: JsonLd) {
return {
export function withoutDataset<T>(data: T): Omit<T, keyof DatasetCore> {
const res = {
...data,
match: undefined,
has: undefined,
Expand All @@ -40,6 +41,16 @@ function withoutDataset(data: JsonLd) {
toJSON: undefined,
[Symbol.iterator]: undefined,
};

delete res.match;
delete res.has;
delete res.add;
delete res.size;
delete res.delete;
delete res.toJSON;
delete res[Symbol.iterator];

return res;
}
export function toBeEqual(receieved: JsonLd, actual: JsonLd) {
expect(viaJson(receieved)).toEqual(viaJson(actual));
Expand Down

0 comments on commit de61976

Please sign in to comment.