forked from hyperledger-archives/composer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generator fixes (hyperledger-archives#819)
- Loading branch information
1 parent
a897d86
commit 91ad5c7
Showing
19 changed files
with
466 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 91 additions & 75 deletions
166
packages/composer-playground/src/app/services/identity.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,170 +1,186 @@ | ||
/* tslint:disable:no-unused-variable */ | ||
|
||
import {TestBed, inject, fakeAsync, tick} from '@angular/core/testing'; | ||
import {IdentityService} from './identity.service'; | ||
import {LocalStorageService} from 'angular-2-local-storage'; | ||
import {ConnectionProfileService} from './connectionprofile.service'; | ||
import {WalletService} from './wallet.service'; | ||
import { TestBed, async, inject, fakeAsync, tick } from '@angular/core/testing'; | ||
import { IdentityService } from './identity.service'; | ||
import { LocalStorageService } from 'angular-2-local-storage'; | ||
import { ConnectionProfileService } from './connectionprofile.service'; | ||
import { ClientService } from './client.service'; | ||
import { WalletService } from './wallet.service'; | ||
import * as sinon from 'sinon'; | ||
|
||
import {FileWallet} from 'composer-common'; | ||
|
||
describe('IdentityService', () => { | ||
|
||
let mockLocalStorageService; | ||
let mockConnectionProfileService; | ||
let mockWalletService; | ||
|
||
|
||
beforeEach(() => { | ||
mockLocalStorageService = sinon.createStubInstance(LocalStorageService); | ||
mockConnectionProfileService = sinon.createStubInstance(ConnectionProfileService); | ||
mockWalletService = sinon.createStubInstance(WalletService); | ||
|
||
TestBed.configureTestingModule({ | ||
providers: [IdentityService, | ||
{provide: LocalStorageService, useValue: mockLocalStorageService}, | ||
{provide: ConnectionProfileService, useValue: mockConnectionProfileService}, | ||
{provide: WalletService, useValue: mockWalletService}] | ||
{provide: LocalStorageService, useValue: mockLocalStorageService}, | ||
{provide: ConnectionProfileService, useValue: mockConnectionProfileService}, | ||
{provide: WalletService, useValue:mockWalletService}] | ||
}); | ||
}); | ||
|
||
describe('getCurrentIdentities', () => { | ||
describe('getCurrentIdentities', () =>{ | ||
it('should get current identities', fakeAsync(inject([IdentityService], (service: IdentityService) => { | ||
mockConnectionProfileService.getCurrentConnectionProfile.returns('bob'); | ||
let stubGetIdentities = sinon.stub(service, 'getIdentities'); | ||
|
||
service.getCurrentIdentities(); | ||
mockConnectionProfileService.getCurrentConnectionProfile.returns("{'name':'profile','type': 'hlf'}") | ||
let stubGetIdentities = sinon.stub(service,'getIdentities'); | ||
stubGetIdentities.returns(Promise.resolve(['identity1','identity2'])) | ||
service.getCurrentIdentities().then((currentIdentities) => { | ||
currentIdentities.should.deep.equal(['identity1','identity2']); | ||
}) | ||
|
||
tick(); | ||
|
||
mockConnectionProfileService.getCurrentConnectionProfile.should.have.been.called; | ||
stubGetIdentities.should.have.been.calledWith('bob'); | ||
stubGetIdentities.should.be.calledWith("{'name':'profile','type': 'hlf'}") | ||
|
||
}))); | ||
}); | ||
}) | ||
|
||
describe('getIdentities', () => { | ||
describe('getIdentities', () =>{ | ||
it('should get identities', fakeAsync(inject([IdentityService], (service: IdentityService) => { | ||
|
||
let stubFileWallet = {list: sinon.stub().returns(Promise.resolve(['identity2', 'identity1']))}; | ||
|
||
let stubFileWallet = sinon.createStubInstance(FileWallet) | ||
stubFileWallet.list.returns(Promise.resolve(['identity2','identity1'])); | ||
mockWalletService.getWallet.returns(stubFileWallet); | ||
|
||
service.getIdentities("{'name':'profile','type': 'hlf'}"); | ||
service.getIdentities("{'name':'profile','type': 'hlf'}").then((identities) => { | ||
tick(); | ||
identities.should.deep.equal(['identity1','identity2']); | ||
mockWalletService.getWallet.should.be.calledWith("{'name':'profile','type': 'hlf'}") | ||
stubFileWallet.list.should.be.called; | ||
}) | ||
|
||
tick(); | ||
|
||
mockWalletService.getWallet.should.have.been.calledWith("{'name':'profile','type': 'hlf'}"); | ||
|
||
stubFileWallet.list.should.have.been.called; | ||
|
||
}))); | ||
}); | ||
}) | ||
|
||
describe('getCurrentIdentity', () => { | ||
it('should get current identity', fakeAsync(inject([IdentityService], (service: IdentityService) => { | ||
let stubGetIdentity = sinon.stub(service, 'getIdentity'); | ||
stubGetIdentity.returns(Promise.resolve('identity1')); | ||
mockConnectionProfileService.getCurrentConnectionProfile.returns("{'name':'profile','type': 'hlf'}"); | ||
let stubGetIdentity = sinon.stub(service,'getIdentity'); | ||
stubGetIdentity.returns(Promise.resolve('identity1')) | ||
mockConnectionProfileService.getCurrentConnectionProfile.returns("{'name':'profile','type': 'hlf'}") | ||
|
||
service.getCurrentIdentity().then((currentIdentity) => { | ||
stubGetIdentity.should.be.calledWith("{'name':'profile','type': 'hlf'}"); | ||
currentIdentity.should.equal('identity1'); | ||
}); | ||
}) | ||
|
||
tick(); | ||
|
||
stubGetIdentity.should.be.calledWith("{'name':'profile','type': 'hlf'}"); | ||
|
||
}))); | ||
}); | ||
}) | ||
|
||
|
||
describe('getIdentity', () => { | ||
it('should get an identity if it exists', fakeAsync(inject([IdentityService], (service: IdentityService) => { | ||
mockLocalStorageService.get.returns('identity1'); | ||
let stubGetIdentities = sinon.stub(service, 'getIdentities'); | ||
stubGetIdentities.returns(Promise.resolve(['identity1', 'identity2'])); | ||
let stubGetIdentities = sinon.stub(service,'getIdentities'); | ||
stubGetIdentities.returns(Promise.resolve(['identity1','identity2'])); | ||
|
||
service.getIdentity("{'name':'profile','type': 'hlf'}").then((identity) => { | ||
stubGetIdentities.should.be.calledWith("{'name':'profile','type': 'hlf'}"); | ||
}); | ||
service.getIdentity("{'name':'profile','type': 'hlf'}"); | ||
|
||
tick(); | ||
|
||
}))); | ||
stubGetIdentities.should.be.calledWith("{'name':'profile','type': 'hlf'}"); | ||
|
||
}))) | ||
it('should return another identity if the wanted identity doesnt exist', fakeAsync(inject([IdentityService], (service: IdentityService) => { | ||
mockLocalStorageService.get.returns('identity3'); | ||
let stubGetIdentities = sinon.stub(service, 'getIdentities'); | ||
stubGetIdentities.returns(Promise.resolve(['identity1', 'identity2'])); | ||
let stubGetIdentities = sinon.stub(service,'getIdentities'); | ||
stubGetIdentities.returns(Promise.resolve(['identity1','identity2'])); | ||
|
||
service.getIdentity("{'name':'profile','type': 'hlf'}").then((identity) => { | ||
stubGetIdentities.should.be.calledWith("{'name':'profile','type': 'hlf'}"); | ||
}); | ||
service.getIdentity("{'name':'profile','type': 'hlf'}"); | ||
|
||
tick(); | ||
|
||
}))); | ||
stubGetIdentities.should.be.calledWith("{'name':'profile','type': 'hlf'}"); | ||
|
||
}))); | ||
it('should return null if no identites exist', fakeAsync(inject([IdentityService], (service: IdentityService) => { | ||
mockLocalStorageService.get.returns('identity3'); | ||
let stubGetIdentities = sinon.stub(service, 'getIdentities'); | ||
let stubGetIdentities = sinon.stub(service,'getIdentities'); | ||
stubGetIdentities.returns(Promise.resolve([])); | ||
|
||
service.getIdentity("{'name':'profile','type': 'hlf'}").then((identity) => { | ||
stubGetIdentities.should.be.calledWith("{'name':'profile','type': 'hlf'}"); | ||
}); | ||
service.getIdentity("{'name':'profile','type': 'hlf'}"); | ||
|
||
tick(); | ||
|
||
}))); | ||
}); | ||
stubGetIdentities.should.be.calledWith("{'name':'profile','type': 'hlf'}"); | ||
|
||
}))) | ||
}) | ||
|
||
describe('setCurrentIdentity', () => { | ||
it('should set current identity', inject([IdentityService], (service: IdentityService) => { | ||
let stubSetIdentity = sinon.stub(service, 'setIdentity'); | ||
mockConnectionProfileService.getCurrentConnectionProfile.returns("{'name':'profile','type': 'hlf'}"); | ||
it('should set current identity', fakeAsync(inject([IdentityService], (service: IdentityService) => { | ||
let stubSetIdentity = sinon.stub(service,'setIdentity'); | ||
mockConnectionProfileService.getCurrentConnectionProfile.returns("{'name':'profile','type': 'hlf'}") | ||
|
||
service.setCurrentIdentity('identity1'); | ||
stubSetIdentity.should.be.calledWith("{'name':'profile','type': 'hlf'}", 'identity1'); | ||
})); | ||
}); | ||
|
||
tick(); | ||
|
||
stubSetIdentity.should.be.calledWith("{'name':'profile','type': 'hlf'}",'identity1'); | ||
|
||
}))) | ||
}) | ||
|
||
describe('setIdentity', () => { | ||
it('should set identity', inject([IdentityService], (service: IdentityService) => { | ||
it('should set identity', fakeAsync(inject([IdentityService], (service: IdentityService) => { | ||
|
||
service.setIdentity("{'name':'profile','type': 'hlf'}", 'identity1'); | ||
mockLocalStorageService.set.should.be.calledWith("currentIdentity:{'name':'profile','type': 'hlf'}", 'identity1') | ||
service.setIdentity("{'name':'profile','type': 'hlf'}",'identity1'); | ||
tick(); | ||
mockLocalStorageService.set.should.be.calledWith("currentIdentity:{'name':'profile','type': 'hlf'}",'identity1') | ||
|
||
})); | ||
}); | ||
}))) | ||
}) | ||
|
||
describe('getUserID', () => { | ||
it('should get user id', fakeAsync(inject([IdentityService], (service: IdentityService) => { | ||
let stubGetCurrentIdentity = sinon.stub(service, 'getCurrentIdentity'); | ||
stubGetCurrentIdentity.returns(Promise.resolve('currentIdentity')); | ||
stubGetCurrentIdentity.returns(Promise.resolve('currentIdentity')) | ||
service.getUserID().then((userID) => { | ||
stubGetCurrentIdentity.should.be.called; | ||
userID.should.equal('currentIdentity'); | ||
}); | ||
}) | ||
|
||
tick(); | ||
|
||
}))); | ||
}); | ||
stubGetCurrentIdentity.should.be.called; | ||
|
||
}))) | ||
}) | ||
|
||
describe('getUserSecret', () => { | ||
it('should get user secret', fakeAsync(inject([IdentityService], (service: IdentityService) => { | ||
let stubGetCurrentIdentity = sinon.stub(service, 'getCurrentIdentity'); | ||
stubGetCurrentIdentity.returns(Promise.resolve('currentIdentity')); | ||
|
||
mockConnectionProfileService.getCurrentConnectionProfile.returns('bob'); | ||
|
||
let stubFileWallet = {get: sinon.stub().returns(Promise.resolve('secret2'))}; | ||
|
||
mockConnectionProfileService.getCurrentConnectionProfile.returns("{'name':'profile','type': 'hlf'}"); | ||
let stubFileWallet = sinon.createStubInstance(FileWallet) | ||
stubFileWallet.get.returns(Promise.resolve('secret2')); | ||
mockWalletService.getWallet.returns(stubFileWallet); | ||
|
||
service.getUserSecret(); | ||
let result = service.getUserSecret().then((result) => { | ||
result.should.equal('secret2'); | ||
stubGetCurrentIdentity.should.be.called; | ||
mockConnectionProfileService.getCurrentConnectionProfile.should.be.called; | ||
mockWalletService.getWallet.should.be.calledWith("{'name':'profile','type': 'hlf'}"); | ||
}) | ||
|
||
tick(); | ||
mockConnectionProfileService.getCurrentConnectionProfile.should.have.been.called; | ||
mockWalletService.getWallet.should.be.calledWith('bob'); | ||
|
||
stubFileWallet.get.should.have.been.calledWith('currentIdentity'); | ||
}))); | ||
}); | ||
}))) | ||
}) | ||
|
||
}); |
Oops, something went wrong.