Some coverage for getting access control

main
jaxoncreed 2 years ago
parent 4d62c244dc
commit 962b474693
  1. 24
      packages/solid/src/resource/Resource.ts
  2. 14
      packages/solid/src/resource/wac/getWacRule.ts
  3. 5
      packages/solid/src/resource/wac/results/WacRuleAbsent.ts
  4. 2
      packages/solid/src/util/rdfUtils.ts
  5. 29
      packages/solid/test/Integration.test.ts

@ -29,6 +29,7 @@ import type { WacRule } from "./wac/WacRule";
import type { GetWacUriError, GetWacUriResult } from "./wac/getWacUri"; import type { GetWacUriError, GetWacUriResult } from "./wac/getWacUri";
import { getWacUri } from "./wac/getWacUri"; import { getWacUri } from "./wac/getWacUri";
import { getWacRuleWithAclUri, type GetWacRuleResult } from "./wac/getWacRule"; import { getWacRuleWithAclUri, type GetWacRuleResult } from "./wac/getWacRule";
import { NoncompliantPodError } from "../requester/results/error/NoncompliantPodError";
/** /**
* Statuses shared between both Leaf and Container * Statuses shared between both Leaf and Container
@ -567,14 +568,29 @@ export abstract class Resource extends (EventEmitter as new () => TypedEmitter<{
// Get the wac uri // Get the wac uri
const wacUriResult = await this.getWacUri(options); const wacUriResult = await this.getWacUri(options);
if (wacUriResult.isError) { if (wacUriResult.isError) return wacUriResult;
return wacUriResult;
}
// Get the wac rule // Get the wac rule
return getWacRuleWithAclUri(wacUriResult.wacUri, { const wacResult = await getWacRuleWithAclUri(wacUriResult.wacUri, {
fetch: this.context.fetch, fetch: this.context.fetch,
}); });
if (wacResult.isError) return wacResult;
// If the wac rules was successfully found
if (wacResult.type === "getWacRuleSuccess") {
this.wacRule = wacResult.wacRule;
return wacResult;
}
// If the WacRule is absent
const parentUri = getParentUri(this.uri);
if (!parentUri) {
return new NoncompliantPodError(
this.uri,
`Resource "${this.uri}" has no Effective ACL resource`,
);
}
const parent = this.context.resourceStore.get(parentUri);
return parent.getWac();
} }
// async setWac(wacRule: WacRule): Promise<> { // async setWac(wacRule: WacRule): Promise<> {

@ -9,12 +9,16 @@ import { rawTurtleToDataset } from "../../util/rdfUtils";
import { AuthorizationShapeType } from "../../.ldo/wac.shapeTypes"; import { AuthorizationShapeType } from "../../.ldo/wac.shapeTypes";
import type { AccessModeList, WacRule } from "./WacRule"; import type { AccessModeList, WacRule } from "./WacRule";
import type { Authorization } from "../../.ldo/wac.typings"; import type { Authorization } from "../../.ldo/wac.typings";
import type { WacRuleAbsent } from "./results/WacRuleAbsent";
export type GetWacRuleError = export type GetWacRuleError =
| HttpErrorResultType | HttpErrorResultType
| NoncompliantPodError | NoncompliantPodError
| UnexpectedResourceError; | UnexpectedResourceError;
export type GetWacRuleResult = GetWacRuleSuccess | GetWacRuleError; export type GetWacRuleResult =
| GetWacRuleSuccess
| GetWacRuleError
| WacRuleAbsent;
export async function getWacRuleWithAclUri( export async function getWacRuleWithAclUri(
aclUri: string, aclUri: string,
@ -25,6 +29,14 @@ export async function getWacRuleWithAclUri(
const errorResult = HttpErrorResult.checkResponse(aclUri, response); const errorResult = HttpErrorResult.checkResponse(aclUri, response);
if (errorResult) return errorResult; if (errorResult) return errorResult;
if (response.status === 404) {
return {
type: "wacRuleAbsent",
uri: aclUri,
isError: false,
};
}
// Parse Turtle // Parse Turtle
const rawTurtle = await response.text(); const rawTurtle = await response.text();
const rawTurtleResult = await rawTurtleToDataset(rawTurtle, aclUri); const rawTurtleResult = await rawTurtleToDataset(rawTurtle, aclUri);

@ -0,0 +1,5 @@
import type { ResourceSuccess } from "../../../requester/results/success/SuccessResult";
export interface WacRuleAbsent extends ResourceSuccess {
type: "wacRuleAbsent";
}

@ -143,7 +143,7 @@ export async function rawTurtleToDataset(
const error = UnexpectedResourceError.fromThrown(baseUri, err); const error = UnexpectedResourceError.fromThrown(baseUri, err);
return new NoncompliantPodError( return new NoncompliantPodError(
baseUri, baseUri,
`Request returned noncompliant turtle: ${error.message}`, `Request returned noncompliant turtle: ${error.message}\n${rawTurtle}`,
); );
} }
} }

@ -1655,5 +1655,34 @@ describe("Integration", () => {
control: true, control: true,
}); });
}); });
it("Gets wac rules for a resource that does not have a corresponding acl", async () => {
const container = solidLdoDataset.getResource(SAMPLE_DATA_URI);
const wacResult = await container.getWac();
expect(wacResult.isError).toBe(false);
const wacSuccess = wacResult as GetWacRuleSuccess;
expect(wacSuccess.wacRule.public).toEqual({
read: false,
write: false,
append: false,
control: false,
});
expect(wacSuccess.wacRule.authenticated).toEqual({
read: false,
write: false,
append: false,
control: false,
});
expect(wacSuccess.wacRule.agent[WEB_ID]).toEqual({
read: true,
write: true,
append: true,
control: true,
});
});
it("returns an error when an error is encountered fetching the aclUri", async () => {
});
}); });
}); });

Loading…
Cancel
Save