-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js-dapi-client): add unit and integration tests for getIdentityB…
…alance
- Loading branch information
Showing
4 changed files
with
305 additions
and
3 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
126 changes: 126 additions & 0 deletions
126
...i-client/test/unit/methods/platform/getIdentityBalance/GetIdentityBalanceResponse.spec.js
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
const { | ||
v0: { | ||
GetIdentityBalanceResponse, | ||
ResponseMetadata, | ||
Proof: ProofResponse, | ||
}, | ||
} = require('@dashevo/dapi-grpc'); | ||
|
||
const GetIdentityBalanceResponseClass = require('../../../../../lib/methods/platform/getIdentityBalance/GetIdentityBalanceResponse'); | ||
const getMetadataFixture = require('../../../../../lib/test/fixtures/getMetadataFixture'); | ||
const InvalidResponseError = require('../../../../../lib/methods/platform/response/errors/InvalidResponseError'); | ||
const getProofFixture = require('../../../../../lib/test/fixtures/getProofFixture'); | ||
const Proof = require('../../../../../lib/methods/platform/response/Proof'); | ||
const Metadata = require('../../../../../lib/methods/platform/response/Metadata'); | ||
|
||
describe('GetIdentityBalanceResponse', () => { | ||
let getIdentityBalanceResponse; | ||
let metadataFixture; | ||
let balance; | ||
let proto; | ||
let proofFixture; | ||
|
||
beforeEach(async () => { | ||
metadataFixture = getMetadataFixture(); | ||
proofFixture = getProofFixture(); | ||
balance = 1337; | ||
|
||
const { GetIdentityBalanceResponseV0 } = GetIdentityBalanceResponse; | ||
proto = new GetIdentityBalanceResponse(); | ||
|
||
const metadata = new ResponseMetadata(); | ||
metadata.setHeight(metadataFixture.height); | ||
metadata.setCoreChainLockedHeight(metadataFixture.coreChainLockedHeight); | ||
metadata.setTimeMs(metadataFixture.timeMs); | ||
metadata.setProtocolVersion(metadataFixture.protocolVersion); | ||
|
||
proto.setV0( | ||
new GetIdentityBalanceResponseV0() | ||
.setBalance(balance) | ||
.setMetadata(metadata), | ||
); | ||
|
||
getIdentityBalanceResponse = new GetIdentityBalanceResponseClass( | ||
balance, | ||
new Metadata(metadataFixture), | ||
); | ||
}); | ||
|
||
it('should return Identity balance', () => { | ||
const identityBalance = getIdentityBalanceResponse.getBalance(); | ||
const identityProof = getIdentityBalanceResponse.getProof(); | ||
|
||
expect(identityBalance).to.equal(balance); | ||
expect(identityProof).to.equal(undefined); | ||
}); | ||
|
||
it('should return proof', () => { | ||
getIdentityBalanceResponse = new GetIdentityBalanceResponseClass( | ||
balance, | ||
new Metadata(metadataFixture), | ||
new Proof(proofFixture), | ||
); | ||
|
||
const identityBalance = getIdentityBalanceResponse.getBalance(); | ||
const proof = getIdentityBalanceResponse.getProof(); | ||
|
||
expect(identityBalance).to.equal(balance); | ||
expect(proof).to.be.an.instanceOf(Proof); | ||
expect(proof.getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); | ||
expect(proof.getQuorumHash()).to.deep.equal(proofFixture.quorumHash); | ||
expect(proof.getSignature()).to.deep.equal(proofFixture.signature); | ||
expect(proof.getRound()).to.deep.equal(proofFixture.round); | ||
}); | ||
|
||
it('should create an instance from proto', () => { | ||
getIdentityBalanceResponse = GetIdentityBalanceResponseClass.createFromProto(proto); | ||
expect(getIdentityBalanceResponse).to.be | ||
.an.instanceOf(GetIdentityBalanceResponseClass); | ||
expect(getIdentityBalanceResponse.getBalance()).to.equal(balance); | ||
|
||
expect(getIdentityBalanceResponse.getMetadata()) | ||
.to.be.an.instanceOf(Metadata); | ||
expect(getIdentityBalanceResponse.getMetadata().getHeight()) | ||
.to.equal(metadataFixture.height); | ||
expect(getIdentityBalanceResponse.getMetadata().getCoreChainLockedHeight()) | ||
.to.equal(metadataFixture.coreChainLockedHeight); | ||
|
||
expect(getIdentityBalanceResponse.getProof()).to.equal(undefined); | ||
}); | ||
|
||
it('should create an instance with proof from proto', () => { | ||
const proofProto = new ProofResponse(); | ||
|
||
proofProto.setQuorumHash(proofFixture.quorumHash); | ||
proofProto.setSignature(proofFixture.signature); | ||
proofProto.setGrovedbProof(proofFixture.merkleProof); | ||
proofProto.setRound(proofFixture.round); | ||
|
||
proto.getV0().setBalance(undefined); | ||
proto.getV0().setProof(proofProto); | ||
|
||
getIdentityBalanceResponse = GetIdentityBalanceResponseClass.createFromProto(proto); | ||
|
||
expect(getIdentityBalanceResponse.getBalance()).to.equal(0); | ||
expect(getIdentityBalanceResponse.getMetadata()).to.deep.equal(metadataFixture); | ||
|
||
const proof = getIdentityBalanceResponse.getProof(); | ||
expect(proof).to.be.an.instanceOf(Proof); | ||
expect(proof.getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); | ||
expect(proof.getQuorumHash()).to.deep.equal(proofFixture.quorumHash); | ||
expect(proof.getSignature()).to.deep.equal(proofFixture.signature); | ||
expect(proof.getRound()).to.deep.equal(proofFixture.round); | ||
}); | ||
|
||
it('should throw InvalidResponseError if Metadata is not defined', () => { | ||
proto.getV0().setMetadata(undefined); | ||
|
||
try { | ||
getIdentityBalanceResponse = GetIdentityBalanceResponseClass.createFromProto(proto); | ||
|
||
expect.fail('should throw InvalidResponseError'); | ||
} catch (e) { | ||
expect(e).to.be.an.instanceOf(InvalidResponseError); | ||
} | ||
}); | ||
}); |
156 changes: 156 additions & 0 deletions
156
...pi-client/test/unit/methods/platform/getIdentityBalance/getIdentityBalanceFactory.spec.js
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 |
---|---|---|
@@ -0,0 +1,156 @@ | ||
const { | ||
v0: { | ||
PlatformPromiseClient, | ||
GetIdentityBalanceRequest, | ||
GetIdentityBalanceResponse, | ||
ResponseMetadata, | ||
Proof: ProofResponse, | ||
}, | ||
} = require('@dashevo/dapi-grpc'); | ||
|
||
const { GetIdentityBalanceResponseV0 } = GetIdentityBalanceResponse; | ||
|
||
const getIdentityBalanceFactory = require('../../../../../lib/methods/platform/getIdentityBalance/getIdentityBalanceFactory'); | ||
const getMetadataFixture = require('../../../../../lib/test/fixtures/getMetadataFixture'); | ||
const getProofFixture = require('../../../../../lib/test/fixtures/getProofFixture'); | ||
const Proof = require('../../../../../lib/methods/platform/response/Proof'); | ||
|
||
describe('getIdentityBalanceFactory', () => { | ||
let grpcTransportMock; | ||
let getIdentityBalance; | ||
let options; | ||
let response; | ||
let balance; | ||
let identityId; | ||
let metadataFixture; | ||
let proofFixture; | ||
let proofResponse; | ||
|
||
beforeEach(async function beforeEach() { | ||
balance = 1337; | ||
|
||
identityId = Buffer.alloc(32).fill(0); | ||
|
||
metadataFixture = getMetadataFixture(); | ||
proofFixture = getProofFixture(); | ||
|
||
const metadata = new ResponseMetadata(); | ||
metadata.setHeight(metadataFixture.height); | ||
metadata.setCoreChainLockedHeight(metadataFixture.coreChainLockedHeight); | ||
metadata.setTimeMs(metadataFixture.timeMs); | ||
metadata.setProtocolVersion(metadataFixture.protocolVersion); | ||
|
||
response = new GetIdentityBalanceResponse(); | ||
|
||
response.setV0( | ||
new GetIdentityBalanceResponseV0() | ||
.setBalance(balance) | ||
.setMetadata(metadata), | ||
); | ||
|
||
proofResponse = new ProofResponse(); | ||
|
||
proofResponse.setQuorumHash(proofFixture.quorumHash); | ||
proofResponse.setSignature(proofFixture.signature); | ||
proofResponse.setGrovedbProof(proofFixture.merkleProof); | ||
proofResponse.setRound(proofFixture.round); | ||
|
||
grpcTransportMock = { | ||
request: this.sinon.stub().resolves(response), | ||
}; | ||
|
||
getIdentityBalance = getIdentityBalanceFactory(grpcTransportMock); | ||
|
||
options = { | ||
timeout: 1000, | ||
}; | ||
}); | ||
|
||
it('should return identity balance', async () => { | ||
const result = await getIdentityBalance(identityId, options); | ||
|
||
const { GetIdentityBalanceRequestV0 } = GetIdentityBalanceRequest; | ||
const request = new GetIdentityBalanceRequest(); | ||
request.setV0( | ||
new GetIdentityBalanceRequestV0() | ||
.setId(identityId) | ||
.setProve(false), | ||
); | ||
|
||
expect(grpcTransportMock.request).to.be.calledOnceWithExactly( | ||
PlatformPromiseClient, | ||
'getIdentityBalance', | ||
request, | ||
options, | ||
); | ||
expect(result.getBalance()).to.deep.equal(balance); | ||
expect(result.getMetadata()).to.deep.equal(metadataFixture); | ||
expect(result.getProof()).to.equal(undefined); | ||
}); | ||
|
||
it('should return proof', async () => { | ||
options.prove = true; | ||
response.getV0().setBalance(undefined); | ||
response.getV0().setProof(proofResponse); | ||
|
||
const result = await getIdentityBalance(identityId, options); | ||
|
||
const { GetIdentityBalanceRequestV0 } = GetIdentityBalanceRequest; | ||
const request = new GetIdentityBalanceRequest(); | ||
request.setV0( | ||
new GetIdentityBalanceRequestV0() | ||
.setId(identityId) | ||
.setProve(true), | ||
); | ||
|
||
expect(grpcTransportMock.request).to.be.calledOnceWithExactly( | ||
PlatformPromiseClient, | ||
'getIdentityBalance', | ||
request, | ||
options, | ||
); | ||
|
||
expect(result.getBalance()).to.deep.equal(0); | ||
|
||
expect(result.getMetadata()).to.deep.equal(metadataFixture); | ||
|
||
expect(result.getProof()).to.be.an.instanceOf(Proof); | ||
expect(result.getProof().getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); | ||
expect(result.getProof().getQuorumHash()).to.deep.equal(proofFixture.quorumHash); | ||
expect(result.getProof().getSignature()).to.deep.equal(proofFixture.signature); | ||
expect(result.getProof().getRound()).to.deep.equal(proofFixture.round); | ||
expect(result.getMetadata()).to.deep.equal(metadataFixture); | ||
expect(result.getMetadata().getHeight()).to.equal(metadataFixture.height); | ||
expect(result.getMetadata().getCoreChainLockedHeight()).to.equal( | ||
metadataFixture.coreChainLockedHeight, | ||
); | ||
}); | ||
|
||
it('should throw unknown error', async () => { | ||
const error = new Error('Unknown found'); | ||
|
||
grpcTransportMock.request.throws(error); | ||
|
||
const { GetIdentityBalanceRequestV0 } = GetIdentityBalanceRequest; | ||
const request = new GetIdentityBalanceRequest(); | ||
request.setV0( | ||
new GetIdentityBalanceRequestV0() | ||
.setId(identityId) | ||
.setProve(false), | ||
); | ||
|
||
try { | ||
await getIdentityBalance(identityId, options); | ||
|
||
expect.fail('should throw unknown error'); | ||
} catch (e) { | ||
expect(e).to.deep.equal(error); | ||
expect(grpcTransportMock.request).to.be.calledOnceWithExactly( | ||
PlatformPromiseClient, | ||
'getIdentityBalance', | ||
request, | ||
options, | ||
); | ||
} | ||
}); | ||
}); |