@@ -22,6 +22,16 @@ describe('S3Datastore', () => {
22
22
( ) => new S3Store ( '.ipfs/datastore' , { s3 } )
23
23
) . to . throw ( )
24
24
} )
25
+ it ( 'createIfMissing defaults to false' , ( ) => {
26
+ const s3 = new S3 ( { params : { Bucket : 'test' } } )
27
+ const store = new S3Store ( '.ipfs' , { s3 } )
28
+ expect ( store . createIfMissing ) . to . equal ( false )
29
+ } )
30
+ it ( 'createIfMissing can be set to true' , ( ) => {
31
+ const s3 = new S3 ( { params : { Bucket : 'test' } } )
32
+ const store = new S3Store ( '.ipfs' , { s3, createIfMissing : true } )
33
+ expect ( store . createIfMissing ) . to . equal ( true )
34
+ } )
25
35
} )
26
36
27
37
describe ( 'put' , ( ) => {
@@ -37,6 +47,54 @@ describe('S3Datastore', () => {
37
47
38
48
store . put ( new Key ( '/z/key' ) , Buffer . from ( 'test data' ) , done )
39
49
} )
50
+ it ( 'should create the bucket when missing if createIfMissing is true' , ( done ) => {
51
+ const s3 = new S3 ( { params : { Bucket : 'my-ipfs-bucket' } } )
52
+ const store = new S3Store ( '.ipfs/datastore' , { s3, createIfMissing : true } )
53
+
54
+ // 1. On the first call upload will fail with a NoSuchBucket error
55
+ // 2. This should result in the `createBucket` standin being called
56
+ // 3. upload is then called a second time and it passes
57
+
58
+ let bucketCreated = false
59
+ standin . replace ( s3 , 'upload' , ( stand , params , callback ) => {
60
+ if ( ! bucketCreated ) return callback ( { code : 'NoSuchBucket' } )
61
+ stand . restore ( )
62
+ return callback ( null )
63
+ } )
64
+
65
+ standin . replace ( s3 , 'createBucket' , ( stand , params , callback ) => {
66
+ bucketCreated = true
67
+ stand . restore ( )
68
+ return callback ( )
69
+ } )
70
+
71
+ store . put ( new Key ( '/z/key' ) , Buffer . from ( 'test data' ) , done )
72
+ } )
73
+ it ( 'should create the bucket when missing if createIfMissing is true' , ( done ) => {
74
+ const s3 = new S3 ( { params : { Bucket : 'my-ipfs-bucket' } } )
75
+ const store = new S3Store ( '.ipfs/datastore' , { s3, createIfMissing : false } )
76
+
77
+ let bucketCreated = false
78
+ standin . replace ( s3 , 'upload' , ( stand , params , callback ) => {
79
+ if ( ! bucketCreated ) return callback ( { code : 'NoSuchBucket' } )
80
+ stand . restore ( )
81
+ return callback ( null )
82
+ } )
83
+
84
+ standin . replace ( s3 , 'createBucket' , ( stand , params , callback ) => {
85
+ bucketCreated = true
86
+ stand . restore ( )
87
+ return callback ( )
88
+ } )
89
+
90
+ store . put ( new Key ( '/z/key' ) , Buffer . from ( 'test data' ) , ( err ) => {
91
+ expect ( bucketCreated ) . to . equal ( false )
92
+ expect ( err ) . to . deep . equal ( {
93
+ code : 'NoSuchBucket'
94
+ } )
95
+ done ( )
96
+ } )
97
+ } )
40
98
} )
41
99
42
100
describe ( 'get' , ( ) => {
0 commit comments