From 6d8f77e81770babb1e59714e528d1c8464ac7b8e Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 30 Jun 2016 14:40:35 +0100 Subject: [PATCH] config API + tests --- API/config/README.md | 52 +++++++++++++ src/config.js | 179 +++++++++++++++++++++++++++++++++++++++++++ src/index.js | 2 +- 3 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 API/config/README.md create mode 100644 src/config.js diff --git a/API/config/README.md b/API/config/README.md new file mode 100644 index 00000000..73edc6aa --- /dev/null +++ b/API/config/README.md @@ -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 **not** 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 **not** 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 diff --git a/src/config.js b/src/config.js new file mode 100644 index 00000000..5bd1a09b --- /dev/null +++ b/src/config.js @@ -0,0 +1,179 @@ +/* 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() + }) + }) + }) + 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', () => { + describe('.get', () => { + it('retrieve the whole config', (done) => { + ipfs.config.get() + .then((config) => { + expect(config).to.exist + done() + }) + .catch((err) => { + expect(err).to.not.exist + }) + }) + }) + + describe('.set', () => { + it('set a new key', (done) => { + ipfs.config.set('Fruit', 'banana') + .then(() => { + ipfs.config.get('Fruit', (err, fruit) => { + expect(err).to.not.exist + expect(fruit).to.equal('banana') + done() + }) + }) + .catch((err) => { + expect(err).to.not.exist + }) + }) + }) + + // 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', () => {}) + }) + }) +} diff --git a/src/index.js b/src/index.js index 574aec51..33c4ad49 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,5 @@ 'use strict' -exports.all = () => {} exports.object = require('./object') exports.files = require('./files') +exports.config = require('./config')