diff --git a/server/handlers/trustHandler.spec.js b/server/handlers/trustHandler.spec.js index 91a0f310..4ade4893 100644 --- a/server/handlers/trustHandler.spec.js +++ b/server/handlers/trustHandler.spec.js @@ -14,7 +14,6 @@ const TrustService = require('../services/TrustService'); const JWTService = require('../services/JWTService'); const TrustRelationshipEnums = require('../utils/trust-enums'); - describe('trustRouter', () => { let app; const authenticatedWalletId = uuid.v4(); @@ -193,48 +192,41 @@ describe('trustRouter', () => { expect(res.body.message).match(/request_type.*one.*of/); }); - it('successfully', async () => { - const limit = 10; - const offset = 0; - const count = 1; - const orderBy = 'created_at'; - const order = 'desc'; - - const getAllTrustRelationshipsStub = sinon - .stub(TrustService.prototype, 'getAllTrustRelationships') - .resolves({ - result: [{ id: trustId }], - count + it('successfully', async () => { + const limit = 10; + const offset = 0; + const orderBy = 'created_at'; + const order = 'desc'; + + const getAllTrustRelationshipsStub = sinon + .stub(TrustService.prototype, 'getAllTrustRelationships') + .resolves([{ id: trustId }]); + + const res = await request(app).get( + `/trust_relationships?type=${TrustRelationshipEnums.ENTITY_TRUST_TYPE.send}&request_type=${TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send}&state=${TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted}&limit=${limit}&offset=${offset}&order=${order}&sort_by=${orderBy}`, + ); + + expect(res).property('statusCode').eq(200); + expect(res.body.trust_relationships).to.have.lengthOf(1); + expect(res.body.trust_relationships[0]).to.eql({ id: trustId }); + + expect(getAllTrustRelationshipsStub).to.have.been.calledWith({ + state: TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted, + type: TrustRelationshipEnums.ENTITY_TRUST_TYPE.send, + request_type: TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send, + limit, + offset, + order, + sort_by: orderBy, + walletId: authenticatedWalletId, }); - - const res = await request(app).get( - `/trust_relationships?type=${TrustRelationshipEnums.ENTITY_TRUST_TYPE.send}&request_type=${TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send}&state=${TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted}&limit=${limit}&offset=${offset}&order=${order}&sort_by=${orderBy}`, - ); - - expect(res).property('statusCode').eq(200); - expect(res.body.trust_relationships).to.have.lengthOf(1); - expect(res.body.trust_relationships[0]).to.eql({ id: trustId }); - - expect(getAllTrustRelationshipsStub).to.have.been.calledWith({ - state: TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted, - type: TrustRelationshipEnums.ENTITY_TRUST_TYPE.send, - request_type: TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send, - limit, - offset, - order, - sort_by: orderBy, - walletId: authenticatedWalletId, }); }); -}); - - - describe('get /trust_relationships/:id', () => { it('missed parameters -- relationshipId must be a guid', async () => { const res = await request(app).get( - `/trust_relationships/trustRelationshipId`, + `/trust_relationships/trustRelationshipId`, ); expect(res).property('statusCode').eq(422); expect(res.body.message).match(/trustRelationshipId.*GUID/); @@ -244,21 +236,20 @@ describe('trustRouter', () => { const trustRelationshipId = uuid.v4(); const trustRelationshipGetByIdStub = sinon - .stub(TrustService.prototype, 'trustRelationshipGetById') - .resolves({id: trustRelationshipId}); + .stub(TrustService.prototype, 'trustRelationshipGetById') + .resolves({ id: trustRelationshipId }); const res = await request(app).get( - `/trust_relationships/${trustRelationshipId}`, + `/trust_relationships/${trustRelationshipId}`, ); expect(res).property('statusCode').eq(200); expect( - trustRelationshipGetByIdStub.calledOnceWithExactly({ - walletLoginId: authenticatedWalletId, - trustRelationshipId, - }), + trustRelationshipGetByIdStub.calledOnceWithExactly({ + walletLoginId: authenticatedWalletId, + trustRelationshipId, + }), ).eql(true); }); - - }) + }); }); diff --git a/server/handlers/walletHandler.spec.js b/server/handlers/walletHandler.spec.js index c1a80e13..8f17a61a 100644 --- a/server/handlers/walletHandler.spec.js +++ b/server/handlers/walletHandler.spec.js @@ -15,7 +15,7 @@ const TrustService = require('../services/TrustService'); const JWTService = require('../services/JWTService'); const TrustRelationshipEnums = require('../utils/trust-enums'); -describe.only('walletRouter', () => { +describe('walletRouter', () => { let app; const authenticatedWalletId = uuid.v4(); @@ -105,12 +105,13 @@ describe.only('walletRouter', () => { it('successfully', async () => { const getTrustRelationshipsStub = sinon .stub(TrustService.prototype, 'getTrustRelationships') - .resolves([{ id: trustRelationshipId }]); + .resolves({ result: [{ id: trustRelationshipId }], count: 1 }); const res = await request(app).get( `/wallets/${walletId}/trust_relationships?state=${TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.requested}`, ); expect(res).property('statusCode').eq(200); expect(res.body.trust_relationships).lengthOf(1); + expect(res.body.total).eql(1); expect(res.body.trust_relationships[0].id).eql(trustRelationshipId); expect( getTrustRelationshipsStub.calledOnceWithExactly(authenticatedWalletId, { @@ -118,6 +119,10 @@ describe.only('walletRouter', () => { state: TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.requested, type: undefined, request_type: undefined, + limit: 500, + offset: 0, + sort_by: 'created_at', + order: 'desc', }), ).eql(true); }); diff --git a/server/handlers/walletHandler/index.js b/server/handlers/walletHandler/index.js index 68f25401..7871513b 100644 --- a/server/handlers/walletHandler/index.js +++ b/server/handlers/walletHandler/index.js @@ -89,7 +89,6 @@ const walletGetTrustRelationships = async (req, res) => { order, } = validatedQuery; - const trustService = new TrustService(); const trust_relationships = await trustService.getTrustRelationships( loggedInWalletId, diff --git a/server/repositories/WalletRepository.js b/server/repositories/WalletRepository.js index 07f09a5f..29783326 100644 --- a/server/repositories/WalletRepository.js +++ b/server/repositories/WalletRepository.js @@ -64,7 +64,7 @@ class WalletRepository extends BaseRepository { created_at_end_date, getCount, ) { - let query = this._session + let query = this._session .getDB() .select('id', 'name', 'about', 'logo_url', 'created_at') .table('wallet') diff --git a/server/services/TrustService.js b/server/services/TrustService.js index 6ee74ca4..65b80cd9 100644 --- a/server/services/TrustService.js +++ b/server/services/TrustService.js @@ -69,7 +69,7 @@ class TrustService { await Promise.all( wallets.map(async (w) => { - const trustRelationships = await this.getTrustRelationships(walletId,{ + const trustRelationships = await this.getTrustRelationships(walletId, { walletId: w.id, state, type, diff --git a/server/services/TrustService.spec.js b/server/services/TrustService.spec.js index 16878151..eb3613b6 100644 --- a/server/services/TrustService.spec.js +++ b/server/services/TrustService.spec.js @@ -26,6 +26,10 @@ describe('TrustService', () => { .stub(Trust.prototype, 'getTrustRelationships') .resolves(['trustRelationships']); + const getTrustRelationshipsCountStub = sinon + .stub(Trust.prototype, 'getTrustRelationshipsCount') + .resolves(1); + const getWalletStub = sinon .stub(WalletService.prototype, 'getWallet') .resolves({ id: 'walletId' }); @@ -48,7 +52,8 @@ describe('TrustService', () => { }, ); - expect(trustRelationship).eql(['trustRelationships']); + expect(trustRelationship.result).eql(['trustRelationships']); + expect(trustRelationship.count).eql(1); expect( getWalletStub.calledOnceWithExactly({ walletId: 'walletId', @@ -66,6 +71,14 @@ describe('TrustService', () => { order: 'order', }), ).eql(true); + expect( + getTrustRelationshipsCountStub.calledOnceWithExactly({ + walletId: 'walletId', + state: 'state', + type: 'type', + request_type: 'request_type', + }), + ).eql(true); expect( hasControlOverStub.calledOnceWithExactly( authenticatedWalletId, @@ -207,22 +220,22 @@ describe('TrustService', () => { TrustService.prototype, 'getTrustRelationships', ); - getTrustRelationshipsStub - .onFirstCall() - .resolves([ + getTrustRelationshipsStub.onFirstCall().resolves({ + result: [ { id: 'trustId1' }, { id: 'trustId2' }, { id: 'trustId3' }, { id: 'trustId4' }, - ]); - getTrustRelationshipsStub - .onSecondCall() - .resolves([ + ], + }); + getTrustRelationshipsStub.onSecondCall().resolves({ + result: [ { id: 'trustId1' }, { id: 'trustId2' }, { id: 'trustId5' }, { id: 'trustId6' }, - ]); + ], + }); const trustRelationships = await trustService.getAllTrustRelationships({ walletId: 'walletId', @@ -248,7 +261,7 @@ describe('TrustService', () => { ), ).eql(true); expect( - getTrustRelationshipsStub.getCall(0).calledWithExactly({ + getTrustRelationshipsStub.getCall(0).calledWithExactly('walletId', { walletId: 'id1', state: 'state', type: 'type', @@ -256,7 +269,7 @@ describe('TrustService', () => { }), ).eql(true); expect( - getTrustRelationshipsStub.getCall(1).calledWithExactly({ + getTrustRelationshipsStub.getCall(1).calledWithExactly('walletId', { walletId: 'id2', state: 'state', type: 'type',