Skip to content

Commit

Permalink
Merge pull request #494 from pranavkparti/get-trust-by-id-fix
Browse files Browse the repository at this point in the history
fix: GET /trust_relationships/{id} works correctly
  • Loading branch information
Kpoke authored Dec 11, 2024
2 parents bf235b0 + e561d42 commit d5433f1
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 65 deletions.
45 changes: 27 additions & 18 deletions server/models/Trust.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
);
}

Expand Down Expand Up @@ -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;
}

Expand Down
92 changes: 46 additions & 46 deletions server/models/Trust.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand All @@ -50,7 +50,7 @@ describe('Trust Model', () => {

it('should get relationships', async () => {
trustRepositoryStub.getByFilter.resolves(['relationship1']);

const result = await trustModel.getTrustRelationships({
managedWallets,
walletId,
Expand All @@ -63,7 +63,7 @@ describe('Trust Model', () => {
limit: 10,
offset: 1,
order: undefined,
sort_by: undefined
sort_by: undefined,
});
});

Expand All @@ -85,7 +85,7 @@ describe('Trust Model', () => {
limit: 10,
offset: 1,
order: undefined,
sort_by: undefined
sort_by: undefined,
},
);
});
Expand All @@ -109,7 +109,7 @@ describe('Trust Model', () => {
limit: 10,
offset: 11,
order: undefined,
sort_by: undefined
sort_by: undefined,
},
);
});
Expand All @@ -133,7 +133,7 @@ describe('Trust Model', () => {
limit: 101,
offset: 1,
order: undefined,
sort_by: undefined
sort_by: undefined,
},
);
});
Expand Down Expand Up @@ -161,7 +161,7 @@ describe('Trust Model', () => {
limit: 100,
offset: 0,
order: undefined,
sort_by: undefined
sort_by: undefined,
},
);
});
Expand Down Expand Up @@ -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',
Expand All @@ -767,7 +768,7 @@ describe('Trust Model', () => {
expect(trustRepositoryStub.update).calledOnceWithExactly({
id: 'trustId',
state: 'new state',
updated_at: formattedDate
updated_at: formattedDate,
});
});

Expand Down Expand Up @@ -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();

Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
);
});
});
});
34 changes: 33 additions & 1 deletion server/repositories/TrustRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -89,7 +122,6 @@ class TrustRepository extends BaseRepository {
if (limitOptions.sort_by) {
column = limitOptions.sort_by;
}

if (limitOptions.limit) {
promise = promise.limit(limitOptions.limit);
}
Expand Down

0 comments on commit d5433f1

Please sign in to comment.