Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/models/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ class Wallet{
if(
trustRelationship.actor_wallet_id === senderWallet.getId() &&
trustRelationship.target_wallet_id === receiveWallet.getId() &&
trustRelationship.request_type === TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.send
trustRelationship.request_type === trustType
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hasTrust method in wallet model was not actually referencing the trustType parameter sent in, so I changed it to be more flexible to reuse for checking other request types

){
return true;
}else{
Expand All @@ -434,7 +434,7 @@ class Wallet{
if(
trustRelationship.actor_wallet_id === receiveWallet.getId() &&
trustRelationship.target_wallet_id === senderWallet.getId() &&
trustRelationship.request_type === TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.receive
trustRelationship.request_type === trustType
){
return true;
}else{
Expand Down
4 changes: 2 additions & 2 deletions server/models/Wallet.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ describe("Wallet", () => {
target_wallet_id: wallet.getId(),
}]);
const result = await wallet.hasTrust(
TrustRelationship.ENTITY_TRUST_TYPE.send,
wallet,
TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.receive,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a result of my change on hasTrust method referencing the trust type parameter now, I also think this test should expect a "receive" request type (should not be sending an entity trust type). Since wallet2 (actor, receiver) is sending a receive request to wallet (target, sender).

wallet2,
wallet,
);
expect(result).eq(true);
fn1.restore();
Expand Down
24 changes: 23 additions & 1 deletion server/routes/trustRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const helper = require("./utils");
const Session = require("../models/Session");
const TrustRelationship = require("../models/TrustRelationship");
const Joi = require("joi");
const HttpError = require("../utils/HttpError");


trustRouter.get('/',
helper.apiKeyHandler,
Expand All @@ -17,6 +19,7 @@ trustRouter.get('/',
Joi.assert(
req.query,
Joi.object({
wallet: Joi.string(),
state: Joi.string(),
type: Joi.string(),
request_type: Joi.string(),
Expand All @@ -34,19 +37,38 @@ trustRouter.get('/',
const session = new Session();
const walletService = new WalletService(session);
const trustService = new TrustService(session);
const wallet = await walletService.getById(res.locals.wallet_id);
const loggedInWallet = await walletService.getById(res.locals.wallet_id);
let wallet;
if(req.query.wallet) {
// check to see if user passed in a wallet name or id (req queries are always default strings)
const queryWallet = await walletService.getByIdOrName(req.query.wallet);
let isManaged = await loggedInWallet.hasTrust(TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.manage, loggedInWallet, queryWallet);
let isYielded = await loggedInWallet.hasTrust(TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.yield, queryWallet, loggedInWallet);
// check if we have right permissions to access the query wallet or the same as logged in wallet
if(isManaged || isYielded || loggedInWallet._id === queryWallet._id) {
wallet = queryWallet;
}
else {
throw new HttpError(401, "Do not have permission to access this wallet");
}
}
// otherwise if no passed in wallet query, then will just look at logged in wallet
wallet = loggedInWallet;
// get all trust relationships of the logged in wallet (where logged in wallet is the actor/target/originator)
const trust_relationships = await wallet.getTrustRelationships(
req.query.state,
req.query.type,
req.query.request_type,
);
const subWallets = await wallet.getSubWallets();
// get all trust relationships of wallets managed by logged in wallet
for(const sw of subWallets){
const trustRelationships = await sw.getTrustRelationships(
req.query.state,
req.query.type,
req.query.request_type,
);
// avoid duplicates (where subwallet trust ID is the same as one of logged in wallet's trust ID)
for(tr of trustRelationships){
if(trust_relationships.every(e => e.id !== tr.id)){
trust_relationships.push(tr);
Expand Down
16 changes: 16 additions & 0 deletions server/routes/trustRouter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ describe("trustRouter", () => {
TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.send
)
});

it("wallet param filter working for own logged in wallet", async () => {
const wallet = new Wallet(uuid.v4())
const wallet2 = new Wallet(uuid.v4())
const fn = sinon.stub(Wallet.prototype, "getTrustRelationshipsTrusted").resolves({
request_type: TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.manage,
type: TrustRelationship.ENTITY_TRUST_TYPE.manage,
actor_wallet_id: wallet.getId(),
target_wallet_id: wallet2.getId(),
})
const res = await request(app)
.get(`/?wallet=${wallet.getId()}`)
expect(res).property("statusCode").eq(200);
expect(res.body.trust_relationships).lengthOf(1);
expect(fn).calledWith(wallet.getId());
});

//TODO
it.skip("wrong state string should throw 422", () => {
Expand Down
5 changes: 4 additions & 1 deletion server/routes/walletRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ walletRouter.get('/',
for (const wallet of subWallets) {
const json = await wallet.toJSON();
json.tokens_in_wallet = await tokenService.countTokenByWallet(wallet);
// Hide unnecessary fields
delete json.password;
delete json.salt;
walletsJson.push(json);
}

Expand All @@ -48,7 +51,7 @@ walletRouter.get('/',
})
);

// TO DO: Add below route to yaml
// Don't need this route anymore?

walletRouter.get('/:wallet_id/trust_relationships',
helper.apiKeyHandler,
Expand Down