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

Commit c172fcb

Browse files
committed
feat(pin): tests
1 parent 59a45d0 commit c172fcb

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@
4747
"greenkeeperio-bot <[email protected]>",
4848
"nginnever <[email protected]>"
4949
]
50-
}
50+
}

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
exports.object = require('./object')
44
exports.files = require('./files')
55
exports.config = require('./config')
6+
exports.pin = require('./pin')

src/pin.js

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const expect = require('chai').expect
7+
const fs = require('fs')
8+
const path = require('path')
9+
10+
const testfile = fs.readFileSync(path.join(__dirname, './data/testfile.txt'))
11+
12+
module.exports = (common) => {
13+
describe('.pin', () => {
14+
let ipfs
15+
16+
before(function (done) {
17+
// CI is slow
18+
this.timeout(20 * 1000)
19+
20+
common.setup((err, factory) => {
21+
expect(err).to.not.exist
22+
factory.spawnNode((err, node) => {
23+
expect(err).to.not.exist
24+
ipfs = node
25+
done()
26+
})
27+
})
28+
})
29+
30+
after((done) => {
31+
common.teardown(done)
32+
})
33+
34+
it('add file for testing', (done) => {
35+
const expectedMultihash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
36+
37+
ipfs.files.add(testfile, (err, res) => {
38+
expect(err).to.not.exist
39+
40+
expect(res).to.have.length(1)
41+
expect(res[0].hash).to.equal(expectedMultihash)
42+
expect(res[0].path).to.equal(expectedMultihash)
43+
done()
44+
})
45+
})
46+
47+
describe('callback API', () => {
48+
it('.remove (first, because adding is recursive)', (done) => {
49+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
50+
51+
ipfs.pin.rm(hash, { recursive: true }, (err, res) => {
52+
expect(err).to.not.exist
53+
expect(res).to.exist
54+
ipfs.pin.ls({ type: 'direct' }, (err, res) => {
55+
expect(err).to.not.exist
56+
expect(res).to.exist
57+
expect(res.Keys).to.be.empty
58+
done()
59+
})
60+
})
61+
})
62+
63+
it('.add', (done) => {
64+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
65+
66+
ipfs.pin.add(hash, { recursive: false }, (err, res) => {
67+
expect(err).to.not.exist
68+
expect(res.Pins[0]).to.be.equal(hash)
69+
done()
70+
})
71+
})
72+
73+
it('.list', (done) => {
74+
ipfs.pin.ls((err, res) => {
75+
expect(err).to.not.exist
76+
expect(res).to.exist
77+
done()
78+
})
79+
})
80+
81+
it('.list for a specific hash', (done) => {
82+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
83+
84+
ipfs.pin.ls(hash, (err, res) => {
85+
expect(err).to.not.exist
86+
expect(res).to.exist
87+
done()
88+
})
89+
})
90+
})
91+
92+
describe('promise API', () => {
93+
it('.add', () => {
94+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
95+
96+
return ipfs.pin.add(hash, { recursive: false })
97+
.then((res) => {
98+
expect(res.Pins[0]).to.be.equal(hash)
99+
})
100+
})
101+
102+
it('.ls', () => {
103+
return ipfs.pin.ls()
104+
.then((res) => {
105+
expect(res).to.exist
106+
})
107+
})
108+
109+
it('.ls hash', () => {
110+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
111+
112+
return ipfs.pin.ls(hash)
113+
.then((res) => {
114+
expect(res).to.exist
115+
})
116+
})
117+
118+
it('.rm', () => {
119+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
120+
121+
return ipfs.pin.rm(hash, { recursive: false })
122+
.then((res) => {
123+
expect(res).to.exist
124+
return ipfs.pin.ls({ type: 'direct' })
125+
})
126+
.then((res) => {
127+
expect(res).to.exist
128+
expect(res.Keys).to.be.empty
129+
})
130+
})
131+
})
132+
})
133+
}

0 commit comments

Comments
 (0)