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

Commit

Permalink
test: stub out ipfs interactions in http injection tests (#2990)
Browse files Browse the repository at this point in the history
* test: stub out ipfs interactions in http injection tests

This makes running the tests faster as we do no network operations,
nor do we wait for ports to open, etc.

Functionality covered by the tests is also covered by the interface
tests so there is no testing gap.
  • Loading branch information
achingbrain authored Apr 20, 2020
1 parent fbb5ef9 commit 553a65b
Show file tree
Hide file tree
Showing 39 changed files with 3,443 additions and 2,959 deletions.
9 changes: 5 additions & 4 deletions packages/ipfs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"test:node": "cross-env ECHO_SERVER_PORT=37481 aegir test -t node",
"test:browser": "cross-env ECHO_SERVER_PORT=37482 aegir test -t browser",
"test:browser:http": "cross-env ECHO_SERVER_PORT=37489 aegir test -t browser -f test/http-api/index.js",
"test:browser:interface:http": "cross-env ECHO_SERVER_PORT=37489 aegir test -t browser -f test/http-api/interface.js",
"test:webworker": "cross-env ECHO_SERVER_PORT=37483 aegir test -t webworker",
"test:electron": "cross-env ECHO_SERVER_PORT=37484 aegir test -t electron-main -t electron-renderer",
"test:electron-main": "cross-env ECHO_SERVER_PORT=37485 aegir test -t electron-main",
Expand All @@ -53,7 +52,8 @@
"test:node:core": "cross-env ECHO_SERVER_PORT=37488 aegir test -t node -f test/core/**/*.js",
"test:node:http": "cross-env ECHO_SERVER_PORT=37489 aegir test -t node -f test/http-api/index.js",
"test:node:gateway": "cross-env ECHO_SERVER_PORT=37490 aegir test -t node -f test/gateway/index.js",
"test:node:interface": "cross-env ECHO_SERVER_PORT=37491 aegir test -t node -f test/core/interface.spec.js",
"test:interface": "cross-env ECHO_SERVER_PORT=37491 aegir test -f test/core/interface.spec.js",
"test:interface:http": "cross-env ECHO_SERVER_PORT=37489 aegir test -f test/http-api/interface.js",
"test:bootstrapers": "cross-env ECHO_SERVER_PORT=37492 IPFS_TEST=bootstrapers aegir test -t browser -f test/bootstrapers.js",
"test:interop": "cross-env IPFS_JS_EXEC=$PWD/src/cli/bin.js IPFS_JS_MODULE=$PWD IPFS_REUSEPORT=false ipfs-interop",
"test:interop:node": "cross-env IPFS_JS_EXEC=$PWD/src/cli/bin.js IPFS_JS_MODULE=$PWD IPFS_REUSEPORT=false ipfs-interop -- -t node",
Expand Down Expand Up @@ -145,7 +145,7 @@
"libp2p-record": "^0.7.0",
"libp2p-secio": "^0.12.2",
"libp2p-tcp": "^0.14.3",
"libp2p-webrtc-star": "^0.17.6",
"libp2p-webrtc-star": "^0.17.9",
"libp2p-websockets": "^0.13.3",
"mafmt": "^7.0.0",
"merge-options": "^2.0.0",
Expand Down Expand Up @@ -185,10 +185,11 @@
"form-data": "^3.0.0",
"go-ipfs-dep": "0.4.23-3",
"interface-ipfs-core": "^0.134.0",
"ipfs-interop": "ipfs/interop#fix/name-pubsub",
"ipfs-interop": "^1.0.1",
"ipfsd-ctl": "^3.0.0",
"iso-random-stream": "^1.1.1",
"it-first": "^1.0.1",
"it-to-buffer": "^1.0.0",
"nanoid": "^3.0.2",
"ncp": "^2.0.0",
"p-event": "^4.1.0",
Expand Down
19 changes: 7 additions & 12 deletions packages/ipfs/src/http/api/resources/dag.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ exports.put = {
query: Joi.object().keys({
format: Joi.string().default('cbor'),
'input-enc': Joi.string().default('json'),
pin: Joi.boolean(),
pin: Joi.boolean().default(false),
hash: Joi.string().valid(...Object.keys(mh.names)).default('sha2-256'),
'cid-base': Joi.string().valid(...multibase.names)
}).unknown()
Expand Down Expand Up @@ -245,16 +245,13 @@ exports.put = {
try {
cid = await ipfs.dag.put(node, {
format: format,
hashAlg: hashAlg
hashAlg: hashAlg,
pin: request.query.pin
})
} catch (err) {
throw Boom.boomify(err, { message: 'Failed to put node' })
}

if (request.query.pin) {
await ipfs.pin.add(cid)
}

return h.response({
Cid: {
'/': cidToString(cid, {
Expand Down Expand Up @@ -287,15 +284,13 @@ exports.resolve = {
let lastRemainderPath = path

if (path) {
const result = ipfs.dag.resolve(lastCid, path)
while (true) {
const resolveResult = (await result.next()).value
if (!CID.isCID(resolveResult.value)) {
for await (const { value, remainderPath } of ipfs.dag.resolve(lastCid, path)) {
if (!CID.isCID(value)) {
break
}

lastRemainderPath = resolveResult.remainderPath
lastCid = resolveResult.value
lastRemainderPath = remainderPath
lastCid = value
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/ipfs/src/http/api/resources/dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exports.findPeer = {
let res

try {
res = await ipfs.dht.findPeer(arg)
res = await ipfs.dht.findPeer(new CID(arg))
} catch (err) {
if (err.code === 'ERR_LOOKUP_FAILED') {
throw Boom.notFound(err.toString())
Expand Down Expand Up @@ -52,9 +52,9 @@ exports.findProvs = {
const ipfs = request.server.app.ipfs
const { arg } = request.query

request.query.maxNumProviders = request.query['num-providers']

const res = await all(ipfs.dht.findProvs(arg, { numProviders: request.query['num-providers'] }))
const res = await all(ipfs.dht.findProvs(new CID(arg), {
numProviders: request.query['num-providers']
}))

return h.response({
Responses: res.map(({ id, addrs }) => ({
Expand Down
47 changes: 31 additions & 16 deletions packages/ipfs/src/http/api/resources/dns.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
'use strict'

const Boom = require('@hapi/boom')
const Joi = require('@hapi/joi')

module.exports = async (request, h) => {
const domain = request.query.arg
module.exports = {
validate: {
options: {
allowUnknown: true,
stripUnknown: true
},
query: Joi.object().keys({
arg: Joi.string().required(),
format: Joi.string(),
recursive: Joi.boolean().default(false)
})
.rename('r', 'recursive', {
override: true,
ignoreUndefined: true
})
},
async handler (request, h) {
const {
arg,
format,
recursive
} = request.query

if (!domain) {
throw Boom.badRequest("Argument 'domain' is required")
}

const format = request.query.format
const path = await request.server.app.ipfs.dns(arg, {
recursive,
format
})

// query parameters are passed as strings and need to be parsed to expected type
let recursive = request.query.recursive || request.query.r
recursive = !(recursive && recursive === 'false')

const path = await request.server.app.ipfs.dns(domain, { recursive, format })
return h.response({
Path: path
})
return h.response({
Path: path
})
}
}
52 changes: 32 additions & 20 deletions packages/ipfs/src/http/api/resources/files-regular.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,13 @@ exports.add = {

exports.ls = {
validate: {
query: Joi.object().keys({
'cid-base': Joi.string().valid(...multibase.names),
stream: Joi.boolean()
}).unknown()
query: Joi.object()
.keys({
arg: Joi.string().required(),
'cid-base': Joi.string().valid(...multibase.names),
stream: Joi.boolean().default(false),
recursive: Joi.boolean().default(false)
}).unknown()
},

// uses common parseKey method that returns a `key`
Expand All @@ -262,8 +265,11 @@ exports.ls = {
async handler (request, h) {
const { ipfs } = request.server.app
const { key } = request.pre.args
const recursive = request.query && request.query.recursive === 'true'
const cidBase = request.query['cid-base']
const {
recursive,
stream,
'cid-base': cidBase
} = request.query

const mapLink = link => {
const output = {
Expand All @@ -286,15 +292,16 @@ exports.ls = {
return output
}

if (!request.query.stream) {
let links
if (!stream) {
try {
links = await all(ipfs.ls(key, { recursive }))
const links = await all(ipfs.ls(key, {
recursive
}))

return h.response({ Objects: [{ Hash: key, Links: links.map(mapLink) }] })
} catch (err) {
throw Boom.boomify(err, { message: 'Failed to list dir' })
}

return h.response({ Objects: [{ Hash: key, Links: links.map(mapLink) }] })
}

return streamResponse(request, h, () => pipe(
Expand Down Expand Up @@ -334,17 +341,22 @@ exports.refs = {
handler (request, h) {
const { ipfs } = request.server.app
const { key } = request.pre.args

const options = {
recursive: request.query.recursive,
format: request.query.format,
edges: request.query.edges,
unique: request.query.unique,
maxDepth: request.query['max-depth']
}
const {
recursive,
format,
edges,
unique,
'max-depth': maxDepth
} = request.query

return streamResponse(request, h, () => pipe(
ipfs.refs(key, options),
ipfs.refs(key, {
recursive,
format,
edges,
unique,
maxDepth
}),
map(({ ref, err }) => ({ Ref: ref, Err: err })),
ndjson.stringify
))
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs/src/http/api/resources/files/flush.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const mfsFlush = {
cidBase
} = request.query

let cid = await ipfs.files.flush(arg || '/', {})
let cid = await ipfs.files.flush(arg || '/')

if (cidBase && cidBase !== 'base58btc' && cid.version === 0) {
cid = cid.toV1()
Expand Down
5 changes: 4 additions & 1 deletion packages/ipfs/src/http/api/resources/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ exports.rename = async (request, h) => {
exports.gen = async (request, h) => {
const { ipfs } = request.server.app
const { arg, type, size } = request.query
const key = await ipfs.key.gen(arg, { type, size: parseInt(size) })
const key = await ipfs.key.gen(arg, {
type,
size: parseInt(size)
})
return h.response(toKeyInfo(key))
}

Expand Down
33 changes: 28 additions & 5 deletions packages/ipfs/src/http/api/resources/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ exports.resolve = {
},
async handler (request, h) {
const { ipfs } = request.server.app
const { arg, stream } = request.query
const {
arg,
nocache,
recursive,
stream
} = request.query

if (!stream) {
const value = await last(ipfs.name.resolve(arg, request.query))
const value = await last(ipfs.name.resolve(arg, {
nocache,
recursive
}))
return h.response({ Path: value })
}

Expand All @@ -39,14 +47,29 @@ exports.publish = {
arg: Joi.string().required(),
resolve: Joi.boolean().default(true),
lifetime: Joi.string().default('24h'),
key: Joi.string().default('self')
ttl: Joi.string(),
key: Joi.string().default('self'),
'allow-offline': Joi.boolean()
}).unknown()
},
async handler (request, h) {
const { ipfs } = request.server.app
const { arg } = request.query
const {
arg,
resolve,
lifetime,
ttl,
key,
'allow-offline': allowOffline
} = request.query

const res = await ipfs.name.publish(arg, request.query)
const res = await ipfs.name.publish(arg, {
resolve,
lifetime,
ttl,
key,
allowOffline
})

return h.response({
Name: res.name,
Expand Down
8 changes: 6 additions & 2 deletions packages/ipfs/src/http/api/resources/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ exports.get = {

let node, cid
try {
node = await ipfs.object.get(key, { enc })
node = await ipfs.object.get(key, {
enc
})
cid = await dagPB.util.cid(dagPB.util.serialize(node))
} catch (err) {
throw Boom.boomify(err, { message: 'Failed to get object' })
Expand Down Expand Up @@ -490,7 +492,9 @@ exports.patchRmLink = {

let cid, node
try {
cid = await ipfs.object.patch.rmLink(root, { name: link })
cid = await ipfs.object.patch.rmLink(root, {
name: link
})
node = await ipfs.object.get(cid)
} catch (err) {
throw Boom.boomify(err, { message: 'Failed to remove link from object' })
Expand Down
12 changes: 9 additions & 3 deletions packages/ipfs/src/http/api/resources/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ exports.ls = {

if (!request.query.stream) {
const res = await pipe(
ipfs.pin.ls(path, { type }),
ipfs.pin.ls(path, {
type
}),
reduce((res, { type, cid }) => {
res.Keys[cidToString(cid, { base: request.query['cid-base'] })] = { Type: type }
return res
Expand Down Expand Up @@ -93,7 +95,9 @@ exports.add = {

let result
try {
result = await ipfs.pin.add(path, { recursive })
result = await ipfs.pin.add(path, {
recursive
})
} catch (err) {
if (err.message.includes('already pinned recursively')) {
throw Boom.boomify(err, { statusCode: 400 })
Expand Down Expand Up @@ -122,7 +126,9 @@ exports.rm = {

let result
try {
result = await ipfs.pin.rm(path, { recursive })
result = await ipfs.pin.rm(path, {
recursive
})
} catch (err) {
throw Boom.boomify(err, { message: 'Failed to remove pin' })
}
Expand Down
4 changes: 3 additions & 1 deletion packages/ipfs/src/http/api/resources/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ module.exports = {
const count = request.query.n || request.query.count || 10

return streamResponse(request, h, () => pipe(
ipfs.ping(peerId, { count }),
ipfs.ping(peerId, {
count
}),
map(pong => ({ Success: pong.success, Time: pong.time, Text: pong.text })),
ndjson.stringify
))
Expand Down
Loading

0 comments on commit 553a65b

Please sign in to comment.