Skip to content

Commit

Permalink
Use id cards with identity registry (hyperledger-archives#1828)
Browse files Browse the repository at this point in the history
Issue Identity uses id card
IdentityComponent uses id card

contributes to hyperledger/composer#1799
  • Loading branch information
cazfletch authored and Caroline Church committed Aug 18, 2017
1 parent ce92f6b commit c6c0e56
Show file tree
Hide file tree
Showing 10 changed files with 626 additions and 224 deletions.
2 changes: 1 addition & 1 deletion packages/composer-playground/.istanbul.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ instrumentation:
check:
global:
statements: 98
branches: 92
branches: 94
functions: 96
lines: 98
Original file line number Diff line number Diff line change
Expand Up @@ -3,95 +3,84 @@
/* tslint:disable:no-var-requires */
/* tslint:disable:max-classes-per-file */
import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import * as sinon from 'sinon';

import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';

import { IdentityIssuedComponent } from './identity-issued.component';
import { IdentityCardService } from '../../services/identity-card.service';
import { WalletService } from '../../services/wallet.service';

import * as sinon from 'sinon';

describe('IdentityIssuedComponent', () => {
let component: IdentityIssuedComponent;
let fixture: ComponentFixture<IdentityIssuedComponent>;

let mockActiveModal = sinon.createStubInstance(NgbActiveModal);
let mockIdentityCardService = sinon.createStubInstance(IdentityCardService);
mockIdentityCardService.getCurrentConnectionProfile.returns({name: 'myProfile'});
mockIdentityCardService.getQualifiedProfileName.returns('xxx-myProfile');
let mockWalletService = sinon.createStubInstance(WalletService);

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ IdentityIssuedComponent ],
providers: [
{ provide: NgbActiveModal, useValue: mockActiveModal },
{ provide: IdentityCardService, useValue: mockIdentityCardService },
{ provide: WalletService, useValue: mockWalletService }
]
})
.compileComponents();

fixture = TestBed.createComponent(IdentityIssuedComponent);
component = fixture.componentInstance;
});

it('should be created', () => {
expect(component).should.be.ok;
});

describe('addToWallet', () => {
it('should add to wallet', fakeAsync(() => {
let walletStub = {contains : sinon.stub().returns(Promise.resolve(false)), add : sinon.stub().returns(Promise.resolve())};
mockWalletService.getWallet.returns(walletStub);

component['userID'] = 'myId';
component['userSecret'] = 'mySecret';

component.addToWallet();

tick();

mockWalletService.getWallet.should.have.been.calledWith('xxx-myProfile');
walletStub.contains.should.have.been.calledWith('myId');
walletStub.add.should.have.been.calledWith('myId', 'mySecret');
mockActiveModal.close.should.have.been.called;
}));

it('should update wallet if exists', fakeAsync(() => {
let walletStub = {contains : sinon.stub().returns(Promise.resolve(true)), update : sinon.stub().returns(Promise.resolve())};
mockWalletService.getWallet.returns(walletStub);

component['userID'] = 'myId';
component['userSecret'] = 'mySecret';

component.addToWallet();

tick();

mockWalletService.getWallet.should.have.been.calledWith('xxx-myProfile');
walletStub.contains.should.have.been.calledWith('myId');
walletStub.update.should.have.been.calledWith('myId', 'mySecret');
mockActiveModal.close.should.have.been.called;
}));

it('should handle error', fakeAsync(() => {
let walletStub = {contains : sinon.stub().returns(Promise.reject('some error')), add : sinon.stub().returns(Promise.resolve())};
mockWalletService.getWallet.returns(walletStub);

component['userID'] = 'myId';
component['userSecret'] = 'mySecret';

component.addToWallet();

tick();

mockWalletService.getWallet.should.have.been.calledWith('xxx-myProfile');
walletStub.contains.should.have.been.calledWith('myId');
walletStub.add.should.not.have.been.called;
mockActiveModal.dismiss.should.have.been.called;
}));
});
let component: IdentityIssuedComponent;
let fixture: ComponentFixture<IdentityIssuedComponent>;

let mockActiveModal;
let mockIdentityCard;

beforeEach(() => {
mockActiveModal = sinon.createStubInstance(NgbActiveModal);
mockIdentityCard = sinon.createStubInstance(IdentityCardService);

TestBed.configureTestingModule({
declarations: [IdentityIssuedComponent],
providers: [
{provide: NgbActiveModal, useValue: mockActiveModal},
{provide: IdentityCardService, useValue: mockIdentityCard},
]
});

fixture = TestBed.createComponent(IdentityIssuedComponent);
component = fixture.componentInstance;
});

it('should be created', () => {
component.should.be.ok;
});

