From fa042d3c261a4ed1984ec12e34dfe9c693faaf0f Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 26 Sep 2019 19:50:23 +0100 Subject: [PATCH] test: add missing tests Our http tests in `js-IPFS` duplicate a whole bunch of tests from this suite, but they also test some unhappy paths that this suite does not. I've pulled them out of `js-IPFS` as part of ipfs/js-ipfs#2495 and am adding them to the interface tests here. --- src/block/get.js | 8 ++++++++ src/block/stat.js | 16 ++++++++++++++++ src/bootstrap/add.js | 15 +++++++++++++++ src/config/set.js | 16 ++++++++++++++++ src/files-mfs/ls.js | 24 ++++++++++++++++++++++++ src/object/data.js | 16 ++++++++++++++++ src/object/get.js | 16 ++++++++++++++++ src/object/links.js | 16 ++++++++++++++++ src/object/patch/add-link.js | 16 ++++++++++++++++ src/object/patch/append-data.js | 18 ++++++++++++++++++ src/object/patch/rm-link.js | 27 +++++++++++++++++++++++++++ src/object/patch/set-data.js | 18 ++++++++++++++++++ src/object/stat.js | 16 ++++++++++++++++ 13 files changed, 222 insertions(+) diff --git a/src/block/get.js b/src/block/get.js index 3238e8233..0cac58251 100644 --- a/src/block/get.js +++ b/src/block/get.js @@ -107,5 +107,13 @@ module.exports = (createCommon, options) => { }) }) }) + + it('should return an error for an invalid CID', () => { + return ipfs.block.get('invalid') + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) }) } diff --git a/src/block/stat.js b/src/block/stat.js index bc251dc95..d5bf3b11c 100644 --- a/src/block/stat.js +++ b/src/block/stat.js @@ -43,5 +43,21 @@ module.exports = (createCommon, options) => { done() }) }) + + it('should return error for missing argument', () => { + return ipfs.block.stat(null) + .then( + () => expect.fail('should have thrown for missing parameter'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) + + it('should return error for invalid argument', () => { + return ipfs.block.stat('invalid') + .then( + () => expect.fail('should have thrown for invalid parameter'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) }) } diff --git a/src/bootstrap/add.js b/src/bootstrap/add.js index 96578d24d..db0b070da 100644 --- a/src/bootstrap/add.js +++ b/src/bootstrap/add.js @@ -60,5 +60,20 @@ module.exports = (createCommon, options) => { done() }) }) + + it('should prevent duplicate inserts of bootstrap peers', async () => { + const removed = await ipfs.bootstrap.rm(null, { all: true }) + expect(removed).to.have.property('Peers').that.is.an('array') + expect(removed.Peers).to.have.lengthOf(0) + + const added = await ipfs.bootstrap.add(validIp4) + expect(added).to.have.property('Peers').that.deep.equals([validIp4]) + + const addedAgain = await ipfs.bootstrap.add(validIp4) + expect(addedAgain).to.have.property('Peers').that.deep.equals([validIp4]) + + const list = await ipfs.bootstrap.list() + expect(list).to.have.property('Peers').that.deep.equals([validIp4]) + }) }) } diff --git a/src/config/set.js b/src/config/set.js index 50b1be49f..140cf271f 100644 --- a/src/config/set.js +++ b/src/config/set.js @@ -72,6 +72,22 @@ module.exports = (createCommon, options) => { }) }) + it('should set a boolean', async () => { + const value = true + const key = 'Datastore.Path' + + await ipfs.config.set(key, value) + expect(await ipfs.config.get(key)).to.equal(value) + }) + + it('should set the other boolean', async () => { + const value = false + const key = 'Datastore.Path' + + await ipfs.config.set(key, value) + expect(await ipfs.config.get(key)).to.equal(value) + }) + it('should set a JSON object', (done) => { const key = 'API.HTTPHeaders.Access-Control-Allow-Origin' const val = ['http://example.io'] diff --git a/src/files-mfs/ls.js b/src/files-mfs/ls.js index 756eb9ae0..a2e58e1ab 100644 --- a/src/files-mfs/ls.js +++ b/src/files-mfs/ls.js @@ -91,5 +91,29 @@ module.exports = (createCommon, options) => { }) }) }) + + it('should list an empty directory', async () => { + const testDir = `/test-${hat()}` + await ipfs.files.mkdir(testDir) + const contents = await ipfs.files.ls(testDir) + + expect(contents).to.be.an('array').and.to.be.empty() + }) + + it('should list an file directly', async () => { + const fileName = `single-file-${hat()}.txt` + const filePath = `/${fileName}` + await ipfs.files.write(filePath, Buffer.from('Hello world'), { + create: true + }) + const contents = await ipfs.files.ls(filePath) + + expect(contents).to.be.an('array').and.have.lengthOf(1).and.to.deep.equal([{ + hash: '', + name: fileName, + size: 0, + type: 0 + }]) + }) }) } diff --git a/src/object/data.js b/src/object/data.js index cd453216b..bfed3f398 100644 --- a/src/object/data.js +++ b/src/object/data.js @@ -118,5 +118,21 @@ module.exports = (createCommon, options) => { }) }) }) + + it('returns error for request without argument', () => { + return ipfs.object.data(null) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) + + it('returns error for request with invalid argument', () => { + ipfs.object.data('invalid', { enc: 'base58' }) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) }) } diff --git a/src/object/get.js b/src/object/get.js index a9f422ceb..4bac9c934 100644 --- a/src/object/get.js +++ b/src/object/get.js @@ -336,5 +336,21 @@ module.exports = (createCommon, options) => { expect(meta.fileSize()).to.equal(data.length) }) }) + + it('should error for request without argument', () => { + return ipfs.object.get(null) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) + + it('returns error for request with invalid argument', () => { + return ipfs.object.get('invalid', { enc: 'base58' }) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) }) } diff --git a/src/object/links.js b/src/object/links.js index 899fa8a74..2dd51d4e0 100644 --- a/src/object/links.js +++ b/src/object/links.js @@ -206,5 +206,21 @@ module.exports = (createCommon, options) => { }) }) }) + + it('returns error for request without argument', () => { + return ipfs.object.links(null) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) + + it('returns error for request with invalid argument', () => { + ipfs.object.links('invalid', { enc: 'base58' }) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) }) } diff --git a/src/object/patch/add-link.js b/src/object/patch/add-link.js index c5589a959..bf571c218 100644 --- a/src/object/patch/add-link.js +++ b/src/object/patch/add-link.js @@ -163,5 +163,21 @@ module.exports = (createCommon, options) => { expect(newParentCid).to.eql(nodeFromObjectPatchCid) }) + + it('returns error for request without arguments', () => { + return ipfs.object.patch.addLink(null, null, null) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) + + it('returns error for request with only one invalid argument', () => { + return ipfs.object.patch.addLink('invalid', null, null) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) }) } diff --git a/src/object/patch/append-data.js b/src/object/patch/append-data.js index 6d313674b..9d7b56a81 100644 --- a/src/object/patch/append-data.js +++ b/src/object/patch/append-data.js @@ -59,5 +59,23 @@ module.exports = (createCommon, options) => { expect(nodeCid).to.not.deep.equal(patchedNodeCid) }) + + it('returns error for request without key & data', () => { + return ipfs.object.patch.appendData(null, null) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) + + it('returns error for request without data', () => { + const filePath = 'test/fixtures/test-data/badnode.json' + + return ipfs.object.patch.appendData(null, filePath) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) }) } diff --git a/src/object/patch/rm-link.js b/src/object/patch/rm-link.js index 430db2ed7..03195bf78 100644 --- a/src/object/patch/rm-link.js +++ b/src/object/patch/rm-link.js @@ -121,5 +121,32 @@ module.exports = (createCommon, options) => { expect(withoutChildCid).to.not.deep.equal(parentCid) expect(withoutChildCid).to.deep.equal(nodeCid) }) + + it('returns error for request without arguments', () => { + return ipfs.object.patch.rmLink(null, null) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) + + it('returns error for request only one invalid argument', () => { + return ipfs.object.patch.rmLink('invalid', null) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) + + it('returns error for request with invalid first argument', () => { + const root = '' + const link = 'foo' + + return ipfs.object.patch.rmLink(root, link) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) }) } diff --git a/src/object/patch/set-data.js b/src/object/patch/set-data.js index 621fc43e4..7a7c619ba 100644 --- a/src/object/patch/set-data.js +++ b/src/object/patch/set-data.js @@ -68,5 +68,23 @@ module.exports = (createCommon, options) => { expect(nodeCid).to.not.deep.equal(patchedNodeCid) expect(patchedNode.Data).to.eql(patchData) }) + + it('returns error for request without key & data', () => { + return ipfs.object.patch.setData(null, null) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) + + it('returns error for request without data', () => { + const filePath = 'test/fixtures/test-data/badnode.json' + + return ipfs.object.patch.setData(null, filePath) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) }) } diff --git a/src/object/stat.js b/src/object/stat.js index c71e08662..3c1da06cc 100644 --- a/src/object/stat.js +++ b/src/object/stat.js @@ -214,5 +214,21 @@ module.exports = (createCommon, options) => { }) }) }) + + it('returns error for request without argument', () => { + return ipfs.object.stat(null) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) + + it('returns error for request with invalid argument', () => { + return ipfs.object.stat('invalid', { enc: 'base58' }) + .then( + () => expect.fail('should have returned an error for invalid argument'), + (err) => expect(err).to.be.an.instanceof(Error) + ) + }) }) }