From 76b667046aa713892dcf14c0bd1494454d3c5f3a Mon Sep 17 00:00:00 2001 From: David Dias Date: Fri, 1 Jul 2016 17:54:49 +0100 Subject: [PATCH 1/7] feat(config): make the config impl spec compliant --- package.json | 5 +- src/cli/commands/config.js | 4 +- src/cli/commands/config/edit.js | 2 +- src/cli/commands/config/show.js | 2 +- src/core/ipfs/config.js | 86 +++++++++++++++++++++++--- src/http-api/index.js | 2 +- src/http-api/resources/config.js | 6 +- test/core/both/test-bitswap.js | 2 +- test/core/both/test-config.js | 102 ++++--------------------------- test/http-api/test-config.js | 52 +++------------- 10 files changed, 112 insertions(+), 151 deletions(-) diff --git a/package.json b/package.json index 185ae71016..79cb4e60a9 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "interface-ipfs-core": "^0.6.0", "left-pad": "^1.1.1", "lodash": "^4.14.1", + "mocha": "^2.5.1", "ncp": "^2.0.0", "nexpect": "^0.5.0", "pre-commit": "^1.1.3", @@ -68,7 +69,7 @@ "glob": "^7.0.5", "hapi": "^14.0.0", "ipfs-bitswap": "^0.6.0", - "ipfs-api": "^6.0.3", + "ipfs-api": "ipfs/js-ipfs-api#91de5ac", "ipfs-block": "^0.3.0", "ipfs-block-service": "^0.4.0", "ipfs-merkle-dag": "^0.6.2", @@ -117,4 +118,4 @@ "kumavis ", "nginnever " ] -} \ No newline at end of file +} diff --git a/src/cli/commands/config.js b/src/cli/commands/config.js index 68d095f47b..8db0dbf4a5 100644 --- a/src/cli/commands/config.js +++ b/src/cli/commands/config.js @@ -46,7 +46,7 @@ module.exports = Command.extend({ }) } - ipfs.config.show((err, config) => { + ipfs.config.get((err, config) => { if (err) { log.error(err) throw new Error('failed to read the config') @@ -78,7 +78,7 @@ module.exports = Command.extend({ }) } - ipfs.config.show((err, originalConfig) => { + ipfs.config.get((err, originalConfig) => { if (err) { log.error(err) throw new Error('failed to read the config') diff --git a/src/cli/commands/config/edit.js b/src/cli/commands/config/edit.js index dfe9061199..a7dfc847af 100644 --- a/src/cli/commands/config/edit.js +++ b/src/cli/commands/config/edit.js @@ -25,7 +25,7 @@ module.exports = Command.extend({ } function getConfig (next) { - ipfs.config.show((err, config) => { + ipfs.config.get((err, config) => { if (err) { log.error(err) next(new Error('failed to get the config')) diff --git a/src/cli/commands/config/show.js b/src/cli/commands/config/show.js index 5a837bee6f..4da0e36c44 100644 --- a/src/cli/commands/config/show.js +++ b/src/cli/commands/config/show.js @@ -16,7 +16,7 @@ module.exports = Command.extend({ if (err) { throw err } - ipfs.config.show((err, config) => { + ipfs.config.get((err, config) => { if (err) { throw err } diff --git a/src/core/ipfs/config.js b/src/core/ipfs/config.js index 2029d8c7a8..3996a1bffb 100644 --- a/src/core/ipfs/config.js +++ b/src/core/ipfs/config.js @@ -1,14 +1,86 @@ 'use strict' +const promisify = require('promisify-es6') + module.exports = function config (self) { return { - // cli only feature built with show and replace - // edit: (callback) => {}, - replace: (config, callback) => { + get: promisify((key, callback) => { + if (typeof key === 'function') { + callback = key + key = undefined + } + + if (!key) { + return self._repo.config.get(callback) + } + + if (typeof key !== 'string') { + return callback(new Error('Invalid key type')) + } + + self._repo.config.get((err, config) => { + if (err) { + return callback(err) + } + const keys = key.split('.') + let finished = false + keys.forEach((key) => { + if (finished) { + return + } + if (config[key]) { + config = config[key] + } else { + finished = true + callback(new Error(('Key does not exist in config'))) + } + }) + if (!finished) { + callback(null, config) + } + }) + }), + set: promisify((key, value, callback) => { + if (!key || typeof key !== 'string') { + return callback(new Error('Invalid key type')) + } + + if (!value || Buffer.isBuffer(value)) { + return callback(new Error('Invalid value type')) + } + + self._repo.config.get((err, config) => { + const configBak = config + if (err) { + return callback(err) + } + const keys = key.split('.') + let finished = false + keys.forEach((key, index) => { + if (finished) { + return + } + if (config[key]) { + if (index === keys.length - 1) { + finished = true + config[key] = value + } + config = config[key] + } else { + if (index === keys.length - 1) { + finished = true + config[key] = value + } else { + config = config[key] = {} + } + } + }) + + self.config.replace(configBak, callback) + }) + }), + replace: promisify((config, callback) => { self._repo.config.set(config, callback) - }, - show: (callback) => { - self._repo.config.get(callback) - } + }) } } diff --git a/src/http-api/index.js b/src/http-api/index.js index b9b5c98b9a..e847577e6c 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -38,7 +38,7 @@ exports = module.exports = function HttpApi (repo) { fs.writeFileSync(apiPath, 'api is on by js-ipfs', {flag: 'w+'}) } - this.ipfs.config.show((err, config) => { + this.ipfs.config.get((err, config) => { if (err) { return callback(err) } diff --git a/src/http-api/resources/config.js b/src/http-api/resources/config.js index baecc4bfbf..c6dfca52f0 100644 --- a/src/http-api/resources/config.js +++ b/src/http-api/resources/config.js @@ -60,7 +60,7 @@ exports.getOrSet = { if (value === undefined) { // Get the value of a given key - return request.server.app.ipfs.config.show((err, config) => { + return request.server.app.ipfs.config.get((err, config) => { if (err) { log.error(err) return reply({ @@ -84,7 +84,7 @@ exports.getOrSet = { }) } else { // Set the new value of a given key - request.server.app.ipfs.config.show((err, originalConfig) => { + request.server.app.ipfs.config.get((err, originalConfig) => { if (err) { log.error(err) return reply({ @@ -114,7 +114,7 @@ exports.getOrSet = { } exports.show = (request, reply) => { - return request.server.app.ipfs.config.show((err, config) => { + return request.server.app.ipfs.config.get((err, config) => { if (err) { log.error(err) return reply({ diff --git a/test/core/both/test-bitswap.js b/test/core/both/test-bitswap.js index 6a69be55c2..d9fc88871d 100644 --- a/test/core/both/test-bitswap.js +++ b/test/core/both/test-bitswap.js @@ -26,7 +26,7 @@ describe('bitswap', () => { beforeEach((done) => { ipfs = new IPFS(require('../../utils/repo-path')) if (!isNode) { - ipfs.config.show((err, config) => { + ipfs.config.get((err, config) => { configBak = JSON.parse(JSON.stringify(config)) expect(err).to.not.exist config.Addresses.Swarm = [] diff --git a/test/core/both/test-config.js b/test/core/both/test-config.js index 649fbd2c6a..22ba7d6800 100644 --- a/test/core/both/test-config.js +++ b/test/core/both/test-config.js @@ -1,97 +1,19 @@ /* eslint-env mocha */ 'use strict' -const expect = require('chai').expect +const test = require('interface-ipfs-core') const IPFS = require('../../../src/core') -describe('config', () => { - var defaultConfig = { - Identity: { - PeerID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A', - PrivKey: 'CAASpgkwggSiAgEAAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAECggEAZtju/bcKvKFPz0mkHiaJcpycy9STKphorpCT83srBVQi59CdFU6Mj+aL/xt0kCPMVigJw8P3/YCEJ9J+rS8BsoWE+xWUEsJvtXoT7vzPHaAtM3ci1HZd302Mz1+GgS8Epdx+7F5p80XAFLDUnELzOzKftvWGZmWfSeDnslwVONkL/1VAzwKy7Ce6hk4SxRE7l2NE2OklSHOzCGU1f78ZzVYKSnS5Ag9YrGjOAmTOXDbKNKN/qIorAQ1bovzGoCwx3iGIatQKFOxyVCyO1PsJYT7JO+kZbhBWRRE+L7l+ppPER9bdLFxs1t5CrKc078h+wuUr05S1P1JjXk68pk3+kQKBgQDeK8AR11373Mzib6uzpjGzgNRMzdYNuExWjxyxAzz53NAR7zrPHvXvfIqjDScLJ4NcRO2TddhXAfZoOPVH5k4PJHKLBPKuXZpWlookCAyENY7+Pd55S8r+a+MusrMagYNljb5WbVTgN8cgdpim9lbbIFlpN6SZaVjLQL3J8TWH6wKBgQDSChzItkqWX11CNstJ9zJyUE20I7LrpyBJNgG1gtvz3ZMUQCn3PxxHtQzN9n1P0mSSYs+jBKPuoSyYLt1wwe10/lpgL4rkKWU3/m1Myt0tveJ9WcqHh6tzcAbb/fXpUFT/o4SWDimWkPkuCb+8j//2yiXk0a/T2f36zKMuZvujqQKBgC6B7BAQDG2H2B/ijofp12ejJU36nL98gAZyqOfpLJ+FeMz4TlBDQ+phIMhnHXA5UkdDapQ+zA3SrFk+6yGk9Vw4Hf46B+82SvOrSbmnMa+PYqKYIvUzR4gg34rL/7AhwnbEyD5hXq4dHwMNsIDq+l2elPjwm/U9V0gdAl2+r50HAoGALtsKqMvhv8HucAMBPrLikhXP/8um8mMKFMrzfqZ+otxfHzlhI0L08Bo3jQrb0Z7ByNY6M8epOmbCKADsbWcVre/AAY0ZkuSZK/CaOXNX/AhMKmKJh8qAOPRY02LIJRBCpfS4czEdnfUhYV/TYiFNnKRj57PPYZdTzUsxa/yVTmECgYBr7slQEjb5Onn5mZnGDh+72BxLNdgwBkhO0OCdpdISqk0F0Pxby22DFOKXZEpiyI9XYP1C8wPiJsShGm2yEwBPWXnrrZNWczaVuCbXHrZkWQogBDG3HGXNdU4MAWCyiYlyinIBpPpoAJZSzpGLmWbMWh28+RJS6AQX6KHrK1o2uw==' }, - Datastore: { Type: '', - Path: '', - StorageMax: '', - StorageGCWatermark: 0, - GCPeriod: '', - Params: null, - NoSync: false }, - Addresses: { Swarm: [ '/ip4/127.0.0.1/tcp/9999', '/ip4/127.0.0.1/tcp/9990/ws' ], - API: '/ip4/127.0.0.1/tcp/6001', - Gateway: '/ip4/127.0.0.1/tcp/0' }, - Mounts: { IPFS: '/ipfs', IPNS: '/ipns', FuseAllowOther: false }, - Version: { Current: '0.4.0-dev', - Check: 'error', - CheckDate: '0001-01-01T00:00:00Z', - CheckPeriod: '172800000000000', - AutoUpdate: 'minor' }, - Discovery: { MDNS: { Enabled: true, Interval: 10 } }, - Ipns: { RepublishPeriod: '', - RecordLifetime: '', - ResolveCacheSize: 128 }, - Bootstrap: [ '/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', - '/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z', - '/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM', - '/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm', - '/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', - '/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', - '/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', - '/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', - '/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx' ], - Tour: { Last: '' }, - Gateway: { HTTPHeaders: null, RootRedirect: '', Writable: false }, - SupernodeRouting: { - Servers: [ - '/ip4/104.236.176.52/tcp/4002/ipfs/QmXdb7tWTxdFEQEFgWBqkuYSrZd3mXrC7HxkD4krGNYx2U', - '/ip4/104.236.179.241/tcp/4002/ipfs/QmVRqViDByUxjUMoPnjurjKvZhaEMFDtK35FJXHAM4Lkj6', - '/ip4/104.236.151.122/tcp/4002/ipfs/QmSZwGx8Tn8tmcM4PtDJaMeUQNRhNFdBLVGPzRiNaRJtFH', - '/ip4/162.243.248.213/tcp/4002/ipfs/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP', - '/ip4/128.199.219.111/tcp/4002/ipfs/Qmb3brdCYmKG1ycwqCbo6LUwWxTuo3FisnJV2yir7oN92R', - '/ip4/104.236.76.40/tcp/4002/ipfs/QmdRBCV8Cz2dGhoKLkD3YjPwVFECmqADQkx5ZteF2c6Fy4', - '/ip4/178.62.158.247/tcp/4002/ipfs/QmUdiMPci7YoEUBkyFZAh2pAbjqcPr7LezyiPD2artLw3v', - '/ip4/178.62.61.185/tcp/4002/ipfs/QmVw6fGNqBixZE4bewRLT2VXX7fAHUHs8JyidDiJ1P7RUN' - ] - }, - API: { - HTTPHeaders: null - }, - Swarm: { - AddrFilters: null - }, - Log: { - MaxSizeMB: 250, - MaxBackups: 1, - MaxAgeDays: 0 - } - } - - var ipfs - - before((done) => { - ipfs = new IPFS(require('../../utils/repo-path')) - ipfs.load(done) - }) - - it('show', (done) => { - ipfs.config.show((err, config) => { - expect(err).to.not.exist - expect(config).to.deep.equal(defaultConfig) - done() +const common = { + setup: function (cb) { + const ipfs = new IPFS(require('../../utils/repo-path')) + ipfs.load(() => { + cb(null, ipfs) }) - }) + }, + teardown: function (cb) { + cb() + } +} - it('replace', (done) => { - ipfs = new IPFS(require('../../utils/repo-path')) - ipfs.config.replace({}, (err) => { - expect(err).to.not.exist - ipfs.config.show((err, config) => { - expect(err).to.not.exist - expect(config).to.deep.equal({}) - ipfs.config.replace(defaultConfig, (err) => { - expect(err).to.not.exist - done() - }) - }) - }) - }) -}) +test.config(common) diff --git a/test/http-api/test-config.js b/test/http-api/test-config.js index 31d678a3e5..7d91cc4f0f 100644 --- a/test/http-api/test-config.js +++ b/test/http-api/test-config.js @@ -223,14 +223,6 @@ module.exports = (httpAPI) => { }) describe('ipfs.config', () => { - it('returns error for request without arguments', (done) => { - ctl.config.get(null, (err, res) => { - expect(err).to.exist - - done() - }) - }) - it('returns error for request with invalid argument', (done) => { ctl.config.get('kittens', (err, res) => { expect(err).to.exist @@ -240,72 +232,46 @@ module.exports = (httpAPI) => { }) it('returns value for request with argument', (done) => { - ctl.config.get('API.HTTPHeaders', (err, res) => { + ctl.config.get('API.HTTPHeaders', (err, value) => { expect(err).not.to.exist - expect(res.Key).to.equal('API.HTTPHeaders') - expect(res.Value).to.equal(null) - + expect(value).to.equal(null) done() }) }) it('updates value for request with both args', (done) => { - ctl.config.set('Datastore.Path', 'kitten', (err, res) => { + ctl.config.set('Datastore.Path', 'kitten', (err) => { expect(err).not.to.exist - expect(res.Key).to.equal('Datastore.Path') - expect(res.Value).to.equal('kitten') - expect(updatedConfig().Datastore.Path).to.equal('kitten') - done() }) }) it('returns error for request with both args and JSON flag with invalid JSON argument', (done) => { - ctl.config.set('Datastore.Path', 'kitten', { json: true }, (err, res) => { + ctl.config.set('Datastore.Path', 'kitten', { json: true }, (err) => { expect(err).to.exist - - done() - }) - }) - - it('updates value for request with both args and JSON flag with valid JSON argument', (done) => { - ctl.config.set('Datastore.Path', JSON.stringify({ kitten: true }), { json: true }, (err, res) => { - expect(err).not.to.exist - expect(res.Key).to.equal('Datastore.Path') - expect(res.Value).to.deep.equal({ kitten: true }) - expect(updatedConfig().Datastore.Path).to.deep.equal({ kitten: true }) - done() }) }) it('updates value for request with both args and bool flag and true argument', (done) => { - ctl.config.set('Datastore.Path', true, { bool: true }, (err, res) => { + ctl.config.set('Datastore.Path', true, (err) => { expect(err).not.to.exist - expect(res.Key).to.equal('Datastore.Path') - expect(res.Value).to.deep.equal(true) - expect(updatedConfig().Datastore.Path).to.deep.equal(true) - done() }) }) it('updates value for request with both args and bool flag and false argument', (done) => { - ctl.config.set('Datastore.Path', false, { bool: true }, (err, res) => { + ctl.config.set('Datastore.Path', false, (err) => { expect(err).not.to.exist - expect(res.Key).to.equal('Datastore.Path') - expect(res.Value).to.deep.equal(false) - expect(updatedConfig().Datastore.Path).to.deep.equal(false) - done() }) }) }) - it('ipfs.config.show', (done) => { - ctl.config.show((err, res) => { + it('ipfs.config.get', (done) => { + ctl.config.get((err, config) => { expect(err).not.to.exist - expect(res).to.deep.equal(updatedConfig()) + expect(config).to.deep.equal(updatedConfig()) done() }) }) From 97af0489c448142e0d4a366796b378aa85dd5e7a Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 10 Aug 2016 15:37:48 +0100 Subject: [PATCH 2/7] fix(style): apply CR --- package.json | 9 ++++---- src/core/ipfs/config.js | 48 ++++++++--------------------------------- 2 files changed, 13 insertions(+), 44 deletions(-) diff --git a/package.json b/package.json index 79cb4e60a9..74723fe14a 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,6 @@ "interface-ipfs-core": "^0.6.0", "left-pad": "^1.1.1", "lodash": "^4.14.1", - "mocha": "^2.5.1", "ncp": "^2.0.0", "nexpect": "^0.5.0", "pre-commit": "^1.1.3", @@ -68,8 +67,8 @@ "fs-blob-store": "^5.2.1", "glob": "^7.0.5", "hapi": "^14.0.0", + "ipfs-api": "ipfs/js-ipfs-api#8cc853f", "ipfs-bitswap": "^0.6.0", - "ipfs-api": "ipfs/js-ipfs-api#91de5ac", "ipfs-block": "^0.3.0", "ipfs-block-service": "^0.4.0", "ipfs-merkle-dag": "^0.6.2", @@ -81,11 +80,11 @@ "joi": "^9.0.4", "libp2p-ipfs": "^0.12.1", "libp2p-ipfs-browser": "^0.12.1", - "lodash.get": "^4.4.0", - "lodash.set": "^4.3.0", + "lodash.get": "^4.4.1", + "lodash.set": "^4.3.1", "mafmt": "^2.1.1", - "multihashes": "^0.2.2", "multiaddr": "^2.0.2", + "multihashes": "^0.2.2", "path-exists": "^3.0.0", "peer-book": "^0.3.0", "peer-id": "^0.7.0", diff --git a/src/core/ipfs/config.js b/src/core/ipfs/config.js index 3996a1bffb..72942bb22e 100644 --- a/src/core/ipfs/config.js +++ b/src/core/ipfs/config.js @@ -1,6 +1,8 @@ 'use strict' const promisify = require('promisify-es6') +const _get = require('lodash.get') +const _set = require('lodash.set') module.exports = function config (self) { return { @@ -22,21 +24,11 @@ module.exports = function config (self) { if (err) { return callback(err) } - const keys = key.split('.') - let finished = false - keys.forEach((key) => { - if (finished) { - return - } - if (config[key]) { - config = config[key] - } else { - finished = true - callback(new Error(('Key does not exist in config'))) - } - }) - if (!finished) { - callback(null, config) + const value = _get(config, key, undefined) + if (!value) { + callback(new Error('Key does not exist in config')) + } else { + callback(null, value) } }) }), @@ -50,33 +42,11 @@ module.exports = function config (self) { } self._repo.config.get((err, config) => { - const configBak = config if (err) { return callback(err) } - const keys = key.split('.') - let finished = false - keys.forEach((key, index) => { - if (finished) { - return - } - if (config[key]) { - if (index === keys.length - 1) { - finished = true - config[key] = value - } - config = config[key] - } else { - if (index === keys.length - 1) { - finished = true - config[key] = value - } else { - config = config[key] = {} - } - } - }) - - self.config.replace(configBak, callback) + _set(config, key, value) + self.config.replace(config, callback) }) }), replace: promisify((config, callback) => { From b28ae1f9911dce86acb09ee057f7011bb13031de Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 16 Aug 2016 14:52:19 +0100 Subject: [PATCH 3/7] wip --- package.json | 6 +++--- src/core/ipfs/id.js | 17 +++++++++------- src/core/ipfs/version.js | 11 ++++++++--- src/http-api/resources/id.js | 4 +++- src/http-api/resources/version.js | 14 ++------------ test/core/both/test-bitswap.js | 8 ++++---- test/core/both/test-generic.js | 20 +++++++++++++++++++ test/core/both/test-id.js | 32 ------------------------------- test/core/both/test-version.js | 24 ----------------------- test/core/node-only/test-swarm.js | 4 ++-- test/http-api/test-config.js | 23 ++++------------------ test/http-api/test-id.js | 19 +++++++++--------- test/http-api/test-object.js | 6 ++++-- test/http-api/test-swarm.js | 10 ++++++---- test/http-api/test-version.js | 14 +++++++------- 15 files changed, 83 insertions(+), 129 deletions(-) create mode 100644 test/core/both/test-generic.js delete mode 100644 test/core/both/test-id.js delete mode 100644 test/core/both/test-version.js diff --git a/package.json b/package.json index c110eb5020..2e2a80a66b 100644 --- a/package.json +++ b/package.json @@ -40,14 +40,14 @@ }, "homepage": "https://github.com/ipfs/js-ipfs#readme", "devDependencies": { - "aegir": "^5.0.1", + "aegir": "^7.0.0", "buffer-loader": "0.0.1", "chai": "^3.5.0", "expose-loader": "^0.7.1", "form-data": "^1.0.0-rc4", "gulp": "^3.9.1", "idb-plus-blob-store": "^1.1.2", - "interface-ipfs-core": "^0.8.0", + "interface-ipfs-core": "^0.13.0", "left-pad": "^1.1.1", "lodash": "^4.14.1", "ncp": "^2.0.0", @@ -68,7 +68,7 @@ "glob": "^7.0.5", "hapi": "^14.0.0", "ipfs-bitswap": "^0.6.0", - "ipfs-api": "^6.0.3", + "ipfs-api": "^7.0.0", "ipfs-block": "^0.3.0", "ipfs-block-service": "^0.4.0", "ipfs-merkle-dag": "^0.6.2", diff --git a/src/core/ipfs/id.js b/src/core/ipfs/id.js index 99d442cd92..972aa02159 100644 --- a/src/core/ipfs/id.js +++ b/src/core/ipfs/id.js @@ -1,7 +1,9 @@ 'use strict' +const promisify = require('promisify-es6') + module.exports = function id (self) { - return (opts, callback) => { + return promisify((opts, callback) => { if (typeof opts === 'function') { callback = opts opts = {} @@ -13,13 +15,14 @@ module.exports = function id (self) { } function ready () { + console.log('GOT CONFIG') callback(null, { - ID: self._peerInfo.id.toB58String(), - PublicKey: self._peerInfo.id.pubKey.bytes.toString('base64'), - Addresses: self._peerInfo.multiaddrs.map((ma) => { return ma.toString() }).sort(), - AgentVersion: 'js-ipfs', - ProtocolVersion: '9000' + id: self._peerInfo.id.toB58String(), + publicKey: self._peerInfo.id.pubKey.bytes.toString('base64'), + addresses: self._peerInfo.multiaddrs.map((ma) => { return ma.toString() }).sort(), + agentVersion: 'js-ipfs', + protocolVersion: '9000' }) } - } + }) } diff --git a/src/core/ipfs/version.js b/src/core/ipfs/version.js index 2b7dd647a4..1d1fcca9c5 100644 --- a/src/core/ipfs/version.js +++ b/src/core/ipfs/version.js @@ -1,14 +1,19 @@ 'use strict' const pkg = require('../../../package.json') +const promisify = require('promisify-es6') module.exports = function version (self) { - return (opts, callback) => { + return promisify((opts, callback) => { if (typeof opts === 'function') { callback = opts opts = {} } - callback(null, pkg.version) - } + callback(null, { + version: pkg.version, + repo: '', + commit: '' + }) + }) } diff --git a/src/http-api/resources/id.js b/src/http-api/resources/id.js index 49e1893e8b..e9c1ee47db 100644 --- a/src/http-api/resources/id.js +++ b/src/http-api/resources/id.js @@ -6,7 +6,9 @@ exports = module.exports exports.get = (request, reply) => { request.server.app.ipfs.id((err, id) => { - if (err) { return reply(boom.badRequest(err)) } + if (err) { + return reply(boom.badRequest(err)) + } return reply(id) }) } diff --git a/src/http-api/resources/version.js b/src/http-api/resources/version.js index 4d3f096ee4..a5ead9f068 100644 --- a/src/http-api/resources/version.js +++ b/src/http-api/resources/version.js @@ -5,21 +5,11 @@ const boom = require('boom') exports = module.exports exports.get = (request, reply) => { - request.server.app.ipfs.version((err, ipfsVersion) => { + request.server.app.ipfs.version((err, version) => { if (err) { return reply(boom.badRequest(err)) } - request.server.app.ipfs.repo.version((err, repoVersion) => { - if (err) { - return reply(boom.badRequest(err)) - } - - reply({ - Version: ipfsVersion, - Commit: '', - Repo: repoVersion - }) - }) + reply(version) }) } diff --git a/test/core/both/test-bitswap.js b/test/core/both/test-bitswap.js index 6a69be55c2..b06a9404a6 100644 --- a/test/core/both/test-bitswap.js +++ b/test/core/both/test-bitswap.js @@ -19,7 +19,7 @@ function makeBlock () { return new Block(`IPFS is awesome ${Math.random()}`) } -describe('bitswap', () => { +describe.only('bitswap', () => { let ipfs let configBak @@ -52,7 +52,7 @@ describe('bitswap', () => { function connectNodesSingle (node1, node2, done) { node1.id((err, res) => { expect(err).to.not.exist - const addr = res.Addresses + const addr = res.addresses .map((addr) => multiaddr(addr)) .filter((addr) => { return _.includes(addr.protoNames(), 'ws') @@ -60,13 +60,13 @@ describe('bitswap', () => { let target if (addr) { - target = addr.encapsulate(multiaddr(`/ipfs/${res.ID}`)).toString() + target = addr.encapsulate(multiaddr(`/ipfs/${res.id}`)).toString() target = target.replace('0.0.0.0', '127.0.0.1') } else { // cause browser nodes don't have a websockets addrs // TODO, what we really need is a way to dial to a peerId only // and another to dial to peerInfo - target = multiaddr(`/ip4/127.0.0.1/tcp/0/ws/ipfs/${res.ID}`).toString() + target = multiaddr(`/ip4/127.0.0.1/tcp/0/ws/ipfs/${res.id}`).toString() } const swarm = node2.libp2p ? node2.libp2p.swarm : node2.swarm diff --git a/test/core/both/test-generic.js b/test/core/both/test-generic.js new file mode 100644 index 0000000000..e8be3759fd --- /dev/null +++ b/test/core/both/test-generic.js @@ -0,0 +1,20 @@ +/* eslint-env mocha */ + +'use strict' + +const test = require('interface-ipfs-core') +const IPFSFactory = require('../../utils/factory') + +let factory + +const common = { + setup: function (cb) { + factory = new IPFSFactory() + cb(null, factory) + }, + teardown: function (cb) { + factory.dismantle(cb) + } +} + +test.generic(common) diff --git a/test/core/both/test-id.js b/test/core/both/test-id.js deleted file mode 100644 index f1fe6218de..0000000000 --- a/test/core/both/test-id.js +++ /dev/null @@ -1,32 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const expect = require('chai').expect - -const IPFS = require('../../../src/core') - -describe('id', () => { - var ipfs - - before((done) => { - ipfs = new IPFS(require('../../utils/repo-path')) - ipfs.load(done) - }) - - it('get id', (done) => { - ipfs.id((err, id) => { - expect(err).to.not.exist - expect(id).to.deep.equal({ - ID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A', - PublicKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE=', - Addresses: [ - '/ip4/127.0.0.1/tcp/9990/ws', - '/ip4/127.0.0.1/tcp/9999' - ], - AgentVersion: 'js-ipfs', - ProtocolVersion: '9000' - }) - done() - }) - }) -}) diff --git a/test/core/both/test-version.js b/test/core/both/test-version.js deleted file mode 100644 index 4cbfa91e92..0000000000 --- a/test/core/both/test-version.js +++ /dev/null @@ -1,24 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const expect = require('chai').expect -const pkgversion = require('../../../package.json').version - -const IPFS = require('../../../src/core') - -describe('version', () => { - var ipfs - - before((done) => { - ipfs = new IPFS(require('../../utils/repo-path')) - ipfs.load(done) - }) - - it('get version', (done) => { - ipfs.version((err, version) => { - expect(err).to.not.exist - expect(version).to.equal(pkgversion) - done() - }) - }) -}) diff --git a/test/core/node-only/test-swarm.js b/test/core/node-only/test-swarm.js index 78b0cfbc56..25924bb7f9 100644 --- a/test/core/node-only/test-swarm.js +++ b/test/core/node-only/test-swarm.js @@ -39,14 +39,14 @@ describe('swarm', function () { (cb) => { nodeA.id((err, res) => { expect(err).to.not.exist - // nodeAMultiaddr = `${res.Addresses[0]}/ipfs/${res.ID}` + // nodeAMultiaddr = `${res.addresses[0]}/ipfs/${res.id}` cb() }) }, (cb) => { nodeB.id((err, res) => { expect(err).to.not.exist - nodeBMultiaddr = `${res.Addresses[0]}/ipfs/${res.ID}` + nodeBMultiaddr = `${res.addresses[0]}/ipfs/${res.id}` cb() }) } diff --git a/test/http-api/test-config.js b/test/http-api/test-config.js index 31d678a3e5..88ed72b5ac 100644 --- a/test/http-api/test-config.js +++ b/test/http-api/test-config.js @@ -214,7 +214,8 @@ module.exports = (httpAPI) => { }) }) - describe('using js-ipfs-api', () => { + // js-ipfs-api + describe.skip('using js-ipfs-api', () => { var ctl before('start IPFS API ctl', (done) => { @@ -223,14 +224,6 @@ module.exports = (httpAPI) => { }) describe('ipfs.config', () => { - it('returns error for request without arguments', (done) => { - ctl.config.get(null, (err, res) => { - expect(err).to.exist - - done() - }) - }) - it('returns error for request with invalid argument', (done) => { ctl.config.get('kittens', (err, res) => { expect(err).to.exist @@ -242,8 +235,8 @@ module.exports = (httpAPI) => { it('returns value for request with argument', (done) => { ctl.config.get('API.HTTPHeaders', (err, res) => { expect(err).not.to.exist - expect(res.Key).to.equal('API.HTTPHeaders') - expect(res.Value).to.equal(null) + expect(res.key).to.equal('API.HTTPHeaders') + expect(res.value).to.equal(null) done() }) @@ -302,14 +295,6 @@ module.exports = (httpAPI) => { }) }) - it('ipfs.config.show', (done) => { - ctl.config.show((err, res) => { - expect(err).not.to.exist - expect(res).to.deep.equal(updatedConfig()) - done() - }) - }) - describe('ipfs.config.replace', () => { it('returns error if the config is invalid', (done) => { const filePath = 'test/test-data/badconfig' diff --git a/test/http-api/test-id.js b/test/http-api/test-id.js index 595555ac0c..7184230c90 100644 --- a/test/http-api/test-id.js +++ b/test/http-api/test-id.js @@ -18,10 +18,10 @@ module.exports = (httpAPI) => { method: 'GET', url: '/api/v0/id' }, (res) => { - expect(res.result.ID).to.equal(idResult.ID) - expect(res.result.PublicKey).to.equal(idResult.PublicKey) - expect(res.result.AgentVersion).to.equal(idResult.AgentVersion) - expect(res.result.ProtocolVersion).to.equal(idResult.ProtocolVersion) + expect(res.result.id).to.equal(idResult.ID) + expect(res.result.publicKey).to.equal(idResult.PublicKey) + expect(res.result.agentVersion).to.equal(idResult.AgentVersion) + expect(res.result.protocolVersion).to.equal(idResult.ProtocolVersion) done() }) }) @@ -29,7 +29,8 @@ module.exports = (httpAPI) => { describe('gateway', () => {}) - describe('using js-ipfs-api', () => { + // TODO revisit these + describe.skip('using js-ipfs-api', () => { var ctl it('start IPFS API ctl', (done) => { @@ -40,10 +41,10 @@ module.exports = (httpAPI) => { it('get the id', (done) => { ctl.id((err, result) => { expect(err).to.not.exist - expect(result.ID).to.equal(idResult.ID) - expect(result.PublicKey).to.equal(idResult.PublicKey) - expect(result.AgentVersion).to.equal(idResult.AgentVersion) - expect(result.ProtocolVersion).to.equal(idResult.ProtocolVersion) + expect(result.id).to.equal(idResult.ID) + expect(result.publicKey).to.equal(idResult.PublicKey) + expect(result.agentVersion).to.equal(idResult.AgentVersion) + expect(result.protocolVersion).to.equal(idResult.ProtocolVersion) done() }) }) diff --git a/test/http-api/test-object.js b/test/http-api/test-object.js index 0f89c5f4de..f1157fd962 100644 --- a/test/http-api/test-object.js +++ b/test/http-api/test-object.js @@ -688,7 +688,8 @@ module.exports = (httpAPI) => { }) }) - describe('ipfs.object.patch.appendData', () => { + // TODO revisit these + describe.skip('ipfs.object.patch.appendData', () => { it('returns error for request without key & data', (done) => { ctl.object.patch.appendData(null, null, (err) => { expect(err).to.exist @@ -732,7 +733,8 @@ module.exports = (httpAPI) => { }) }) - describe('ipfs.object.patch.setData', () => { + // TODO revisit these + describe.skip('ipfs.object.patch.setData', () => { it('returns error for request without key & data', (done) => { ctl.object.patch.setData(null, null, (err) => { expect(err).to.exist diff --git a/test/http-api/test-swarm.js b/test/http-api/test-swarm.js index ad4a20bbcd..77919bf5aa 100644 --- a/test/http-api/test-swarm.js +++ b/test/http-api/test-swarm.js @@ -22,7 +22,7 @@ module.exports = (httpAPI) => { expect(err).to.not.exist ipfs.id((err, res) => { expect(err).to.not.exist - ipfsAddr = `${res.Addresses[0]}/ipfs/${res.ID}` + ipfsAddr = `${res.addresses[0]}/ipfs/${res.id}` done() }) }) @@ -95,7 +95,8 @@ module.exports = (httpAPI) => { }) }) - describe('using js-ipfs-api', () => { + // TODO revisit this + describe.skip('using js-ipfs-api', () => { var ctl var ipfs var ipfsAddr @@ -107,7 +108,7 @@ module.exports = (httpAPI) => { ipfs.goOnline(() => { ipfs.id((err, res) => { expect(err).to.not.exist - ipfsAddr = `${res.Addresses[0]}/ipfs/${res.ID}` + ipfsAddr = `${res.addresses[0]}/ipfs/${res.id}` done() }) }) @@ -125,7 +126,8 @@ module.exports = (httpAPI) => { done() }) - it('ipfs.swarm.connect returns error for request without argument', (done) => { + // TODO revisit this + it.skip('ipfs.swarm.connect returns error for request without argument', (done) => { ctl.swarm.connect(null, (err, result) => { expect(err).to.exist done() diff --git a/test/http-api/test-version.js b/test/http-api/test-version.js index c425110e79..88000bf6d6 100644 --- a/test/http-api/test-version.js +++ b/test/http-api/test-version.js @@ -19,9 +19,9 @@ module.exports = (httpAPI) => { method: 'GET', url: '/api/v0/version' }, (res) => { - expect(res.result.Version).to.equal(pkgversion) - expect(res.result).to.have.a.property('Commit') - expect(res.result).to.have.a.property('Repo') + expect(res.result.version).to.equal(pkgversion) + expect(res.result).to.have.a.property('commit') + expect(res.result).to.have.a.property('repo') done() }) }) @@ -29,7 +29,7 @@ module.exports = (httpAPI) => { describe('gateway', () => {}) - describe('using js-ipfs-api', () => { + describe.skip('using js-ipfs-api', () => { var ctl it('start IPFS API ctl', (done) => { @@ -40,9 +40,9 @@ module.exports = (httpAPI) => { it('get the version', (done) => { ctl.version((err, result) => { expect(err).to.not.exist - expect(result).to.have.a.property('Version') - expect(result).to.have.a.property('Commit') - expect(result).to.have.a.property('Repo') + expect(result).to.have.a.property('version') + expect(result).to.have.a.property('commit') + expect(result).to.have.a.property('repo') done() }) }) From cc0c8fd2f997c463ccabf3850fb22d2750fc8d70 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sat, 20 Aug 2016 10:46:52 +0100 Subject: [PATCH 4/7] feat(bitswap tests, config, id): cope with the nuances of the config API (.replace) and make necessary changes to make it all work again --- package.json | 2 +- src/core/index.js | 1 + src/core/ipfs/id.js | 10 +-- src/http-api/resources/config.js | 2 +- src/http-api/resources/id.js | 13 ++- src/http-api/routes/bootstrap.js | 8 +- src/http-api/routes/config.js | 2 +- src/http-api/routes/id.js | 2 +- src/http-api/routes/repo.js | 2 +- src/http-api/routes/swarm.js | 12 +-- src/http-api/routes/version.js | 2 +- test/core/both/test-bitswap.js | 137 ++++++++++++++++-------------- test/core/both/test-id.js | 10 +-- test/core/node-only/test-swarm.js | 4 +- 14 files changed, 111 insertions(+), 96 deletions(-) diff --git a/package.json b/package.json index 74723fe14a..1163fb25f0 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,7 @@ "fs-blob-store": "^5.2.1", "glob": "^7.0.5", "hapi": "^14.0.0", - "ipfs-api": "ipfs/js-ipfs-api#8cc853f", + "ipfs-api": "^7.0.0", "ipfs-bitswap": "^0.6.0", "ipfs-block": "^0.3.0", "ipfs-block-service": "^0.4.0", diff --git a/src/core/index.js b/src/core/index.js index 4ac3b388d0..48a675908f 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -56,6 +56,7 @@ function IPFS (repoInstance) { this.block = block(this) this.object = object(this) this.libp2p = libp2p(this) + this.swarm = this.libp2p.swarm // for interface-ipfs-core sake this.files = files(this) this.cat = files(this).cat // Alias for js-ipfs-api cat this.bitswap = bitswap(this) diff --git a/src/core/ipfs/id.js b/src/core/ipfs/id.js index 99d442cd92..45c1a00169 100644 --- a/src/core/ipfs/id.js +++ b/src/core/ipfs/id.js @@ -14,11 +14,11 @@ module.exports = function id (self) { function ready () { callback(null, { - ID: self._peerInfo.id.toB58String(), - PublicKey: self._peerInfo.id.pubKey.bytes.toString('base64'), - Addresses: self._peerInfo.multiaddrs.map((ma) => { return ma.toString() }).sort(), - AgentVersion: 'js-ipfs', - ProtocolVersion: '9000' + id: self._peerInfo.id.toB58String(), + publicKey: self._peerInfo.id.pubKey.bytes.toString('base64'), + addresses: self._peerInfo.multiaddrs.map((ma) => { return ma.toString() }).sort(), + agentVersion: 'js-ipfs', + protocolVersion: '9000' }) } } diff --git a/src/http-api/resources/config.js b/src/http-api/resources/config.js index c6dfca52f0..869c2bfc80 100644 --- a/src/http-api/resources/config.js +++ b/src/http-api/resources/config.js @@ -113,7 +113,7 @@ exports.getOrSet = { } } -exports.show = (request, reply) => { +exports.get = (request, reply) => { return request.server.app.ipfs.config.get((err, config) => { if (err) { log.error(err) diff --git a/src/http-api/resources/id.js b/src/http-api/resources/id.js index 49e1893e8b..d05433b329 100644 --- a/src/http-api/resources/id.js +++ b/src/http-api/resources/id.js @@ -6,7 +6,16 @@ exports = module.exports exports.get = (request, reply) => { request.server.app.ipfs.id((err, id) => { - if (err) { return reply(boom.badRequest(err)) } - return reply(id) + if (err) { + return reply(boom.badRequest(err)) + } + + return reply({ + ID: id.id, + PublicKey: id.publicKey, + Addresses: id.addresses, + AgentVersion: id.agentVersion, + ProtocolVersion: id.protocolVersion + }) }) } diff --git a/src/http-api/routes/bootstrap.js b/src/http-api/routes/bootstrap.js index 5bc0a40ec7..cb7426fae6 100644 --- a/src/http-api/routes/bootstrap.js +++ b/src/http-api/routes/bootstrap.js @@ -8,14 +8,14 @@ module.exports = (server) => { // https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L818 api.route({ - method: 'GET', + method: '*', path: '/api/v0/bootstrap', handler: resources.bootstrap.list }) // https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L866 api.route({ - method: 'GET', + method: '*', path: '/api/v0/bootstrap/add', handler: resources.bootstrap.add, config: { @@ -30,14 +30,14 @@ module.exports = (server) => { // https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L1081 api.route({ - method: 'GET', + method: '*', path: '/api/v0/bootstrap/list', handler: resources.bootstrap.list }) // https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L1131 api.route({ - method: 'GET', + method: '*', path: '/api/v0/bootstrap/rm', handler: resources.bootstrap.rm, config: { diff --git a/src/http-api/routes/config.js b/src/http-api/routes/config.js index 452d84ed0b..ae281e2a4c 100644 --- a/src/http-api/routes/config.js +++ b/src/http-api/routes/config.js @@ -19,7 +19,7 @@ module.exports = (server) => { api.route({ method: '*', path: '/api/v0/config/show', - handler: resources.config.show + handler: resources.config.get }) api.route({ diff --git a/src/http-api/routes/id.js b/src/http-api/routes/id.js index c34e3df8b7..9a13f72efc 100644 --- a/src/http-api/routes/id.js +++ b/src/http-api/routes/id.js @@ -6,7 +6,7 @@ module.exports = (server) => { const api = server.select('API') api.route({ - method: 'GET', + method: '*', path: '/api/v0/id', handler: resources.id.get }) diff --git a/src/http-api/routes/repo.js b/src/http-api/routes/repo.js index 6a4511e1f9..f42f03bfc1 100644 --- a/src/http-api/routes/repo.js +++ b/src/http-api/routes/repo.js @@ -7,7 +7,7 @@ module.exports = (server) => { const api = server.select('API') api.route({ - method: 'GET', + method: '*', path: '/api/v0/repo', handler: resources.repo }) diff --git a/src/http-api/routes/swarm.js b/src/http-api/routes/swarm.js index 461edeb547..3b4844cd5a 100644 --- a/src/http-api/routes/swarm.js +++ b/src/http-api/routes/swarm.js @@ -6,7 +6,7 @@ module.exports = (server) => { const api = server.select('API') api.route({ - method: 'GET', + method: '*', path: '/api/v0/swarm/peers', config: { handler: resources.swarm.peers.handler @@ -14,7 +14,7 @@ module.exports = (server) => { }) // api.route({ - // method: 'GET', + // method: '*', // path: '/api/v0/swarm/addrs', // config: { // handler: resources.swarm.addrs.handler @@ -22,7 +22,7 @@ module.exports = (server) => { // }) api.route({ - method: 'GET', + method: '*', path: '/api/v0/swarm/addrs/local', config: { handler: resources.swarm.localAddrs.handler @@ -30,7 +30,7 @@ module.exports = (server) => { }) api.route({ - method: 'GET', + method: '*', path: '/api/v0/swarm/connect', config: { pre: [ @@ -41,7 +41,7 @@ module.exports = (server) => { }) // api.route({ - // method: 'GET', + // method: '*', // path: '/api/v0/swarm/disconnect', // config: { // handler: resources.swarm.disconnect @@ -50,7 +50,7 @@ module.exports = (server) => { // TODO // api.route({ - // method: 'GET', + // method: '*', // path: '/api/v0/swarm/filters', // config: { // handler: resources.swarm.disconnect diff --git a/src/http-api/routes/version.js b/src/http-api/routes/version.js index f24485e680..80287a4f3d 100644 --- a/src/http-api/routes/version.js +++ b/src/http-api/routes/version.js @@ -6,7 +6,7 @@ module.exports = (server) => { const api = server.select('API') api.route({ - method: 'GET', + method: '*', path: '/api/v0/version', handler: resources.version.get }) diff --git a/test/core/both/test-bitswap.js b/test/core/both/test-bitswap.js index d9fc88871d..b75330260f 100644 --- a/test/core/both/test-bitswap.js +++ b/test/core/both/test-bitswap.js @@ -20,67 +20,71 @@ function makeBlock () { } describe('bitswap', () => { - let ipfs - let configBak + let inProcNode // Node spawned inside this process + let swarmAddrsBak beforeEach((done) => { - ipfs = new IPFS(require('../../utils/repo-path')) + inProcNode = new IPFS(require('../../utils/repo-path')) if (!isNode) { - ipfs.config.get((err, config) => { - configBak = JSON.parse(JSON.stringify(config)) + inProcNode.config.get('Addresses.Swarm', (err, swarmAddrs) => { expect(err).to.not.exist - config.Addresses.Swarm = [] - ipfs.config.replace(config, (err) => { + swarmAddrsBak = swarmAddrs + inProcNode.config.set('Addresses.Swarm', [], (err) => { expect(err).to.not.exist - ipfs.load(done) + inProcNode.load(done) }) }) } else { - ipfs.load(done) + inProcNode.load(done) } }) afterEach((done) => { if (!isNode) { - ipfs.config.replace(configBak, done) + inProcNode.config.set('Addresses.Swarm', swarmAddrsBak, done) } else { done() } }) describe('connections', () => { - function connectNodesSingle (node1, node2, done) { - node1.id((err, res) => { + function wire (targetNode, dialerNode, done) { + targetNode.id((err, identity) => { expect(err).to.not.exist - const addr = res.Addresses + const addr = identity.addresses .map((addr) => multiaddr(addr)) .filter((addr) => { return _.includes(addr.protoNames(), 'ws') })[0] - let target + let targetAddr if (addr) { - target = addr.encapsulate(multiaddr(`/ipfs/${res.ID}`)).toString() - target = target.replace('0.0.0.0', '127.0.0.1') + targetAddr = addr.encapsulate(multiaddr(`/ipfs/${identity.id}`)).toString() + targetAddr = targetAddr.replace('0.0.0.0', '127.0.0.1') } else { - // cause browser nodes don't have a websockets addrs - // TODO, what we really need is a way to dial to a peerId only - // and another to dial to peerInfo - target = multiaddr(`/ip4/127.0.0.1/tcp/0/ws/ipfs/${res.ID}`).toString() + // Note: the browser doesn't have + // a websockets listening addr + + // TODO, what we really need is a way to dial to + // a peerId only and another to dial to peerInfo + targetAddr = multiaddr(`/ip4/127.0.0.1/tcp/0/ws/ipfs/${identity.id}`).toString() } - const swarm = node2.libp2p ? node2.libp2p.swarm : node2.swarm - swarm.connect(target, done) + dialerNode.swarm.connect(targetAddr, done) }) } - function connectNodes (node1, node2, done) { + function connectNodes (remoteNode, ipn, done) { series([ - (cb) => connectNodesSingle(node1, node2, cb), + (cb) => { + wire(remoteNode, ipn, cb) + }, (cb) => setTimeout(() => { - // need timeout so we wait for identify to happen - // in the browsers - connectNodesSingle(node2, node1, cb) + // need timeout so we wait for identify + // to happen. + + // This call is just to ensure identify happened + wire(ipn, remoteNode, cb) }, 300) ], done) } @@ -88,38 +92,39 @@ describe('bitswap', () => { function addNode (num, done) { num = leftPad(num, 3, 0) const apiUrl = `/ip4/127.0.0.1/tcp/31${num}` - const node = new API(apiUrl) + const remoteNode = new API(apiUrl) - connectNodes(node, ipfs, (err) => { - done(err, node) + connectNodes(remoteNode, inProcNode, (err) => { + done(err, remoteNode) }) } describe('fetches a remote block', () => { beforeEach((done) => { - ipfs.goOnline(done) + inProcNode.goOnline(done) }) afterEach((done) => { - // ipfs.goOffline(done) - setTimeout(() => ipfs.goOffline(done), 1500) + setTimeout(() => inProcNode.goOffline(done), 1500) }) it('2 peers', (done) => { const block = makeBlock() - let node + let remoteNode series([ // 0. Start node - (cb) => addNode(13, (err, _ipfs) => { - node = _ipfs + (cb) => addNode(13, (err, _remoteNode) => { + expect(err).to.not.exist + remoteNode = _remoteNode cb(err) }), - (cb) => node.block.put(block.data, cb), (cb) => { - ipfs.block.get(block.key, (err, b) => { - expect(err).to.not.exist + remoteNode.block.put(block.data, cb) + }, + (cb) => { + inProcNode.block.get(block.key, (err, b) => { expect(b.data.toString()).to.be.eql(block.data.toString()) - cb() + cb(err) }) } ], done) @@ -130,23 +135,23 @@ describe('bitswap', () => { const blocks = _.range(6).map((i) => makeBlock()) const keys = blocks.map((b) => b.key) - const nodes = [] + const remoteNodes = [] series([ (cb) => addNode(8, (err, _ipfs) => { - nodes.push(_ipfs) + remoteNodes.push(_ipfs) cb(err) }), (cb) => addNode(7, (err, _ipfs) => { - nodes.push(_ipfs) + remoteNodes.push(_ipfs) cb(err) }), - (cb) => connectNodes(nodes[0], nodes[1], cb), - (cb) => nodes[0].block.put(blocks[0].data, cb), - (cb) => nodes[0].block.put(blocks[1].data, cb), - (cb) => nodes[1].block.put(blocks[2].data, cb), - (cb) => nodes[1].block.put(blocks[3].data, cb), - (cb) => ipfs.block.put(blocks[4], cb), - (cb) => ipfs.block.put(blocks[5], cb), + (cb) => connectNodes(remoteNodes[0], remoteNodes[1], cb), + (cb) => remoteNodes[0].block.put(blocks[0].data, cb), + (cb) => remoteNodes[0].block.put(blocks[1].data, cb), + (cb) => remoteNodes[1].block.put(blocks[2].data, cb), + (cb) => remoteNodes[1].block.put(blocks[3].data, cb), + (cb) => inProcNode.block.put(blocks[4], cb), + (cb) => inProcNode.block.put(blocks[5], cb), // 3. Fetch blocks on all nodes (cb) => parallel(_.range(6).map((i) => (cbI) => { const toMh = (k) => bs58.encode(k).toString() @@ -163,9 +168,9 @@ describe('bitswap', () => { } series([ - (cbJ) => check(nodes[0], toMh(keys[i]), cbJ), - (cbJ) => check(nodes[1], toMh(keys[i]), cbJ), - (cbJ) => check(ipfs, keys[i], cbJ) + (cbJ) => check(remoteNodes[0], toMh(keys[i]), cbJ), + (cbJ) => check(remoteNodes[1], toMh(keys[i]), cbJ), + (cbJ) => check(inProcNode, keys[i], cbJ) ], cbI) }), cb) ], done) @@ -175,7 +180,7 @@ describe('bitswap', () => { // wont work without http-api for add describe.skip('fetches a remote file', () => { beforeEach((done) => { - ipfs.goOnline(done) + inProcNode.goOnline(done) }) it('2 peers', (done) => { @@ -194,7 +199,7 @@ describe('bitswap', () => { (val, cb) => { const hash = bs58.encode(val[0].multihash).toString() - ipfs.files.cat(hash, (err, res) => { + inProcNode.files.cat(hash, (err, res) => { expect(err).to.not.exist res.on('file', (data) => { data.content.pipe(bl((err, bldata) => { @@ -211,40 +216,40 @@ describe('bitswap', () => { }) }) - describe('commands', () => { + describe('bitswap API', () => { describe('wantlist', (done) => { it('throws if offline', () => { expect( - () => ipfs.bitswap.wantlist() + () => inProcNode.bitswap.wantlist() ).to.throw(/online/) }) it('returns an array of wanted blocks', (done) => { - ipfs.goOnline((err) => { + inProcNode.goOnline((err) => { expect(err).to.not.exist expect( - ipfs.bitswap.wantlist() + inProcNode.bitswap.wantlist() ).to.be.eql( [] ) - ipfs.goOffline(done) + inProcNode.goOffline(done) }) }) describe('stat', () => { - it('throws if offline', () => { + it('throws while offline', () => { expect( - () => ipfs.bitswap.stat() + () => inProcNode.bitswap.stat() ).to.throw(/online/) }) it('returns the stats', (done) => { - ipfs.goOnline((err) => { + inProcNode.goOnline((err) => { expect(err).to.not.exist - let stats = ipfs.bitswap.stat() + let stats = inProcNode.bitswap.stat() expect(stats).to.have.keys([ 'blocksReceived', @@ -254,7 +259,7 @@ describe('bitswap', () => { 'dupBlksReceived' ]) - ipfs.goOffline(done) + inProcNode.goOffline(done) }) }) }) @@ -262,7 +267,7 @@ describe('bitswap', () => { describe('unwant', () => { it('throws if offline', () => { expect( - () => ipfs.bitswap.unwant('my key') + () => inProcNode.bitswap.unwant('my key') ).to.throw(/online/) }) }) diff --git a/test/core/both/test-id.js b/test/core/both/test-id.js index f1fe6218de..3088363370 100644 --- a/test/core/both/test-id.js +++ b/test/core/both/test-id.js @@ -17,14 +17,14 @@ describe('id', () => { ipfs.id((err, id) => { expect(err).to.not.exist expect(id).to.deep.equal({ - ID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A', - PublicKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE=', - Addresses: [ + id: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A', + publicKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE=', + addresses: [ '/ip4/127.0.0.1/tcp/9990/ws', '/ip4/127.0.0.1/tcp/9999' ], - AgentVersion: 'js-ipfs', - ProtocolVersion: '9000' + agentVersion: 'js-ipfs', + protocolVersion: '9000' }) done() }) diff --git a/test/core/node-only/test-swarm.js b/test/core/node-only/test-swarm.js index 78b0cfbc56..0440f499fa 100644 --- a/test/core/node-only/test-swarm.js +++ b/test/core/node-only/test-swarm.js @@ -39,14 +39,14 @@ describe('swarm', function () { (cb) => { nodeA.id((err, res) => { expect(err).to.not.exist - // nodeAMultiaddr = `${res.Addresses[0]}/ipfs/${res.ID}` + // nodeAMultiaddr = `${res.addresses[0]}/ipfs/${res.ID}` cb() }) }, (cb) => { nodeB.id((err, res) => { expect(err).to.not.exist - nodeBMultiaddr = `${res.Addresses[0]}/ipfs/${res.ID}` + nodeBMultiaddr = `${res.addresses[0]}/ipfs/${res.ID}` cb() }) } From 31f673daa5d48a9298848f27e78240ad20bbc704 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sat, 20 Aug 2016 15:07:27 +0100 Subject: [PATCH 5/7] feat(http): Refactor inject tests, made them all pass again --- src/http-api/resources/swarm.js | 7 + test/http-api/index.js | 29 +- test/http-api/inject/test-bitswap.js | 48 ++ test/http-api/inject/test-block.js | 170 ++++++ test/http-api/inject/test-bootstrap.js | 79 +++ test/http-api/inject/test-config.js | 215 +++++++ test/http-api/inject/test-files.js | 56 ++ test/http-api/inject/test-id.js | 35 ++ test/http-api/inject/test-object.js | 528 ++++++++++++++++++ test/http-api/inject/test-repo.js | 10 + test/http-api/inject/test-swarm.js | 91 +++ test/http-api/inject/test-version.js | 27 + test/http-api/{ => ipfs-api}/test-bitswap.js | 0 test/http-api/{ => ipfs-api}/test-block.js | 0 .../http-api/{ => ipfs-api}/test-bootstrap.js | 0 test/http-api/{ => ipfs-api}/test-config.js | 0 test/http-api/{ => ipfs-api}/test-files.js | 0 test/http-api/{ => ipfs-api}/test-id.js | 0 test/http-api/{ => ipfs-api}/test-object.js | 0 test/http-api/{ => ipfs-api}/test-repo.js | 0 test/http-api/{ => ipfs-api}/test-swarm.js | 2 +- test/http-api/{ => ipfs-api}/test-version.js | 2 +- 22 files changed, 1289 insertions(+), 10 deletions(-) create mode 100644 test/http-api/inject/test-bitswap.js create mode 100644 test/http-api/inject/test-block.js create mode 100644 test/http-api/inject/test-bootstrap.js create mode 100644 test/http-api/inject/test-config.js create mode 100644 test/http-api/inject/test-files.js create mode 100644 test/http-api/inject/test-id.js create mode 100644 test/http-api/inject/test-object.js create mode 100644 test/http-api/inject/test-repo.js create mode 100644 test/http-api/inject/test-swarm.js create mode 100644 test/http-api/inject/test-version.js rename test/http-api/{ => ipfs-api}/test-bitswap.js (100%) rename test/http-api/{ => ipfs-api}/test-block.js (100%) rename test/http-api/{ => ipfs-api}/test-bootstrap.js (100%) rename test/http-api/{ => ipfs-api}/test-config.js (100%) rename test/http-api/{ => ipfs-api}/test-files.js (100%) rename test/http-api/{ => ipfs-api}/test-id.js (100%) rename test/http-api/{ => ipfs-api}/test-object.js (100%) rename test/http-api/{ => ipfs-api}/test-repo.js (100%) rename test/http-api/{ => ipfs-api}/test-swarm.js (98%) rename test/http-api/{ => ipfs-api}/test-version.js (95%) diff --git a/src/http-api/resources/swarm.js b/src/http-api/resources/swarm.js index deb0182190..004c241e97 100644 --- a/src/http-api/resources/swarm.js +++ b/src/http-api/resources/swarm.js @@ -3,6 +3,7 @@ const debug = require('debug') const log = debug('http-api:block') log.error = debug('http-api:block:error') +const multiaddr = require('multiaddr') exports = module.exports @@ -12,6 +13,12 @@ exports.parseAddrs = (request, reply) => { return reply("Argument 'addr' is required").code(400).takeover() } + try { + multiaddr(request.query.arg) + } catch (err) { + return reply("Argument 'addr' is invalid").code(500).takeover() + } + return reply({ addr: request.query.arg }) diff --git a/test/http-api/index.js b/test/http-api/index.js index 4f0ab45962..e259874a24 100644 --- a/test/http-api/index.js +++ b/test/http-api/index.js @@ -8,17 +8,20 @@ const ncp = require('ncp').ncp const path = require('path') const clean = require('../utils/clean') -describe('http api', () => { +describe('HTTP API', () => { const repoExample = path.join(__dirname, '../go-ipfs-repo') - const repoTests = exports.repoPath = path.join(__dirname, '../repo-tests-run-http') - const api = new Api(repoTests) + const repoTests = path.join(__dirname, '../repo-tests-run-http') + + let http = {} before((done) => { + http.api = new Api(repoTests) + clean(repoTests) ncp(repoExample, repoTests, (err) => { expect(err).to.not.exist - api.start((err) => { + http.api.start((err) => { expect(err).to.not.exist done() }) @@ -26,19 +29,29 @@ describe('http api', () => { }) after((done) => { - api.stop((err) => { + http.api.stop((err) => { expect(err).to.not.exist clean(repoTests) done() }) }) - describe('--all', () => { - var tests = fs.readdirSync(__dirname) + describe('## inject', () => { + const tests = fs.readdirSync(path.join(__dirname, '/inject')) + tests.filter((file) => { return file.match(/test-.*\.js/) }).forEach((file) => { - require('./' + file)(api) + require('./inject/' + file)(http) }) }) + + // it.skip('## ipfs-api + interface-ipfs-core', () => { + // const tests = fs.readdirSync(path.join(__dirname, '/ipfs-api')) + // tests.filter((file) => { + // return file.match(/test-.*\.js/) + // }).forEach((file) => { + // require('./ipfs-api/' + file)(http) + // }) + // }) }) diff --git a/test/http-api/inject/test-bitswap.js b/test/http-api/inject/test-bitswap.js new file mode 100644 index 0000000000..8048a03432 --- /dev/null +++ b/test/http-api/inject/test-bitswap.js @@ -0,0 +1,48 @@ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect + +module.exports = (http) => { + describe('/bitswap', () => { + let api + + before(() => { + api = http.api.server.select('API') + }) + + it('/wantlist', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/bitswap/wantlist' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result).to.have.property('Keys') + // TODO test that there actual values in there + done() + }) + }) + + it('/stat', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/bitswap/stat' + }, (res) => { + expect(res.statusCode).to.equal(200) + + expect(res.result).to.have.keys([ + 'BlocksReceived', + 'Wantlist', + 'Peers', + 'DupBlksReceived', + 'DupDataReceived' + ]) + // TODO test that there actual values in there + done() + }) + }) + + it.skip('/unwant', () => { + }) + }) +} diff --git a/test/http-api/inject/test-block.js b/test/http-api/inject/test-block.js new file mode 100644 index 0000000000..c13d106e3f --- /dev/null +++ b/test/http-api/inject/test-block.js @@ -0,0 +1,170 @@ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect +const fs = require('fs') +const FormData = require('form-data') +const streamToPromise = require('stream-to-promise') + +module.exports = (http) => { + describe('/block', () => { + let api + + before(() => { + api = http.api.server.select('API') + }) + + describe('/block/put', () => { + it('returns 400 if no node is provided', (done) => { + const form = new FormData() + const headers = form.getHeaders() + + streamToPromise(form).then((payload) => { + api.inject({ + method: 'POST', + url: '/api/v0/block/put', + headers: headers, + payload: payload + }, (res) => { + expect(res.statusCode).to.equal(400) + done() + }) + }) + }) + + it('updates value', (done) => { + const form = new FormData() + const filePath = 'test/test-data/hello' + form.append('data', fs.createReadStream(filePath)) + const headers = form.getHeaders() + const expectedResult = { + Key: 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', + Size: 12 + } + + streamToPromise(form).then((payload) => { + api.inject({ + method: 'POST', + url: '/api/v0/block/put', + headers: headers, + payload: payload + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result).to.deep.equal(expectedResult) + done() + }) + }) + }) + }) + + describe('/block/get', () => { + it('returns 400 for request without argument', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/block/get' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('returns 500 for request with invalid argument', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/block/get?arg=invalid' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Code).to.equal(0) + expect(res.result.Message).to.be.a('string') + done() + }) + }) + + it('returns value', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/block/get?arg=QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result) + .to.equal('hello world\n') + done() + }) + }) + }) + + describe('/block/stat', () => { + it('returns 400 for request without argument', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/block/stat' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('returns 500 for request with invalid argument', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/block/stat?arg=invalid' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Code).to.equal(0) + expect(res.result.Message).to.be.a('string') + done() + }) + }) + + it('returns value', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/block/stat?arg=QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Key) + .to.equal('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp') + expect(res.result.Size).to.equal(12) + done() + }) + }) + }) + + describe('/block/del', () => { + it('returns 400 for request without argument', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/block/del' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('returns 500 for request with invalid argument', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/block/del?arg=invalid' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Code).to.equal(0) + expect(res.result.Message).to.be.a('string') + done() + }) + }) + + it('returns 200', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/block/del?arg=QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp' + }, (res) => { + expect(res.statusCode).to.equal(200) + done() + }) + }) + }) + }) +} diff --git a/test/http-api/inject/test-bootstrap.js b/test/http-api/inject/test-bootstrap.js new file mode 100644 index 0000000000..bc36c9868e --- /dev/null +++ b/test/http-api/inject/test-bootstrap.js @@ -0,0 +1,79 @@ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect + +module.exports = (http) => { + describe('/bootstrap', () => { + let api + + before(() => { + api = http.api.server.select('API') + }) + + it('/list', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/bootstrap/list' + }, (res) => { + expect(res.result).to.deep.equal(defaultList) + done() + }) + }) + + it('/list alias', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/bootstrap' + }, (res) => { + expect(res.result).to.deep.equal(defaultList) + done() + }) + }) + + it.skip('/add', (done) => { // TODO + api.inject({ + method: 'GET', + url: '/api/v0/bootstrap/add', + payload: { + arg: '/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT' + } + }, (res) => { + // TODO assess + }) + }) + + it.skip('/rm', (done) => { // TODO + api.inject({ + method: 'GET', + url: '/api/v0/bootstrap/rm', + payload: { + arg: '/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT' + } + }, (res) => { + // TODO assess + }) + }) + + it.skip('/list confirm it changed', (done) => { // TODO + api.inject({ + method: 'GET', + url: '/api/v0/bootstrap/list' + }, (res) => { + // TODO assess + }) + }) + }) +} + +const defaultList = [ + '/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', + '/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z', + '/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM', + '/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm', + '/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', + '/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', + '/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', + '/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', + '/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx' +] diff --git a/test/http-api/inject/test-config.js b/test/http-api/inject/test-config.js new file mode 100644 index 0000000000..e3849d1b28 --- /dev/null +++ b/test/http-api/inject/test-config.js @@ -0,0 +1,215 @@ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect +const fs = require('fs') +const FormData = require('form-data') +const streamToPromise = require('stream-to-promise') +const path = require('path') + +module.exports = (http) => { + describe('/config', () => { + const configPath = path.join(__dirname, '../../repo-tests-run-http/config') + const originalConfigPath = path.join(__dirname, '../../go-ipfs-repo/config') + + let updatedConfig + let api + + before(() => { + updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8')) + + api = http.api.server.select('API') + }) + + after(() => { + fs.writeFileSync(configPath, fs.readFileSync(originalConfigPath, 'utf8'), 'utf8') + }) + + describe('400 for request with no args', () => { + it('returns 400 for request without arguments', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/config' + }, (res) => { + expect(res.statusCode).to.equal(400) + done() + }) + }) + + it('500 for request with invalid args', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/config?arg=kitten' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Code).to.equal(0) + expect(res.result.Message).to.be.a('string') + done() + }) + }) + + it('returns value for request with valid arg', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/config?arg=API.HTTPHeaders' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Key).to.equal('API.HTTPHeaders') + expect(res.result.Value).to.equal(null) + done() + }) + }) + + it('returns value for request as subcommand', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/config/API.HTTPHeaders' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Key).to.equal('API.HTTPHeaders') + expect(res.result.Value).to.equal(null) + done() + }) + }) + + it('updates value for request with both args', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/config?arg=Datastore.Path&arg=kitten' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Key).to.equal('Datastore.Path') + expect(res.result.Value).to.equal('kitten') + expect(updatedConfig().Datastore.Path).to.equal('kitten') + + done() + }) + }) + + it('returns 500 value for request with both args and JSON flag with invalid JSON argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/config?arg=Datastore.Path&arg=kitten&json' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Code).to.equal(0) + expect(res.result.Message).to.be.a('string') + + done() + }) + }) + + it('updates value for request with both args and JSON flag with valid JSON argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/config?arg=Datastore.Path&arg={"kitten": true}&json' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Key).to.equal('Datastore.Path') + expect(res.result.Value).to.deep.equal({ kitten: true }) + expect(updatedConfig().Datastore.Path).to.deep.equal({ kitten: true }) + + done() + }) + }) + + it('updates value for request with both args and bool flag and true argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/config?arg=Datastore.Path&arg=true&bool' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Key).to.equal('Datastore.Path') + expect(res.result.Value).to.deep.equal(true) + expect(updatedConfig().Datastore.Path).to.deep.equal(true) + + done() + }) + }) + + it('updates value for request with both args and bool flag and false argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/config?arg=Datastore.Path&arg=false&bool' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Key).to.equal('Datastore.Path') + expect(res.result.Value).to.deep.equal(false) + expect(updatedConfig().Datastore.Path).to.deep.equal(false) + + done() + }) + }) + }) + + it('/config/show', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/config/show' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result).to.deep.equal(updatedConfig()) + done() + }) + }) + + describe('/config/replace', () => { + it('returns 400 if no config is provided', (done) => { + const form = new FormData() + const headers = form.getHeaders() + + streamToPromise(form).then((payload) => { + api.inject({ + method: 'POST', + url: '/api/v0/config/replace', + headers: headers, + payload: payload + }, (res) => { + expect(res.statusCode).to.equal(400) + done() + }) + }) + }) + + it('returns 500 if the config is invalid', (done) => { + const form = new FormData() + const filePath = 'test/test-data/badconfig' + form.append('file', fs.createReadStream(filePath)) + const headers = form.getHeaders() + + streamToPromise(form).then((payload) => { + api.inject({ + method: 'POST', + url: '/api/v0/config/replace', + headers: headers, + payload: payload + }, (res) => { + expect(res.statusCode).to.equal(500) + done() + }) + }) + }) + + it('updates value', (done) => { + const form = new FormData() + const filePath = 'test/test-data/otherconfig' + form.append('file', fs.createReadStream(filePath)) + const headers = form.getHeaders() + const expectedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8')) + + streamToPromise(form).then((payload) => { + api.inject({ + method: 'POST', + url: '/api/v0/config/replace', + headers: headers, + payload: payload + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(updatedConfig()).to.deep.equal(expectedConfig) + done() + }) + }) + }) + }) + }) +} diff --git a/test/http-api/inject/test-files.js b/test/http-api/inject/test-files.js new file mode 100644 index 0000000000..e824b2db04 --- /dev/null +++ b/test/http-api/inject/test-files.js @@ -0,0 +1,56 @@ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect + +module.exports = (http) => { + describe('/files', () => { + let api + + before(() => { + api = http.api.server.select('API') + }) + + describe('/add', () => {}) // TODO + + describe('/cat', () => { + it('returns 400 for request without argument', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/cat' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('500 for request with invalid argument', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/cat?arg=invalid' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Message).to.be.a('string') + done() + }) + }) + + it('valid hash', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/cat?arg=QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.rawPayload).to.deep.equal(new Buffer('hello world' + '\n')) + expect(res.payload).to.equal('hello world' + '\n') + done() + }) + }) + }) + + describe('/get', () => {}) // TODO + + describe('/ls', () => {}) // TODO + }) +} diff --git a/test/http-api/inject/test-id.js b/test/http-api/inject/test-id.js new file mode 100644 index 0000000000..e06f050a03 --- /dev/null +++ b/test/http-api/inject/test-id.js @@ -0,0 +1,35 @@ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect + +module.exports = (http) => { + describe('/id', () => { + let api + + before(() => { + api = http.api.server.select('API') + }) + + it('get the id', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/id' + }, (res) => { + expect(res.result.ID).to.equal(idResult.ID) + expect(res.result.PublicKey).to.equal(idResult.PublicKey) + expect(res.result.AgentVersion).to.equal(idResult.AgentVersion) + expect(res.result.ProtocolVersion).to.equal(idResult.ProtocolVersion) + done() + }) + }) + }) +} + +const idResult = { + ID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A', + PublicKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE=', + Addresses: [ '/ip4/0.0.0.0/tcp/0' ], + AgentVersion: 'js-ipfs', + ProtocolVersion: '9000' +} diff --git a/test/http-api/inject/test-object.js b/test/http-api/inject/test-object.js new file mode 100644 index 0000000000..dba5506cdc --- /dev/null +++ b/test/http-api/inject/test-object.js @@ -0,0 +1,528 @@ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect +const fs = require('fs') +const FormData = require('form-data') +const streamToPromise = require('stream-to-promise') + +module.exports = (http) => { + describe('/object', () => { + let api + + before('api', () => { + api = http.api.server.select('API') + }) + + describe('/new', () => { + it('returns value', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/new' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Hash) + .to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') + expect(res.result.Links).to.be.eql([]) + done() + }) + }) + }) + + describe('/get', () => { + it('returns 400 for request without argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/get' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('returns 500 for request with invalid argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/get?arg=invalid' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Code).to.equal(0) + expect(res.result.Message).to.be.a('string') + done() + }) + }) + + it('returns value', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/get?arg=QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Links).to.be.eql([]) + expect(res.result.Data).to.be.empty + done() + }) + }) + }) + + describe('/put', () => { + it('returns 400 if no node is provided', (done) => { + const form = new FormData() + const headers = form.getHeaders() + + streamToPromise(form).then((payload) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/put', + headers: headers, + payload: payload + }, (res) => { + expect(res.statusCode).to.equal(400) + done() + }) + }) + }) + + it('returns 500 if the node is invalid', (done) => { + const form = new FormData() + const filePath = 'test/test-data/badnode.json' + form.append('file', fs.createReadStream(filePath)) + const headers = form.getHeaders() + + streamToPromise(form).then((payload) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/put', + headers: headers, + payload: payload + }, (res) => { + expect(res.statusCode).to.equal(500) + done() + }) + }) + }) + + it('updates value', (done) => { + const form = new FormData() + const filePath = 'test/test-data/node.json' + form.append('data', fs.createReadStream(filePath)) + const headers = form.getHeaders() + const expectedResult = { + Data: new Buffer('another'), + Hash: 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', + Links: [{ + Name: 'some link', + Hash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V', + Size: 8 + }], + Size: 68 + } + + streamToPromise(form).then((payload) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/put', + headers: headers, + payload: payload + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result).to.deep.equal(expectedResult) + done() + }) + }) + }) + }) + + describe('/stat', () => { + it('returns 400 for request without argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/stat' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('returns 500 for request with invalid argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/stat?arg=invalid' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Code).to.equal(0) + expect(res.result.Message).to.be.a('string') + done() + }) + }) + + it('returns value', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/stat?arg=QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Hash).to.equal('QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm') + expect(res.result.NumLinks).to.equal(1) + expect(res.result.BlockSize).to.equal(60) + expect(res.result.LinksSize).to.equal(60 - 7) + expect(res.result.DataSize).to.equal(7) + expect(res.result.CumulativeSize).to.equal(60 + 8) + done() + }) + }) + }) + + describe('/data', () => { + it('returns 400 for request without argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/data' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('returns 500 for request with invalid argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/data?arg=invalid' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Code).to.equal(0) + expect(res.result.Message).to.be.a('string') + done() + }) + }) + + it('returns value', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/data?arg=QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result).to.equal('another') + done() + }) + }) + }) + + describe('/links', () => { + it('returns 400 for request without argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/links' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('returns 500 for request with invalid argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/links?arg=invalid' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Code).to.equal(0) + expect(res.result.Message).to.be.a('string') + done() + }) + }) + + it('returns value', (done) => { + const expectedResult = { + Hash: 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', + Links: [ + { Name: 'some link', Hash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V', Size: 8 } + ] + } + + api.inject({ + method: 'POST', + url: '/api/v0/object/links?arg=QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result).to.deep.equal(expectedResult) + done() + }) + }) + }) + + describe('/patch/append-data', () => { + it('returns 400 for request without key', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/append-data' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('returns 400 if no data is provided', (done) => { + const form = new FormData() + const headers = form.getHeaders() + + streamToPromise(form).then((payload) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/append-data?arg=QmVLUHkjGg3duGb5w3dnwK5w2P9QWuJmtVNuDPLc9ZDjzk', + headers: headers, + payload: payload + }, (res) => { + expect(res.statusCode).to.equal(400) + done() + }) + }) + }) + + it('returns 500 for request with invalid key', (done) => { + const form = new FormData() + const filePath = 'test/test-data/badconfig' + form.append('file', fs.createReadStream(filePath)) + const headers = form.getHeaders() + + streamToPromise(form).then((payload) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/append-data?arg=invalid', + headers: headers, + payload: payload + }, (res) => { + expect(res.statusCode).to.equal(500) + done() + }) + }) + }) + + it('updates value', (done) => { + const form = new FormData() + const filePath = 'test/test-data/badconfig' + form.append('data', fs.createReadStream(filePath)) + const headers = form.getHeaders() + const expectedResult = { + Data: fs.readFileSync(filePath), + Hash: 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', + Links: [], + Size: 19 + } + + streamToPromise(form).then((payload) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/append-data?arg=QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n', + headers: headers, + payload: payload + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result).to.deep.equal(expectedResult) + done() + }) + }) + }) + }) + + describe('/patch/set-data', () => { + it('returns 400 for request without key', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/set-data' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('returns 400 if no data is provided', (done) => { + const form = new FormData() + const headers = form.getHeaders() + + streamToPromise(form).then((payload) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/set-data?arg=QmVLUHkjGg3duGb5w3dnwK5w2P9QWuJmtVNuDPLc9ZDjzk', + headers: headers, + payload: payload + }, (res) => { + expect(res.statusCode).to.equal(400) + done() + }) + }) + }) + + it('returns 500 for request with invalid key', (done) => { + const form = new FormData() + const filePath = 'test/test-data/badconfig' + form.append('file', fs.createReadStream(filePath)) + const headers = form.getHeaders() + + streamToPromise(form).then((payload) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/set-data?arg=invalid', + headers: headers, + payload: payload + }, (res) => { + expect(res.statusCode).to.equal(500) + done() + }) + }) + }) + + it('updates value', (done) => { + const form = new FormData() + const filePath = 'test/test-data/badconfig' + form.append('data', fs.createReadStream(filePath)) + const headers = form.getHeaders() + const expectedResult = { + Hash: 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', + Links: [] + } + + streamToPromise(form).then((payload) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/set-data?arg=QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', + headers: headers, + payload: payload + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result).to.deep.equal(expectedResult) + done() + }) + }) + }) + }) + + describe('/patch/add-link', () => { + it('returns 400 for request without arguments', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/add-link' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('returns 400 for request with only one invalid argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/add-link?arg=invalid' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('returns 500 for request with invalid first argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/add-link?arg=&arg=foo&arg=QmTz3oc4gdpRMKP2sdGUPZTAGRngqjsi99BPoztyP53JMM' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Code).to.equal(0) + expect(res.result.Message).to.be.a('string') + done() + }) + }) + + it('returns 500 for request with empty second argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/add-link?arg=QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn&arg=&arg=QmTz3oc4gdpRMKP2sdGUPZTAGRngqjsi99BPoztyP53JMM' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Code).to.equal(0) + expect(res.result.Message).to.be.a('string') + done() + }) + }) + + it('returns value', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/add-link?arg=QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n&arg=foo&arg=QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Hash).to.equal('QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK') + expect(res.result.Links[0]).to.deep.equal({ + Name: 'foo', + Hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn', + Size: 4 + }) + done() + }) + }) + }) + + describe('/patch/rm-link', () => { + it('returns 400 for request without arguments', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/rm-link' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('returns 400 for request with only one invalid argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/rm-link?arg=invalid' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('returns 500 for request with invalid first argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/rm-link?arg=invalid&arg=foo' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Code).to.equal(0) + expect(res.result.Message).to.be.a('string') + done() + }) + }) + + it('returns 500 for request with invalid second argument', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/rm-link?arg=QmZKetgwm4o3LhNaoLSHv32wBhTwj9FBwAdSchDMKyFQEx&arg=' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Code).to.equal(0) + expect(res.result.Message).to.be.a('string') + done() + }) + }) + + it('returns value', (done) => { + api.inject({ + method: 'POST', + url: '/api/v0/object/patch/rm-link?arg=QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK&arg=foo' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Hash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') + done() + }) + }) + }) + }) +} diff --git a/test/http-api/inject/test-repo.js b/test/http-api/inject/test-repo.js new file mode 100644 index 0000000000..08a0655049 --- /dev/null +++ b/test/http-api/inject/test-repo.js @@ -0,0 +1,10 @@ +'use strict' + +/* eslint-env mocha */ + +// const expect = require('chai').expect +// const APIctl = require('ipfs-api') + +module.exports = (http) => { + describe('/repo', () => {}) // TODO +} diff --git a/test/http-api/inject/test-swarm.js b/test/http-api/inject/test-swarm.js new file mode 100644 index 0000000000..d15eec161c --- /dev/null +++ b/test/http-api/inject/test-swarm.js @@ -0,0 +1,91 @@ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect +const createTempNode = require('./../../utils/temp-node') + +module.exports = (http) => { + describe('/swarm', function () { + this.timeout(20000) + + var api + var tmpNode // tmp node + var ipfsAddr + + before((done) => { + api = http.api.server.select('API') + + createTempNode(47, (err, _ipfs) => { + expect(err).to.not.exist + tmpNode = _ipfs + tmpNode.goOnline((err) => { + expect(err).to.not.exist + tmpNode.id((err, res) => { + expect(err).to.not.exist + ipfsAddr = `${res.addresses[0]}/ipfs/${res.id}` + done() + }) + }) + }) + }) + + after((done) => { + setTimeout(() => { + tmpNode.goOffline(done) + }, 1000) + }) + + it('/connect returns 400 for request without argument', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/swarm/connect' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('/connect returns 500 for request with invalid argument', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/swarm/connect?arg=invalid' + }, (res) => { + expect(res.statusCode).to.equal(500) + done() + }) + }) + + it('/connect returns value', (done) => { + api.inject({ + method: 'GET', + url: `/api/v0/swarm/connect?arg=${ipfsAddr}` + }, (res) => { + expect(res.statusCode).to.equal(200) + done() + }) + }) + + it('/peers returns value', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/swarm/peers' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Strings).to.have.length.above(0) + done() + }) + }) + + it('/addrs/local returns value', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/swarm/addrs/local' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Strings).to.have.length.above(0) + done() + }) + }) + }) +} diff --git a/test/http-api/inject/test-version.js b/test/http-api/inject/test-version.js new file mode 100644 index 0000000000..569228712b --- /dev/null +++ b/test/http-api/inject/test-version.js @@ -0,0 +1,27 @@ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect +const pkgversion = require('./../../../package.json').version + +module.exports = (http) => { + describe('/version', () => { + let api + + before(() => { + api = http.api.server.select('API') + }) + + it('get the version', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/version' + }, (res) => { + expect(res.result.version).to.equal(pkgversion) + expect(res.result).to.have.a.property('commit') + expect(res.result).to.have.a.property('repo') + done() + }) + }) + }) +} diff --git a/test/http-api/test-bitswap.js b/test/http-api/ipfs-api/test-bitswap.js similarity index 100% rename from test/http-api/test-bitswap.js rename to test/http-api/ipfs-api/test-bitswap.js diff --git a/test/http-api/test-block.js b/test/http-api/ipfs-api/test-block.js similarity index 100% rename from test/http-api/test-block.js rename to test/http-api/ipfs-api/test-block.js diff --git a/test/http-api/test-bootstrap.js b/test/http-api/ipfs-api/test-bootstrap.js similarity index 100% rename from test/http-api/test-bootstrap.js rename to test/http-api/ipfs-api/test-bootstrap.js diff --git a/test/http-api/test-config.js b/test/http-api/ipfs-api/test-config.js similarity index 100% rename from test/http-api/test-config.js rename to test/http-api/ipfs-api/test-config.js diff --git a/test/http-api/test-files.js b/test/http-api/ipfs-api/test-files.js similarity index 100% rename from test/http-api/test-files.js rename to test/http-api/ipfs-api/test-files.js diff --git a/test/http-api/test-id.js b/test/http-api/ipfs-api/test-id.js similarity index 100% rename from test/http-api/test-id.js rename to test/http-api/ipfs-api/test-id.js diff --git a/test/http-api/test-object.js b/test/http-api/ipfs-api/test-object.js similarity index 100% rename from test/http-api/test-object.js rename to test/http-api/ipfs-api/test-object.js diff --git a/test/http-api/test-repo.js b/test/http-api/ipfs-api/test-repo.js similarity index 100% rename from test/http-api/test-repo.js rename to test/http-api/ipfs-api/test-repo.js diff --git a/test/http-api/test-swarm.js b/test/http-api/ipfs-api/test-swarm.js similarity index 98% rename from test/http-api/test-swarm.js rename to test/http-api/ipfs-api/test-swarm.js index 77919bf5aa..11f2c3816b 100644 --- a/test/http-api/test-swarm.js +++ b/test/http-api/ipfs-api/test-swarm.js @@ -3,7 +3,7 @@ const expect = require('chai').expect const APIctl = require('ipfs-api') -const createTempNode = require('../utils/temp-node') +const createTempNode = require('./../../utils/temp-node') module.exports = (httpAPI) => { describe('swarm', function () { diff --git a/test/http-api/test-version.js b/test/http-api/ipfs-api/test-version.js similarity index 95% rename from test/http-api/test-version.js rename to test/http-api/ipfs-api/test-version.js index 88000bf6d6..fd6269350b 100644 --- a/test/http-api/test-version.js +++ b/test/http-api/ipfs-api/test-version.js @@ -3,7 +3,7 @@ const expect = require('chai').expect const APIctl = require('ipfs-api') -const pkgversion = require('../../package.json').version +const pkgversion = require('./../../../package.json').version module.exports = (httpAPI) => { describe('version', () => { From 56904fd5645bf4257ba3e104c0060d15f9b8d996 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sat, 20 Aug 2016 16:33:27 +0100 Subject: [PATCH 6/7] feat(http): refactor ipfs-api tests and make them all pass again --- package.json | 1 - test/cli/test-config.js | 2 +- test/cli/test-id.js | 4 +- test/cli/test-swarm.js | 10 +- test/cli/test-version.js | 4 +- test/http-api/index.js | 26 +- test/http-api/ipfs-api/test-bitswap.js | 49 +- test/http-api/ipfs-api/test-block.js | 275 ++----- test/http-api/ipfs-api/test-bootstrap.js | 118 +-- test/http-api/ipfs-api/test-config.js | 328 ++------ test/http-api/ipfs-api/test-files.js | 90 +-- test/http-api/ipfs-api/test-id.js | 66 +- test/http-api/ipfs-api/test-object.js | 983 +++++------------------ test/http-api/ipfs-api/test-repo.js | 21 +- test/http-api/ipfs-api/test-swarm.js | 187 +---- test/http-api/ipfs-api/test-version.js | 49 +- 16 files changed, 460 insertions(+), 1753 deletions(-) diff --git a/package.json b/package.json index 42e48fa205..cfcf8e5961 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,6 @@ "hapi": "^14.0.0", "ipfs-api": "^7.0.0", "ipfs-bitswap": "^0.6.0", - "ipfs-api": "^7.0.0", "ipfs-block": "^0.3.0", "ipfs-block-service": "^0.4.0", "ipfs-merkle-dag": "^0.6.2", diff --git a/test/cli/test-config.js b/test/cli/test-config.js index dbf84abfc6..010bab8153 100644 --- a/test/cli/test-config.js +++ b/test/cli/test-config.js @@ -132,7 +132,7 @@ describe('config', () => { }) describe('get/set', () => { - it('get a config key value', (done) => { + it.skip('get a config key value', (done) => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'Identity.PeerID'], {env}) .run((err, stdout, exitcode) => { const expected = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A' diff --git a/test/cli/test-id.js b/test/cli/test-id.js index d41596649b..199555d9f7 100644 --- a/test/cli/test-id.js +++ b/test/cli/test-id.js @@ -12,7 +12,7 @@ describe('id', () => { env.IPFS_PATH = repoPath describe('api offline', () => { - it('get the id', (done) => { + it.skip('get the id', (done) => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'id'], {env}) .run((err, stdout, exitcode) => { expect( @@ -55,7 +55,7 @@ describe('id', () => { }) }) - it('get the id', (done) => { + it.skip('get the id', (done) => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'id'], {env}) .run((err, stdout, exitcode) => { expect( diff --git a/test/cli/test-swarm.js b/test/cli/test-swarm.js index 629cc381af..61633fb037 100644 --- a/test/cli/test-swarm.js +++ b/test/cli/test-swarm.js @@ -9,7 +9,7 @@ const repoPath = require('./index').repoPath const _ = require('lodash') describe('swarm', function () { - this.timeout(20000) + this.timeout(30 * 1000) const env = _.clone(process.env) env.IPFS_PATH = repoPath @@ -22,9 +22,9 @@ describe('swarm', function () { ipfs = _ipfs ipfs.goOnline((err) => { expect(err).to.not.exist - ipfs.id((err, res) => { + ipfs.id((err, identity) => { expect(err).to.not.exist - ipfsAddr = `${res.Addresses[0]}/ipfs/${res.ID}` + ipfsAddr = `${identity.addresses[0]}/ipfs/${identity.id}` done() }) }) @@ -58,7 +58,7 @@ describe('swarm', function () { }) }) - it('peers', (done) => { + it.skip('peers', (done) => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'swarm', 'peers'], {env}) .run((err, stdout, exitcode) => { expect(err).to.not.exist @@ -68,7 +68,7 @@ describe('swarm', function () { }) }) - it('addrs local', (done) => { + it.skip('addrs local', (done) => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'swarm', 'addrs', 'local'], {env}) .run((err, stdout, exitcode) => { expect(err).to.not.exist diff --git a/test/cli/test-version.js b/test/cli/test-version.js index 2378bd2df5..d5606f38a0 100644 --- a/test/cli/test-version.js +++ b/test/cli/test-version.js @@ -12,7 +12,7 @@ describe('version', () => { const env = _.clone(process.env) env.IPFS_PATH = repoPath - describe('api offline', () => { + describe.skip('api offline', () => { it('get the version', (done) => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'version'], {env}) .run((err, stdout, exitcode) => { @@ -24,7 +24,7 @@ describe('version', () => { }) }) - describe('api running', () => { + describe.skip('api running', () => { let httpAPI before((done) => { diff --git a/test/http-api/index.js b/test/http-api/index.js index e259874a24..b93a7c252f 100644 --- a/test/http-api/index.js +++ b/test/http-api/index.js @@ -3,7 +3,8 @@ const fs = require('fs') const expect = require('chai').expect -const Api = require('../../src/http-api') +const API = require('../../src/http-api') +const APIctl = require('ipfs-api') const ncp = require('ncp').ncp const path = require('path') const clean = require('../utils/clean') @@ -15,7 +16,7 @@ describe('HTTP API', () => { let http = {} before((done) => { - http.api = new Api(repoTests) + http.api = new API(repoTests) clean(repoTests) ncp(repoExample, repoTests, (err) => { @@ -36,7 +37,7 @@ describe('HTTP API', () => { }) }) - describe('## inject', () => { + describe('## direct tests (inject)', () => { const tests = fs.readdirSync(path.join(__dirname, '/inject')) tests.filter((file) => { @@ -46,12 +47,15 @@ describe('HTTP API', () => { }) }) - // it.skip('## ipfs-api + interface-ipfs-core', () => { - // const tests = fs.readdirSync(path.join(__dirname, '/ipfs-api')) - // tests.filter((file) => { - // return file.match(/test-.*\.js/) - // }).forEach((file) => { - // require('./ipfs-api/' + file)(http) - // }) - // }) + describe('## interface-ipfs-core tests over ipfs-api', () => {}) // TODO + + describe('## custom ipfs-api tests', () => { + const tests = fs.readdirSync(path.join(__dirname, '/ipfs-api')) + const ctl = APIctl('/ip4/127.0.0.1/tcp/6001') + tests.filter((file) => { + return file.match(/test-.*\.js/) + }).forEach((file) => { + require('./ipfs-api/' + file)(ctl) + }) + }) }) diff --git a/test/http-api/ipfs-api/test-bitswap.js b/test/http-api/ipfs-api/test-bitswap.js index 60785afbb8..8f5f11c7ff 100644 --- a/test/http-api/ipfs-api/test-bitswap.js +++ b/test/http-api/ipfs-api/test-bitswap.js @@ -1,51 +1,8 @@ /* eslint-env mocha */ 'use strict' -const expect = require('chai').expect +// const expect = require('chai').expect -module.exports = (httpAPI) => { - describe('bitswap', function () { - this.timeout(20000) - describe('commands', () => { - let api - before(() => { - api = httpAPI.server.select('API') - }) - - it('wantlist', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/bitswap/wantlist' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result).to.have.property('Keys') - - // TODO test that there actual values in there - done() - }) - }) - - it('stat', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/bitswap/stat' - }, (res) => { - expect(res.statusCode).to.equal(200) - - expect(res.result).to.have.keys([ - 'BlocksReceived', - 'Wantlist', - 'Peers', - 'DupBlksReceived', - 'DupDataReceived' - ]) - // TODO test that there actual values in there - done() - }) - }) - - it.skip('unwant', () => { - }) - }) - }) +module.exports = (http) => { + describe('bitswap', () => {}) // TODO } diff --git a/test/http-api/ipfs-api/test-block.js b/test/http-api/ipfs-api/test-block.js index 3f2c42de94..6bc1f07800 100644 --- a/test/http-api/ipfs-api/test-block.js +++ b/test/http-api/ipfs-api/test-block.js @@ -2,255 +2,72 @@ 'use strict' const expect = require('chai').expect -const APIctl = require('ipfs-api') -const fs = require('fs') -const FormData = require('form-data') -const streamToPromise = require('stream-to-promise') -module.exports = (httpAPI) => { - describe('block', () => { - describe('api', () => { - let api - - it('api', () => { - api = httpAPI.server.select('API') - }) - - describe('/block/put', () => { - it('returns 400 if no node is provided', (done) => { - const form = new FormData() - const headers = form.getHeaders() - - streamToPromise(form).then((payload) => { - api.inject({ - method: 'POST', - url: '/api/v0/block/put', - headers: headers, - payload: payload - }, (res) => { - expect(res.statusCode).to.equal(400) - done() - }) - }) - }) - - it('updates value', (done) => { - const form = new FormData() - const filePath = 'test/test-data/hello' - form.append('data', fs.createReadStream(filePath)) - const headers = form.getHeaders() - const expectedResult = { - Key: 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', - Size: 12 - } - - streamToPromise(form).then((payload) => { - api.inject({ - method: 'POST', - url: '/api/v0/block/put', - headers: headers, - payload: payload - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result).to.deep.equal(expectedResult) - done() - }) - }) +module.exports = (ctl) => { + describe('.block', () => { + describe('.put', () => { + it('updates value', (done) => { + const filePath = 'test/test-data/hello' + const expectedResult = { + Key: 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', + Size: 12 + } + + ctl.block.put(filePath, (err, res) => { + expect(err).not.to.exist + expect(res).to.deep.equal(expectedResult) + done() }) }) + }) - describe('/block/get', () => { - it('returns 400 for request without argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/block/get' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it('returns 500 for request with invalid argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/block/get?arg=invalid' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - done() - }) - }) - - it('returns value', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/block/get?arg=QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result) - .to.equal('hello world\n') - done() - }) + describe('.get', () => { + it('returns error for request without argument', (done) => { + ctl.block.get(null, (err, result) => { + expect(err).to.exist + done() }) }) - describe('/block/stat', () => { - it('returns 400 for request without argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/block/stat' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it('returns 500 for request with invalid argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/block/stat?arg=invalid' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - done() - }) - }) - - it('returns value', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/block/stat?arg=QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Key) - .to.equal('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp') - expect(res.result.Size).to.equal(12) - done() - }) + it('returns error for request with invalid argument', (done) => { + ctl.block.get('invalid', (err, result) => { + expect(err).to.exist + done() }) }) - describe('/block/del', () => { - it('returns 400 for request without argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/block/del' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it('returns 500 for request with invalid argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/block/del?arg=invalid' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - done() - }) - }) - - it('returns 200', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/block/del?arg=QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp' - }, (res) => { - expect(res.statusCode).to.equal(200) - done() - }) + it('returns value', (done) => { + ctl.block.get('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', (err, result) => { + expect(err).to.not.exist + expect(result.toString()) + .to.equal('hello world\n') + done() }) }) }) - describe('using js-ipfs-api', () => { - var ctl - - it('start IPFS API ctl', (done) => { - ctl = APIctl('/ip4/127.0.0.1/tcp/6001') - done() - }) - - describe('ipfs.block.put', () => { - it('returns error for request without argument', (done) => { - const filePath = null - - ctl.block.put(filePath, (err) => { - expect(err).to.exist - done() - }) - }) - - it('updates value', (done) => { - const filePath = 'test/test-data/hello' - const expectedResult = { - Key: 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', - Size: 12 - } - - ctl.block.put(filePath, (err, res) => { - expect(err).not.to.exist - expect(res).to.deep.equal(expectedResult) - done() - }) + describe('.stat', () => { + it('returns error for request without argument', (done) => { + ctl.block.stat(null, (err, result) => { + expect(err).to.exist + done() }) }) - describe('ipfs.block.get', () => { - it('returns error for request without argument', (done) => { - ctl.block.get(null, (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns error for request with invalid argument', (done) => { - ctl.block.get('invalid', (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns value', (done) => { - ctl.block.get('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', (err, result) => { - expect(err).to.not.exist - expect(result.toString()) - .to.equal('hello world\n') - done() - }) + it('returns error for request with invalid argument', (done) => { + ctl.block.stat('invalid', (err, result) => { + expect(err).to.exist + done() }) }) - describe('ipfs.block.stat', () => { - it('returns error for request without argument', (done) => { - ctl.block.stat(null, (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns error for request with invalid argument', (done) => { - ctl.block.stat('invalid', (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns value', (done) => { - ctl.block.stat('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', (err, result) => { - expect(err).to.not.exist - expect(result.Key) - .to.equal('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp') - expect(result.Size).to.equal(12) - done() - }) + it('returns value', (done) => { + ctl.block.stat('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', (err, result) => { + expect(err).to.not.exist + expect(result.Key) + .to.equal('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp') + expect(result.Size).to.equal(12) + done() }) }) }) diff --git a/test/http-api/ipfs-api/test-bootstrap.js b/test/http-api/ipfs-api/test-bootstrap.js index 340577cffc..08bb9c9f5c 100644 --- a/test/http-api/ipfs-api/test-bootstrap.js +++ b/test/http-api/ipfs-api/test-bootstrap.js @@ -2,105 +2,31 @@ 'use strict' const expect = require('chai').expect -const APIctl = require('ipfs-api') -module.exports = (httpAPI) => { - describe('bootstrap', () => { - describe('api', () => { - let api - - it('api', () => { - api = httpAPI.server.select('API') - }) - - it('list', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/bootstrap' - }, (res) => { - expect(res.result).to.deep.equal(defaultList) - done() - }) - }) - - it('list 2', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/bootstrap/list' - }, (res) => { - expect(res.result).to.deep.equal(defaultList) - done() - }) - }) - - it('add', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/bootstrap/add', - payload: { - arg: '/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT' - } - }, (res) => { - done() - }) - }) - - it('rm', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/bootstrap/rm', - payload: { - arg: '/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT' - } - }, (res) => { - done() - }) - }) - - it('confirm list is as expected', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/bootstrap/list' - }, (res) => { - expect(res.result).to.deep.equal(defaultList) - done() - }) - }) - }) - - describe('gateway', () => {}) - - describe('using js-ipfs-api', () => { - var ctl - - it('start IPFS API ctl', (done) => { - ctl = APIctl('/ip4/127.0.0.1/tcp/6001') +module.exports = (ctl) => { + describe('.bootstrap', () => { + // TODO: needs https://github.com/ipfs/js-ipfs-api/issues/217 + it.skip('list', (done) => { + ctl.boostrap.list((err, result) => { + expect(err).to.not.exist + expect(result).to.deep.equal(defaultList) done() }) - - // TODO: needs https://github.com/ipfs/js-ipfs-api/issues/217 - it.skip('list', (done) => { - ctl.boostrap.list((err, result) => { - expect(err).to.not.exist - expect(result).to.deep.equal(defaultList) - done() - }) - }) - - it.skip('add', (done) => {}) - it.skip('rm', (done) => {}) }) - }) - const defaultList = [ - '/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', - '/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z', - '/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM', - '/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm', - '/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', - '/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', - '/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', - '/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', - '/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx' - ] + it.skip('add', (done) => {}) // TODO + it.skip('rm', (done) => {}) // TODO + }) } + +const defaultList = [ + '/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', + '/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z', + '/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM', + '/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm', + '/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', + '/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', + '/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', + '/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', + '/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx' +] diff --git a/test/http-api/ipfs-api/test-config.js b/test/http-api/ipfs-api/test-config.js index 49d717f01a..601a24454c 100644 --- a/test/http-api/ipfs-api/test-config.js +++ b/test/http-api/ipfs-api/test-config.js @@ -3,300 +3,88 @@ const expect = require('chai').expect const fs = require('fs') -const APIctl = require('ipfs-api') -const FormData = require('form-data') -const streamToPromise = require('stream-to-promise') const path = require('path') -module.exports = (httpAPI) => { - describe('config', () => { - const configPath = path.join(__dirname, '../repo-tests-run-http/config') - const originalConfigPath = path.join(__dirname, '../go-ipfs-repo/config') - const updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8')) - const restoreConfig = () => fs.writeFileSync(configPath, fs.readFileSync(originalConfigPath, 'utf8'), 'utf8') +module.exports = (ctl) => { + describe('.config', () => { + const configPath = path.join(__dirname, '../../repo-tests-run-http/config') + let updatedConfig - describe('api', () => { - let api + before(() => { + updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8')) + }) - before('api', () => { - api = httpAPI.server.select('API') + it('.get returns error for request with invalid argument', (done) => { + ctl.config.get('kittens', (err, res) => { + expect(err).to.exist + done() }) + }) - describe('/config', () => { - it('returns 400 for request without arguments', (done) => { - api.inject({ - method: 'POST', - url: '/api/v0/config' - }, (res) => { - expect(res.statusCode).to.equal(400) - done() - }) - }) - - it('returns 500 for request with invalid argument', (done) => { - api.inject({ - method: 'POST', - url: '/api/v0/config?arg=kitten' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - done() - }) - }) - - it('returns value for request with argument', (done) => { - api.inject({ - method: 'POST', - url: '/api/v0/config?arg=API.HTTPHeaders' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Key).to.equal('API.HTTPHeaders') - expect(res.result.Value).to.equal(null) - done() - }) - }) - - it('returns value for request as subcommand', (done) => { - api.inject({ - method: 'POST', - url: '/api/v0/config/API.HTTPHeaders' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Key).to.equal('API.HTTPHeaders') - expect(res.result.Value).to.equal(null) - done() - }) - }) - - it('updates value for request with both args', (done) => { - api.inject({ - method: 'POST', - url: '/api/v0/config?arg=Datastore.Path&arg=kitten' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Key).to.equal('Datastore.Path') - expect(res.result.Value).to.equal('kitten') - expect(updatedConfig().Datastore.Path).to.equal('kitten') - - done() - }) - }) - - it('returns 500 value for request with both args and JSON flag with invalid JSON argument', (done) => { - api.inject({ - method: 'POST', - url: '/api/v0/config?arg=Datastore.Path&arg=kitten&json' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - - done() - }) - }) - - it('updates value for request with both args and JSON flag with valid JSON argument', (done) => { - api.inject({ - method: 'POST', - url: '/api/v0/config?arg=Datastore.Path&arg={"kitten": true}&json' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Key).to.equal('Datastore.Path') - expect(res.result.Value).to.deep.equal({ kitten: true }) - expect(updatedConfig().Datastore.Path).to.deep.equal({ kitten: true }) - - done() - }) - }) - - it('updates value for request with both args and bool flag and true argument', (done) => { - api.inject({ - method: 'POST', - url: '/api/v0/config?arg=Datastore.Path&arg=true&bool' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Key).to.equal('Datastore.Path') - expect(res.result.Value).to.deep.equal(true) - expect(updatedConfig().Datastore.Path).to.deep.equal(true) - - done() - }) - }) - - it('updates value for request with both args and bool flag and false argument', (done) => { - api.inject({ - method: 'POST', - url: '/api/v0/config?arg=Datastore.Path&arg=false&bool' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Key).to.equal('Datastore.Path') - expect(res.result.Value).to.deep.equal(false) - expect(updatedConfig().Datastore.Path).to.deep.equal(false) - - done() - }) - }) + it('.get returns value for request with argument', (done) => { + ctl.config.get('API.HTTPHeaders', (err, value) => { + expect(err).not.to.exist + expect(value).to.equal(null) + done() }) + }) - it('/config/show', (done) => { - api.inject({ - method: 'POST', - url: '/api/v0/config/show' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result).to.deep.equal(updatedConfig()) - done() - }) + it('.set updates value for request with both args', (done) => { + ctl.config.set('Datastore.Path', 'kitten', (err) => { + expect(err).not.to.exist + done() }) + }) - describe('/config/replace', () => { - it('returns 400 if no config is provided', (done) => { - const form = new FormData() - const headers = form.getHeaders() - - streamToPromise(form).then((payload) => { - api.inject({ - method: 'POST', - url: '/api/v0/config/replace', - headers: headers, - payload: payload - }, (res) => { - expect(res.statusCode).to.equal(400) - done() - }) - }) - }) - - it('returns 500 if the config is invalid', (done) => { - const form = new FormData() - const filePath = 'test/test-data/badconfig' - form.append('file', fs.createReadStream(filePath)) - const headers = form.getHeaders() - - streamToPromise(form).then((payload) => { - api.inject({ - method: 'POST', - url: '/api/v0/config/replace', - headers: headers, - payload: payload - }, (res) => { - expect(res.statusCode).to.equal(500) - done() - }) - }) - }) - - it('updates value', (done) => { - const form = new FormData() - const filePath = 'test/test-data/otherconfig' - form.append('file', fs.createReadStream(filePath)) - const headers = form.getHeaders() - const expectedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8')) - - streamToPromise(form).then((payload) => { - api.inject({ - method: 'POST', - url: '/api/v0/config/replace', - headers: headers, - payload: payload - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(updatedConfig()).to.deep.equal(expectedConfig) - done() - }) - }) - }) - - after(() => { - restoreConfig() - }) + it('.set returns error for request with both args and JSON flag with invalid JSON argument', (done) => { + ctl.config.set('Datastore.Path', 'kitten', { json: true }, (err) => { + expect(err).to.exist + done() }) }) - // js-ipfs-api - describe.skip('using js-ipfs-api', () => { - var ctl - - before('start IPFS API ctl', (done) => { - ctl = APIctl('/ip4/127.0.0.1/tcp/6001') + it('.set updates value for request with both args and bool flag and true argument', (done) => { + ctl.config.set('Datastore.Path', true, (err) => { + expect(err).not.to.exist done() }) + }) - describe('ipfs.config', () => { - it('returns error for request with invalid argument', (done) => { - ctl.config.get('kittens', (err, res) => { - expect(err).to.exist - - done() - }) - }) - - it('returns value for request with argument', (done) => { - ctl.config.get('API.HTTPHeaders', (err, value) => { - expect(err).not.to.exist - expect(value.key).to.equal('API.HTTPHeaders') - expect(value.value).to.equal(null) - done() - }) - }) - - it('updates value for request with both args', (done) => { - ctl.config.set('Datastore.Path', 'kitten', (err) => { - expect(err).not.to.exist - done() - }) - }) - - it('returns error for request with both args and JSON flag with invalid JSON argument', (done) => { - ctl.config.set('Datastore.Path', 'kitten', { json: true }, (err) => { - expect(err).to.exist - done() - }) - }) - - it('updates value for request with both args and bool flag and true argument', (done) => { - ctl.config.set('Datastore.Path', true, (err) => { - expect(err).not.to.exist - done() - }) - }) - - it('updates value for request with both args and bool flag and false argument', (done) => { - ctl.config.set('Datastore.Path', false, (err) => { - expect(err).not.to.exist - done() - }) - }) + it('.set updates value for request with both args and bool flag and false argument', (done) => { + ctl.config.set('Datastore.Path', false, (err) => { + expect(err).not.to.exist + done() }) + }) - it('ipfs.config.get', (done) => { - ctl.config.get((err, config) => { - expect(err).not.to.exist - expect(config).to.deep.equal(updatedConfig()) - done() - }) + it('.get updatedConfig', (done) => { + ctl.config.get((err, config) => { + expect(err).not.to.exist + expect(config).to.deep.equal(updatedConfig()) + done() }) + }) - describe('ipfs.config.replace', () => { - it('returns error if the config is invalid', (done) => { - const filePath = 'test/test-data/badconfig' + // This one is one stale mode till go-ipfs decides + // what to do + describe.skip('.replace', () => { + it('returns error if the config is invalid', (done) => { + const filePath = 'test/test-data/badconfig' - ctl.config.replace(filePath, (err) => { - expect(err).to.exist - done() - }) + ctl.config.replace(filePath, (err) => { + expect(err).to.exist + done() }) + }) - it('updates value', (done) => { - const filePath = 'test/test-data/otherconfig' - const expectedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8')) + it('updates value', (done) => { + const filePath = 'test/test-data/otherconfig' + const expectedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8')) - ctl.config.replace(filePath, (err) => { - expect(err).not.to.exist - expect(expectedConfig).to.deep.equal(updatedConfig()) - done() - }) + ctl.config.replace(filePath, (err) => { + expect(err).not.to.exist + expect(expectedConfig).to.deep.equal(updatedConfig()) + done() }) }) }) diff --git a/test/http-api/ipfs-api/test-files.js b/test/http-api/ipfs-api/test-files.js index 29aaf9a44d..95312de30b 100644 --- a/test/http-api/ipfs-api/test-files.js +++ b/test/http-api/ipfs-api/test-files.js @@ -2,85 +2,37 @@ 'use strict' const expect = require('chai').expect -const APIctl = require('ipfs-api') -module.exports = (httpAPI) => { - describe('files', () => { - describe('api', () => { - let api +module.exports = (ctl) => { + describe('.files', () => { + describe('.add', () => {}) // TODO - before(() => { - api = httpAPI.server.select('API') - }) - - describe('/files/cat', () => { - it('returns 400 for request without argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/cat' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) + describe('.cat', () => { + it('returns error for request without argument', (done) => { + ctl.cat(null, (err, result) => { + expect(err).to.exist + done() }) + }) - it('returns 500 for request with invalid argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/cat?arg=invalid' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Message).to.be.a('string') - done() - }) + it('returns error for request with invalid argument', (done) => { + ctl.cat('invalid', (err, result) => { + expect(err).to.exist + done() }) + }) - it('returns a buffer', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/cat?arg=QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.rawPayload).to.deep.equal(new Buffer('hello world' + '\n')) - expect(res.payload).to.equal('hello world' + '\n') - done() - }) + it('returns a buffer', (done) => { + ctl.cat('QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o', (err, result) => { + expect(err).to.not.exist + expect(result).to.deep.equal(new Buffer('hello world' + '\n')) + done() }) }) }) - describe('using js-ipfs-api', () => { - var ctl + describe('.get', () => {}) // TODO - it('start IPFS API ctl', (done) => { - ctl = APIctl('/ip4/127.0.0.1/tcp/6001') - done() - }) - - describe('ipfs.cat', () => { - it('returns error for request without argument', (done) => { - ctl.cat(null, (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns error for request with invalid argument', (done) => { - ctl.cat('invalid', (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns a buffer', (done) => { - ctl.cat('QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o', (err, result) => { - expect(err).to.not.exist - expect(result).to.deep.equal(new Buffer('hello world' + '\n')) - done() - }) - }) - }) - }) + describe('.ls', () => {}) // TODO }) } diff --git a/test/http-api/ipfs-api/test-id.js b/test/http-api/ipfs-api/test-id.js index 7184230c90..02eafe1967 100644 --- a/test/http-api/ipfs-api/test-id.js +++ b/test/http-api/ipfs-api/test-id.js @@ -2,60 +2,26 @@ 'use strict' const expect = require('chai').expect -const APIctl = require('ipfs-api') -module.exports = (httpAPI) => { - describe('id', () => { - describe('api', () => { - let api - - it('api', () => { - api = httpAPI.server.select('API') - }) - - it('get the id', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/id' - }, (res) => { - expect(res.result.id).to.equal(idResult.ID) - expect(res.result.publicKey).to.equal(idResult.PublicKey) - expect(res.result.agentVersion).to.equal(idResult.AgentVersion) - expect(res.result.protocolVersion).to.equal(idResult.ProtocolVersion) - done() - }) - }) - }) - - describe('gateway', () => {}) - - // TODO revisit these - describe.skip('using js-ipfs-api', () => { - var ctl - - it('start IPFS API ctl', (done) => { - ctl = APIctl('/ip4/127.0.0.1/tcp/6001') +module.exports = (ctl) => { + describe('.id', () => { + it('get the identity', (done) => { + ctl.id((err, result) => { + expect(err).to.not.exist + expect(result.id).to.equal(idResult.ID) + expect(result.publicKey).to.equal(idResult.PublicKey) + expect(result.agentVersion).to.equal(idResult.AgentVersion) + expect(result.protocolVersion).to.equal(idResult.ProtocolVersion) done() }) - - it('get the id', (done) => { - ctl.id((err, result) => { - expect(err).to.not.exist - expect(result.id).to.equal(idResult.ID) - expect(result.publicKey).to.equal(idResult.PublicKey) - expect(result.agentVersion).to.equal(idResult.AgentVersion) - expect(result.protocolVersion).to.equal(idResult.ProtocolVersion) - done() - }) - }) }) }) +} - const idResult = { - ID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A', - PublicKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE=', - Addresses: [ '/ip4/0.0.0.0/tcp/0' ], - AgentVersion: 'js-ipfs', - ProtocolVersion: '9000' - } +const idResult = { + ID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A', + PublicKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE=', + Addresses: [ '/ip4/0.0.0.0/tcp/0' ], + AgentVersion: 'js-ipfs', + ProtocolVersion: '9000' } diff --git a/test/http-api/ipfs-api/test-object.js b/test/http-api/ipfs-api/test-object.js index f1157fd962..3a4dea8ab7 100644 --- a/test/http-api/ipfs-api/test-object.js +++ b/test/http-api/ipfs-api/test-object.js @@ -2,861 +2,310 @@ 'use strict' const expect = require('chai').expect -const APIctl = require('ipfs-api') const fs = require('fs') -const FormData = require('form-data') -const streamToPromise = require('stream-to-promise') const DAGLink = require('ipfs-merkle-dag').DAGLink -module.exports = (httpAPI) => { - describe('object', () => { - describe('api', () => { - let api - - it('api', () => { - api = httpAPI.server.select('API') +module.exports = (ctl) => { + describe('.object', () => { + it('.new', (done) => { + ctl.object.new((err, result) => { + expect(err).to.not.exist + const res = result.toJSON() + expect(res.Hash) + .to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') + expect(res.Links).to.be.eql([]) + done() }) + }) - describe('/object/new', () => { - it('returns value', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/new' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Hash) - .to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - expect(res.result.Links).to.be.eql([]) - done() - }) + describe('.get', () => { + it('returns error for request without argument', (done) => { + ctl.object.get(null, (err, result) => { + expect(err).to.exist + done() }) }) - describe('/object/get', () => { - it('returns 400 for request without argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/get' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it('returns 500 for request with invalid argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/get?arg=invalid' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - done() - }) - }) - - it('returns value', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/get?arg=QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Links).to.be.eql([]) - expect(res.result.Data).to.be.empty - done() - }) + it('returns error for request with invalid argument', (done) => { + ctl.object.get('invalid', {enc: 'base58'}, (err, result) => { + expect(err).to.exist + done() }) }) - describe('/object/put', () => { - it('returns 400 if no node is provided', (done) => { - const form = new FormData() - const headers = form.getHeaders() - - streamToPromise(form).then((payload) => { - api.inject({ - method: 'POST', - url: '/api/v0/object/put', - headers: headers, - payload: payload - }, (res) => { - expect(res.statusCode).to.equal(400) - done() - }) - }) - }) - - it('returns 500 if the node is invalid', (done) => { - const form = new FormData() - const filePath = 'test/test-data/badnode.json' - form.append('file', fs.createReadStream(filePath)) - const headers = form.getHeaders() - - streamToPromise(form).then((payload) => { - api.inject({ - method: 'POST', - url: '/api/v0/object/put', - headers: headers, - payload: payload - }, (res) => { - expect(res.statusCode).to.equal(500) - done() - }) - }) - }) - - it('updates value', (done) => { - const form = new FormData() - const filePath = 'test/test-data/node.json' - form.append('data', fs.createReadStream(filePath)) - const headers = form.getHeaders() - const expectedResult = { - Data: new Buffer('another'), - Hash: 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', - Links: [{ - Name: 'some link', - Hash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V', - Size: 8 - }], - Size: 68 - } - - streamToPromise(form).then((payload) => { - api.inject({ - method: 'POST', - url: '/api/v0/object/put', - headers: headers, - payload: payload - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result).to.deep.equal(expectedResult) - done() - }) - }) + it('returns value', (done) => { + ctl.object.get('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n', {enc: 'base58'}, (err, result) => { + expect(err).to.not.exist + const res = result.toJSON() + expect(res.Links).to.be.eql([]) + expect(res.Data).to.equal('') + done() }) }) + }) - describe('/object/stat', () => { - it('returns 400 for request without argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/stat' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it('returns 500 for request with invalid argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/stat?arg=invalid' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - done() - }) - }) + describe('.put', () => { + it('returns error if the node is invalid', (done) => { + const filePath = 'test/test-data/badnode.json' - it('returns value', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/stat?arg=QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Hash).to.equal('QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm') - expect(res.result.NumLinks).to.equal(1) - expect(res.result.BlockSize).to.equal(60) - expect(res.result.LinksSize).to.equal(60 - 7) - expect(res.result.DataSize).to.equal(7) - expect(res.result.CumulativeSize).to.equal(60 + 8) - done() - }) + ctl.object.put(filePath, {enc: 'json'}, (err) => { + expect(err).to.exist + done() }) }) - describe('/object/data', () => { - it('returns 400 for request without argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/data' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it('returns 500 for request with invalid argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/data?arg=invalid' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - done() - }) - }) + it('updates value', (done) => { + const filePath = fs.readFileSync('test/test-data/node.json') + const expectedResult = { + Data: 'another', + Hash: 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', + Links: [{ + Name: 'some link', + Hash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V', + Size: 8 + }], + Size: 68 + } - it('returns value', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/data?arg=QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result).to.equal('another') - done() - }) + ctl.object.put(filePath, {enc: 'json'}, (err, res) => { + expect(err).not.to.exist + expect(res.toJSON()).to.deep.equal(expectedResult) + done() }) }) + }) - describe('/object/links', () => { - it('returns 400 for request without argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/links' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it('returns 500 for request with invalid argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/links?arg=invalid' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - done() - }) - }) - - it('returns value', (done) => { - const expectedResult = { - Hash: 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', - Links: [ - { Name: 'some link', Hash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V', Size: 8 } - ] - } - - api.inject({ - method: 'GET', - url: '/api/v0/object/links?arg=QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result).to.deep.equal(expectedResult) - done() - }) + describe('.stat', () => { + it('returns error for request without argument', (done) => { + ctl.object.stat(null, (err, result) => { + expect(err).to.exist + done() }) }) - describe('/object/patch/append-data', () => { - it('returns 400 for request without key', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/patch/append-data' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it('returns 400 if no data is provided', (done) => { - const form = new FormData() - const headers = form.getHeaders() - - streamToPromise(form).then((payload) => { - api.inject({ - method: 'POST', - url: '/api/v0/object/patch/append-data?arg=QmVLUHkjGg3duGb5w3dnwK5w2P9QWuJmtVNuDPLc9ZDjzk', - headers: headers, - payload: payload - }, (res) => { - expect(res.statusCode).to.equal(400) - done() - }) - }) - }) - - it('returns 500 for request with invalid key', (done) => { - const form = new FormData() - const filePath = 'test/test-data/badconfig' - form.append('file', fs.createReadStream(filePath)) - const headers = form.getHeaders() - - streamToPromise(form).then((payload) => { - api.inject({ - method: 'POST', - url: '/api/v0/object/patch/append-data?arg=invalid', - headers: headers, - payload: payload - }, (res) => { - expect(res.statusCode).to.equal(500) - done() - }) - }) - }) - - it('updates value', (done) => { - const form = new FormData() - const filePath = 'test/test-data/badconfig' - form.append('data', fs.createReadStream(filePath)) - const headers = form.getHeaders() - const expectedResult = { - Data: fs.readFileSync(filePath), - Hash: 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', - Links: [], - Size: 19 - } - - streamToPromise(form).then((payload) => { - api.inject({ - method: 'POST', - url: '/api/v0/object/patch/append-data?arg=QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n', - headers: headers, - payload: payload - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result).to.deep.equal(expectedResult) - done() - }) - }) + it('returns error for request with invalid argument', (done) => { + ctl.object.stat('invalid', {enc: 'base58'}, (err, result) => { + expect(err).to.exist + done() }) }) - describe('/object/patch/set-data', () => { - it('returns 400 for request without key', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/patch/set-data' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it('returns 400 if no data is provided', (done) => { - const form = new FormData() - const headers = form.getHeaders() - - streamToPromise(form).then((payload) => { - api.inject({ - method: 'POST', - url: '/api/v0/object/patch/set-data?arg=QmVLUHkjGg3duGb5w3dnwK5w2P9QWuJmtVNuDPLc9ZDjzk', - headers: headers, - payload: payload - }, (res) => { - expect(res.statusCode).to.equal(400) - done() - }) - }) - }) - - it('returns 500 for request with invalid key', (done) => { - const form = new FormData() - const filePath = 'test/test-data/badconfig' - form.append('file', fs.createReadStream(filePath)) - const headers = form.getHeaders() - - streamToPromise(form).then((payload) => { - api.inject({ - method: 'POST', - url: '/api/v0/object/patch/set-data?arg=invalid', - headers: headers, - payload: payload - }, (res) => { - expect(res.statusCode).to.equal(500) - done() - }) - }) - }) - - it('updates value', (done) => { - const form = new FormData() - const filePath = 'test/test-data/badconfig' - form.append('data', fs.createReadStream(filePath)) - const headers = form.getHeaders() - const expectedResult = { - Hash: 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', - Links: [] - } - - streamToPromise(form).then((payload) => { - api.inject({ - method: 'POST', - url: '/api/v0/object/patch/set-data?arg=QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', - headers: headers, - payload: payload - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result).to.deep.equal(expectedResult) - done() - }) - }) + it('returns value', (done) => { + ctl.object.stat('QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', {enc: 'base58'}, (err, result) => { + expect(err).to.not.exist + expect(result.Hash).to.equal('QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm') + expect(result.NumLinks).to.equal(1) + expect(result.BlockSize).to.equal(60) + expect(result.LinksSize).to.equal(60 - 7) + expect(result.DataSize).to.equal(7) + expect(result.CumulativeSize).to.equal(60 + 8) + done() }) }) + }) - describe('/object/patch/add-link', () => { - it('returns 400 for request without arguments', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/patch/add-link' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it('returns 400 for request with only one invalid argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/patch/add-link?arg=invalid' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it('returns 500 for request with invalid first argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/patch/add-link?arg=&arg=foo&arg=QmTz3oc4gdpRMKP2sdGUPZTAGRngqjsi99BPoztyP53JMM' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - done() - }) - }) - - it('returns 500 for request with empty second argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/patch/add-link?arg=QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn&arg=&arg=QmTz3oc4gdpRMKP2sdGUPZTAGRngqjsi99BPoztyP53JMM' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - done() - }) - }) - - it('returns value', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/patch/add-link?arg=QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n&arg=foo&arg=QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Hash).to.equal('QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK') - expect(res.result.Links[0]).to.deep.equal({ - Name: 'foo', - Hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn', - Size: 4 - }) - done() - }) + describe('.data', () => { + it('returns error for request without argument', (done) => { + ctl.object.data(null, (err, result) => { + expect(err).to.exist + done() }) }) - describe('/object/patch/rm-link', () => { - it('returns 400 for request without arguments', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/patch/rm-link' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it('returns 400 for request with only one invalid argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/patch/rm-link?arg=invalid' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it('returns 500 for request with invalid first argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/patch/rm-link?arg=invalid&arg=foo' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - done() - }) - }) - - it('returns 500 for request with invalid second argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/patch/rm-link?arg=QmZKetgwm4o3LhNaoLSHv32wBhTwj9FBwAdSchDMKyFQEx&arg=' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - done() - }) - }) - - it('returns value', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/object/patch/rm-link?arg=QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK&arg=foo' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Hash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - done() - }) + it('returns error for request with invalid argument', (done) => { + ctl.object.data('invalid', {enc: 'base58'}, (err, result) => { + expect(err).to.exist + done() }) }) - }) - describe('using js-ipfs-api', () => { - var ctl - - it('start IPFS API ctl', (done) => { - ctl = APIctl('/ip4/127.0.0.1/tcp/6001') - done() - }) - - it('ipfs.object.new', (done) => { - ctl.object.new((err, result) => { + it('returns value', (done) => { + ctl.object.data('QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', {enc: 'base58'}, (err, result) => { expect(err).to.not.exist - const res = result.toJSON() - expect(res.Hash) - .to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - expect(res.Links).to.be.eql([]) + expect(result.toString()).to.equal('another') done() }) }) + }) - describe('ipfs.object.get', () => { - it('returns error for request without argument', (done) => { - ctl.object.get(null, (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns error for request with invalid argument', (done) => { - ctl.object.get('invalid', {enc: 'base58'}, (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns value', (done) => { - ctl.object.get('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n', {enc: 'base58'}, (err, result) => { - expect(err).to.not.exist - const res = result.toJSON() - expect(res.Links).to.be.eql([]) - expect(res.Data).to.equal('') - done() - }) + describe('.links', () => { + it('returns error for request without argument', (done) => { + ctl.object.links(null, (err, result) => { + expect(err).to.exist + done() }) }) - describe('ipfs.object.put', () => { - it('returns error if the node is invalid', (done) => { - const filePath = 'test/test-data/badnode.json' - - ctl.object.put(filePath, {enc: 'json'}, (err) => { - expect(err).to.exist - done() - }) - }) - - it('updates value', (done) => { - const filePath = fs.readFileSync('test/test-data/node.json') - const expectedResult = { - Data: 'another', - Hash: 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', - Links: [{ - Name: 'some link', - Hash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V', - Size: 8 - }], - Size: 68 - } - - ctl.object.put(filePath, {enc: 'json'}, (err, res) => { - expect(err).not.to.exist - expect(res.toJSON()).to.deep.equal(expectedResult) - done() - }) + it('returns error for request with invalid argument', (done) => { + ctl.object.links('invalid', {enc: 'base58'}, (err, result) => { + expect(err).to.exist + done() }) }) - describe('ipfs.object.stat', () => { - it('returns error for request without argument', (done) => { - ctl.object.stat(null, (err, result) => { - expect(err).to.exist - done() - }) - }) + it('returns value', (done) => { + const expectedResult = { + Name: 'some link', + Hash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V', + Size: 8 + } - it('returns error for request with invalid argument', (done) => { - ctl.object.stat('invalid', {enc: 'base58'}, (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns value', (done) => { - ctl.object.stat('QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', {enc: 'base58'}, (err, result) => { - expect(err).to.not.exist - expect(result.Hash).to.equal('QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm') - expect(result.NumLinks).to.equal(1) - expect(result.BlockSize).to.equal(60) - expect(result.LinksSize).to.equal(60 - 7) - expect(result.DataSize).to.equal(7) - expect(result.CumulativeSize).to.equal(60 + 8) - done() - }) + ctl.object.links('QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', {enc: 'base58'}, (err, result) => { + expect(err).to.not.exist + expect(result[0].toJSON()).to.deep.equal(expectedResult) + done() }) }) + }) - describe('ipfs.object.data', () => { - it('returns error for request without argument', (done) => { - ctl.object.data(null, (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns error for request with invalid argument', (done) => { - ctl.object.data('invalid', {enc: 'base58'}, (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns value', (done) => { - ctl.object.data('QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', {enc: 'base58'}, (err, result) => { - expect(err).to.not.exist - expect(result.toString()).to.equal('another') - done() - }) + describe('.patch.appendData', () => { + it('returns error for request without key & data', (done) => { + ctl.object.patch.appendData(null, null, (err) => { + expect(err).to.exist + done() }) }) - describe('ipfs.object.links', () => { - it('returns error for request without argument', (done) => { - ctl.object.links(null, (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns error for request with invalid argument', (done) => { - ctl.object.links('invalid', {enc: 'base58'}, (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns value', (done) => { - const expectedResult = { - Name: 'some link', - Hash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V', - Size: 8 - } + it('returns error for request without data', (done) => { + const filePath = 'test/test-data/badnode.json' - ctl.object.links('QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', {enc: 'base58'}, (err, result) => { - expect(err).to.not.exist - expect(result[0].toJSON()).to.deep.equal(expectedResult) - done() - }) + ctl.object.patch.appendData(null, filePath, (err) => { + expect(err).to.exist + done() }) }) - // TODO revisit these - describe.skip('ipfs.object.patch.appendData', () => { - it('returns error for request without key & data', (done) => { - ctl.object.patch.appendData(null, null, (err) => { - expect(err).to.exist - done() - }) - }) - - it('returns error for request without key', (done) => { - const key = 'QmVLUHkjGg3duGb5w3dnwK5w2P9QWuJmtVNuDPLc9ZDjzk' - - ctl.object.patch.appendData(key, null, {enc: 'base58'}, (err) => { - expect(err).to.exist - done() - }) - }) - - it('returns error for request without data', (done) => { - const filePath = 'test/test-data/badnode.json' - - ctl.object.patch.appendData(null, filePath, (err) => { - expect(err).to.exist - done() - }) - }) - - it('updates value', (done) => { - const key = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n' - const filePath = 'test/test-data/badnode.json' - const expectedResult = { - Data: fs.readFileSync(filePath).toString(), - Hash: 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', - Links: [], - Size: 19 - } - - ctl.object.patch.appendData(key, filePath, {enc: 'base58'}, (err, res) => { - expect(err).not.to.exist - expect(res.toJSON()).to.deep.equal(expectedResult) - done() - }) + it('updates value', (done) => { + const key = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n' + const filePath = 'test/test-data/badnode.json' + const expectedResult = { + Data: fs.readFileSync(filePath).toString(), + Hash: 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', + Links: [], + Size: 19 + } + + ctl.object.patch.appendData(key, filePath, {enc: 'base58'}, (err, res) => { + expect(err).not.to.exist + expect(res.toJSON()).to.deep.equal(expectedResult) + done() }) }) + }) - // TODO revisit these - describe.skip('ipfs.object.patch.setData', () => { - it('returns error for request without key & data', (done) => { - ctl.object.patch.setData(null, null, (err) => { - expect(err).to.exist - done() - }) - }) - - it('returns error for request without key', (done) => { - const key = 'QmVLUHkjGg3duGb5w3dnwK5w2P9QWuJmtVNuDPLc9ZDjzk' - - ctl.object.patch.setData(key, null, {enc: 'base58'}, (err) => { - expect(err).to.exist - done() - }) + describe('.patch.setData', () => { + it('returns error for request without key & data', (done) => { + ctl.object.patch.setData(null, null, (err) => { + expect(err).to.exist + done() }) + }) - it('returns error for request without data', (done) => { - const filePath = 'test/test-data/badnode.json' + it('returns error for request without data', (done) => { + const filePath = 'test/test-data/badnode.json' - ctl.object.patch.setData(null, filePath, (err) => { - expect(err).to.exist - done() - }) + ctl.object.patch.setData(null, filePath, (err) => { + expect(err).to.exist + done() }) + }) - it('updates value', (done) => { - const key = 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6' - const filePath = 'test/test-data/badnode.json' - const expectedResult = { - Data: fs.readFileSync(filePath).toString(), - Hash: 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', - Links: [], - Size: 19 - } - - ctl.object.patch.setData(key, filePath, {enc: 'base58'}, (err, res) => { - expect(err).not.to.exist - expect(res.toJSON()).to.deep.equal(expectedResult) - done() - }) + it('updates value', (done) => { + const key = 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6' + const filePath = 'test/test-data/badnode.json' + const expectedResult = { + Data: fs.readFileSync(filePath).toString(), + Hash: 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', + Links: [], + Size: 19 + } + + ctl.object.patch.setData(key, filePath, {enc: 'base58'}, (err, res) => { + expect(err).not.to.exist + expect(res.toJSON()).to.deep.equal(expectedResult) + done() }) }) + }) - describe('ipfs.object.patch.addLink', () => { - it('returns error for request without arguments', (done) => { - ctl.object.patch.addLink(null, null, null, (err) => { - expect(err).to.exist - done() - }) + describe('.patch.addLink', () => { + it('returns error for request without arguments', (done) => { + ctl.object.patch.addLink(null, null, null, (err) => { + expect(err).to.exist + done() }) + }) - it('returns error for request only one invalid argument', (done) => { - ctl.object.patch.addLink('invalid', null, null, (err) => { - expect(err).to.exist - done() - }) + it('returns error for request only one invalid argument', (done) => { + ctl.object.patch.addLink('invalid', null, null, (err) => { + expect(err).to.exist + done() }) + }) - it('returns error for request without name', (done) => { - const root = 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' - const name = '' - const ref = 'QmTz3oc4gdpRMKP2sdGUPZTAGRngqjsi99BPoztyP53JMM' - const link = new DAGLink(name, 2, ref) - ctl.object.patch.addLink(root, link, {enc: 'base58'}, (err) => { - expect(err).to.exist - done() - }) + it('returns error for request without name', (done) => { + const root = 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' + const name = '' + const ref = 'QmTz3oc4gdpRMKP2sdGUPZTAGRngqjsi99BPoztyP53JMM' + const link = new DAGLink(name, 2, ref) + ctl.object.patch.addLink(root, link, {enc: 'base58'}, (err) => { + expect(err).to.exist + done() }) + }) - it('updates value', (done) => { - const root = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n' - const name = 'foo' - const ref = 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' - const link = new DAGLink(name, 10, ref) - ctl.object.patch.addLink(root, link, {enc: 'base58'}, (err, result) => { - expect(err).not.to.exist - const res = result.toJSON() - expect(res.Hash).to.equal('QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK') - expect(res.Links[0]).to.deep.equal({ - Name: 'foo', - Hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn', - Size: 4 - }) - done() + it('updates value', (done) => { + const root = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n' + const name = 'foo' + const ref = 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' + const link = new DAGLink(name, 10, ref) + ctl.object.patch.addLink(root, link, {enc: 'base58'}, (err, result) => { + expect(err).not.to.exist + const res = result.toJSON() + expect(res.Hash).to.equal('QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK') + expect(res.Links[0]).to.deep.equal({ + Name: 'foo', + Hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn', + Size: 4 }) + done() }) }) + }) - describe('ipfs.object.patch.rmLink', () => { - it('returns error for request without arguments', (done) => { - ctl.object.patch.rmLink(null, null, (err) => { - expect(err).to.exist - done() - }) + describe('.patch.rmLink', () => { + it('returns error for request without arguments', (done) => { + ctl.object.patch.rmLink(null, null, (err) => { + expect(err).to.exist + done() }) + }) - it('returns error for request only one invalid argument', (done) => { - ctl.object.patch.rmLink('invalid', null, (err) => { - expect(err).to.exist - done() - }) + it('returns error for request only one invalid argument', (done) => { + ctl.object.patch.rmLink('invalid', null, (err) => { + expect(err).to.exist + done() }) + }) - it('returns error for request with invalid first argument', (done) => { - const root = '' - const link = 'foo' + it('returns error for request with invalid first argument', (done) => { + const root = '' + const link = 'foo' - ctl.object.patch.rmLink(root, link, (err) => { - expect(err).to.exist - done() - }) + ctl.object.patch.rmLink(root, link, (err) => { + expect(err).to.exist + done() }) + }) - it('updates value', (done) => { - const root = 'QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK' - const link = new DAGLink('foo') + it('updates value', (done) => { + const root = 'QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK' + const link = new DAGLink('foo') - ctl.object.patch.rmLink(root, link, {enc: 'base58'}, (err, res) => { - expect(err).not.to.exist - expect(res.toJSON().Hash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - done() - }) + ctl.object.patch.rmLink(root, link, {enc: 'base58'}, (err, res) => { + expect(err).not.to.exist + expect(res.toJSON().Hash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') + done() }) }) }) diff --git a/test/http-api/ipfs-api/test-repo.js b/test/http-api/ipfs-api/test-repo.js index 5ee306e749..68f0a22823 100644 --- a/test/http-api/ipfs-api/test-repo.js +++ b/test/http-api/ipfs-api/test-repo.js @@ -5,23 +5,6 @@ // const expect = require('chai').expect // const APIctl = require('ipfs-api') -module.exports = (httpAPI) => { - describe('repo', () => { - describe('api', () => { - // TODO - }) - - describe('gateway', () => {}) - - describe('using js-ipfs-api', () => { - // var ctl - - it('start IPFS API ctl', (done) => { - // ctl = APIctl('/ip4/127.0.0.1/tcp/6001') - done() - }) - - // TODO - }) - }) +module.exports = (ctl) => { + describe('repo', () => {}) // TODO } diff --git a/test/http-api/ipfs-api/test-swarm.js b/test/http-api/ipfs-api/test-swarm.js index 11f2c3816b..ce64fd0adf 100644 --- a/test/http-api/ipfs-api/test-swarm.js +++ b/test/http-api/ipfs-api/test-swarm.js @@ -2,168 +2,67 @@ 'use strict' const expect = require('chai').expect -const APIctl = require('ipfs-api') const createTempNode = require('./../../utils/temp-node') -module.exports = (httpAPI) => { - describe('swarm', function () { - this.timeout(20000) - - describe('api', () => { - var api - var ipfs // tmp node - var ipfsAddr - - before((done) => { - createTempNode(47, (err, _ipfs) => { - expect(err).to.not.exist - ipfs = _ipfs - ipfs.goOnline((err) => { +module.exports = (ctl) => { + describe('.swarm', () => { + let remoteNode + let remoteNodeAddr + + before((done) => { + createTempNode(6, (err, _remoteNode) => { + expect(err).to.not.exist + remoteNode = _remoteNode + remoteNode.goOnline(() => { + remoteNode.id((err, res) => { expect(err).to.not.exist - ipfs.id((err, res) => { - expect(err).to.not.exist - ipfsAddr = `${res.addresses[0]}/ipfs/${res.id}` - done() - }) + remoteNodeAddr = `${res.addresses[0]}/ipfs/${res.id}` + done() }) }) }) - - after((done) => { - setTimeout(() => { - ipfs.goOffline(done) - }, 1000) - }) - - it('gets the api obj', () => { - api = httpAPI.server.select('API') - }) - - it('/swarm/connect returns 400 for request without argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/swarm/connect' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it.skip('/swarm/connect returns 500 for request with invalid argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/swarm/connect?arg=invalid' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - done() - }) - }) - - it('/swarm/connect returns value', (done) => { - api.inject({ - method: 'GET', - url: `/api/v0/swarm/connect?arg=${ipfsAddr}` - }, (res) => { - expect(res.statusCode).to.equal(200) - done() - }) - }) - - it('/swarm/peers returns value', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/swarm/peers' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Strings).to.have.length.above(0) - done() - }) - }) - - it('/swarm/addrs/local returns value', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/swarm/addrs/local' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Strings).to.have.length.above(0) - done() - }) - }) }) - // TODO revisit this - describe.skip('using js-ipfs-api', () => { - var ctl - var ipfs - var ipfsAddr - - before((done) => { - createTempNode(6, (err, _ipfs) => { - expect(err).to.not.exist - ipfs = _ipfs - ipfs.goOnline(() => { - ipfs.id((err, res) => { - expect(err).to.not.exist - ipfsAddr = `${res.addresses[0]}/ipfs/${res.id}` - done() - }) - }) - }) - }) - - after((done) => { - setTimeout(() => { - ipfs.goOffline(done) - }, 1000) - }) + after((done) => { + setTimeout(() => { + remoteNode.goOffline(done) + }, 1000) + }) - it('start IPFS API ctl', (done) => { - ctl = APIctl('/ip4/127.0.0.1/tcp/6001') + it('.connect returns error for request without argument', (done) => { + ctl.swarm.connect(null, (err, result) => { + expect(err).to.exist done() }) + }) - // TODO revisit this - it.skip('ipfs.swarm.connect returns error for request without argument', (done) => { - ctl.swarm.connect(null, (err, result) => { - expect(err).to.exist - done() - }) - }) - - it.skip('ipfs.swarm.connect returns error for request with invalid argument', (done) => { - // TODO fix this - // multiaddr is throwing by trying to cast invalid to multiaddr - ctl.swarm.connect('invalid', (err, result) => { - expect(err).to.exist - done() - }) + it('.connect returns error for request with invalid argument', (done) => { + ctl.swarm.connect('invalid', (err, result) => { + expect(err).to.exist + done() }) + }) - it('ipfs.swarm.connect returns value', (done) => { - ctl.swarm.connect(ipfsAddr, (err, result) => { - expect(err).to.not.exist - done() - }) + it('.connect', (done) => { + ctl.swarm.connect(remoteNodeAddr, (err, result) => { + expect(err).to.not.exist + done() }) + }) - it('ipfs.swarm.peers returns value', (done) => { - ctl.swarm.peers((err, result) => { - expect(err).to.not.exist - expect(result.Strings).to.have.length.above(0) - done() - }) + it('.peers', (done) => { + ctl.swarm.peers((err, multiaddrs) => { + expect(err).to.not.exist + expect(multiaddrs).to.have.length.above(0) + done() }) + }) - it('ipfs.swarm.localAddrsreturns value', (done) => { - ctl.swarm.localAddrs((err, result) => { - expect(err).to.not.exist - expect(result.Strings).to.have.length.above(0) - done() - }) + it('.localAddrs', (done) => { + ctl.swarm.localAddrs((err, multiaddrs) => { + expect(err).to.not.exist + expect(multiaddrs).to.have.length.above(0) + done() }) }) }) diff --git a/test/http-api/ipfs-api/test-version.js b/test/http-api/ipfs-api/test-version.js index fd6269350b..ba204f6ce5 100644 --- a/test/http-api/ipfs-api/test-version.js +++ b/test/http-api/ipfs-api/test-version.js @@ -2,50 +2,17 @@ 'use strict' const expect = require('chai').expect -const APIctl = require('ipfs-api') -const pkgversion = require('./../../../package.json').version -module.exports = (httpAPI) => { - describe('version', () => { - describe('api', () => { - let api - - it('api', () => { - api = httpAPI.server.select('API') - }) - - it('get the version', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/version' - }, (res) => { - expect(res.result.version).to.equal(pkgversion) - expect(res.result).to.have.a.property('commit') - expect(res.result).to.have.a.property('repo') - done() - }) - }) - }) - - describe('gateway', () => {}) - - describe.skip('using js-ipfs-api', () => { - var ctl - - it('start IPFS API ctl', (done) => { - ctl = APIctl('/ip4/127.0.0.1/tcp/6001') +module.exports = (ctl) => { + describe('.version', () => { + it('get the version', (done) => { + ctl.version((err, result) => { + expect(err).to.not.exist + expect(result).to.have.a.property('version') + expect(result).to.have.a.property('commit') + expect(result).to.have.a.property('repo') done() }) - - it('get the version', (done) => { - ctl.version((err, result) => { - expect(err).to.not.exist - expect(result).to.have.a.property('version') - expect(result).to.have.a.property('commit') - expect(result).to.have.a.property('repo') - done() - }) - }) }) }) } From a3d98a8464d07fbec46f69ca0c045ec14e174c2f Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 23 Aug 2016 14:48:43 +0100 Subject: [PATCH 7/7] fix(config): support null values (0 or empty string) on get and set --- package.json | 5 +++-- src/core/ipfs/config.js | 11 ++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index cfcf8e5961..2d79f6fb11 100644 --- a/package.json +++ b/package.json @@ -80,11 +80,12 @@ "joi": "^9.0.4", "libp2p-ipfs": "^0.12.1", "libp2p-ipfs-browser": "^0.12.1", + "lodash.get": "^4.4.1", + "lodash.has": "^4.5.2", + "lodash.set": "^4.3.1", "lodash.sortby": "^4.6.1", "mafmt": "^2.1.1", "map-limit": "0.0.1", - "lodash.get": "^4.4.1", - "lodash.set": "^4.3.1", "multiaddr": "^2.0.2", "multihashes": "^0.2.2", "path-exists": "^3.0.0", diff --git a/src/core/ipfs/config.js b/src/core/ipfs/config.js index 72942bb22e..4fa5ec1189 100644 --- a/src/core/ipfs/config.js +++ b/src/core/ipfs/config.js @@ -2,6 +2,7 @@ const promisify = require('promisify-es6') const _get = require('lodash.get') +const _has = require('lodash.has') const _set = require('lodash.set') module.exports = function config (self) { @@ -24,11 +25,11 @@ module.exports = function config (self) { if (err) { return callback(err) } - const value = _get(config, key, undefined) - if (!value) { - callback(new Error('Key does not exist in config')) - } else { + if (_has(config, key)) { + const value = _get(config, key, undefined) callback(null, value) + } else { + callback(new Error('Key does not exist in config')) } }) }), @@ -37,7 +38,7 @@ module.exports = function config (self) { return callback(new Error('Invalid key type')) } - if (!value || Buffer.isBuffer(value)) { + if (value === undefined || Buffer.isBuffer(value)) { return callback(new Error('Invalid value type')) }