Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
config API + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jul 1, 2016
1 parent 666bc94 commit 2803c66
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 1 deletion.
52 changes: 52 additions & 0 deletions API/config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
config API
==========

#### `config.get`

> Returns the currently being used config. If the daemon is off, it returns the stored config.
##### `Go` **WIP**

##### `JavaScript` - ipfs.config.get([key, callback])

`key` is the key of the value that should be fetched from the config file. If no key is passed, then the whole config should be returned. `key` should be of type String.

`callback` must follow `function (err, config) {}` signature, where `err` is an error if the operation was not successful and `config` is a JSON object containing the configuration of the IPFS node.

If no callback is passed, a [promise][] is returned

#### `config.set`

> Adds or replaces a config value.
##### `Go` **WIP**

##### `JavaScript` - ipfs.config.set(key, value, [callback])

`key` is the key value that will be added or replaced (in case of the value already). `key` should be of type String.

`value` value to be set.

`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.

If no callback is passed, a [promise][] is returned

Note that this operation will spark the restart of any service, i.e: if a config.replace changes the multiaddrs of the Swarm, Swarm will have to be restarted manually for the changes to take difference.

#### `config.replace`

> Adds or replaces a config value.
##### `Go` **WIP**

##### `JavaScript` - ipfs.config.replace(config, [callback])

`config` is a JSON object that contains the new config.

`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.

If no callback is passed, a [promise][] is returned

Note that this operation will spark the restart of any service, i.e: if a config.replace changes the multiaddrs of the Swarm, Swarm will have to be restarted manually for the changes to take difference.

[promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
145 changes: 145 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect

module.exports = (common) => {
describe('.config', () => {
let ipfs

before((done) => {
common.setup((err, _ipfs) => {
expect(err).to.not.exist
ipfs = _ipfs
done()
})
})

after((done) => {
common.teardown(done)
})

describe('callback API', () => {
describe('.get', () => {
it('retrieve the whole config', (done) => {
ipfs.config.get((err, config) => {
expect(err).to.not.exist
expect(config).to.exist
done()
})
})

it('retrieve a value through a key', (done) => {
ipfs.config.get('Identity', (err, identity) => {
expect(err).to.not.exist
expect(identity).to.exist
done()
})
})

it('retrieve a value through a nested key', (done) => {
ipfs.config.get('Addresses.Swarm', (err, swarmAddrs) => {
expect(err).to.not.exist
expect(swarmAddrs).to.exist
done()
})
})

it('fail on non valid key', (done) => {
ipfs.config.get(1234, (err, peerId) => {
expect(err).to.exist
done()
})
})

it('fail on non existent key', (done) => {
ipfs.config.get('Bananas', (err, peerId) => {
expect(err).to.exist
done()
})
})
})
https://github.com/ipfs/go-ipfs/issues/2927
describe('.set', () => {
it('set a new key', (done) => {
ipfs.config.set('Fruit', 'banana', (err) => {
expect(err).to.not.exist
ipfs.config.get('Fruit', (err, fruit) => {
expect(err).to.not.exist
expect(fruit).to.equal('banana')
done()
})
})
})

it('set an already existing key', (done) => {
ipfs.config.set('Fruit', 'morango', (err) => {
expect(err).to.not.exist
ipfs.config.get('Fruit', (err, fruit) => {
expect(err).to.not.exist
expect(fruit).to.equal('morango')
done()
})
})
})

it('set a json object', (done) => {
const key = 'API.HTTPHeaders.Access-Control-Allow-Origin'
const val = ['http://example.io']
ipfs.config.set(key, val, function (err) {
expect(err).to.not.exist
ipfs.config.get(key, function (err, result) {
expect(err).to.not.exist
expect(result).to.deep.equal(val)
done()
})
})
})

it('fail on non valid key', (done) => {
ipfs.config.set(new Buffer('heeey'), '', (err) => {
expect(err).to.exist
done()
})
})

it('fail on non valid value', (done) => {
ipfs.config.set('Fruit', new Buffer('abc'), (err) => {
expect(err).to.exist
done()
})
})
})

// Waiting for fix on go-ipfs
// - https://github.com/ipfs/js-ipfs-api/pull/307#discussion_r69281789
// - https://github.com/ipfs/go-ipfs/issues/2927
describe.skip('.replace', () => {
const config = {
Fruit: 'Bananas'
}

it('replace the whole config', (done) => {
ipfs.config.replace(config, (err) => {
expect(err).to.not.exist
ipfs.config.get((err, _config) => {
expect(err).to.not.exist
expect(_config).to.deep.equal(config)
})
})
})

it('replace to empty config', (done) => {
ipfs.config.replace({}, (err) => {
expect(err).to.not.exist
ipfs.config.get((err, _config) => {
expect(err).to.not.exist
expect(_config).to.deep.equal(config)
})
})
})
})
})
describe('promise API', () => {})
})
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'

exports.all = () => {}
exports.object = require('./object')
exports.files = require('./files')
exports.config = require('./config')

0 comments on commit 2803c66

Please sign in to comment.