diff --git a/test/files.js b/test/files.js index a2d83632..d1c9ca96 100644 --- a/test/files.js +++ b/test/files.js @@ -12,10 +12,10 @@ const { spawnInitAndStartJsDaemon, stopDaemon } = require('./utils/daemon') - -class ExpectedError extends Error { - -} +const { + compare, + compareErrors +} = require('./utils/compare') function checkNodeTypes (daemon, file) { return daemon.api.object.get(file.hash) @@ -52,48 +52,6 @@ function addFile (daemon, data) { }) } -const compare = (...ops) => { - expect(ops.length).to.be.above(1) - - return Promise.all( - ops - ) - .then(results => { - expect(results.length).to.equal(ops.length) - - const result = results.pop() - - results.forEach(res => expect(res).to.deep.equal(result)) - }) -} - -const compareErrors = (...ops) => { - expect(ops.length).to.be.above(1) - - return Promise.all( - // even if operations fail, their errors should be the same - ops.map(op => op.then(() => { - throw new ExpectedError('Expected operation to fail') - }).catch(error => { - if (error instanceof ExpectedError) { - throw error - } - - return { - message: error.message, - code: error.code - } - })) - ) - .then(results => { - expect(results.length).to.equal(ops.length) - - const result = results.pop() - - results.forEach(res => expect(res).to.deep.equal(result)) - }) -} - describe('files', function () { this.timeout(50 * 1000) diff --git a/test/keys.js b/test/keys.js new file mode 100644 index 00000000..e703b37d --- /dev/null +++ b/test/keys.js @@ -0,0 +1,78 @@ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +chai.use(dirtyChai) +const { + spawnInitAndStartGoDaemon, + spawnInitAndStartJsDaemon, + stopDaemon +} = require('./utils/daemon') +const { + compare +} = require('./utils/compare') + +describe.only('keys', function () { + this.timeout(50 * 1000) + + let go + let js + + before(() => { + return Promise.all([ + spawnInitAndStartGoDaemon(), + spawnInitAndStartJsDaemon() + ]) + .then(([goDaemon, jsDaemon]) => { + go = goDaemon + js = jsDaemon + }) + }) + + after(() => { + return Promise.all([ + stopDaemon(go), + stopDaemon(js) + ]) + }) + + it('lists keys', () => { + const operation = (daemon) => { + return daemon.api.key.list() + } + + return compare( + // key identities will be different so just compare the names + operation(go).then(keys => keys.map(key => key.name)), + operation(js).then(keys => keys.map(key => key.name)) + ) + }) + + const keyTypes = [{ + name: 'rsa', + options: { + type: 'rsa' + } + }, { + name: 'ed25519', + options: { + type: 'ed25519' + } + }] + + keyTypes.forEach(type => { + it(`generates ${type.name} keys`, () => { + const operation = (daemon) => { + return daemon.api.key.gen(type.name, type.options) + .then(() => daemon.api.key.list()) + } + + return compare( + // key identities will be different so just compare the names + operation(go).then(keys => keys.map(key => key.name)), + operation(js).then(keys => keys.map(key => key.name)) + ) + }) + }) +}) diff --git a/test/node.js b/test/node.js index a83c4c90..ce0dc5a0 100644 --- a/test/node.js +++ b/test/node.js @@ -8,3 +8,4 @@ require('./exchange-files') require('./kad-dht') require('./pin') require('./files') +require('./keys') diff --git a/test/utils/compare.js b/test/utils/compare.js new file mode 100644 index 00000000..43a50f68 --- /dev/null +++ b/test/utils/compare.js @@ -0,0 +1,54 @@ +'use strict' + +const expect = require('chai').expect + +class ExpectedError extends Error { + +} + +const compare = (...ops) => { + expect(ops.length).to.be.above(1) + + return Promise.all( + ops + ) + .then(results => { + expect(results.length).to.equal(ops.length) + + const result = results.pop() + + results.forEach(res => expect(res).to.deep.equal(result)) + }) +} + +const compareErrors = (...ops) => { + expect(ops.length).to.be.above(1) + + return Promise.all( + // even if operations fail, their errors should be the same + ops.map(op => op.then(() => { + throw new ExpectedError('Expected operation to fail') + }).catch(error => { + if (error instanceof ExpectedError) { + throw error + } + + return { + message: error.message, + code: error.code + } + })) + ) + .then(results => { + expect(results.length).to.equal(ops.length) + + const result = results.pop() + + results.forEach(res => expect(res).to.deep.equal(result)) + }) +} + +module.exports = { + compare, + compareErrors +}