Skip to content

Commit

Permalink
Ensure .card extension in for exported card files (hyperledger-archiv…
Browse files Browse the repository at this point in the history
…es#3390)

For `composer card create` and `composer card export` commands, append the `.card` extension if one is not present in the specified card file name.

Signed-off-by: Mark S. Lewis <[email protected]>
  • Loading branch information
bestbeforetoday authored and Simon Stone committed Feb 13, 2018
1 parent 4be0d62 commit 55ed9a0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
14 changes: 1 addition & 13 deletions packages/composer-cli/lib/cmds/card/lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class Create {
* @returns {Promise} resolved with the filename of the card when it has been written
*/
static createCard(metadata,profileData,argv){
let fileName;
// setup the id card with the meta data
let idCard = new IdCard(metadata,profileData);

Expand All @@ -110,18 +109,7 @@ class Create {
idCard.setCredentials(newCredentials);
}

// handle the filename
// Default is userName@businessNetworkName.card if the card includes a business network name;
// otherwise userName@connectionProfileName.card.
if (!argv.file) {
if (metadata.hasOwnProperty('businessNetwork')){
fileName = metadata.userName+'@'+ metadata.businessNetwork+'.card';
} else {
fileName = metadata.userName+'@'+ profileData.name +'.card';
}
} else {
fileName = argv.file;
}
const fileName = cmdUtil.sanitizeCardFileName(argv.file || cmdUtil.getDefaultCardName(idCard));

// finally write out the card file
return Export.writeCardToFile(fileName,idCard)
Expand Down
2 changes: 1 addition & 1 deletion packages/composer-cli/lib/cmds/card/lib/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Export {
static handler(args) {

const cardName = args.name;
const fileName = args.file || (cardName + '.card');
const fileName = cmdUtil.sanitizeCardFileName(args.file || cardName);

const adminConnection = cmdUtil.createAdminConnection();
return adminConnection.exportCard(cardName)
Expand Down
13 changes: 13 additions & 0 deletions packages/composer-cli/lib/cmds/utils/cmdutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,19 @@ class CmdUtil {
}
return archiveFileContents;
}

/**
* Generate a suitable card file name based on a proposed file name.
* This implementation simply appends '.card' if it is missing.
* @param {String} fileName Proposed card file name
* @return {String} A card file name
*/
static sanitizeCardFileName(fileName) {
if (!/\.card$/i.test(fileName)) {
fileName = fileName + '.card';
}
return fileName;
}
}

module.exports = CmdUtil;
26 changes: 21 additions & 5 deletions packages/composer-cli/test/card/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ describe('composer card export CLI', function() {
sandbox.stub(CmdUtil, 'createAdminConnection').returns(adminConnectionStub);
consoleLogSpy = sandbox.spy(console, 'log');
sandbox.stub(process, 'exit');
// Throw by default for writing files so we can allow only certain file names in specific tests
sandbox.stub(fs, 'writeFileSync').throws();

testCard = new IdCard({ userName: 'conga' }, { name: 'profileName' });
});
Expand All @@ -47,7 +49,7 @@ describe('composer card export CLI', function() {
});

it('should export to specified file name', function() {
sandbox.stub(fs, 'writeFileSync').withArgs(cardFileName);
fs.writeFileSync.withArgs(cardFileName).returns();
const args = {
file: cardFileName,
name : 'CARD_NAME'
Expand All @@ -60,22 +62,36 @@ describe('composer card export CLI', function() {
});
});

it('should always export to .card file', function() {
const fileName = '/TestCard';
fs.writeFileSync.withArgs(fileName + '.card').returns();
const args = {
file: fileName,
name : 'CARD_NAME'
};

adminConnectionStub.exportCard.resolves(testCard);
return ExportCmd.handler(args).then(() => {
sinon.assert.calledOnce(adminConnectionStub.exportCard);
sinon.assert.calledWith(consoleLogSpy, sinon.match(args.name));
});
});

it('should export to default file name if none specified', function() {
const args = {
name : 'CARD_NAME'
};
const expectedFileName = args.name + '.card';
sandbox.stub(fs, 'writeFileSync').withArgs(expectedFileName);
const expectedFileName = sinon.match(new RegExp(args.name + '\.card$'));
fs.writeFileSync.withArgs(expectedFileName).returns();

adminConnectionStub.exportCard.resolves(testCard);
return ExportCmd.handler(args).then(() => {
sinon.assert.calledOnce(adminConnectionStub.exportCard);
sinon.assert.calledWith(consoleLogSpy, sinon.match(expectedFileName));
sinon.assert.calledWith(consoleLogSpy, expectedFileName);
});
});

it('should copy with the file system write failing', function() {
sandbox.stub(fs, 'writeFileSync').withArgs(cardFileName).throws(new Error('Failed to write'));
const args = {
file: cardFileName,
name : 'CARD_NAME'
Expand Down
16 changes: 16 additions & 0 deletions packages/composer-cli/test/utils/cmdutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,20 @@ describe('composer transaction cmdutils unit tests', () => {
});
});

describe('#sanitizeCardFileName', () => {
it('should append .card if missing', () => {
CmdUtil.sanitizeCardFileName('card').should.equal('card.card');
});

it ('should not change file name with .card extension', () => {
const fileName = 'fileName.card';
CmdUtil.sanitizeCardFileName(fileName).should.equal(fileName);
});

it ('should use case-insensitive detection of .card extension', () => {
const fileName = 'fileName.CARD';
CmdUtil.sanitizeCardFileName(fileName).should.equal(fileName);
});
});

});

0 comments on commit 55ed9a0

Please sign in to comment.