Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: rework http-api tests to use ipfsd-ctl (#1234)
Browse files Browse the repository at this point in the history
* feat: rework http-api tests to use ipfsd-ctl

* fix: copy repo

* fix: revert fixture changes

* fix: revert fixture changes

* fix: remove unused todo
  • Loading branch information
dryajov authored and daviddias committed Feb 26, 2018
1 parent 4416e6d commit 52cb801
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 187 deletions.
37 changes: 25 additions & 12 deletions test/http-api/extra/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@ chai.use(dirtyChai)

const multihash = require('multihashes')
const waterfall = require('async/waterfall')
const getCtl = require('./utils/get-ctl.js')

module.exports = (http) => {
let ctl = null
before(() => {
ctl = getCtl(http)
const DaemonFactory = require('ipfsd-ctl')
const df = DaemonFactory.create({ exec: 'src/cli/bin.js' })

describe('extra block', () => {
let ipfs = null
let ipfsd = null
before(function (done) {
this.timeout(20 * 1000)

df.spawn({ initOptions: { bits: 512 } }, (err, _ipfsd) => {
expect(err).to.not.exist()
ipfsd = _ipfsd
ipfs = ipfsd.api
done()
})
})

after((done) => ipfsd.stop(done))

describe('.block', () => {
describe('.put', () => {
it('updates value', (done) => {
Expand All @@ -25,7 +38,7 @@ module.exports = (http) => {
}

waterfall([
(cb) => ctl.block.put(data, cb),
(cb) => ipfs.block.put(data, cb),
(block, cb) => {
expect(block.cid.multihash).to.eql(
multihash.fromB58String(expectedResult.key)
Expand All @@ -38,14 +51,14 @@ module.exports = (http) => {

describe('.get', () => {
it('returns error for request with invalid argument', (done) => {
ctl.block.get('invalid', (err, result) => {
ipfs.block.get('invalid', (err, result) => {
expect(err).to.exist()
done()
})
})

it('returns value', (done) => {
ctl.block.get('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', (err, result) => {
ipfs.block.get('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', (err, result) => {
expect(err).to.not.exist()
expect(result.data.toString())
.to.equal('hello world\n')
Expand All @@ -56,21 +69,21 @@ module.exports = (http) => {

describe('.stat', () => {
it('returns error for request without argument', (done) => {
ctl.block.stat(null, (err, result) => {
ipfs.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) => {
ipfs.block.stat('invalid', (err, result) => {
expect(err).to.exist()
done()
})
})

it('returns value', (done) => {
ctl.block.stat('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', (err, result) => {
ipfs.block.stat('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', (err, result) => {
expect(err).to.not.exist()
expect(result.key)
.to.equal('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
Expand All @@ -80,4 +93,4 @@ module.exports = (http) => {
})
})
})
}
})
50 changes: 32 additions & 18 deletions test/http-api/extra/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const getCtl = require('./utils/get-ctl.js')

module.exports = (http) => {
let ctl = null
before(() => {
ctl = getCtl(http)
const DaemonFactory = require('ipfsd-ctl')
const df = DaemonFactory.create({ exec: 'src/cli/bin.js' })

describe('extra bootstrap', () => {
let ipfs = null
let ipfsd = null
before(function (done) {
this.timeout(20 * 1000)

df.spawn({ initOptions: { bits: 512 } }, (err, _ipfsd) => {
expect(err).to.not.exist()
ipfsd = _ipfsd
ipfs = ipfsd.api
done()
})
})

after((done) => ipfsd.stop(done))

describe('.bootstrap', () => {
const invalidArg = 'this/Is/So/Invalid/'
const validIp4 = '/ip4/101.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z'
Expand All @@ -21,35 +35,35 @@ module.exports = (http) => {
this.timeout(40 * 1000)

it('returns an error when called with an invalid arg', (done) => {
ctl.bootstrap.add(invalidArg, (err) => {
ipfs.bootstrap.add(invalidArg, (err) => {
expect(err).to.be.an.instanceof(Error)
done()
})
})

it('returns a list of containing the bootstrap peer when called with a valid arg (ip4)', (done) => {
ctl.bootstrap.add(validIp4, (err, res) => {
ipfs.bootstrap.add(validIp4, (err, res) => {
expect(err).to.not.exist()
expect(res).to.be.eql({ Peers: [validIp4] })
done()
})
})

it('prevents duplicate inserts of bootstrap peers', () => {
return ctl
return ipfs
.bootstrap
.rm(null, { all: true })
.then((res) => {
expect(res.Peers.length).to.equal(0)
return ctl.bootstrap.add(validIp4)
return ipfs.bootstrap.add(validIp4)
})
.then(res => {
expect(res).to.be.eql({ Peers: [validIp4] })
return ctl.bootstrap.add(validIp4)
return ipfs.bootstrap.add(validIp4)
})
.then((res) => {
expect(res).to.be.eql({ Peers: [validIp4] })
return ctl.bootstrap.list()
return ipfs.bootstrap.list()
})
.then((res) => {
expect(res).to.exist()
Expand All @@ -60,7 +74,7 @@ module.exports = (http) => {
})

it('returns a list of bootstrap peers when called with the default option', (done) => {
ctl.bootstrap.add({ default: true }, (err, res) => {
ipfs.bootstrap.add({ default: true }, (err, res) => {
expect(err).to.not.exist()
peers = res.Peers
expect(peers).to.exist()
Expand All @@ -72,7 +86,7 @@ module.exports = (http) => {

describe('.list', () => {
it('returns a list of peers', (done) => {
ctl.bootstrap.list((err, res) => {
ipfs.bootstrap.list((err, res) => {
expect(err).to.not.exist()
peers = res.Peers
expect(peers).to.exist()
Expand All @@ -83,14 +97,14 @@ module.exports = (http) => {

describe('.rm', () => {
it('returns an error when called with an invalid arg', (done) => {
ctl.bootstrap.rm(invalidArg, (err) => {
ipfs.bootstrap.rm(invalidArg, (err) => {
expect(err).to.be.an.instanceof(Error)
done()
})
})

it('returns empty list because no peers removed when called without an arg or options', (done) => {
ctl.bootstrap.rm(null, (err, res) => {
ipfs.bootstrap.rm(null, (err, res) => {
expect(err).to.not.exist()
peers = res.Peers
expect(peers).to.exist()
Expand All @@ -100,7 +114,7 @@ module.exports = (http) => {
})

it('returns list containing the peer removed when called with a valid arg (ip4)', (done) => {
ctl.bootstrap.rm(validIp4, (err, res) => {
ipfs.bootstrap.rm(validIp4, (err, res) => {
expect(err).to.not.exist()

peers = res.Peers
Expand All @@ -111,7 +125,7 @@ module.exports = (http) => {
})

it('returns list of all peers removed when all option is passed', (done) => {
ctl.bootstrap.rm(null, { all: true }, (err, res) => {
ipfs.bootstrap.rm(null, { all: true }, (err, res) => {
expect(err).to.not.exist()
peers = res.Peers
expect(peers).to.exist()
Expand All @@ -120,4 +134,4 @@ module.exports = (http) => {
})
})
})
}
})
81 changes: 60 additions & 21 deletions test/http-api/extra/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,108 @@ const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const ncp = require('ncp').ncp
const rimraf = require('rimraf')
const waterfall = require('async/waterfall')

const fs = require('fs')
const path = require('path')
const getCtl = require('./utils/get-ctl.js')

module.exports = (http) => {
let ctl = null
before(() => {
ctl = getCtl(http)
})
describe('.config', () => {
const configPath = path.join(__dirname, '../../repo-tests-run/config')
const DaemonFactory = require('ipfsd-ctl')
const df = DaemonFactory.create({ exec: 'src/cli/bin.js' })

describe('extra config', () => {
const repoExample = path.join(__dirname, '../../fixtures/go-ipfs-repo')
const repoTests = path.join(__dirname, '../../repo-tests-run')

let updatedConfig = null

let ipfs = null
let ipfsd = null
before(function (done) {
this.timeout(20 * 1000)

ncp(repoExample, repoTests, (err) => {
expect(err).to.not.exist()

waterfall([
(cb) => df.spawn({
repoPath: repoTests,
initOptions: { bits: 512 },
disposable: false,
start: true
}, cb),
(_ipfsd, cb) => {
ipfsd = _ipfsd
ipfsd.start(cb)
}
], (err) => {
expect(err).to.not.exist()
ipfs = ipfsd.api

updatedConfig = () => {
const file = fs.readFileSync(path.join(__dirname, '../../repo-tests-run/config'))
return JSON.parse(file, 'utf8')
}

let updatedConfig
done()
})
})
})

before(() => {
updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8'))
after((done) => {
rimraf(repoTests, (err) => {
expect(err).to.not.exist()
ipfsd.stop(done)
})
})

describe('.config', () => {
it('.get returns error for request with invalid argument', (done) => {
ctl.config.get('kittens', (err, res) => {
ipfs.config.get('kittens', (err, res) => {
expect(err).to.exist()
done()
})
})

it('.get returns value for request with argument', (done) => {
ctl.config.get('API.HTTPHeaders', (err, value) => {
ipfs.config.get('API.HTTPHeaders', (err, value) => {
expect(err).not.to.exist()
expect(value).to.equal(null)
done()
})
})

it('.set updates value for request with both args', (done) => {
ctl.config.set('Datastore.Path', 'kitten', (err) => {
ipfs.config.set('Datastore.Path', 'kitten', (err) => {
expect(err).not.to.exist()
done()
})
})

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) => {
ipfs.config.set('Datastore.Path', 'kitten', { json: true }, (err) => {
expect(err).to.exist()
done()
})
})

it('.set updates value for request with both args and bool flag and true argument', (done) => {
ctl.config.set('Datastore.Path', true, (err) => {
ipfs.config.set('Datastore.Path', true, (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) => {
ipfs.config.set('Datastore.Path', false, (err) => {
expect(err).not.to.exist()
done()
})
})

it('.get updatedConfig', (done) => {
ctl.config.get((err, config) => {
ipfs.config.get((err, config) => {
expect(err).not.to.exist()
expect(config).to.be.eql(updatedConfig())
done()
Expand All @@ -81,7 +120,7 @@ module.exports = (http) => {
it('returns error if the config is invalid', (done) => {
const filePath = 'test/fixtures/test-data/badconfig'

ctl.config.replace(filePath, (err) => {
ipfs.config.replace(filePath, (err) => {
expect(err).to.exist()
done()
})
Expand All @@ -91,12 +130,12 @@ module.exports = (http) => {
const filePath = 'test/fixtures/test-data/otherconfig'
const expectedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8'))

ctl.config.replace(filePath, (err) => {
ipfs.config.replace(filePath, (err) => {
expect(err).not.to.exist()
expect(expectedConfig).to.deep.equal(updatedConfig())
done()
})
})
})
})
}
})
Loading

0 comments on commit 52cb801

Please sign in to comment.