Skip to content

Commit

Permalink
Don't add card if invalid (hyperledger-archives#2291)
Browse files Browse the repository at this point in the history
If when adding a business network card the card activation fails the card will still be created

contributes to hyperledger/composer#2219
  • Loading branch information
cazfletch authored Oct 5, 2017
1 parent a60f981 commit 8faf86f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export class LoginComponent implements OnInit {
importIdentity() {
this.drawerService.open(ImportIdentityComponent).result.then((result) => {
return this.identityCardService.addIdentityCard(result);
}).then((cardRef) => {
}).then((cardRef: string) => {
this.alertService.successStatus$.next({
title: 'ID Card imported',
text: 'The ID card ' + this.identityCardService.getIdentityCard(cardRef).getUserName() + ' was successfully imported',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('IdentityCardService', () => {
let credentials;

beforeEach(() => {
credentials = { certificate: 'CERTIFICATE', privateKey: 'PRIVATE_KEY' };
credentials = {certificate: 'CERTIFICATE', privateKey: 'PRIVATE_KEY'};
mockAdminService.exportIdentity.withArgs(sinon.match.any, 'card1').resolves(credentials);
mockAdminService.exportIdentity.withArgs(sinon.match.any, 'card2').resolves(credentials);

Expand All @@ -97,14 +97,14 @@ describe('IdentityCardService', () => {
userName: 'card1',
businessNetworkName: 'assassin-network',
enrollmentSecret: 'adminpw'
}, { name: 'hlfv1' });
}, {name: 'hlfv1'});

idCard2 = new IdCard({
version: 1,
userName: 'card2',
businessNetworkName: 'assassin-network',
enrollmentSecret: 'adminpw'
}, { name: 'hlfv1' });
}, {name: 'hlfv1'});

mockCardMap = new Map<string, IdCard>();
mockCardMap.set('uuid1xxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', idCard1);
Expand Down Expand Up @@ -337,10 +337,10 @@ describe('IdentityCardService', () => {
};

service.createIdentityCard('admin', 'cashless-network', 'adminpw', connectionProfile, null, [])
.then((cardRef) => {
.then((cardRef: string) => {
let myCard = service.getIdentityCard(cardRef);
myCard.getCredentials().should.be.empty;
}).catch((error) => {
}).catch((error) => {
fail('test failed with error' + error);
});

Expand All @@ -362,11 +362,11 @@ describe('IdentityCardService', () => {
};

service.createIdentityCard('admin', 'cashless-network', null, connectionProfile, credentials, ['PeerAdmin', 'ChannelAdmin'])
.then((cardRef) => {
.then((cardRef: string) => {
let myCard = service.getIdentityCard(cardRef);
myCard.getCredentials().should.deep.equal(credentials);
myCard.getRoles().should.deep.equal(['PeerAdmin', 'ChannelAdmin']);
}).catch((error) => {
}).catch((error) => {
fail('test failed with error' + error);
});

Expand Down Expand Up @@ -394,7 +394,10 @@ describe('IdentityCardService', () => {
service['indestructibleCards'].length.should.equal(0);
mockIdentityCardStorageService.set.should.have.been.calledTwice;
mockIdentityCardStorageService.set.should.have.been.calledWith(result);
mockIdentityCardStorageService.set.should.have.been.calledWith(result + '-pd', {unused: true, indestructible: false});
mockIdentityCardStorageService.set.should.have.been.calledWith(result + '-pd', {
unused: true,
indestructible: false
});
activateIdentityCardStub.should.not.have.been.called;
})));

Expand All @@ -416,7 +419,10 @@ describe('IdentityCardService', () => {
service['indestructibleCards'].length.should.equal(0);
mockIdentityCardStorageService.set.should.have.been.calledTwice;
mockIdentityCardStorageService.set.should.have.been.calledWith(result);
mockIdentityCardStorageService.set.should.have.been.calledWith(result + '-pd', {unused: true, indestructible: false});
mockIdentityCardStorageService.set.should.have.been.calledWith(result + '-pd', {
unused: true,
indestructible: false
});
activateIdentityCardStub.should.have.been.called;
})));

Expand All @@ -437,7 +443,68 @@ describe('IdentityCardService', () => {
service['indestructibleCards'][0].should.equal(result);
mockIdentityCardStorageService.set.should.have.been.calledTwice;
mockIdentityCardStorageService.set.should.have.been.calledWith(result);
mockIdentityCardStorageService.set.should.have.been.calledWith(result + '-pd', {unused: true, indestructible: true});
mockIdentityCardStorageService.set.should.have.been.calledWith(result + '-pd', {
unused: true,
indestructible: true
});
})));

it('should remove the card if error happens after creation', fakeAsync(inject([IdentityCardService], (service: IdentityCardService) => {
let activateIdentityCardStub = sinon.stub(service, 'activateIdentityCard');
activateIdentityCardStub.returns(Promise.reject('some error'));
let mockIdCard = sinon.createStubInstance(IdCard);
mockIdCard.getUserName.returns('bcc');
mockIdCard.getCredentials.returns({certificate: 'CERTIFICATE', privateKey: 'PRIVATE_KEY'});

let mockDeleteIdentity = sinon.stub(service, 'deleteIdentityCard').returns(Promise.resolve());

let result;
service.addIdentityCard(mockIdCard)
.then((ca) => {
throw new Error('should not get here');
})
.catch((error) => {
error.should.equal('some error');
});

tick();

service['idCards'].size.should.equal(1);
service['indestructibleCards'].length.should.equal(0);
mockIdentityCardStorageService.set.should.have.been.calledTwice;

activateIdentityCardStub.should.have.been.called;

mockDeleteIdentity.should.have.been.called;
})));

it('should handle error before the card creation', fakeAsync(inject([IdentityCardService], (service: IdentityCardService) => {
let activateIdentityCardStub = sinon.stub(service, 'activateIdentityCard');
activateIdentityCardStub.returns(Promise.reject('some error'));
let mockIdCard = sinon.createStubInstance(IdCard);
mockIdCard.getUserName.returns('bcc');
mockIdCard.getCredentials.returns({certificate: 'CERTIFICATE', privateKey: 'PRIVATE_KEY'});

let mockDeleteIdentity = sinon.stub(service, 'deleteIdentityCard').returns(Promise.reject('another error'));

service.addIdentityCard(mockIdCard)
.then((cardRef) => {
throw new Error('should not get here');
})
.catch((error) => {
error.should.equal('some error');
});

tick();

service['idCards'].size.should.equal(1);
service['indestructibleCards'].length.should.equal(0);
mockIdentityCardStorageService.set.should.have.been.calledTwice;

activateIdentityCardStub.should.have.been.called;

mockDeleteIdentity.should.have.been.called;

})));
});

Expand Down Expand Up @@ -629,13 +696,13 @@ describe('IdentityCardService', () => {

mockIdCard2 = sinon.createStubInstance(IdCard);
mockIdCard1.getUserName.returns('admin');
mockIdCard2.getEnrollmentCredentials.returns({ });
mockIdCard2.getEnrollmentCredentials.returns({});
mockIdCard2.getCredentials.returns({certificate: 'CERTIFICATE', privateKey: 'PRIVATE_KEY'});
mockIdCard2.getConnectionProfile.returns({name: 'hlfv1'});

mockIdCard3 = sinon.createStubInstance(IdCard);
mockIdCard1.getUserName.returns('admin');
mockIdCard3.getEnrollmentCredentials.returns({ });
mockIdCard3.getEnrollmentCredentials.returns({});
mockIdCard3.getCredentials.returns({});
mockIdCard3.getConnectionProfile.returns({name: 'hlfv1'});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export class IdentityCardService {
});
}

createIdentityCard(userName: string, businessNetworkName: string, enrollmentSecret: string, connectionProfile: any, credentials?: any, roles?: string[]): Promise<string> {
createIdentityCard(userName: string, businessNetworkName: string, enrollmentSecret: string, connectionProfile: any, credentials?: any, roles?: string[]): Promise<string | void> {
const metadata: any = {
version: 1,
userName: userName,
Expand All @@ -194,7 +194,7 @@ export class IdentityCardService {
return this.addIdentityCard(card);
}

addIdentityCard(card: IdCard, indestructible: boolean = false): Promise<string> {
addIdentityCard(card: IdCard, indestructible: boolean = false): Promise<string | void> {
let cardRef: string = uuid.v4();
let data = {
unused: true,
Expand All @@ -217,6 +217,16 @@ export class IdentityCardService {
})
.then(() => {
return cardRef;
})
.catch((actualError) => {
return this.deleteIdentityCard(cardRef)
.then(() => {
return Promise.reject(actualError);
})
// ignore this error it is probably just because it didn't atually create the card
.catch(() => {
return Promise.reject(actualError);
});
});
}

Expand Down

0 comments on commit 8faf86f

Please sign in to comment.