Skip to content

Commit

Permalink
Add migration scenario test (#10)
Browse files Browse the repository at this point in the history
Runs against the result of `npm pack` to give some confidence that
the published module will work.

Signed-off-by: Mark S. Lewis <mark_lewis@uk.ibm.com>
  • Loading branch information
bestbeforetoday authored Mar 2, 2020
1 parent 35853fa commit fde20af
Show file tree
Hide file tree
Showing 18 changed files with 329 additions and 101 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
dist
*.tsbuildinfo
coverage
*.tgz
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ test
jest.config.js
coverage
azure-pipelines.yml
scenario
*.tgz
*.sh
19 changes: 12 additions & 7 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,28 @@ stages:
jobs:

- job: Test
displayName: Test with
displayName: Test with Node.js
strategy:
matrix:
'Node.js 10':
'10':
versionSpec: '10.x'
'Node.js 12':
'12':
versionSpec: '12.x'

steps:

- task: NodeTool@0
inputs:
versionSpec: "$(versionSpec)"
displayName: 'Install Node.js'
displayName: Install Node.js

- script: npm install
displayName: 'npm install'
- script: |
npm install
npm run compile
displayName: Build
- script: npm test
displayName: 'Run tests'
displayName: Unit test

- script: npm run scenario
displayName: Scenario test
10 changes: 7 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true
preset: "ts-jest",
testEnvironment: "node",
collectCoverage: true,
testPathIgnorePatterns: [
"/node_modules/",
"/scenario/"
]
};
224 changes: 139 additions & 85 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"clean": "rm -rf dist",
"compile": "tsc --project .",
"prepack": "npm run clean && npm run compile && npm test",
"test": "jest test"
"clean": "rm -rf dist *.tgz",
"compile": "tsc",
"scenario": "./runScenario.sh",
"test": "jest"
},
"repository": {
"type": "git",
Expand All @@ -30,7 +30,7 @@
"fabric-network": "^1.4.7",
"jest": "^25.1.0",
"ts-jest": "^25.2.1",
"typescript": "^3.8.2"
"typescript": "^3.8.3"
},
"dependencies": {
"rimraf": "^3.0.2",
Expand Down
20 changes: 20 additions & 0 deletions runScenario.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

set -eu -o pipefail

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )

createPackFile() {
local packFile
packFile=$( npm pack )
targetFile=$( echo "${packFile}" | sed -e 's/[^-]*\.tgz$/dev.tgz/' )
mv "${packFile}" "${targetFile}"
}

scenarioTest() {
npm install
npm test
}

( cd "${DIR}" && createPackFile )
( cd "${DIR}/scenario" && scenarioTest )
2 changes: 2 additions & 0 deletions scenario/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock.json

5 changes: 5 additions & 0 deletions scenario/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
rootDir: "./test"
};
18 changes: 18 additions & 0 deletions scenario/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "scenario",
"version": "1.0.0",
"private": true,
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"author": "",
"license": "Apache-2.0",
"devDependencies": {
"fabric-network": "^2.0.0-beta.3",
"fabric-wallet-migration": "file:../fabric-wallet-migration-dev.tgz",
"jest": "^25.1.0",
"typescript": "^3.8.3"
}
}
96 changes: 96 additions & 0 deletions scenario/test/Migrate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* SPDX-License-Identifier: Apache-2.0
*/

import { WalletStores } from "fabric-wallet-migration";
import { Wallet, Wallets, Identity, X509Identity } from "fabric-network";

import fs = require("fs");
import os = require("os");
import path = require("path");
import util = require("util");
import _rimraf = require("rimraf");
const rimraf = util.promisify(_rimraf);

const oldWalletPath = path.resolve(__dirname, "..", "wallet");

async function createTempDir() {
const prefix = path.join(os.tmpdir(), "wallet-");
return await fs.promises.mkdtemp(prefix);
}

