This repository was archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsolid-profile-test.js
More file actions
404 lines (389 loc) · 14.6 KB
/
Copy pathsolid-profile-test.js
File metadata and controls
404 lines (389 loc) · 14.6 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
'use strict'
var solid = SolidClient
var vocab = solid.vocab
var webClient = solid.web
var serverUrl = 'https://localhost:8443/'
var profileUrl = serverUrl + 'profile/test-profile-card'
var rawProfileSource = require('test-minimal-profile')
var prefsUrl = serverUrl + 'settings/test-test.ttl'
var rawPrefsSource = require('test-minimal-prefs')
var defaultPrivateTypeRegistryUrl = serverUrl + 'profile/privateTypeIndex.ttl'
var defaultPublicTypeRegistryUrl = serverUrl + 'profile/publicTypeIndex.ttl'
var defaultPrivateAppRegistryUrl = serverUrl + 'profile/privateAppRegistry.ttl'
var defaultPublicAppRegistryUrl = serverUrl + 'profile/publicAppRegistry.ttl'
var AppRegistration = solid.AppRegistration
function clearRegistries () {
return clearResource(defaultPublicTypeRegistryUrl)
.then(function () {
return clearResource(defaultPrivateTypeRegistryUrl)
})
.then(function () {
return clearResource(defaultPrivateAppRegistryUrl)
})
.then(function () {
return clearResource(defaultPrivateAppRegistryUrl)
})
.then(function () {
return clearResource(defaultPublicAppRegistryUrl)
})
}
function clearResource (url) {
return solid.web.del(url)
.catch(function () {
// do nothing (likely tried to delete a non-existent resource)
})
}
function ensureProfile (profileUrl) {
var createProfile = false
return solid.web.head(profileUrl)
.catch(function (error) {
if (error.code === 404) {
console.log('Profile not found.')
createProfile = true
} else {
console.log('Error on HEAD profile url:', error)
}
return createProfile
})
.then(function () {
if (createProfile) {
console.log('Creating test profile...')
return solid.web.put(profileUrl, rawProfileSource, 'text/turtle')
} else {
console.log('Profile detected, no problem.')
}
})
.catch(function (error) {
console.log('Error creating test profile:', error)
})
.then(function () {
if (createProfile) {
console.log('Profile created.')
}
})
}
function resetProfile (url) {
console.log('Resetting profile...')
// First, delete the current profile
return clearResource(url)
.then(function () {
// Re-add the profile from template
return ensureProfile(url)
})
.then(function () {
// Delete the private profile (prefs.ttl)
return clearResource(prefsUrl)
})
.then(function () {
// Re-add the private profile
return solid.web.put(prefsUrl, rawPrefsSource, 'text/turtle')
})
.then(function () {
return solid.getProfile(profileUrl)
})
}
QUnit.module('SolidProfile tests', {
/**
* Runs after the last test in this module
*/
after: function (details) {
return resetProfile(profileUrl)
.then(function () {
console.log('Profile reset.')
return clearRegistries()
})
}
})
QUnit.test('getProfile() test', function (assert) {
assert.expect(3)
return ensureProfile(profileUrl)
.then(function () {
return solid.getProfile(profileUrl)
})
.then(function (profile) {
assert.ok(profile.isLoaded)
assert.deepEqual(profile.response.types,
['http://www.w3.org/ns/ldp#Resource'])
assert.deepEqual(profile.storage, [serverUrl])
})
})
QUnit.test('initTypeRegistryPublic() test', function (assert) {
assert.expect(6)
var profile
return clearResource(defaultPublicTypeRegistryUrl)
.then(function () {
// Make sure registry does not exist
// return resetProfile(profileUrl)
return solid.getProfile(profileUrl)
})
.then(function (profileResult) {
profile = profileResult
return solid.typeRegistry.initTypeRegistryPublic(profile, webClient)
})
.then(function () {
// Check to make sure the type index is loaded after init
assert.ok(profile.hasTypeRegistryPublic())
assert.equal(profile.typeIndexListed.uri, defaultPublicTypeRegistryUrl,
'public type index uri should be loaded after registry init')
assert.ok(profile.typeIndexListed.graph,
'public type index graph should be loaded after init')
// reload the profile
return solid.getProfile(profileUrl)
})
.then(function (profileResult) {
profile = profileResult
assert.ok(profile.hasTypeRegistryPublic(),
'hasTypeRegistryPublic() should be true after initTypeRegistryPublic() + profile reload')
assert.equal(profile.typeIndexListed.uri, defaultPublicTypeRegistryUrl,
'public type index uri should be loaded after registry init + profile reload')
// The profile has been reloaded, but the type registry wasn't loaded.
// Load it now.
return profile.loadTypeRegistry(webClient)
})
.then(function (profileResult) {
profile = profileResult
assert.ok(profile.typeIndexListed.graph,
'public type index graph should be loaded after profile reload + loadTypeRegistry()')
// Test to make sure that the freshly initialized registry is empty
})
.then(function () {
return clearResource(defaultPublicTypeRegistryUrl)
})
})
QUnit.test('initTypeRegistryPrivate() test', function (assert) {
assert.expect(6)
var profile
return clearResource(defaultPrivateTypeRegistryUrl)
.then(function () {
// Make sure registry does not exist
// return resetProfile(profileUrl)
return solid.getProfile(profileUrl)
})
.then(function (profileResult) {
profile = profileResult
return solid.typeRegistry.initTypeRegistryPrivate(profile, webClient)
})
.then(function () {
// Check to make sure the type index is loaded after init
assert.ok(profile.hasTypeRegistryPrivate())
assert.equal(profile.typeIndexUnlisted.uri, defaultPrivateTypeRegistryUrl,
'private type index uri should be loaded after registry init')
assert.ok(profile.typeIndexUnlisted.graph,
'private type index graph should be loaded after init')
// reload the profile
return solid.getProfile(profileUrl)
})
.then(function (profileResult) {
profile = profileResult
assert.ok(profile.hasTypeRegistryPrivate(),
'hasTypeRegistryPrivate() should be true after initTypeRegistryPrivate() + profile reload')
assert.equal(profile.typeIndexUnlisted.uri, defaultPrivateTypeRegistryUrl,
'private type index uri should be loaded after registry init + profile reload')
// The profile has been reloaded, but the type registry wasn't loaded.
// Load it now.
return profile.loadTypeRegistry(webClient)
})
.then(function (profileResult) {
profile = profileResult
assert.ok(profile.typeIndexUnlisted.graph,
'private type index graph should be loaded after profile reload + loadTypeRegistry()')
// Test to make sure that the freshly initialized registry is empty
})
.then(function () {
return clearResource(defaultPrivateTypeRegistryUrl)
})
})
QUnit.test('registerType()/unregisterType() test', function (assert) {
assert.expect(5)
var classToRegister = vocab.sioc('Post')
var locationToRegister = 'https://localhost:8443/posts-container/'
var isListed = true
var profile
console.log('** registerType() -- reset profile and registries')
return clearRegistries()
.then(function () {
return resetProfile(profileUrl)
})
.then(function (profileResult) {
profile = profileResult
return profile.registerType(classToRegister, locationToRegister,
'container', isListed)
})
.then(function (profileResult) {
profile = profileResult
// Now the type is registered, and the profile's type registry is refreshed
// querying the registry now will include the new container
var registrations = profile.typeRegistryForClass(vocab.sioc('Post'))
assert.equal(registrations.length, 1)
var newRegistration = registrations[0]
assert.equal(newRegistration.locationType, 'container')
assert.equal(newRegistration.locationUri, locationToRegister)
assert.equal(newRegistration.rdfClass.uri, vocab.sioc('Post').uri)
return profile
})
.catch(function (err) {
console.log('Error while registerType()', err)
})
.then(function () {
console.log('** unregisterType() test - reloading profile')
return solid.getProfile(profileUrl)
})
.then(function (profileResult) {
profile = profileResult
var classToRemove = vocab.sioc('Post')
var isListed = true
console.log('loadedProfile: ', profileResult)
console.log('Calling unregisterType()')
// At this point, the profile has been loaded, but the type registries
// have not been. They will be loaded during unregisterType()
return profile.unregisterType(classToRemove, isListed)
})
.catch(function (err) {
console.log('Error while unregisterType:', err)
})
.then(function (profileResult) {
profile = profileResult
// Check to make sure the registration was removed
var registrations = profile.typeRegistryForClass(vocab.sioc('Post'))
assert.equal(registrations.length, 0)
})
.then(function () {
console.log('clearing registries...')
return clearRegistries()
})
})
QUnit.test('initAppRegistryPublic() test', function (assert) {
assert.expect(7)
var profile
console.log('** initAppRegistryPublic() test')
return clearResource(defaultPublicAppRegistryUrl)
.then(function () {
// Make sure registry does not exist
// return resetProfile(profileUrl)
return solid.getProfile(profileUrl)
})
.then(function (profileResult) {
profile = profileResult
return solid.appRegistry.initAppRegistryPublic(profile, webClient)
})
.then(function () {
// Check to make sure the app registry is loaded after init
assert.ok(profile.hasAppRegistryPublic())
assert.equal(profile.appRegistryListed.uri, defaultPublicAppRegistryUrl,
'public app registry uri should be loaded after registry init')
assert.ok(profile.appRegistryListed.graph,
'public app registry graph should be loaded after init')
// reload the profile
return solid.getProfile(profileUrl)
})
.then(function (profileResult) {
profile = profileResult
assert.ok(profile.hasAppRegistryPublic(),
'hasTypeRegistryPublic() should be true after initAppRegistryPublic() + profile reload')
assert.equal(profile.appRegistryListed.uri, defaultPublicAppRegistryUrl,
'public app registry uri should be loaded after registry init + profile reload')
assert.notOk(profile.appRegistryListed.graph)
// The profile has been reloaded, but the app registry wasn't loaded.
// Load it now.
return profile.loadAppRegistry(webClient)
})
.then(function (profileResult) {
profile = profileResult
assert.ok(profile.appRegistryListed.graph,
'public app registry graph should be loaded after profile reload + loadAppRegistry()')
// Test to make sure that the freshly initialized registry is empty
})
.then(function () {
// return clearResource(defaultPublicAppRegistryUrl)
})
})
QUnit.test('initAppRegistryPrivate() test', function (assert) {
assert.expect(6)
var profile
return clearResource(defaultPrivateAppRegistryUrl)
.then(function () {
// Make sure registry does not exist
// return resetProfile(profileUrl)
return solid.getProfile(profileUrl)
})
.then(function (profileResult) {
profile = profileResult
return solid.appRegistry.initAppRegistryPrivate(profile, webClient)
})
.then(function () {
// Check to make sure the app registry is loaded after init
assert.ok(profile.hasAppRegistryPrivate())
assert.equal(profile.appRegistryUnlisted.uri, defaultPrivateAppRegistryUrl,
'private app registry uri should be loaded after registry init')
assert.ok(profile.appRegistryUnlisted.graph,
'private app registry graph should be loaded after init')
// reload the profile
return solid.getProfile(profileUrl)
})
.then(function (profileResult) {
profile = profileResult
assert.ok(profile.hasAppRegistryPrivate(),
'hasAppRegistryPrivate() should be true after initAppRegistryPrivate() + profile reload')
assert.equal(profile.appRegistryUnlisted.uri, defaultPrivateAppRegistryUrl,
'private app registry uri should be loaded after registry init + profile reload')
// The profile has been reloaded, but the app registry wasn't loaded.
// Load it now.
return profile.loadAppRegistry(webClient)
})
.then(function (profileResult) {
profile = profileResult
assert.ok(profile.appRegistryUnlisted.graph,
'private app registry graph should be loaded after profile reload + loadAppRegistry()')
// Test to make sure that the freshly initialized registry is empty
})
.then(function () {
// return clearResource(defaultPrivateAppRegistryUrl)
})
})
QUnit.test('profile.appsForType() test', function (assert) {
var REDIRECT_URI = 'https://solid.github.io/contacts/?uri={uri}'
assert.expect(9)
var profile
var typesForApp = [
vocab.vcard('AddressBook')
]
return ensureProfile(profileUrl)
.then(function () {
return solid.getProfile(profileUrl)
})
.then(function (loadedProfile) {
profile = loadedProfile
// The registries have been initialized by the preceding tests
assert.ok(profile.hasAppRegistryPrivate())
assert.ok(profile.hasAppRegistryPublic())
// Check to make sure no registry entry exists
var registeredApps = profile.appsForType(vocab.vcard('AddressBook'))
assert.deepEqual(registeredApps, [],
'An empty app registry should have no registrations for AddressBook')
var options = {
name: 'Contact Manager',
shortdesc: 'desc',
redirectTemplateUri: REDIRECT_URI
}
var isListed = true
var app = new AppRegistration(options, typesForApp, isListed)
return profile.registerApp(app, webClient)
})
.then(function (updatedProfile) {
profile = updatedProfile
return profile.appsForType(vocab.vcard('AddressBook'))
})
.then(function (registrationResults) {
assert.equal(registrationResults.length, 1,
'Only one app should have been registered')
var app = registrationResults[0]
assert.equal(app.name, 'Contact Manager')
assert.equal(app.shortdesc, 'desc')
assert.equal(app.redirectTemplateUri, REDIRECT_URI)
assert.equal(app.types.length, 1)
assert.ok(app.isListed)
})
.then(function () {
return clearRegistries()
})
})