forked from SolidOS/solid-logic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilityLogic.test.ts
More file actions
179 lines (167 loc) · 7.04 KB
/
Copy pathutilityLogic.test.ts
File metadata and controls
179 lines (167 loc) · 7.04 KB
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
/**
* @jest-environment jsdom
*
*/
import fetchMock from "jest-fetch-mock";
import { UpdateManager, sym, Fetcher, Store } from "rdflib";
import { createAclLogic } from "../src/acl/aclLogic";
import { WebOperationError } from "../src/logic/CustomError";
import { createContainerLogic } from "../src/util/containerLogic";
import { ns } from "../src/util/ns";
import { createUtilityLogic } from "../src/util/utilityLogic";
import { alice, AlicePhotoFolder, AlicePhotos, AlicePreferences, AlicePreferencesFile, AlicePrivateTypeIndex, AlicePrivateTypes, AliceProfile, AlicePublicTypeIndex, AlicePublicTypes, bob, BobProfile, club, ClubPreferences, ClubPreferencesFile, ClubPrivateTypeIndex, ClubPrivateTypes, ClubProfile, ClubPublicTypeIndex, ClubPublicTypes } from "./helpers/dataSetup";
window.$SolidTestEnvironment = { username: alice.uri }
const prefixes = Object.keys(ns).map(prefix => `@prefix ${prefix}: ${ns[prefix]('')}.\n`).join('') // In turtle
describe("utilityLogic", () => {
let store;
let options;
let web = {}
let requests = []
let statustoBeReturned = 200
let utilityLogic
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.mockResponse("Not Found", {
status: 404,
});
requests = []
statustoBeReturned = 200
const init = { headers: { "Content-Type": "text/turtle" } } // Fetch options tend to be called this
fetchMock.mockIf(/^https?.*$/, async req => {
if (req.method !== 'GET') {
requests.push(req)
if (req.method === 'PUT') {
const contents = await req.text()
web[req.url] = contents // Update our dummy web
console.log(`Tetst: Updated ${req.url} on PUT to <<<${web[req.url]}>>>`)
}
return { status: statustoBeReturned }
}
const contents = web[req.url]
if (contents !== undefined) { //
return {
body: prefixes + contents, // Add namespaces to anything
status: 200,
headers: {
"Content-Type": "text/turtle",
"WAC-Allow": 'user="write", public="read"',
"Accept-Patch": "application/sparql-update"
}
}
} // if contents
return {
status: 404,
body: 'Not Found'
}
})
web = {}
web[alice.doc().uri] = AliceProfile
web[AlicePreferencesFile.uri] = AlicePreferences
web[AlicePrivateTypeIndex.uri] = AlicePrivateTypes
web[AlicePublicTypeIndex.uri] = AlicePublicTypes
web[AlicePhotoFolder.uri] = AlicePhotos
web[bob.doc().uri] = BobProfile
web[club.doc().uri] = ClubProfile
web[ClubPreferencesFile.uri] = ClubPreferences
web[ClubPrivateTypeIndex.uri] = ClubPrivateTypes
web[ClubPublicTypeIndex.uri] = ClubPublicTypes
options = { fetch: fetch };
store = new Store()
store.fetcher = new Fetcher(store, options);
store.updater = new UpdateManager(store);
requests = []
utilityLogic = createUtilityLogic(store, createAclLogic(store), createContainerLogic(store))
});
describe('loadOrCreateIfNotExists', () => {
it('exists', () => {
expect(utilityLogic.loadOrCreateIfNotExists).toBeInstanceOf(Function)
})
it('does nothing if existing file', async () => {
const result = await utilityLogic.loadOrCreateIfNotExists(alice.doc())
expect(requests).toEqual([])
})
it('creates empty file if did not exist', async () => {
const suggestion = 'https://bob.example.com/settings/prefsSuggestion.ttl'
const result = await utilityLogic.loadOrCreateIfNotExists(sym(suggestion))
expect(requests[0].method).toEqual('PUT')
expect(requests[0].url).toEqual(suggestion)
})
})
describe('followOrCreateLink', () => {
it('exists', () => {
expect(utilityLogic.followOrCreateLink).toBeInstanceOf(Function)
})
it('follows existing link', async () => {
const suggestion = 'https://alice.example.com/settings/prefsSuggestion.ttl'
const result = await utilityLogic.followOrCreateLink(alice, ns.space('preferencesFile'), sym(suggestion), alice.doc())
expect(result).toEqual(AlicePreferencesFile)
})
it('creates empty file if did not exist and new link', async () => {
const suggestion = 'https://bob.example.com/settings/prefsSuggestion.ttl'
const result = await utilityLogic.followOrCreateLink(bob, ns.space('preferencesFile'), sym(suggestion), bob.doc())
expect(result).toEqual(sym(suggestion))
expect(requests[0].method).toEqual('PATCH')
expect(requests[0].url).toEqual(bob.doc().uri)
expect(requests[1].method).toEqual('PUT')
expect(requests[1].url).toEqual(suggestion)
expect(store.holds(bob, ns.space('preferencesFile'), sym(suggestion), bob.doc())).toEqual(true)
})
//
it('returns null if it cannot create the new file', async () => {
const suggestion = 'https://bob.example.com/settings/prefsSuggestion.ttl'
statustoBeReturned = 403 // Unauthorized
expect(async () => {
await utilityLogic.followOrCreateLink(bob, ns.space('preferencesFile'), sym(suggestion), bob.doc())
}).rejects.toThrow(WebOperationError)
})
})
describe("setSinglePeerAccess", () => {
beforeEach(() => {
fetchMock.mockOnceIf(
"https://owner.com/some/resource",
"hello", {
headers: {
Link: '<https://owner.com/some/acl>; rel="acl"'
}
});
fetchMock.mockOnceIf(
"https://owner.com/some/acl",
"Created", {
status: 201
});
});
it("Creates the right ACL doc", async () => {
await utilityLogic.setSinglePeerAccess({
ownerWebId: "https://owner.com/#me",
peerWebId: "https://peer.com/#me",
accessToModes: "acl:Read, acl:Control",
defaultModes: "acl:Write",
target: "https://owner.com/some/resource"
});
expect(fetchMock.mock.calls).toEqual([
[ "https://owner.com/some/resource", fetchMock.mock.calls[0][1] ],
[ "https://owner.com/some/acl", {
body: '@prefix acl: <http://www.w3.org/ns/auth/acl#>.\n' +
'\n' +
'<#alice> a acl:Authorization;\n' +
' acl:agent <https://owner.com/#me>;\n' +
' acl:accessTo <https://owner.com/some/resource>;\n' +
' acl:default <https://owner.com/some/resource>;\n' +
' acl:mode acl:Read, acl:Write, acl:Control.\n' +
'<#bobAccessTo> a acl:Authorization;\n' +
' acl:agent <https://peer.com/#me>;\n' +
' acl:accessTo <https://owner.com/some/resource>;\n' +
' acl:mode acl:Read, acl:Control.\n' +
'<#bobDefault> a acl:Authorization;\n' +
' acl:agent <https://peer.com/#me>;\n' +
' acl:default <https://owner.com/some/resource>;\n' +
' acl:mode acl:Write.\n',
headers: [
["Content-Type", "text/turtle"]
],
method: "PUT"
}]
]);
});
});
})