From 39ac44f355519dbc331e31087f516d7297021a56 Mon Sep 17 00:00:00 2001 From: Pranav Kakaraparti Date: Sat, 19 Aug 2023 14:26:41 -0500 Subject: [PATCH 1/5] fix: /wallets/:id/trust_relationships returns 404 for invalid id --- server/services/TrustService.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/server/services/TrustService.js b/server/services/TrustService.js index 8a5e34b7..51c16815 100644 --- a/server/services/TrustService.js +++ b/server/services/TrustService.js @@ -17,6 +17,11 @@ class TrustService { offset = 0, limit, }) { + // check of wallet exists first + // throws error if no wallet matching walletId exists + const walletService = new WalletService() + await walletService.getWallet(walletId) + return this._trust.getTrustRelationships({ walletId, state, @@ -104,6 +109,11 @@ class TrustService { } async cancelTrustRequest({ walletLoginId, trustRelationshipId }) { + // check if trust relationship matching trustRelationshipId exists + // const trs = await this._session.getDB().select('*').from('wallet_trust').where('id', trustRelationshipId) + // console.log('TRUSTS', trs) + + return this._trust.cancelTrustRequest({ walletId: walletLoginId, trustRelationshipId, From 6718a177f5978219fca886e53d83891ac094906b Mon Sep 17 00:00:00 2001 From: Pranav Kakaraparti Date: Tue, 22 Aug 2023 01:02:42 -0500 Subject: [PATCH 2/5] feat: delete /trust_relationship/:id --- server/models/Trust.js | 8 ++++++++ server/services/TrustService.js | 5 ----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/server/models/Trust.js b/server/models/Trust.js index 9b1d0660..c14a7b1c 100644 --- a/server/models/Trust.js +++ b/server/models/Trust.js @@ -336,6 +336,14 @@ class Trust { 'wallet_trust.id': trustRelationshipId, }); const [trustRelationship] = trustRelationships; + + if(!trustRelationship){ + throw new HttpError( + 404, + 'No such trust relationship exists or it is not associated with the current wallet.' + ) + } + if (trustRelationship?.originator_wallet_id !== walletId) { throw new HttpError( 403, diff --git a/server/services/TrustService.js b/server/services/TrustService.js index a1a23a2c..664be38c 100644 --- a/server/services/TrustService.js +++ b/server/services/TrustService.js @@ -109,11 +109,6 @@ class TrustService { } async cancelTrustRequest({ walletLoginId, trustRelationshipId }) { - // check if trust relationship matching trustRelationshipId exists - // const trs = await this._session.getDB().select('*').from('wallet_trust').where('id', trustRelationshipId) - // console.log('TRUSTS', trs) - - return this._trust.cancelTrustRequest({ walletId: walletLoginId, trustRelationshipId, From 08b9eb36052f467de062cc033f983dc45321da0a Mon Sep 17 00:00:00 2001 From: Pranav Kakaraparti Date: Tue, 22 Aug 2023 21:11:48 -0500 Subject: [PATCH 3/5] fix: 404 error code- POST /tr_rel/:id/accept, /decline, DELETE /tr_rel/:id --- server/models/Trust.js | 27 +++++---------------------- server/services/TrustService.js | 2 +- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/server/models/Trust.js b/server/models/Trust.js index c14a7b1c..86a5fd5a 100644 --- a/server/models/Trust.js +++ b/server/models/Trust.js @@ -289,8 +289,8 @@ class Trust { if (!trustRelationship) { throw new HttpError( - 403, - 'Have no permission to accept this relationship', + 404, + 'No such trust relationship exists or it is not associated with the current wallet.', ); } await this.checkManageCircle({ walletId, trustRelationship }); @@ -317,8 +317,8 @@ class Trust { if (!trustRelationship) { throw new HttpError( - 403, - 'Have no permission to decline this relationship', + 404, + 'No such trust relationship exists or it is not associated with the current wallet.', ); } @@ -332,24 +332,7 @@ class Trust { * Cancel a trust relationship request */ async cancelTrustRequest({ trustRelationshipId, walletId }) { - const trustRelationships = await this._trustRepository.getByFilter({ - 'wallet_trust.id': trustRelationshipId, - }); - const [trustRelationship] = trustRelationships; - - if(!trustRelationship){ - throw new HttpError( - 404, - 'No such trust relationship exists or it is not associated with the current wallet.' - ) - } - - if (trustRelationship?.originator_wallet_id !== walletId) { - throw new HttpError( - 403, - 'Have no permission to cancel this relationship', - ); - } + const trustRelationship = await this.getTrustRelationshipById({ walletId, trustRelationshipId}) return this.updateTrustState( trustRelationship, diff --git a/server/services/TrustService.js b/server/services/TrustService.js index 664be38c..e88ff3ce 100644 --- a/server/services/TrustService.js +++ b/server/services/TrustService.js @@ -17,7 +17,7 @@ class TrustService { offset = 0, limit, }) { - // check of wallet exists first + // check if wallet exists first // throws error if no wallet matching walletId exists const walletService = new WalletService() await walletService.getWallet(walletId) From e756e008cf2e261326cc50afbeb4aa1e11d4739f Mon Sep 17 00:00:00 2001 From: Pranav Kakaraparti Date: Tue, 22 Aug 2023 21:12:06 -0500 Subject: [PATCH 4/5] fix: fixed broken tests --- server/models/Trust.spec.js | 51 +++++++++++++++++++++------- server/services/TrustService.spec.js | 7 ++++ 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/server/models/Trust.spec.js b/server/models/Trust.spec.js index 3825bd1f..15476b3c 100644 --- a/server/models/Trust.spec.js +++ b/server/models/Trust.spec.js @@ -626,9 +626,9 @@ describe('Trust Model', () => { error = e; } - expect(error.code).eql(403); + expect(error.code).eql(404); expect(error.message).eql( - 'Have no permission to accept this relationship', + 'No such trust relationship exists or it is not associated with the current wallet.', ); expect(getTrustRelationshipsRequestedToMeStub).calledOnceWithExactly( walletId, @@ -693,9 +693,9 @@ describe('Trust Model', () => { error = e; } - expect(error.code).eql(403); + expect(error.code).eql(404); expect(error.message).eql( - 'Have no permission to decline this relationship', + 'No such trust relationship exists or it is not associated with the current wallet.', ); expect(getTrustRelationshipsRequestedToMeStub).calledOnceWithExactly( walletId, @@ -740,6 +740,22 @@ describe('Trust Model', () => { trustRepositoryStub.getByFilter.resolves([]); const trustRelationshipId = uuid(); const walletId = uuid(); + + const filter = { + and: [ + { + or: [ + { actor_wallet_id: walletId }, + { target_wallet_id: walletId }, + { originator_wallet_id: walletId }, + ], + }, + { + 'wallet_trust.id': trustRelationshipId, + }, + ], + }; + let error; try { await trustModel.cancelTrustRequest({ @@ -750,13 +766,11 @@ describe('Trust Model', () => { error = e; } - expect(error.code).eql(403); + expect(error.code).eql(404); expect(error.message).eql( - 'Have no permission to cancel this relationship', + 'No such trust relationship exists or it is not associated with the current wallet.', ); - expect(trustRepositoryStub.getByFilter).calledOnceWithExactly({ - 'wallet_trust.id': trustRelationshipId, - }); + expect(trustRepositoryStub.getByFilter).calledOnceWithExactly(filter); expect(updateTrustStateStub).not.called; }); @@ -764,6 +778,21 @@ describe('Trust Model', () => { const trustRelationshipId = uuid(); const walletId = uuid(); + const filter = { + and: [ + { + or: [ + { actor_wallet_id: walletId }, + { target_wallet_id: walletId }, + { originator_wallet_id: walletId }, + ], + }, + { + 'wallet_trust.id': trustRelationshipId, + }, + ], + }; + trustRepositoryStub.getByFilter.resolves([ { originator_wallet_id: walletId, id: trustRelationshipId }, ]); @@ -775,9 +804,7 @@ describe('Trust Model', () => { expect(result).eql('state cancelled'); - expect(trustRepositoryStub.getByFilter).calledOnceWithExactly({ - 'wallet_trust.id': trustRelationshipId, - }); + expect(trustRepositoryStub.getByFilter).calledOnceWithExactly(filter); expect(updateTrustStateStub).calledOnceWithExactly( { originator_wallet_id: walletId, id: trustRelationshipId }, TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.cancelled_by_originator, diff --git a/server/services/TrustService.spec.js b/server/services/TrustService.spec.js index c616de4a..0cd664c0 100644 --- a/server/services/TrustService.spec.js +++ b/server/services/TrustService.spec.js @@ -23,6 +23,10 @@ describe('TrustService', () => { .stub(Trust.prototype, 'getTrustRelationships') .resolves(['trustRelationships']); + const getWalletStub = sinon + .stub(WalletService.prototype, 'getWallet') + .resolves({id: 'walletId'}) + const trustRelationship = await trustService.getTrustRelationships({ walletId: 'walletId', state: 'state', @@ -32,6 +36,9 @@ describe('TrustService', () => { }); expect(trustRelationship).eql(['trustRelationships']); + expect(getWalletStub.calledOnceWithExactly({ + walletId: 'walletId', + })) expect( getTrustRelationshipsStub.calledOnceWithExactly({ walletId: 'walletId', From 06df1554466f15409edf1ff991bfdcf6bd6a6060 Mon Sep 17 00:00:00 2001 From: Pranav Kakaraparti Date: Tue, 22 Aug 2023 21:28:50 -0500 Subject: [PATCH 5/5] fix: undo cancel trust request and fix tests --- server/models/Trust.js | 19 ++++++++++++++++++- server/models/Trust.spec.js | 38 ++++++------------------------------- 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/server/models/Trust.js b/server/models/Trust.js index e94f3d49..d6e98ad6 100644 --- a/server/models/Trust.js +++ b/server/models/Trust.js @@ -335,7 +335,24 @@ class Trust { * Cancel a trust relationship request */ async cancelTrustRequest({ trustRelationshipId, walletId }) { - const trustRelationship = await this.getTrustRelationshipById({ walletId, trustRelationshipId}) + const trustRelationships = await this._trustRepository.getByFilter({ + 'wallet_trust.id': trustRelationshipId, + }); + const [trustRelationship] = trustRelationships; + + if(!trustRelationship){ + throw new HttpError( + 404, + 'No such trust relationship exists or it is not associated with the current wallet.' + ) + } + + if (trustRelationship?.originator_wallet_id !== walletId) { + throw new HttpError( + 403, + 'Have no permission to cancel this relationship', + ); + } return this.updateTrustState( trustRelationship, diff --git a/server/models/Trust.spec.js b/server/models/Trust.spec.js index 1abd3a83..482a49c1 100644 --- a/server/models/Trust.spec.js +++ b/server/models/Trust.spec.js @@ -744,21 +744,6 @@ describe('Trust Model', () => { const trustRelationshipId = uuid(); const walletId = uuid(); - const filter = { - and: [ - { - or: [ - { actor_wallet_id: walletId }, - { target_wallet_id: walletId }, - { originator_wallet_id: walletId }, - ], - }, - { - 'wallet_trust.id': trustRelationshipId, - }, - ], - }; - let error; try { await trustModel.cancelTrustRequest({ @@ -773,7 +758,9 @@ describe('Trust Model', () => { expect(error.message).eql( 'No such trust relationship exists or it is not associated with the current wallet.', ); - expect(trustRepositoryStub.getByFilter).calledOnceWithExactly(filter); + expect(trustRepositoryStub.getByFilter).calledOnceWithExactly({ + 'wallet_trust.id': trustRelationshipId, + }); expect(updateTrustStateStub).not.called; }); @@ -781,21 +768,6 @@ describe('Trust Model', () => { const trustRelationshipId = uuid(); const walletId = uuid(); - const filter = { - and: [ - { - or: [ - { actor_wallet_id: walletId }, - { target_wallet_id: walletId }, - { originator_wallet_id: walletId }, - ], - }, - { - 'wallet_trust.id': trustRelationshipId, - }, - ], - }; - trustRepositoryStub.getByFilter.resolves([ { originator_wallet_id: walletId, id: trustRelationshipId }, ]); @@ -807,7 +779,9 @@ describe('Trust Model', () => { expect(result).eql('state cancelled'); - expect(trustRepositoryStub.getByFilter).calledOnceWithExactly(filter); + expect(trustRepositoryStub.getByFilter).calledOnceWithExactly({ + 'wallet_trust.id': trustRelationshipId, + }); expect(updateTrustStateStub).calledOnceWithExactly( { originator_wallet_id: walletId, id: trustRelationshipId }, TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.cancelled_by_originator,