|
| 1 | +/* eslint max-nested-callbacks: ["error", 6] */ |
| 2 | +/* eslint-env mocha */ |
| 3 | +'use strict' |
| 4 | + |
| 5 | +const hat = require('hat') |
| 6 | + |
| 7 | +const { fixture } = require('./utils') |
| 8 | +const { spawnNodeWithId } = require('../utils/spawn') |
| 9 | +const { getDescribe, getIt, expect } = require('../utils/mocha') |
| 10 | + |
| 11 | +module.exports = (createCommon, options) => { |
| 12 | + const describe = getDescribe(options) |
| 13 | + const it = getIt(options) |
| 14 | + const common = createCommon() |
| 15 | + |
| 16 | + describe('.name.resolve', function () { |
| 17 | + const keyName = hat() |
| 18 | + let ipfs |
| 19 | + let nodeId |
| 20 | + let keyId |
| 21 | + |
| 22 | + before(function (done) { |
| 23 | + // CI takes longer to instantiate the daemon, so we need to increase the |
| 24 | + // timeout for the before step |
| 25 | + this.timeout(60 * 1000) |
| 26 | + |
| 27 | + common.setup((err, factory) => { |
| 28 | + expect(err).to.not.exist() |
| 29 | + |
| 30 | + spawnNodeWithId(factory, (err, node) => { |
| 31 | + expect(err).to.not.exist() |
| 32 | + |
| 33 | + ipfs = node |
| 34 | + nodeId = node.peerId.id |
| 35 | + |
| 36 | + ipfs.files.add(fixture.data, { pin: false }, done) |
| 37 | + }) |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + after((done) => common.teardown(done)) |
| 42 | + |
| 43 | + it('should resolve a record with the default params after a publish', function (done) { |
| 44 | + this.timeout(50 * 1000) |
| 45 | + |
| 46 | + const value = fixture.cid |
| 47 | + |
| 48 | + ipfs.name.publish(value, (err, res) => { |
| 49 | + expect(err).to.not.exist() |
| 50 | + expect(res).to.exist() |
| 51 | + |
| 52 | + ipfs.name.resolve(nodeId, (err, res) => { |
| 53 | + expect(err).to.not.exist() |
| 54 | + expect(res).to.exist() |
| 55 | + expect(res.path).to.equal(`/ipfs/${value}`) |
| 56 | + |
| 57 | + done() |
| 58 | + }) |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + it('should not get the entry if its validity time expired', function (done) { |
| 63 | + this.timeout(50 * 1000) |
| 64 | + |
| 65 | + const value = fixture.cid |
| 66 | + const publishOptions = { |
| 67 | + resolve: true, |
| 68 | + lifetime: '1ms', |
| 69 | + ttl: '10s', |
| 70 | + key: 'self' |
| 71 | + } |
| 72 | + |
| 73 | + ipfs.name.publish(value, publishOptions, (err, res) => { |
| 74 | + expect(err).to.not.exist() |
| 75 | + expect(res).to.exist() |
| 76 | + |
| 77 | + // guarantee that the record has an expired validity. |
| 78 | + setTimeout(function () { |
| 79 | + ipfs.name.resolve(nodeId, (err, res) => { |
| 80 | + expect(err).to.exist() |
| 81 | + expect(err.message).to.equal('record has expired') |
| 82 | + expect(res).to.not.exist() |
| 83 | + |
| 84 | + done() |
| 85 | + }) |
| 86 | + }, 1) |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + it('should recursively resolve to an IPFS hash', function (done) { |
| 91 | + this.timeout(100 * 1000) |
| 92 | + |
| 93 | + const value = fixture.cid |
| 94 | + const publishOptions = { |
| 95 | + resolve: false, |
| 96 | + lifetime: '24h', |
| 97 | + ttl: '10s', |
| 98 | + key: 'self' |
| 99 | + } |
| 100 | + |
| 101 | + // Generate new key |
| 102 | + ipfs.key.gen(keyName, { type: 'rsa', size: 2048 }, (err, key) => { |
| 103 | + expect(err).to.not.exist() |
| 104 | + |
| 105 | + keyId = key.id |
| 106 | + |
| 107 | + // publish ipfs |
| 108 | + ipfs.name.publish(value, publishOptions, (err, res) => { |
| 109 | + expect(err).to.not.exist() |
| 110 | + expect(res).to.exist() |
| 111 | + |
| 112 | + publishOptions.key = keyName |
| 113 | + |
| 114 | + // publish ipns with the generated key |
| 115 | + ipfs.name.publish(`/ipns/${nodeId}`, publishOptions, (err, res) => { |
| 116 | + expect(err).to.not.exist() |
| 117 | + expect(res).to.exist() |
| 118 | + |
| 119 | + const resolveOptions = { |
| 120 | + nocache: false, |
| 121 | + recursive: true |
| 122 | + } |
| 123 | + |
| 124 | + // recursive resolve (will get ipns first, and will resolve again to find the ipfs) |
| 125 | + ipfs.name.resolve(keyId, resolveOptions, (err, res) => { |
| 126 | + expect(err).to.not.exist() |
| 127 | + expect(res).to.exist() |
| 128 | + expect(res.path).to.equal(`/ipfs/${value}`) |
| 129 | + |
| 130 | + done() |
| 131 | + }) |
| 132 | + }) |
| 133 | + }) |
| 134 | + }) |
| 135 | + }) |
| 136 | + }) |
| 137 | +} |
0 commit comments