From b75e13b52a6dfee0223266c2cdfe0e414d1ea0bb Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Sun, 10 Dec 2017 22:50:49 +1300 Subject: [PATCH] feat: key tests (#180) * feat: key tests * docs(key.md): add spec file --- SPEC/KEY.md | 124 ++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 1 + src/key.js | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 268 insertions(+) create mode 100644 SPEC/KEY.md create mode 100644 src/key.js diff --git a/SPEC/KEY.md b/SPEC/KEY.md new file mode 100644 index 00000000..e9172f1b --- /dev/null +++ b/SPEC/KEY.md @@ -0,0 +1,124 @@ +Key API +======= + +#### `gen` + +> Generate a new key + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.key.gen(name, options, [callback]) + +Where: + +- `name` is a local name for the key +- `options` is an object that contains following properties + - 'type' - the key type, one of 'rsa' + - 'size' - the key size in bits + +`callback` must follow `function (err, key) {}` signature, where `err` is an Error if the operation was not successful. `key` is an object that describes the key; `name` and `id`. + +If no `callback` is passed, a promise is returned. + +**Example:** + +```JavaScript +ipfs.key.add( + 'my-key', + { type: 'rsa', size: 2048 }, + (err, key) => console.log(key)) + + +{ + Name: 'my-key', + Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg' +} +``` + +#### `list` + +> List all the keys + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.key.list([callback]) + +`callback` must follow `function (err, keys) {}` signature, where `err` is an Error if the operation was not successful. `keys` is an object with the property `Keys` that is an array of `KeyInfo` (`name` and `id`) + +If no `callback` is passed, a promise is returned. + +**Example:** + +```JavaScript +ipfs.key.list((err, keys) => console.log(keys)) + +{ + Keys: [ + { Name: 'self', + Id: 'QmRT6i9wXVSmxKi3MxVRduZqF3Wvv8DuV5utMXPN3BxPML' }, + { Name: 'my-key', + Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg' } + ] +} +``` + +#### `rm` + +> Remove a key + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.key.rm(name, [callback]) + +Where: +- `name` is the local name for the key + +`callback` must follow `function (err, key) {}` signature, where `err` is an Error if the operation was not successful. `key` is an object that describes the removed key. + +If no `callback` is passed, a promise is returned. + +**Example:** + +```JavaScript +ipfs.key.rm('my-key', (err, key) => console.log(key)) + +{ + Keys: [ + { Name: 'my-key', + Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg' } + ] +} +``` + +#### `rename` + +> Rename a key + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.key.rename(oldName, newName, [callback]) + +Where: +- `oldName` is the local name for the key +- `newName` a new name for key + +`callback` must follow `function (err, key) {}` signature, where `err` is an Error if the operation was not successful. `key` is an object that describes the renamed key. + +If no `callback` is passed, a promise is returned. + +**Example:** + +```JavaScript +ipfs.key.rename( + 'my-key', + 'my-new-key', + (err, key) => console.log(key)) + +{ + Was: 'my-key', + Now: 'my-new-key', + Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg', + Overwrite: false +} +``` + diff --git a/src/index.js b/src/index.js index 7291caf9..60f79d0e 100644 --- a/src/index.js +++ b/src/index.js @@ -11,3 +11,4 @@ exports.block = require('./block') exports.dht = require('./dht') exports.dag = require('./dag') exports.pubsub = require('./pubsub') +exports.key = require('./key') diff --git a/src/key.js b/src/key.js new file mode 100644 index 00000000..5f7e1e2f --- /dev/null +++ b/src/key.js @@ -0,0 +1,143 @@ +/* eslint-env mocha */ +/* eslint max-nested-callbacks: ["error", 8] */ + +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +chai.use(dirtyChai) +const hat = require('hat') + +module.exports = (common) => { + describe('.key', () => { + const keyTypes = [ + {type: 'rsa', size: 2048} + ] + const keys = [] + let ipfs + + before(function (done) { + // CI takes longer to instantiate the daemon, so we need to increase the + // timeout for the before step + this.timeout(60 * 1000) + + common.setup((err, factory) => { + expect(err).to.not.exist() + factory.spawnNode((err, node) => { + expect(err).to.not.exist() + ipfs = node + done() + }) + }) + }) + + after((done) => common.teardown(done)) + + describe('.gen', () => { + keyTypes.forEach((kt) => { + it(`creates a new ${kt.type} key`, function (done) { + this.timeout(20 * 1000) + const name = hat() + ipfs.key.gen(name, kt, (err, key) => { + expect(err).to.not.exist() + expect(key).to.exist() + expect(key).to.have.property('Name', name) + expect(key).to.have.property('Id') + keys.push(key) + done() + }) + }) + }) + }) + + describe('.list', () => { + let listedKeys + it('lists all the keys', (done) => { + ipfs.key.list((err, res) => { + expect(err).to.not.exist() + expect(res).to.exist() + expect(res.Keys).to.exist() + expect(res.Keys.length).to.be.above(keys.length - 1) + listedKeys = res.Keys + done() + }) + }) + + it('contains the created keys', () => { + keys.forEach(ki => { + const found = listedKeys.filter(lk => ki.Name === lk.Name && ki.Id === lk.Id) + expect(found).to.have.length(1) + }) + }) + }) + + describe('.rename', () => { + let oldName + let newName + + before(() => { + oldName = keys[0].Name + newName = 'x' + oldName + }) + + it('renames a key', (done) => { + ipfs.key.rename(oldName, newName, (err, res) => { + expect(err).to.not.exist() + expect(res).to.exist() + expect(res).to.have.property('Was', oldName) + expect(res).to.have.property('Now', newName) + expect(res).to.have.property('Id', keys[0].Id) + keys[0].Name = newName + done() + }) + }) + + it('contains the new name', (done) => { + ipfs.key.list((err, res) => { + expect(err).to.not.exist() + const found = res.Keys.filter(k => k.Name === newName) + expect(found).to.have.length(1) + done() + }) + }) + + it('does not contain the old name', (done) => { + ipfs.key.list((err, res) => { + expect(err).to.not.exist() + const found = res.Keys.filter(k => k.Name === oldName) + expect(found).to.have.length(0) + done() + }) + }) + }) + + describe('.rm', () => { + let key + before(() => { + key = keys[0] + }) + + it('removes a key', function (done) { + ipfs.key.rm(key.name, (err, res) => { + expect(err).to.not.exist() + expect(res).to.exist() + expect(res).to.have.property('Keys') + expect(res.Keys).to.have.length(1) + expect(res.Keys[0]).to.have.property('Name', key.Name) + expect(res.Keys[0]).to.have.property('Id', key.Id) + done() + }) + }) + + it('does not contain the removed name', (done) => { + ipfs.key.list((err, res) => { + expect(err).to.not.exist() + const found = res.Keys.filter(k => k.Name === key.name) + expect(found).to.have.length(0) + done() + }) + }) + }) + }) +}