/
policy.test.ts
276 lines (238 loc) · 9.27 KB
/
policy.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* Copyright 2020 Inrupt Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { jest, describe, it, expect } from "@jest/globals";
jest.mock("../fetcher.ts", () => ({
fetch: jest.fn().mockImplementation(() =>
Promise.resolve(
new Response(undefined, {
headers: { Location: "https://arbitrary.pod/resource" },
})
)
),
}));
import { Response } from "cross-fetch";
import { rdf, acp } from "../constants";
import { createSolidDataset } from "../resource/solidDataset";
import { getUrl, getUrlAll } from "../thing/get";
import { removeUrl } from "../thing/remove";
import { setUrl } from "../thing/set";
import {
asUrl,
createThing,
getThing,
getThingAll,
setThing,
} from "../thing/thing";
import {
createPolicy,
getPolicy,
getPolicyAll,
removePolicy,
savePolicyDatasetAt,
setPolicy,
} from "./policy";
const policyUrl = "https://some.pod/policy-resource";
describe("savePolicyDatasetAt", () => {
it("sets the type of acp:AccessPolicy if not set yet", async () => {
// The type assertion to any is necessary due to invalid Jest type definitions:
// https://github.com/facebook/jest/pull/10600
const mockFetch = jest
.fn(window.fetch)
.mockResolvedValue(new Response() as any);
const newDataset = createSolidDataset();
const savedDataset = await savePolicyDatasetAt(policyUrl, newDataset, {
fetch: mockFetch,
});
const savedDatasetThing = getThing(savedDataset, policyUrl);
expect(savedDatasetThing).not.toBeNull();
expect(getUrl(savedDatasetThing!, rdf.type)).toBe(acp.AccessPolicyResource);
});
it("overwrites an existing type that might be set", async () => {
// The type assertion to any is necessary due to invalid Jest type definitions:
// https://github.com/facebook/jest/pull/10600
const mockFetch = jest
.fn(window.fetch)
.mockResolvedValue(new Response() as any);
let newDatasetThing = createThing({ url: policyUrl });
newDatasetThing = setUrl(
newDatasetThing,
rdf.type,
"https://arbitrary.vocab/ArbitraryClass"
);
const newDataset = setThing(createSolidDataset(), newDatasetThing);
const savedDataset = await savePolicyDatasetAt(policyUrl, newDataset, {
fetch: mockFetch,
});
const savedDatasetThing = getThing(savedDataset, policyUrl);
expect(savedDatasetThing).not.toBeNull();
expect(getUrlAll(savedDatasetThing!, rdf.type)).toEqual([
acp.AccessPolicyResource,
]);
});
it("calls the included fetcher by default", async () => {
const mockedFetcher = jest.requireMock("../fetcher.ts") as {
fetch: jest.Mock<
ReturnType<typeof window.fetch>,
[RequestInfo, RequestInit?]
>;
};
await savePolicyDatasetAt(policyUrl, createSolidDataset());
expect(mockedFetcher.fetch.mock.calls[0][0]).toBe(policyUrl);
});
it("uses the given fetcher if provided", async () => {
// The type assertion to any is necessary due to invalid Jest type definitions:
// https://github.com/facebook/jest/pull/10600
const mockFetch = jest
.fn(window.fetch)
.mockResolvedValue(new Response() as any);
await savePolicyDatasetAt(policyUrl, createSolidDataset(), {
fetch: mockFetch,
});
expect(mockFetch.mock.calls[0][0]).toBe(policyUrl);
});
});
describe("createPolicy", () => {
it("creates a Thing of type acp:AccessPolicy", () => {
const newPolicy = createPolicy("https://some.pod/policy-resource#policy");
expect(getUrl(newPolicy, rdf.type)).toBe(acp.AccessPolicy);
expect(asUrl(newPolicy)).toBe("https://some.pod/policy-resource#policy");
});
});
describe("getPolicy", () => {
it("returns the Policy with the given URL", () => {
let mockPolicy = createThing({
url: "https://some.pod/policy-resource#policy",
});
mockPolicy = setUrl(mockPolicy, rdf.type, acp.AccessPolicy);
const policyDataset = setThing(createSolidDataset(), mockPolicy);
expect(
getPolicy(policyDataset, "https://some.pod/policy-resource#policy")
).not.toBeNull();
});
it("returns null if the given URL identifies something that is not an Access Policy", () => {
let notAPolicy = createThing({
url: "https://some.pod/policy-resource#not-a-policy",
});
notAPolicy = setUrl(
notAPolicy,
rdf.type,
"https://arbitrary.vocab/not-a-policy"
);
const policyDataset = setThing(createSolidDataset(), notAPolicy);
expect(
getPolicy(policyDataset, "https://some.pod/policy-resource#not-a-policy")
).toBeNull();
});
it("returns null if there is no Thing at the given URL", () => {
expect(
getPolicy(createSolidDataset(), "https://some.pod/policy-resource#policy")
).toBeNull();
});
});
describe("getPolicyAll", () => {
it("returns included Policies", () => {
let mockPolicy = createThing({
url: "https://some.pod/policy-resource#policy",
});
mockPolicy = setUrl(mockPolicy, rdf.type, acp.AccessPolicy);
const policyDataset = setThing(createSolidDataset(), mockPolicy);
expect(getPolicyAll(policyDataset)).toHaveLength(1);
});
it("returns only those Things whose type is of acp:AccessPolicy", () => {
let mockPolicy = createThing({
url: "https://some.pod/policy-resource#policy",
});
mockPolicy = setUrl(mockPolicy, rdf.type, acp.AccessPolicy);
let notAPolicy = createThing({
url: "https://some.pod/policy-resource#not-a-policy",
});
notAPolicy = setUrl(
notAPolicy,
rdf.type,
"https://arbitrary.vocab/not-a-policy"
);
let policyDataset = setThing(createSolidDataset(), mockPolicy);
policyDataset = setThing(policyDataset, notAPolicy);
expect(getPolicyAll(policyDataset)).toHaveLength(1);
});
it("returns an empty array if there are no Thing in the given PolicyDataset", () => {
expect(getPolicyAll(createSolidDataset())).toHaveLength(0);
});
});
describe("setPolicy", () => {
it("replaces existing instances of the set Access Policy", () => {
const somePredicate = "https://some.vocab/predicate";
let mockPolicy = createThing({
url: "https://some.pod/policy-resource#policy",
});
mockPolicy = setUrl(mockPolicy, rdf.type, acp.AccessPolicy);
mockPolicy = setUrl(mockPolicy, somePredicate, "https://example.test");
const policyDataset = setThing(createSolidDataset(), mockPolicy);
const updatedPolicy = removeUrl(
mockPolicy,
somePredicate,
"https://example.test"
);
const updatedPolicyDataset = setPolicy(policyDataset, updatedPolicy);
const policyAfterUpdate = getPolicy(
updatedPolicyDataset,
"https://some.pod/policy-resource#policy"
);
expect(getUrl(policyAfterUpdate!, somePredicate)).toBeNull();
});
});
describe("removePolicy", () => {
it("removes the given Access Policy from the Access Policy Resource", () => {
let mockPolicy = createThing({
url: "https://some.pod/policy-resource#policy",
});
mockPolicy = setUrl(mockPolicy, rdf.type, acp.AccessPolicy);
const policyDataset = setThing(createSolidDataset(), mockPolicy);
const updatedPolicyDataset = removePolicy(policyDataset, mockPolicy);
expect(getThingAll(updatedPolicyDataset)).toHaveLength(0);
});
it("accepts a plain URL to remove an Access Policy", () => {
let mockPolicy = createThing({
url: "https://some.pod/policy-resource#policy",
});
mockPolicy = setUrl(mockPolicy, rdf.type, acp.AccessPolicy);
const policyDataset = setThing(createSolidDataset(), mockPolicy);
const updatedPolicyDataset = removePolicy(
policyDataset,
"https://some.pod/policy-resource#policy"
);
expect(getThingAll(updatedPolicyDataset)).toHaveLength(0);
});
it("does not remove unrelated policies", () => {
let mockPolicy1 = createThing({
url: "https://some.pod/policy-resource#policy1",
});
mockPolicy1 = setUrl(mockPolicy1, rdf.type, acp.AccessPolicy);
let mockPolicy2 = createThing({
url: "https://some.pod/policy-resource#policy2",
});
mockPolicy2 = setUrl(mockPolicy2, rdf.type, acp.AccessPolicy);
let policyDataset = setThing(createSolidDataset(), mockPolicy1);
policyDataset = setThing(policyDataset, mockPolicy2);
const updatedPolicyDataset = removePolicy(policyDataset, mockPolicy1);
expect(getThingAll(updatedPolicyDataset)).toHaveLength(1);
});
});