Skip to content

Commit

Permalink
feat(js-dapi-client): add unit and integration tests for getIdentityB…
Browse files Browse the repository at this point in the history
…alance
  • Loading branch information
pshenmic committed Sep 4, 2024
1 parent 4131d1d commit ef1f048
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const InvalidResponseError = require('../response/errors/InvalidResponseError');

class GetIdentityBalanceResponse extends AbstractResponse {
/**
* @param {Buffer} identity
* @param {number} balance
* @param {Metadata} metadata
* @param {Proof} [proof]
*/
Expand All @@ -14,9 +14,9 @@ class GetIdentityBalanceResponse extends AbstractResponse {
}

/**
* @returns {Buffer}
* @returns {number}
*/
getIdentity() {
getBalance() {
return this.balance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
GetDataContractResponse,
GetDocumentsResponse,
GetIdentityResponse,
GetIdentityBalanceResponse,
GetIdentityByPublicKeyHashResponse,
GetIdentitiesContractKeysResponse,
GetEpochsInfoResponse,
Expand All @@ -27,6 +28,7 @@ const PlatformMethodsFacade = require('../../../../lib/methods/platform/Platform

const { WaitForStateTransitionResultResponseV0 } = WaitForStateTransitionResultResponse;
const { GetIdentityResponseV0 } = GetIdentityResponse;
const { GetIdentityBalanceResponseV0 } = GetIdentityBalanceResponse;
const { GetIdentityByPublicKeyHashResponseV0 } = GetIdentityByPublicKeyHashResponse;
const { GetIdentitiesContractKeysResponseV0 } = GetIdentitiesContractKeysResponse;
const { GetDocumentsResponseV0 } = GetDocumentsResponse;
Expand Down Expand Up @@ -318,4 +320,22 @@ describe('PlatformMethodsFacade', () => {
expect(grpcTransportMock.request).to.be.calledOnce();
});
});

describe('#getIdentityBalance', () => {
it('should get identity balance', async () => {
const response = new GetIdentityBalanceResponse();

response.setV0(
new GetIdentityBalanceResponseV0()
.setMetadata(new ResponseMetadata())
.setBalance(1337),
);

grpcTransportMock.request.resolves(response);

await platformMethods.getIdentityBalance('41nthkqvHBLnqiMkSbsdTNANzYu9bgdv4etKoRUunY1M');

expect(grpcTransportMock.request).to.be.calledOnce();
});
});
});
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);
}
});
});
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,
);
}
});
});

0 comments on commit ef1f048

Please sign in to comment.