|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const each = require('async/each') |
| 5 | +const hat = require('hat') |
| 6 | + |
| 7 | +const { fixtures } = require('./utils') |
| 8 | +const { getDescribe, getIt, expect } = require('../utils/mocha') |
| 9 | + |
| 10 | +module.exports = (createCommon, options) => { |
| 11 | + const describe = getDescribe(options) |
| 12 | + const it = getIt(options) |
| 13 | + const common = createCommon() |
| 14 | + |
| 15 | + describe('.name.resolve', function () { |
| 16 | + this.timeout(50 * 1000) |
| 17 | + |
| 18 | + const keyName = hat() |
| 19 | + let ipfs |
| 20 | + let nodeId |
| 21 | + let keyId |
| 22 | + |
| 23 | + before(function (done) { |
| 24 | + this.timeout(60 * 1000) |
| 25 | + |
| 26 | + common.setup((err, factory) => { |
| 27 | + expect(err).to.not.exist() |
| 28 | + |
| 29 | + factory.spawnNode((err, node) => { |
| 30 | + expect(err).to.not.exist() |
| 31 | + |
| 32 | + ipfs = node |
| 33 | + ipfs.id().then((res) => { |
| 34 | + expect(res.id).to.exist() |
| 35 | + |
| 36 | + nodeId = res.id |
| 37 | + |
| 38 | + ipfs.key.gen(keyName, { type: 'rsa', size: 2048 }, (err, key) => { |
| 39 | + expect(err).to.not.exist() |
| 40 | + expect(key).to.exist() |
| 41 | + expect(key).to.have.property('name', keyName) |
| 42 | + expect(key).to.have.property('id') |
| 43 | + |
| 44 | + keyId = key.id |
| 45 | + populate() |
| 46 | + }) |
| 47 | + }) |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + function populate () { |
| 52 | + each(fixtures.files, (file, cb) => { |
| 53 | + ipfs.files.add(file.data, { pin: false }, cb) |
| 54 | + }, done) |
| 55 | + } |
| 56 | + }) |
| 57 | + |
| 58 | + after((done) => common.teardown(done)) |
| 59 | + |
| 60 | + it('name resolve should resolve correctly after a publish', (done) => { |
| 61 | + this.timeout(60 * 1000) |
| 62 | + |
| 63 | + const value = fixtures.files[0].cid |
| 64 | + |
| 65 | + ipfs.name.publish(value, true, '1m', '10s', 'self', (err, res) => { |
| 66 | + expect(err).to.not.exist() |
| 67 | + expect(res).to.exist() |
| 68 | + |
| 69 | + ipfs.name.resolve(nodeId, false, false, (err, res) => { |
| 70 | + expect(err).to.not.exist() |
| 71 | + expect(res).to.exist() |
| 72 | + expect(res).to.equal(`/ipfs/${value}`) |
| 73 | + |
| 74 | + done() |
| 75 | + }) |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + it('name resolve should not get the entry correctly if its validity time expired', (done) => { |
| 80 | + this.timeout(60 * 1000) |
| 81 | + |
| 82 | + const value = fixtures.files[0].cid |
| 83 | + |
| 84 | + ipfs.name.publish(value, true, '10ns', '10s', 'self', (err, res) => { |
| 85 | + expect(err).to.not.exist() |
| 86 | + expect(res).to.exist() |
| 87 | + |
| 88 | + setTimeout(function () { |
| 89 | + ipfs.name.resolve(nodeId, false, false, (err, res) => { |
| 90 | + expect(err).to.exist() |
| 91 | + expect(res).to.not.exist() |
| 92 | + |
| 93 | + done() |
| 94 | + }) |
| 95 | + }, 1) |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + it('name resolve should should go recursively until finding an ipfs hash', (done) => { |
| 100 | + this.timeout(60 * 1000) |
| 101 | + |
| 102 | + const value = fixtures.files[0].cid |
| 103 | + |
| 104 | + ipfs.name.publish(value, true, '24h', '10s', 'self', (err, res) => { |
| 105 | + expect(err).to.not.exist() |
| 106 | + expect(res).to.exist() |
| 107 | + |
| 108 | + ipfs.name.publish(`/ipns/${nodeId}`, true, '24h', '10s', keyName, (err, res) => { |
| 109 | + expect(err).to.not.exist() |
| 110 | + expect(res).to.exist() |
| 111 | + |
| 112 | + ipfs.name.resolve(keyId, false, true, (err, res) => { |
| 113 | + expect(err).to.not.exist() |
| 114 | + expect(res).to.exist() |
| 115 | + expect(res).to.equal(`/ipfs/${value}`) |
| 116 | + |
| 117 | + done() |
| 118 | + }) |
| 119 | + }) |
| 120 | + }) |
| 121 | + }) |
| 122 | + }) |
| 123 | +} |
0 commit comments