@@ -10,6 +10,7 @@ import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError';
10
10
import { posix } from 'path' ;
11
11
import { Readable } from 'stream' ;
12
12
import { RepresentationMetadata } from '../../../src/ldp/representation/RepresentationMetadata' ;
13
+ import { RuntimeConfig } from '../../../src/init/RuntimeConfig' ;
13
14
import streamifyArray from 'streamify-array' ;
14
15
import { UnsupportedMediaTypeHttpError } from '../../../src/util/errors/UnsupportedMediaTypeHttpError' ;
15
16
import { CONTENT_TYPE_QUADS , DATA_TYPE_BINARY , DATA_TYPE_QUAD } from '../../../src/util/ContentTypes' ;
@@ -21,7 +22,7 @@ import { literal, namedNode, quad as quadRDF, triple } from '@rdfjs/data-model';
21
22
const { join : joinPath } = posix ;
22
23
23
24
const base = 'http://test.com/' ;
24
- const root = '/Users/default/home/public/' ;
25
+ const rootFilepath = '/Users/default/home/public/' ;
25
26
26
27
fsPromises . rmdir = jest . fn ( ) ;
27
28
fsPromises . lstat = jest . fn ( ) ;
@@ -48,7 +49,11 @@ describe('A FileResourceStore', (): void => {
48
49
beforeEach ( async ( ) : Promise < void > => {
49
50
jest . clearAllMocks ( ) ;
50
51
51
- store = new FileResourceStore ( base , root , new InteractionController ( ) , new MetadataController ( ) ) ;
52
+ store = new FileResourceStore (
53
+ new RuntimeConfig ( { base, rootFilepath } ) ,
54
+ new InteractionController ( ) ,
55
+ new MetadataController ( ) ,
56
+ ) ;
52
57
53
58
representation = {
54
59
data : streamifyArray ( [ rawData ] ) ,
@@ -131,7 +136,7 @@ describe('A FileResourceStore', (): void => {
131
136
// Write container (POST)
132
137
representation . metadata = { linkRel : { type : new Set ( [ LINK_TYPE_LDP_BC ] ) } , slug : 'myContainer/' , raw : [ ] } ;
133
138
const identifier = await store . addResource ( { path : base } , representation ) ;
134
- expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'myContainer/' ) , { recursive : true } ) ;
139
+ expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'myContainer/' ) , { recursive : true } ) ;
135
140
expect ( identifier . path ) . toBe ( `${ base } myContainer/` ) ;
136
141
137
142
// Read container
@@ -155,7 +160,7 @@ describe('A FileResourceStore', (): void => {
155
160
// Tests
156
161
representation . metadata = { linkRel : { type : new Set ( [ LINK_TYPE_LDP_BC ] ) } , slug : 'myContainer/' , raw : [ ] } ;
157
162
await expect ( store . addResource ( { path : `${ base } foo` } , representation ) ) . rejects . toThrow ( MethodNotAllowedHttpError ) ;
158
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'foo' ) ) ;
163
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'foo' ) ) ;
159
164
} ) ;
160
165
161
166
it ( 'errors 405 for POST invalid path ending without slash.' , async ( ) : Promise < void > => {
@@ -172,17 +177,17 @@ describe('A FileResourceStore', (): void => {
172
177
representation . metadata = { linkRel : { type : new Set ( [ LINK_TYPE_LDP_BC ] ) } , slug : 'myContainer/' , raw : [ ] } ;
173
178
await expect ( store . addResource ( { path : `${ base } doesnotexist` } , representation ) )
174
179
. rejects . toThrow ( MethodNotAllowedHttpError ) ;
175
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'doesnotexist' ) ) ;
180
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'doesnotexist' ) ) ;
176
181
177
182
representation . metadata = { linkRel : { type : new Set ( [ LINK_TYPE_LDPR ] ) } , slug : 'file.txt' , raw : [ ] } ;
178
183
await expect ( store . addResource ( { path : `${ base } doesnotexist` } , representation ) )
179
184
. rejects . toThrow ( MethodNotAllowedHttpError ) ;
180
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'doesnotexist' ) ) ;
185
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'doesnotexist' ) ) ;
181
186
182
187
representation . metadata = { linkRel : { type : new Set ( ) } , slug : 'file.txt' , raw : [ ] } ;
183
188
await expect ( store . addResource ( { path : `${ base } existingresource` } , representation ) )
184
189
. rejects . toThrow ( MethodNotAllowedHttpError ) ;
185
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'existingresource' ) ) ;
190
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'existingresource' ) ) ;
186
191
} ) ;
187
192
188
193
it ( 'can set data.' , async ( ) : Promise < void > => {
@@ -204,7 +209,7 @@ describe('A FileResourceStore', (): void => {
204
209
205
210
// Tests
206
211
await store . setRepresentation ( { path : `${ base } file.txt` } , representation ) ;
207
- expect ( fs . createWriteStream as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'file.txt' ) ) ;
212
+ expect ( fs . createWriteStream as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'file.txt' ) ) ;
208
213
const result = await store . getRepresentation ( { path : `${ base } file.txt` } ) ;
209
214
expect ( result ) . toEqual ( {
210
215
dataType : DATA_TYPE_BINARY ,
@@ -217,9 +222,9 @@ describe('A FileResourceStore', (): void => {
217
222
} ,
218
223
} ) ;
219
224
await expect ( arrayifyStream ( result . data ) ) . resolves . toEqual ( [ rawData ] ) ;
220
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'file.txt' ) ) ;
221
- expect ( fs . createReadStream as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'file.txt' ) ) ;
222
- expect ( fs . createReadStream as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'file.txt.metadata' ) ) ;
225
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'file.txt' ) ) ;
226
+ expect ( fs . createReadStream as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'file.txt' ) ) ;
227
+ expect ( fs . createReadStream as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'file.txt.metadata' ) ) ;
223
228
} ) ;
224
229
225
230
it ( 'can delete data.' , async ( ) : Promise < void > => {
@@ -239,7 +244,7 @@ describe('A FileResourceStore', (): void => {
239
244
240
245
// Tests
241
246
await store . deleteResource ( { path : `${ base } file.txt` } ) ;
242
- expect ( fsPromises . unlink as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'file.txt' ) ) ;
247
+ expect ( fsPromises . unlink as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'file.txt' ) ) ;
243
248
await expect ( store . getRepresentation ( { path : `${ base } file.txt` } ) ) . rejects . toThrow ( NotFoundHttpError ) ;
244
249
} ) ;
245
250
@@ -253,8 +258,9 @@ describe('A FileResourceStore', (): void => {
253
258
representation . metadata = { linkRel : { type : new Set ( [ LINK_TYPE_LDPR ] ) } , slug : 'file.txt' , raw : [ ] } ;
254
259
const identifier = await store . addResource ( { path : `${ base } doesnotexistyet/` } , representation ) ;
255
260
expect ( identifier . path ) . toBe ( `${ base } doesnotexistyet/file.txt` ) ;
256
- expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'doesnotexistyet/' ) , { recursive : true } ) ;
257
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'doesnotexistyet/' ) ) ;
261
+ expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'doesnotexistyet/' ) ,
262
+ { recursive : true } ) ;
263
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'doesnotexistyet/' ) ) ;
258
264
} ) ;
259
265
260
266
it ( 'creates metadata file when metadata triples are passed.' , async ( ) : Promise < void > => {
@@ -277,8 +283,8 @@ describe('A FileResourceStore', (): void => {
277
283
representation . metadata = { linkRel : { type : new Set ( [ LINK_TYPE_LDPR ] ) } , raw : [ quad ] } ;
278
284
representation . data = readableMock ;
279
285
await store . addResource ( { path : `${ base } foo/` } , representation ) ;
280
- expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'foo/' ) , { recursive : true } ) ;
281
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'foo/' ) ) ;
286
+ expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'foo/' ) , { recursive : true } ) ;
287
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'foo/' ) ) ;
282
288
283
289
representation . metadata = { linkRel : { type : new Set ( [ LINK_TYPE_LDPR ] ) } , raw : [ quad ] } ;
284
290
await store . setRepresentation ( { path : `${ base } foo/file.txt` } , representation ) ;
@@ -298,8 +304,8 @@ describe('A FileResourceStore', (): void => {
298
304
299
305
// Tests
300
306
await expect ( store . deleteResource ( { path : `${ base } notempty/` } ) ) . rejects . toThrow ( ConflictHttpError ) ;
301
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'notempty/' ) ) ;
302
- expect ( fsPromises . readdir as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'notempty/' ) ) ;
307
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'notempty/' ) ) ;
308
+ expect ( fsPromises . readdir as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'notempty/' ) ) ;
303
309
} ) ;
304
310
305
311
it ( 'deletes metadata file when deleting container.' , async ( ) : Promise < void > => {
@@ -312,10 +318,10 @@ describe('A FileResourceStore', (): void => {
312
318
313
319
// Tests
314
320
await store . deleteResource ( { path : `${ base } foo/` } ) ;
315
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'foo/' ) ) ;
316
- expect ( fsPromises . readdir as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'foo/' ) ) ;
317
- expect ( fsPromises . unlink as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'foo' , '.metadata' ) ) ;
318
- expect ( fsPromises . rmdir as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'foo/' ) ) ;
321
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'foo/' ) ) ;
322
+ expect ( fsPromises . readdir as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'foo/' ) ) ;
323
+ expect ( fsPromises . unlink as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'foo' , '.metadata' ) ) ;
324
+ expect ( fsPromises . rmdir as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'foo/' ) ) ;
319
325
} ) ;
320
326
321
327
it ( 'errors 404 when accessing non resource (file/directory), e.g. special files.' , async ( ) : Promise < void > => {
@@ -368,10 +374,10 @@ describe('A FileResourceStore', (): void => {
368
374
} ,
369
375
} ) ;
370
376
await expect ( arrayifyStream ( result . data ) ) . resolves . toEqualRdfQuadArray ( quads ) ;
371
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'foo/' ) ) ;
372
- expect ( fsPromises . readdir as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'foo/' ) ) ;
373
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'foo' , 'file.txt' ) ) ;
374
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'foo' , '.nonresource' ) ) ;
377
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'foo/' ) ) ;
378
+ expect ( fsPromises . readdir as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'foo/' ) ) ;
379
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'foo' , 'file.txt' ) ) ;
380
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'foo' , '.nonresource' ) ) ;
375
381
} ) ;
376
382
377
383
it ( 'can overwrite representation with PUT.' , async ( ) : Promise < void > => {
@@ -387,8 +393,8 @@ describe('A FileResourceStore', (): void => {
387
393
representation . metadata = { linkRel : { type : new Set ( [ LINK_TYPE_LDPR ] ) } , raw : [ ] } ;
388
394
await store . setRepresentation ( { path : `${ base } alreadyexists.txt` } , representation ) ;
389
395
expect ( fs . createWriteStream as jest . Mock ) . toBeCalledTimes ( 1 ) ;
390
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'alreadyexists.txt' ) ) ;
391
- expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledWith ( root , { recursive : true } ) ;
396
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'alreadyexists.txt' ) ) ;
397
+ expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledWith ( rootFilepath , { recursive : true } ) ;
392
398
} ) ;
393
399
394
400
it ( 'errors when overwriting container with PUT.' , async ( ) : Promise < void > => {
@@ -399,7 +405,7 @@ describe('A FileResourceStore', (): void => {
399
405
// Tests
400
406
await expect ( store . setRepresentation ( { path : `${ base } alreadyexists` } , representation ) ) . rejects
401
407
. toThrow ( ConflictHttpError ) ;
402
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'alreadyexists' ) ) ;
408
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'alreadyexists' ) ) ;
403
409
representation . metadata = { linkRel : { type : new Set ( [ LINK_TYPE_LDP_BC ] ) } , raw : [ ] } ;
404
410
await expect ( store . setRepresentation ( { path : `${ base } alreadyexists/` } , representation ) ) . rejects
405
411
. toThrow ( ConflictHttpError ) ;
@@ -445,9 +451,9 @@ describe('A FileResourceStore', (): void => {
445
451
// Tests
446
452
representation . metadata = { linkRel : { type : new Set ( [ LINK_TYPE_LDPR ] ) } , slug : 'file.txt' , raw : [ quad ] } ;
447
453
await expect ( store . addResource ( { path : base } , representation ) ) . rejects . toThrow ( Error ) ;
448
- expect ( fs . createWriteStream as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'file.txt.metadata' ) ) ;
449
- expect ( fs . createWriteStream as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'file.txt' ) ) ;
450
- expect ( fsPromises . unlink as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'file.txt.metadata' ) ) ;
454
+ expect ( fs . createWriteStream as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'file.txt.metadata' ) ) ;
455
+ expect ( fs . createWriteStream as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'file.txt' ) ) ;
456
+ expect ( fsPromises . unlink as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'file.txt.metadata' ) ) ;
451
457
} ) ;
452
458
453
459
it ( 'undoes container creation when metadata file creation fails.' , async ( ) : Promise < void > => {
@@ -461,7 +467,7 @@ describe('A FileResourceStore', (): void => {
461
467
// Tests
462
468
representation . metadata = { linkRel : { type : new Set ( [ LINK_TYPE_LDP_BC ] ) } , slug : 'foo/' , raw : [ quad ] } ;
463
469
await expect ( store . addResource ( { path : base } , representation ) ) . rejects . toThrow ( Error ) ;
464
- expect ( fsPromises . rmdir as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'foo/' ) ) ;
470
+ expect ( fsPromises . rmdir as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'foo/' ) ) ;
465
471
} ) ;
466
472
467
473
it ( 'creates container when POSTing without linkRel and with slug ending with slash.' , async ( ) : Promise < void > => {
@@ -474,7 +480,7 @@ describe('A FileResourceStore', (): void => {
474
480
const identifier = await store . addResource ( { path : base } , representation ) ;
475
481
expect ( identifier . path ) . toBe ( `${ base } myContainer/` ) ;
476
482
expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledTimes ( 1 ) ;
477
- expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'myContainer/' ) , { recursive : true } ) ;
483
+ expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'myContainer/' ) , { recursive : true } ) ;
478
484
} ) ;
479
485
480
486
it ( 'returns no contentType when unknown for representation.' , async ( ) : Promise < void > => {
@@ -514,9 +520,9 @@ describe('A FileResourceStore', (): void => {
514
520
// Tests
515
521
representation . metadata = { raw : [ ] } ;
516
522
await store . setRepresentation ( { path : `${ base } file.txt` } , representation ) ;
517
- expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledWith ( root , { recursive : true } ) ;
523
+ expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledWith ( rootFilepath , { recursive : true } ) ;
518
524
expect ( fs . createWriteStream as jest . Mock ) . toBeCalledTimes ( 1 ) ;
519
- expect ( fs . createWriteStream as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'file.txt' ) ) ;
525
+ expect ( fs . createWriteStream as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'file.txt' ) ) ;
520
526
} ) ;
521
527
522
528
it ( 'creates container when POST to existing container path ending without slash and slug without slash.' ,
@@ -530,7 +536,7 @@ describe('A FileResourceStore', (): void => {
530
536
representation . metadata = { linkRel : { type : new Set ( [ LINK_TYPE_LDP_BC ] ) } , slug : 'bar' , raw : [ ] } ;
531
537
const identifier = await store . addResource ( { path : `${ base } foo` } , representation ) ;
532
538
expect ( identifier . path ) . toBe ( `${ base } foo/bar/` ) ;
533
- expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'foo' ) ) ;
534
- expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledWith ( joinPath ( root , 'foo' , 'bar/' ) , { recursive : false } ) ;
539
+ expect ( fsPromises . lstat as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'foo' ) ) ;
540
+ expect ( fsPromises . mkdir as jest . Mock ) . toBeCalledWith ( joinPath ( rootFilepath , 'foo' , 'bar/' ) , { recursive : false } ) ;
535
541
} ) ;
536
542
} ) ;
0 commit comments