Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

[WIP] test: add tests for key manipulations #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 4 additions & 46 deletions test/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
78 changes: 78 additions & 0 deletions test/keys.js
Original file line number Diff line number Diff line change
@@ -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))
)
})
})
})
1 change: 1 addition & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ require('./exchange-files')
require('./kad-dht')
require('./pin')
require('./files')
require('./keys')
54 changes: 54 additions & 0 deletions test/utils/compare.js
Original file line number Diff line number Diff line change
@@ -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
}