Skip to content

Commit

Permalink
test(cli): test for signing contract
Browse files Browse the repository at this point in the history
Signed-off-by: sanket shevkar <[email protected]>
  • Loading branch information
sanketshevkar authored and jeromesimeon committed Oct 28, 2021
1 parent bafc232 commit 622ffba
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 48 deletions.
20 changes: 11 additions & 9 deletions packages/cicero-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,9 @@ require('yargs')
default: false
});
}, (argv) => {
if (argv.verbose) {
Logger.info(`verifying signatures of contract ${argv.contract}`);
}

try {
argv = Commands.validateVerifyArgs(argv);
Expand All @@ -631,15 +634,6 @@ require('yargs')
describe: 'path to a smart legal contract slc file',
type: 'string'
});
yargs.option('template', {
describe: 'path to a template',
type: 'string'
});
yargs.option('warnings', {
describe: 'print warnings',
type: 'boolean',
default: false
});
yargs.option('keystore', {
describe: 'p12 keystore path',
type: 'string',
Expand All @@ -660,7 +654,15 @@ require('yargs')
type: 'string',
default: null
});
yargs.option('warnings', {
describe: 'print warnings',
type: 'boolean',
default: false
});
}, (argv) => {
if (argv.verbose) {
Logger.info(`sign contract ${argv.contract} for signatory ${argv.signatory}`);
}

try {
argv = Commands.validateSignArgs(argv);
Expand Down
17 changes: 11 additions & 6 deletions packages/cicero-cli/lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,13 +655,18 @@ class Commands {
*/
static validateSignArgs(argv) {
argv = Commands.validateCommonArgs(argv);
if (argv.template) {
argv = Commands.validateDataArgs(argv);
}

if(!argv.target){
Logger.info('Using ergo as the default target for the archive.');
argv.target = 'ergo';
if (!argv.keystore) {
throw new Error('please define path of the keystore using --keystore');
}
if (!argv.passphrase) {
throw new Error('please define the passphrase of the keystore using --pasphrase');
}
if (!argv.signatory) {
throw new Error('please define the signatory signing the contract using --signatory');
}
if(argv.verbose) {
Logger.info(`verifying signatures of contract ${argv.contract}`);
}

return argv;
Expand Down
50 changes: 17 additions & 33 deletions packages/cicero-cli/test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ chai.use(require('chai-things'));
chai.use(require('chai-as-promised'));

const Commands = require('../lib/commands');
const { ContractInstance } = require('@accordproject/cicero-core');

const template = path.resolve(__dirname, 'data/latedeliveryandpenalty/');
const templateJs = path.resolve(__dirname, 'data/latedeliveryandpenalty_js/');
Expand Down Expand Up @@ -1308,37 +1309,16 @@ describe('#archive', async () => {
});

describe('#validateSignArgs', () => {
it('no args specified', () => {
process.chdir(path.resolve(__dirname, 'data/signContract/'));
it('all args specified', () => {
process.chdir(path.resolve(__dirname, 'data/contractsigning/'));
const args = Commands.validateSignArgs({
contract: 'latedeliveryandpenalty@0.17.0-d0c1a14e8a7af52e0927a23b8b30af3b5a75bee1ab788a15736e603b88a6312c.slc',
keystore: 'keystore.p12',
passphrase: 'password',
signatory: 'partyA',
_: ['sign']
});
args.template.should.match(/cicero-cli[/\\]test[/\\]data[/\\]signContract$/);
args.target.should.match(/ergo/);
});
it('only target arg specified', () => {
process.chdir(path.resolve(__dirname, 'data/signContract/'));
const args = Commands.validateSignArgs({
_: ['sign'],
target: 'ergo'
});
args.template.should.match(/cicero-cli[/\\]test[/\\]data[/\\]signContract$/);
args.target.should.match(/ergo/);
});
it('template arg specified', () => {
process.chdir(path.resolve(__dirname));
const args = Commands.validateSignArgs({
_: ['sign', 'data/signContract/']
});
args.template.should.match(/cicero-cli[/\\]test[/\\]data[/\\]signContract$/);
args.target.should.match(/ergo/);
});
it('verbose flag specified', () => {
process.chdir(path.resolve(__dirname, 'data/signContract/'));
Commands.validateSignArgs({
_: ['sign'],
verbose: true
});
args.contract.should.match(/.slc$/);
});
it('bad package.json', () => {
process.chdir(path.resolve(__dirname, 'data/'));
Expand All @@ -1350,12 +1330,16 @@ describe('#validateSignArgs', () => {

describe('#sign', async () => {
it('should sign the contract for a party/individual', async () => {
const slcPath = path.resolve(__dirname, 'data/signContract/latedeliveryandpenalty@0.17.0-d0c1a14e8a7af52e0927a23b8b30af3b5a75bee1ab788a15736e603b88a6312c.slc');
const keystore = path.resolve(__dirname, 'data/keystore.p12');
const archiveName = 'test.slc';
const slcPath = path.resolve(__dirname, 'data/contractsigning/latedeliveryandpenalty@0.17.0-d0c1a14e8a7af52e0927a23b8b30af3b5a75bee1ab788a15736e603b88a6312c.slc');
const keystore = path.resolve(__dirname, 'data/contractsigning/keystore.p12');
const signatory = 'partyA';
const outputPath = path.resolve(__dirname, 'data/');
const result = await Commands.sign(slcPath, keystore, 'password', signatory, outputPath);
result.should.be.true;
const result = await Commands.sign(slcPath, keystore, 'password', signatory, archiveName);
result.should.eql(true);
const newInstance = await ContractInstance.fromArchive(fs.readFileSync(archiveName));
newInstance.should.not.be.null;
newInstance.contractSignatures.should.have.lengthOf(1);
fs.unlinkSync(archiveName);
});
});

Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 622ffba

Please sign in to comment.