diff --git a/server/models/Trust.js b/server/models/Trust.js index 632573e4..f895e573 100644 --- a/server/models/Trust.js +++ b/server/models/Trust.js @@ -472,7 +472,7 @@ class Trust { if (!trustRelationship) { throw new HttpError( 404, - 'No such trust relationship exists or it is not associated with the current wallet.', + `Cannot find trust relationship by id: ${trustRelationshipId}`, ); } @@ -539,30 +539,39 @@ class Trust { } async getTrustRelationshipById({ walletId, trustRelationshipId }) { - const filter = { - and: [ - { - or: [ - { actor_wallet_id: walletId }, - { target_wallet_id: walletId }, - { originator_wallet_id: walletId }, - ], - }, - { - 'wallet_trust.id': trustRelationshipId, - }, - ], - }; - - const [trustRelationship] = await this._trustRepository.getByFilter(filter); + const trustRelationship = await this._trustRepository.getById( + trustRelationshipId, + ); if (!trustRelationship) { throw new HttpError( 404, - 'No such trust relationship exists or it is not associated with the current wallet.', + `Cannot find trust relationship by id: ${trustRelationshipId}`, ); } + const walletModel = new Wallet(this._session); + const hasControlOverActor = await walletModel.hasControlOver( + walletId, + trustRelationship.actor_wallet_id, + ); + const hasControlOverTarget = await walletModel.hasControlOver( + walletId, + trustRelationship.target_wallet_id, + ); + const hasControlOverOriginator = await walletModel.hasControlOver( + walletId, + trustRelationship.originator_wallet_id, + ); + + if ( + !hasControlOverActor && + !hasControlOverTarget && + !hasControlOverOriginator + ) { + throw new HttpError(403, 'Have no permission to get this relationship'); + } + return trustRelationship; } diff --git a/server/models/Trust.spec.js b/server/models/Trust.spec.js index 9b804e6f..ede53c45 100644 --- a/server/models/Trust.spec.js +++ b/server/models/Trust.spec.js @@ -27,19 +27,19 @@ describe('Trust Model', () => { describe('getTrustRelationships', () => { const walletId = uuid(); - const managedWallets = [{id: '90f8b2ab-c101-405d-922a-0a64dbe64ab6'}]; - const managedWalletIds = managedWallets.map(wallet => wallet.id); - const orConditions = [ - { actor_wallet_id: walletId }, - { target_wallet_id: walletId }, - { originator_wallet_id: walletId }, - ]; - - managedWalletIds.forEach((managedWalletId) => { - orConditions.push({ actor_wallet_id: managedWalletId }); - orConditions.push({ target_wallet_id: managedWalletId }); - orConditions.push({ originator_wallet_id: managedWalletId }); - }); + const managedWallets = [{ id: '90f8b2ab-c101-405d-922a-0a64dbe64ab6' }]; + const managedWalletIds = managedWallets.map((wallet) => wallet.id); + const orConditions = [ + { actor_wallet_id: walletId }, + { target_wallet_id: walletId }, + { originator_wallet_id: walletId }, + ]; + + managedWalletIds.forEach((managedWalletId) => { + orConditions.push({ actor_wallet_id: managedWalletId }); + orConditions.push({ target_wallet_id: managedWalletId }); + orConditions.push({ originator_wallet_id: managedWalletId }); + }); const filter = { and: [ { @@ -50,7 +50,7 @@ describe('Trust Model', () => { it('should get relationships', async () => { trustRepositoryStub.getByFilter.resolves(['relationship1']); - + const result = await trustModel.getTrustRelationships({ managedWallets, walletId, @@ -63,7 +63,7 @@ describe('Trust Model', () => { limit: 10, offset: 1, order: undefined, - sort_by: undefined + sort_by: undefined, }); }); @@ -85,7 +85,7 @@ describe('Trust Model', () => { limit: 10, offset: 1, order: undefined, - sort_by: undefined + sort_by: undefined, }, ); }); @@ -109,7 +109,7 @@ describe('Trust Model', () => { limit: 10, offset: 11, order: undefined, - sort_by: undefined + sort_by: undefined, }, ); }); @@ -133,7 +133,7 @@ describe('Trust Model', () => { limit: 101, offset: 1, order: undefined, - sort_by: undefined + sort_by: undefined, }, ); }); @@ -161,7 +161,7 @@ describe('Trust Model', () => { limit: 100, offset: 0, order: undefined, - sort_by: undefined + sort_by: undefined, }, ); }); @@ -739,13 +739,14 @@ describe('Trust Model', () => { it('updateTrustState', async () => { trustRepositoryStub.update.resolves({ status: 'updated' }); - const now = new Date(); - const formattedDate = `${(now.getMonth() + 1).toString().padStart(2, '0')}/${now + const now = new Date(); + const formattedDate = `${(now.getMonth() + 1) + .toString() + .padStart(2, '0')}/${now .getDate() .toString() .padStart(2, '0')}/${now.getFullYear()}`; - const result = await trustModel.updateTrustState( { id: 'trustId', @@ -767,7 +768,7 @@ describe('Trust Model', () => { expect(trustRepositoryStub.update).calledOnceWithExactly({ id: 'trustId', state: 'new state', - updated_at: formattedDate + updated_at: formattedDate, }); }); @@ -910,7 +911,7 @@ describe('Trust Model', () => { }); it('should error out -- no permission to accept', async () => { - trustRepositoryStub.getByFilter.resolves({count: 0, result: []}); + trustRepositoryStub.getByFilter.resolves({ count: 0, result: [] }); const trustRelationshipId = uuid(); const walletId = uuid(); @@ -926,7 +927,7 @@ describe('Trust Model', () => { expect(error.code).eql(404); expect(error.message).eql( - 'No such trust relationship exists or it is not associated with the current wallet.', + `Cannot find trust relationship by id: ${trustRelationshipId}`, ); expect(trustRepositoryStub.getByFilter).calledOnceWithExactly({ 'wallet_trust.id': trustRelationshipId, @@ -938,9 +939,10 @@ describe('Trust Model', () => { const trustRelationshipId = uuid(); const walletId = uuid(); - trustRepositoryStub.getByFilter.resolves({count: 1, result:[ - { originator_wallet_id: walletId, id: trustRelationshipId }, - ]}); + trustRepositoryStub.getByFilter.resolves({ + count: 1, + result: [{ originator_wallet_id: walletId, id: trustRelationshipId }], + }); updateTrustStateStub.resolves('state cancelled'); const result = await trustModel.cancelTrustRequest({ trustRelationshipId, @@ -1084,31 +1086,29 @@ describe('Trust Model', () => { describe('getTrustRelationshipById', () => { const walletId = uuid(); const trustRelationshipId = uuid(); - const filter = { - and: [ - { - or: [ - { actor_wallet_id: walletId }, - { target_wallet_id: walletId }, - { originator_wallet_id: walletId }, - ], - }, - { - 'wallet_trust.id': trustRelationshipId, - }, - ], - }; + const hasControlStub = sinon.stub(Wallet.prototype, 'hasControlOver'); it('should get relationship', async () => { - trustRepositoryStub.getByFilter.resolves(['trustRelationship']); + trustRepositoryStub.getById.resolves({ + id: trustRelationshipId, + actor_wallet_id: walletId, + target_wallet_id: walletId, + originator_wallet_id: walletId, + }); + hasControlStub.resolves(true); const result = await trustModel.getTrustRelationshipById({ walletId, trustRelationshipId, }); - expect(result).eql('trustRelationship'); - expect(trustRepositoryStub.getByFilter).calledOnceWithExactly({ - ...filter, + expect(result).eql({ + id: trustRelationshipId, + actor_wallet_id: walletId, + target_wallet_id: walletId, + originator_wallet_id: walletId, }); + expect(trustRepositoryStub.getById).calledOnceWithExactly( + trustRelationshipId, + ); }); }); }); diff --git a/server/repositories/TrustRepository.js b/server/repositories/TrustRepository.js index 24614392..54e9cb44 100644 --- a/server/repositories/TrustRepository.js +++ b/server/repositories/TrustRepository.js @@ -7,6 +7,39 @@ class TrustRepository extends BaseRepository { this._session = session; } + async getById(id) { + const object = await this._session + .getDB() + .select( + 'wallet_trust.*', + 'originator_wallet.name as originating_wallet', + 'actor_wallet.name as actor_wallet', + 'target_wallet.name as target_wallet', + ) + .table(this._tableName) + .leftJoin( + 'wallet as originator_wallet', + 'wallet_trust.originator_wallet_id', + '=', + 'originator_wallet.id', + ) + .leftJoin( + 'wallet as actor_wallet', + 'wallet_trust.actor_wallet_id', + '=', + 'actor_wallet.id', + ) + .leftJoin( + 'wallet as target_wallet', + 'wallet_trust.target_wallet_id', + '=', + 'target_wallet.id', + ) + .where('wallet_trust.id', id) + .first(); + return object; + } + async getByOriginatorId(id) { const list = await this._session .getDB() @@ -89,7 +122,6 @@ class TrustRepository extends BaseRepository { if (limitOptions.sort_by) { column = limitOptions.sort_by; } - if (limitOptions.limit) { promise = promise.limit(limitOptions.limit); }