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

Commit b75e13b

Browse files
richardschneiderdaviddias
authored andcommitted
feat: key tests (#180)
* feat: key tests * docs(key.md): add spec file
1 parent 42a6ac0 commit b75e13b

File tree

3 files changed

+268
-0
lines changed

3 files changed

+268
-0
lines changed

Diff for: SPEC/KEY.md

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
Key API
2+
=======
3+
4+
#### `gen`
5+
6+
> Generate a new key
7+
8+
##### `Go` **WIP**
9+
10+
##### `JavaScript` - ipfs.key.gen(name, options, [callback])
11+
12+
Where:
13+
14+
- `name` is a local name for the key
15+
- `options` is an object that contains following properties
16+
- 'type' - the key type, one of 'rsa'
17+
- 'size' - the key size in bits
18+
19+
`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`.
20+
21+
If no `callback` is passed, a promise is returned.
22+
23+
**Example:**
24+
25+
```JavaScript
26+
ipfs.key.add(
27+
'my-key',
28+
{ type: 'rsa', size: 2048 },
29+
(err, key) => console.log(key))
30+
31+
32+
{
33+
Name: 'my-key',
34+
Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg'
35+
}
36+
```
37+
38+
#### `list`
39+
40+
> List all the keys
41+
42+
##### `Go` **WIP**
43+
44+
##### `JavaScript` - ipfs.key.list([callback])
45+
46+
`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`)
47+
48+
If no `callback` is passed, a promise is returned.
49+
50+
**Example:**
51+
52+
```JavaScript
53+
ipfs.key.list((err, keys) => console.log(keys))
54+
55+
{
56+
Keys: [
57+
{ Name: 'self',
58+
Id: 'QmRT6i9wXVSmxKi3MxVRduZqF3Wvv8DuV5utMXPN3BxPML' },
59+
{ Name: 'my-key',
60+
Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg' }
61+
]
62+
}
63+
```
64+
65+
#### `rm`
66+
67+
> Remove a key
68+
69+
##### `Go` **WIP**
70+
71+
##### `JavaScript` - ipfs.key.rm(name, [callback])
72+
73+
Where:
74+
- `name` is the local name for the key
75+
76+
`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.
77+
78+
If no `callback` is passed, a promise is returned.
79+
80+
**Example:**
81+
82+
```JavaScript
83+
ipfs.key.rm('my-key', (err, key) => console.log(key))
84+
85+
{
86+
Keys: [
87+
{ Name: 'my-key',
88+
Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg' }
89+
]
90+
}
91+
```
92+
93+
#### `rename`
94+
95+
> Rename a key
96+
97+
##### `Go` **WIP**
98+
99+
##### `JavaScript` - ipfs.key.rename(oldName, newName, [callback])
100+
101+
Where:
102+
- `oldName` is the local name for the key
103+
- `newName` a new name for key
104+
105+
`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.
106+
107+
If no `callback` is passed, a promise is returned.
108+
109+
**Example:**
110+
111+
```JavaScript
112+
ipfs.key.rename(
113+
'my-key',
114+
'my-new-key',
115+
(err, key) => console.log(key))
116+
117+
{
118+
Was: 'my-key',
119+
Now: 'my-new-key',
120+
Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg',
121+
Overwrite: false
122+
}
123+
```
124+

Diff for: src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ exports.block = require('./block')
1111
exports.dht = require('./dht')
1212
exports.dag = require('./dag')
1313
exports.pubsub = require('./pubsub')
14+
exports.key = require('./key')

Diff for: src/key.js

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const chai = require('chai')
7+
const dirtyChai = require('dirty-chai')
8+
const expect = chai.expect
9+
chai.use(dirtyChai)
10+
const hat = require('hat')
11+
12+
module.exports = (common) => {
13+
describe('.key', () => {
14+
const keyTypes = [
15+
{type: 'rsa', size: 2048}
16+
]
17+
const keys = []
18+
let ipfs
19+
20+
before(function (done) {
21+
// CI takes longer to instantiate the daemon, so we need to increase the
22+
// timeout for the before step
23+
this.timeout(60 * 1000)
24+
25+
common.setup((err, factory) => {
26+
expect(err).to.not.exist()
27+
factory.spawnNode((err, node) => {
28+
expect(err).to.not.exist()
29+
ipfs = node
30+
done()
31+
})
32+
})
33+
})
34+
35+
after((done) => common.teardown(done))
36+
37+
describe('.gen', () => {
38+
keyTypes.forEach((kt) => {
39+
it(`creates a new ${kt.type} key`, function (done) {
40+
this.timeout(20 * 1000)
41+
const name = hat()
42+
ipfs.key.gen(name, kt, (err, key) => {
43+
expect(err).to.not.exist()
44+
expect(key).to.exist()
45+
expect(key).to.have.property('Name', name)
46+
expect(key).to.have.property('Id')
47+
keys.push(key)
48+
done()
49+
})
50+
})
51+
})
52+
})
53+
54+
describe('.list', () => {
55+
let listedKeys
56+
it('lists all the keys', (done) => {
57+
ipfs.key.list((err, res) => {
58+
expect(err).to.not.exist()
59+
expect(res).to.exist()
60+
expect(res.Keys).to.exist()
61+
expect(res.Keys.length).to.be.above(keys.length - 1)
62+
listedKeys = res.Keys
63+
done()
64+
})
65+
})
66+
67+
it('contains the created keys', () => {
68+
keys.forEach(ki => {
69+
const found = listedKeys.filter(lk => ki.Name === lk.Name && ki.Id === lk.Id)
70+
expect(found).to.have.length(1)
71+
})
72+
})
73+
})
74+
75+
describe('.rename', () => {
76+
let oldName
77+
let newName
78+
79+
before(() => {
80+
oldName = keys[0].Name
81+
newName = 'x' + oldName
82+
})
83+
84+
it('renames a key', (done) => {
85+
ipfs.key.rename(oldName, newName, (err, res) => {
86+
expect(err).to.not.exist()
87+
expect(res).to.exist()
88+
expect(res).to.have.property('Was', oldName)
89+
expect(res).to.have.property('Now', newName)
90+
expect(res).to.have.property('Id', keys[0].Id)
91+
keys[0].Name = newName
92+
done()
93+
})
94+
})
95+
96+
it('contains the new name', (done) => {
97+
ipfs.key.list((err, res) => {
98+
expect(err).to.not.exist()
99+
const found = res.Keys.filter(k => k.Name === newName)
100+
expect(found).to.have.length(1)
101+
done()
102+
})
103+
})
104+
105+
it('does not contain the old name', (done) => {
106+
ipfs.key.list((err, res) => {
107+
expect(err).to.not.exist()
108+
const found = res.Keys.filter(k => k.Name === oldName)
109+
expect(found).to.have.length(0)
110+
done()
111+
})
112+
})
113+
})
114+
115+
describe('.rm', () => {
116+
let key
117+
before(() => {
118+
key = keys[0]
119+
})
120+
121+
it('removes a key', function (done) {
122+
ipfs.key.rm(key.name, (err, res) => {
123+
expect(err).to.not.exist()
124+
expect(res).to.exist()
125+
expect(res).to.have.property('Keys')
126+
expect(res.Keys).to.have.length(1)
127+
expect(res.Keys[0]).to.have.property('Name', key.Name)
128+
expect(res.Keys[0]).to.have.property('Id', key.Id)
129+
done()
130+
})
131+
})
132+
133+
it('does not contain the removed name', (done) => {
134+
ipfs.key.list((err, res) => {
135+
expect(err).to.not.exist()
136+
const found = res.Keys.filter(k => k.Name === key.name)
137+
expect(found).to.have.length(0)
138+
done()
139+
})
140+
})
141+
})
142+
})
143+
}

0 commit comments

Comments
 (0)