describe("Wallet migration", () => {
let walletPath: string;
let wallet: Wallet;

beforeAll(async () => {
walletPath = await createTempDir();
await migrate();
wallet = await Wallets.newFileSystemWallet(walletPath);
});

afterAll(async () => {
await rimraf(walletPath);
});

async function migrate() {
const walletStore = WalletStores.newFileSystemWalletStore(oldWalletPath);
const oldWallet = new Wallet(walletStore);

const newWallet = await Wallets.newFileSystemWallet(walletPath);

const migratedLabels: string[] = [];
const identityLabels = await oldWallet.list();
for (const label of identityLabels) {
const identity = await oldWallet.get(label);
if (identity) {
await newWallet.put(label, identity);
migratedLabels.push(label);
}
}

return migratedLabels;
}

async function getAll() {
const identities = new Map<string, Identity>();

const labels = await wallet.list();
for (const label of labels) {
const identity = await wallet.get(label);
if (identity) {
identities.set(label, identity);
}
}

return identities;
}

it("has expected labels", async () => {
const labels = await wallet.list();
expect(labels).toEqual(["user1", "user2"]);
});

it("contains X.509 identities", async () => {
const identities = await getAll();
identities.forEach((identity) => {
expect(identity.type).toEqual("X.509");
});
});

it("identities have non-empty certificates", async () => {
const identities = await getAll();
identities.forEach((identity) => {
const x509Identity = identity as X509Identity;
expect(x509Identity.credentials.certificate).toMatch(/.+/);
});
});

it("identities have non-empty private keys", async () => {
const identities = await getAll();
identities.forEach((identity) => {
const x509Identity = identity as X509Identity;
expect(x509Identity.credentials.privateKey).toMatch(/.+/);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgyzr9w9VY9qI3koXq
Ovxz4chsz16obqzKdRG0cKUpvUyhRANCAAQ3HCLhuIk/ZKOUrTJblTfTbPrYEcEp
GiHtmkUcswGiEIk0Z8kzp4zgMKPvFoOjukht4dhk/+eDMFAvLa12ER41
-----END PRIVATE KEY-----
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENxwi4biJP2SjlK0yW5U302z62BHB
KRoh7ZpFHLMBohCJNGfJM6eM4DCj7xaDo7pIbeHYZP/ngzBQLy2tdhEeNQ==
-----END PUBLIC KEY-----
1 change: 1 addition & 0 deletions scenario/wallet/user1/user1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"user1","mspid":"Org1MSP","roles":null,"affiliation":"","enrollmentSecret":"","enrollment":{"signingIdentity":"8ee3f5bf29b8a30a038c38c5fc2c825e3b9823582ddebc4b3c39ee9cd493712c","identity":{"certificate":"-----BEGIN CERTIFICATE-----\nMIICWTCCAf+gAwIBAgIUSKAUBU+/6sqUHKjxbbH/ZvkkgAUwCgYIKoZIzj0EAwIw\nfzELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNh\nbiBGcmFuY2lzY28xHzAdBgNVBAoTFkludGVybmV0IFdpZGdldHMsIEluYy4xDDAK\nBgNVBAsTA1dXVzEUMBIGA1UEAxMLZXhhbXBsZS5jb20wHhcNMjAwMjI0MTUzNzAw\nWhcNMjEwMjIzMTU0MjAwWjBdMQswCQYDVQQGEwJVUzEXMBUGA1UECBMOTm9ydGgg\nQ2Fyb2xpbmExFDASBgNVBAoTC0h5cGVybGVkZ2VyMQ8wDQYDVQQLEwZjbGllbnQx\nDjAMBgNVBAMTBWFkbWluMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENxwi4biJ\nP2SjlK0yW5U302z62BHBKRoh7ZpFHLMBohCJNGfJM6eM4DCj7xaDo7pIbeHYZP/n\ngzBQLy2tdhEeNaN7MHkwDgYDVR0PAQH/BAQDAgeAMAwGA1UdEwEB/wQCMAAwHQYD\nVR0OBBYEFCBh9T6woSaKdCYc3xp0b2fHqGDaMB8GA1UdIwQYMBaAFBdnQj2qnoI/\nxMUdn1vDmdG1nEgQMBkGA1UdEQQSMBCCDmRvY2tlci1kZXNrdG9wMAoGCCqGSM49\nBAMCA0gAMEUCIQDvXlen/JmnziTlKKhVu/zpIk+LbtdsJv6xlf4AXzjfSgIgRQ94\nurE2nabbhBB8QDIyI7+vnXLNPbT7OneAefxnUI4=\n-----END CERTIFICATE-----"}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgvUT1OcbWZO8Mu9Il
PGMhpFMsT63gGKIbN6EH0M5aw3OhRANCAARnkEt2rIASwyCYBFlDs8xT27v7YMoq
ktBB435jO3AgtOLsvcQjnIMHTkEHVU4M4NTpKmCVT8Yy/zS9B21AcWhN
-----END PRIVATE KEY-----
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEZ5BLdqyAEsMgmARZQ7PMU9u7+2DK
KpLQQeN+YztwILTi7L3EI5yDB05BB1VODODU6SpglU/GMv80vQdtQHFoTQ==
-----END PUBLIC KEY-----
1 change: 1 addition & 0 deletions scenario/wallet/user2/user2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"user2","mspid":"Org1MSP","roles":null,"affiliation":"","enrollmentSecret":"","enrollment":{"signingIdentity":"00d1b0fa0256847ecc75b3ff087e00e54008883449b7411a05ad7882ee1f1aab","identity":{"certificate":"-----BEGIN CERTIFICATE-----\nMIICvDCCAmOgAwIBAgIUE/PYgz1FszgQyZdJMTD7WVotlZ4wCgYIKoZIzj0EAwIw\nfzELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNh\nbiBGcmFuY2lzY28xHzAdBgNVBAoTFkludGVybmV0IFdpZGdldHMsIEluYy4xDDAK\nBgNVBAsTA1dXVzEUMBIGA1UEAxMLZXhhbXBsZS5jb20wHhcNMjAwMjI0MTUzNzAw\nWhcNMjEwMjIzMTU0MjAwWjBhMQswCQYDVQQGEwJVUzEXMBUGA1UECBMOTm9ydGgg\nQ2Fyb2xpbmExFDASBgNVBAoTC0h5cGVybGVkZ2VyMQ8wDQYDVQQLEwZjbGllbnQx\nEjAQBgNVBAMTCW9yZzFBZG1pbjBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABGeQ\nS3asgBLDIJgEWUOzzFPbu/tgyiqS0EHjfmM7cCC04uy9xCOcgwdOQQdVTgzg1Okq\nYJVPxjL/NL0HbUBxaE2jgdowgdcwDgYDVR0PAQH/BAQDAgeAMAwGA1UdEwEB/wQC\nMAAwHQYDVR0OBBYEFPoIUbE0sNAmYKoUxiRCxYOAG1bJMB8GA1UdIwQYMBaAFBdn\nQj2qnoI/xMUdn1vDmdG1nEgQMBkGA1UdEQQSMBCCDmRvY2tlci1kZXNrdG9wMFwG\nCCoDBAUGBwgBBFB7ImF0dHJzIjp7ImhmLkFmZmlsaWF0aW9uIjoiIiwiaGYuRW5y\nb2xsbWVudElEIjoib3JnMUFkbWluIiwiaGYuVHlwZSI6ImNsaWVudCJ9fTAKBggq\nhkjOPQQDAgNHADBEAiAgZFVt/vMETbX6SFvbwCLBm9EhmxnuJ1QnXwD3xbFTpwIg\nK4UQSGTVbujzmp6tvE9fClhKOkMKaGBzoWMU+32ZqtA=\n-----END CERTIFICATE-----"}}}
2 changes: 1 addition & 1 deletion src/IdentityConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class IdentityConverter implements IdentityConverter {
identity: {
certificate: storeData.credentials.certificate,
},
signingIdentity: uuid.v4().replace("-", "")
signingIdentity: uuid.v4()
}
};
const privateKey = storeData.credentials.privateKey;
Expand Down

0 comments on commit fde20af

Please sign in to comment.