Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit ac2d9ce

Browse files
committed
Bucket creation tests
1 parent 7267fab commit ac2d9ce

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

test/index.spec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ describe('S3Datastore', () => {
2222
() => new S3Store('.ipfs/datastore', { s3 })
2323
).to.throw()
2424
})
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+
})
2535
})
2636

2737
describe('put', () => {
@@ -37,6 +47,54 @@ describe('S3Datastore', () => {
3747

3848
store.put(new Key('/z/key'), Buffer.from('test data'), done)
3949
})
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+
})
4098
})
4199

42100
describe('get', () => {

0 commit comments

Comments
 (0)