describe('addToWallet', () => {
let mockCard;

beforeEach(() => {
mockCard = {
getConnectionProfile: sinon.stub().returns({name: 'myProfile'}),
getBusinessNetworkName: sinon.stub().returns('myNetwork')
};

mockIdentityCard.getCurrentIdentityCard.returns(mockCard);

mockIdentityCard.createIdentityCard.returns(Promise.resolve());

component['userID'] = 'myId';
component['userSecret'] = 'mySecret';
});

it('should add to wallet', fakeAsync(() => {
component.addToWallet();

tick();

mockIdentityCard.getCurrentIdentityCard.should.have.been.calledTwice;
mockCard.getConnectionProfile.should.have.been.called;
mockCard.getBusinessNetworkName.should.have.been.called;

mockIdentityCard.createIdentityCard.should.have.been.calledWith('myId', 'myNetwork', 'myId', 'mySecret', {name: 'myProfile'});

mockActiveModal.close.should.have.been.called;
}));

it('should handle error', fakeAsync(() => {
mockIdentityCard.createIdentityCard.returns(Promise.reject('some error'));

component.addToWallet();

tick();

mockIdentityCard.getCurrentIdentityCard.should.have.been.calledTwice;
mockCard.getConnectionProfile.should.have.been.called;
mockCard.getBusinessNetworkName.should.have.been.called;

mockIdentityCard.createIdentityCard.should.have.been.calledWith('myId', 'myNetwork', 'myId', 'mySecret', {name: 'myProfile'});
mockActiveModal.dismiss.should.have.been.called;
}));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Component, Input } from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';

import { IdentityCardService } from '../../services/identity-card.service';
import { WalletService } from '../../services/wallet.service';

@Component({
selector: 'identity-issued-modal',
Expand All @@ -18,29 +17,20 @@ export class IdentityIssuedComponent {
@Input() userSecret: string;

constructor(private activeModal: NgbActiveModal,
private identityCardService: IdentityCardService,
private walletService: WalletService) {
private identityCardService: IdentityCardService) {

}

addToWallet() {
let connectionProfile = this.identityCardService.getCurrentConnectionProfile();
let connectionProfileRef = this.identityCardService.getQualifiedProfileName(connectionProfile);
let wallet = this.walletService.getWallet(connectionProfileRef);

return wallet.contains(this.userID)
.then((inWallet) => {
if (inWallet) {
return wallet.update(this.userID, this.userSecret);
} else {
return wallet.add(this.userID, this.userSecret);
}
})
.then(() => {
this.activeModal.close();
})
.catch((error) => {
this.activeModal.dismiss(error);
});
addToWallet(): Promise<void> {
let connectionProfile = this.identityCardService.getCurrentIdentityCard().getConnectionProfile();
let businessNetworkName = this.identityCardService.getCurrentIdentityCard().getBusinessNetworkName();

return this.identityCardService.createIdentityCard(this.userID, businessNetworkName, this.userID, this.userSecret, connectionProfile)
.then(() => {
this.activeModal.close();
})
.catch((error) => {
this.activeModal.dismiss(error);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="main-view-content">
<div class="flex">
<div class="flex-container">
<h1 class="flex">My Wallet</h1>
<h1 class="flex">My IDs for {{businessNetworkName}}</h1>
<button type="button" class="secondary" (click)="issueNewId()">
<span>+ Issue New ID</span>
</button>
Expand All @@ -13,26 +13,27 @@ <h1 class="flex">My Wallet</h1>
<div class="id">ID Name</div>
<div class="status">Status</div>
</div>
<div class="identity" *ngFor="let id of myIdentities" (dblclick)="setCurrentIdentity(id)">
<div class="selected-border" [ngClass]="{'selected-border--selected': id === currentIdentity}"></div>
<div class="identity" *ngFor="let cardRef of cardRefs" (dblclick)="setCurrentIdentity(cardRef)">
<div class="selected-border"
[ngClass]="{'selected-border--selected': cardRef === currentIdentity}"></div>
<div class="id">
{{id}}
{{identityCards.get(cardRef).getName()}}
</div>
<div class="flex" *ngIf="id===currentIdentity">
<div class="flex" *ngIf="cardRef===currentIdentity">
In Use
</div>
<div class="flex" *ngIf="id!==currentIdentity">
<div class="flex" *ngIf="cardRef!==currentIdentity">
<i>In my wallet</i>
</div>
<div class="actions" *ngIf="id!==currentIdentity">
<button class="clear" (click)="setCurrentIdentity(id)">Use now</button>
<button class="clear" (click)="removeIdentity(id); $event.stopPropagation()">Remove</button>
<div class="actions" *ngIf="cardRef!==currentIdentity">
<button class="clear" (click)="setCurrentIdentity(cardRef)">Use now</button>
<button class="clear" (click)="removeIdentity(cardRef); $event.stopPropagation()">Remove</button>
</div>
</div>
</div>
<div class="flex">
<div class="flex-container">
<h1 class="flex">All IDs for {{ deployedPackageName }}</h1>
<h1 class="flex">All IDs for {{ businessNetworkName }}</h1>
</div>

<div class="identity-title">
Expand All @@ -44,7 +45,7 @@ <h1 class="flex">All IDs for {{ deployedPackageName }}</h1>
</div>
<div class="identity" *ngFor="let id of allIdentities">
<div class="selected-border"
[ngClass]="{'selected-border--selected': id.name === currentIdentity}"></div>
[ngClass]="{'selected-border--selected': id.ref === currentIdentity}"></div>
<div class="id cell-24">
{{ id.name }}
</div>
Expand All @@ -55,7 +56,7 @@ <h1 class="flex">All IDs for {{ deployedPackageName }}</h1>
{{ id.state }}
</div>
<div class="cell-24">
<div class="actions" *ngIf="id.name!==currentIdentity&&id.state!=='REVOKED'">
<div class="actions" *ngIf="id.ref !== currentIdentity && id.state!=='REVOKED'">
<button class="clear" (click)="revokeIdentity(id)">Revoke</button>
</div>
</div>
Expand Down
Loading

0 comments on commit c6c0e56

Please sign in to comment.