|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const chai = require('chai') |
| 5 | +chai.use(require('dirty-chai')) |
| 6 | +const chaiAsPromised = require("chai-as-promised") |
| 7 | +chai.use(chaiAsPromised) |
| 8 | +const expect = chai.expect |
| 9 | + |
| 10 | +const path = require('path') |
| 11 | +const migration = require('../../migrations/migration-8') |
| 12 | +const Key = require('interface-datastore').Key |
| 13 | +const Datastore = require('datastore-fs') |
| 14 | + |
| 15 | +const log = require('debug')('js-ipfs-repo-migrations:migration-8') |
| 16 | + |
| 17 | +const fixtures = [ |
| 18 | + ['aAa', 'key_mfawc'], |
| 19 | + ['bbb', 'key_mjrge'], |
| 20 | + ['self', 'key_onswyzq'] |
| 21 | +] |
| 22 | + |
| 23 | +async function bootstrapKeys (dir, encoded) { |
| 24 | + const store = new Datastore(path.join(dir, 'keys'), { extension: '.data', createIfMissing: true }) |
| 25 | + await store.open() |
| 26 | + |
| 27 | + let name |
| 28 | + for (let keyNames of fixtures) { |
| 29 | + name = encoded ? keyNames[1] : keyNames[0] |
| 30 | + await store.put(new Key(`/pkcs8/${name}`), '') |
| 31 | + await store.put(new Key(`/info/${name}`), '') |
| 32 | + } |
| 33 | + |
| 34 | + await store.close() |
| 35 | +} |
| 36 | + |
| 37 | +async function validateKeys (dir, shouldBeEncoded) { |
| 38 | + const store = new Datastore(path.join(dir, 'keys'), { extension: '.data', createIfMissing: false }) |
| 39 | + await store.open() |
| 40 | + |
| 41 | + let name |
| 42 | + for (let keyNames of fixtures) { |
| 43 | + name = shouldBeEncoded ? keyNames[1] : keyNames[0] |
| 44 | + expect(await store.has(new Key(`/pkcs8/${name}`))).to.be.true() |
| 45 | + expect(await store.has(new Key(`/info/${name}`))).to.be.true() |
| 46 | + } |
| 47 | + |
| 48 | + await store.close() |
| 49 | +} |
| 50 | + |
| 51 | +module.exports = (setup, cleanup) => { |
| 52 | + let dir |
| 53 | + |
| 54 | + beforeEach(async () => { |
| 55 | + dir = await setup() |
| 56 | + }) |
| 57 | + afterEach(() => cleanup(dir)) |
| 58 | + |
| 59 | + it('should migrate forward', async () => { |
| 60 | + await bootstrapKeys(dir, false) |
| 61 | + await migration.migrate(dir) |
| 62 | + await validateKeys(dir, true) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should migrate backward', async () => { |
| 66 | + await bootstrapKeys(dir, true) |
| 67 | + await migration.revert(dir) |
| 68 | + await validateKeys(dir, false) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should fail to migrate backward with invalid key name', async () => { |
| 72 | + const store = new Datastore(path.join(dir, 'keys'), { extension: '.data', createIfMissing: true }) |
| 73 | + await store.open() |
| 74 | + |
| 75 | + await store.put(new Key('/pkcs8/mfawc'), '') |
| 76 | + await store.put(new Key('/info/mfawc'), '') |
| 77 | + |
| 78 | + await store.close() |
| 79 | + |
| 80 | + expect(migration.revert(dir)).to.eventually.rejectedWith('Unknown format of key\'s name!') |
| 81 | + }) |
| 82 | +} |
0 commit comments