Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: tests for object API
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
Alan Shaw committed Dec 14, 2018
1 parent 6ac74bd commit 0295b2f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 62 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
"ipfs-bitswap": "~0.21.0",
"ipfs-block": "~0.8.0",
"ipfs-block-service": "~0.15.1",
"ipfs-http-client": "^28.0.1",
"ipfs-http-client": "github:ipfs/js-ipfs-http-client#feat/cid-base-option2",
"ipfs-http-response": "~0.2.1",
"ipfs-mfs": "~0.8.0",
"ipfs-multipart": "~0.1.0",
Expand Down
47 changes: 20 additions & 27 deletions src/cli/commands/object/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const multibase = require('multibase')
const { print } = require('../../utils')
const dagPB = require('ipld-dag-pb')
const { cidToString } = require('../../../utils/cid')

module.exports = {
Expand All @@ -28,32 +27,26 @@ module.exports = {
throw err
}

dagPB.util.cid(node, (err, result) => {
if (err) {
throw err
}

let data = node.data

if (Buffer.isBuffer(data)) {
data = node.data.toString(dataEncoding || undefined)
}

const answer = {
Data: data,
Hash: cidToString(result, { base: cidBase, upgrade: false }),
Size: node.size,
Links: node.links.map((l) => {
return {
Name: l.name,
Size: l.size,
Hash: cidToString(l.cid, { base: cidBase, upgrade: false })
}
})
}

print(JSON.stringify(answer))
})
let data = node.data

if (Buffer.isBuffer(data)) {
data = node.data.toString(dataEncoding || undefined)
}

const answer = {
Data: data,
Hash: cidToString(key, { base: cidBase, upgrade: false }),
Size: node.size,
Links: node.links.map((l) => {
return {
Name: l.name,
Size: l.size,
Hash: cidToString(l.cid, { base: cidBase, upgrade: false })
}
})
}

print(JSON.stringify(answer))
})
}
}
21 changes: 9 additions & 12 deletions src/http/api/resources/object.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const mh = require('multihashes')
const CID = require('cids')
const multipart = require('ipfs-multipart')
const dagPB = require('ipld-dag-pb')
const DAGLink = dagPB.DAGLink
Expand All @@ -23,7 +23,7 @@ exports.parseKey = (request, reply) => {

try {
return reply({
key: mh.fromB58String(request.query.arg)
key: new CID(request.query.arg)
})
} catch (err) {
log.error(err)
Expand Down Expand Up @@ -323,10 +323,7 @@ exports.links = {
const key = request.pre.args.key
const ipfs = request.server.app.ipfs

waterfall([
(cb) => ipfs.object.get(key, cb),
(node, cb) => dagPB.util.cid(node, (err, cid) => cb(err, { node, cid }))
], (err, results) => {
ipfs.object.get(key, (err, node) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -335,10 +332,10 @@ exports.links = {
}).code(500)
}

const nodeJSON = results.node.toJSON()
const nodeJSON = node.toJSON()

return reply({
Hash: cidToString(results.cid, { base: request.query['cid-base'], upgrade: false }),
Hash: cidToString(key, { base: request.query['cid-base'], upgrade: false }),
Links: nodeJSON.links.map((l) => {
return {
Name: l.name,
Expand Down Expand Up @@ -379,7 +376,7 @@ exports.parseKeyAndData = (request, reply) => {
return reply({
data: file,
// TODO: support ipfs paths: https://github.com/ipfs/http-api-spec/pull/68/files#diff-2625016b50d68d922257f74801cac29cR3880
key: mh.fromB58String(request.query.arg)
key: new CID(request.query.arg)
})
} catch (err) {
return reply({
Expand Down Expand Up @@ -517,9 +514,9 @@ exports.patchAddLink = {

try {
return reply({
root: mh.fromB58String(request.query.arg[0]),
root: new CID(request.query.arg[0]),
name: request.query.arg[1],
ref: mh.fromB58String(request.query.arg[2])
ref: new CID(request.query.arg[2])
})
} catch (err) {
log.error(err)
Expand Down Expand Up @@ -590,7 +587,7 @@ exports.patchRmLink = {

try {
return reply({
root: mh.fromB58String(request.query.arg[0]),
root: new CID(request.query.arg[0]),
link: request.query.arg[1]
})
} catch (err) {
Expand Down
54 changes: 32 additions & 22 deletions test/cli/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const path = require('path')
const fs = require('fs')
const crypto = require('crypto')
const os = require('os')
const multibase = require('multibase')

describe('object', () => runOnAndOff((thing) => {
let ipfs
Expand All @@ -33,22 +34,15 @@ describe('object', () => runOnAndOff((thing) => {
})
})

it('should new and print CID encoded in specified base', () => {
// TODO: unskip after switch to v1 CIDs by default
it.skip('should new and print CID encoded in specified base', () => {
return ipfs('object new --cid-base=base64').then((out) => {
expect(out).to.eql(
'mAXASIOOwxEKY/BwUmvv0yJlvuSQnrkHkZJuTTKSVmRt4UrhV\n'
)
})
})

it('should new and print CID encoded in specified base', () => {
return ipfs('object new --cid-base=base32').then((out) => {
expect(out).to.eql(
'bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku\n'
)
})
})

it('get', () => {
return ipfs('object get QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n').then((out) => {
const result = JSON.parse(out)
Expand Down Expand Up @@ -86,13 +80,18 @@ describe('object', () => runOnAndOff((thing) => {
})

it('should get and print CIDs encoded in specified base', () => {
return ipfs('object put test/fixtures/test-data/node.json')
.then(out => out.replace('added', '').trim())
return ipfs('add test/fixtures/planets -r --cid-version=1')
.then(out => {
const lines = out.trim().split('\n')
return lines[lines.length - 1].split(' ')[1]
})
.then(cid => ipfs(`object get ${cid} --cid-base=base64`))
.then(out => {
const result = JSON.parse(out)
expect(result.Hash).to.equal('mAXASIKbM02Neyt6L1RRLYVEOuNlqDOzTvBboo3cI/u6f/+Vk')
expect(result.Links[0].Hash).to.equal('mAXASIIq3psXnRzeHisc4Y8t2c50V1GZt5E5XVr9Vovnpq19E')
expect(multibase.isEncoded(result.Hash)).to.deep.equal('base64')
result.Links.forEach(l => {
expect(multibase.isEncoded(l.Hash)).to.deep.equal('base64')
})
})
})

Expand All @@ -104,7 +103,8 @@ describe('object', () => runOnAndOff((thing) => {
})
})

it('should put and print CID encoded in specified base', () => {
// TODO: unskip after switch to v1 CIDs by default
it.skip('should put and print CID encoded in specified base', () => {
return ipfs('object put test/fixtures/test-data/node.json --cid-base=base64')
.then((out) => {
expect(out).to.eql(
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('object', () => runOnAndOff((thing) => {
})
})

it('unadulterated data', function () {
it('unaltered data', function () {
this.timeout(10 * 1000)

// has to be big enough to span several DAGNodes
Expand Down Expand Up @@ -163,11 +163,17 @@ describe('object', () => runOnAndOff((thing) => {
})

it('should get links and print CIDs encoded in specified base', () => {
return ipfs('object put test/fixtures/test-data/node.json')
.then(out => out.replace('added', '').trim())
return ipfs('add test/fixtures/planets -r --cid-version=1')
.then(out => {
const lines = out.trim().split('\n')
return lines[lines.length - 1].split(' ')[1]
})
.then(cid => ipfs(`object links ${cid} --cid-base=base64`))
.then(out => {
expect(out).to.equal('mAXASIIq3psXnRzeHisc4Y8t2c50V1GZt5E5XVr9Vovnpq19E 8 some link\n')
out.trim().split('\n').forEach(line => {
const cid = line.split(' ')[0]
expect(multibase.isEncoded(cid)).to.deep.equal('base64')
})
})
})

Expand All @@ -182,7 +188,8 @@ describe('object', () => runOnAndOff((thing) => {
})
})

it('should append-data and print CID encoded in specified base', () => {
// TODO: unskip after switch to v1 CIDs by default
it.skip('should append-data and print CID encoded in specified base', () => {
return ipfs('object patch append-data QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n test/fixtures/test-data/badconfig --cid-base=base64').then((out) => {
expect(out).to.eql(
'mAXASIP+BZ7jGtaTyLGOs0xYcQvH7K9kVKEbyzXAkwLoZwrRj\n'
Expand All @@ -198,7 +205,8 @@ describe('object', () => runOnAndOff((thing) => {
})
})

it('should set-data and print CID encoded in specified base', () => {
// TODO: unskip after switch to v1 CIDs by default
it.skip('should set-data and print CID encoded in specified base', () => {
return ipfs('object patch set-data QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6 test/fixtures/test-data/badconfig --cid-base=base64').then((out) => {
expect(out).to.eql(
'mAXASIP+BZ7jGtaTyLGOs0xYcQvH7K9kVKEbyzXAkwLoZwrRj\n'
Expand All @@ -214,7 +222,8 @@ describe('object', () => runOnAndOff((thing) => {
})
})

it('should add-link and print CID encoded in specified base', () => {
// TODO: unskip after switch to v1 CIDs by default
it.skip('should add-link and print CID encoded in specified base', () => {
return ipfs('object patch add-link QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n foo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn --cid-base=base64').then((out) => {
expect(out).to.eql(
'mAXASIOEVPbXq2xYoEsRZhaPB61btcy1x359osjv4a2L/lgPs\n'
Expand All @@ -230,7 +239,8 @@ describe('object', () => runOnAndOff((thing) => {
})
})

it('should rm-link and print CID encoded in specified base', () => {
// TODO: unskip after switch to v1 CIDs by default
it.skip('should rm-link and print CID encoded in specified base', () => {
return ipfs('object patch rm-link QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK foo --cid-base=base64').then((out) => {
expect(out).to.eql(
'mAXASIOOwxEKY/BwUmvv0yJlvuSQnrkHkZJuTTKSVmRt4UrhV\n'
Expand Down

0 comments on commit 0295b2f

Please sign in to comment.