Skip to content

Commit

Permalink
Merge pull request #408 from pranavkparti/404-fix
Browse files Browse the repository at this point in the history
407 Add 404 errors for some endpoints
  • Loading branch information
Kpoke authored Aug 24, 2023
2 parents 62ec4a0 + 06df155 commit 96eee38
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
16 changes: 12 additions & 4 deletions server/models/Trust.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,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 });
Expand All @@ -320,8 +320,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.',
);
}

Expand All @@ -339,6 +339,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,
Expand Down
13 changes: 7 additions & 6 deletions server/models/Trust.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,9 +629,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,
Expand Down Expand Up @@ -696,9 +696,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,
Expand Down Expand Up @@ -743,6 +743,7 @@ describe('Trust Model', () => {
trustRepositoryStub.getByFilter.resolves([]);
const trustRelationshipId = uuid();
const walletId = uuid();

let error;
try {
await trustModel.cancelTrustRequest({
Expand All @@ -753,9 +754,9 @@ 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,
Expand Down
5 changes: 5 additions & 0 deletions server/services/TrustService.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class TrustService {
offset = 0,
limit,
}) {
// check if 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,
Expand Down
7 changes: 7 additions & 0 deletions server/services/TrustService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -32,6 +36,9 @@ describe('TrustService', () => {
});

expect(trustRelationship).eql(['trustRelationships']);
expect(getWalletStub.calledOnceWithExactly({
walletId: 'walletId',
}))
expect(
getTrustRelationshipsStub.calledOnceWithExactly({
walletId: 'walletId',
Expand Down

0 comments on commit 96eee38

Please sign in to comment.