Skip to content

Commit

Permalink
feat: added check for duplicate trust request
Browse files Browse the repository at this point in the history
  • Loading branch information
dadiorchen committed Dec 10, 2020
1 parent f8c4c8a commit e36d226
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 32 deletions.
64 changes: 47 additions & 17 deletions server/models/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,21 +204,6 @@ class Wallet{
// }


//check if I (current wallet) can add a new trust like this
const trustRelationships = await this.getTrustRelationships();
if(trustRelationships.some(trustRelationship => {
expect(trustRelationship).property("type").defined();
expect(trustRelationship).property("target_entity_id").number();
return (
trustRelationship.type === TrustRelationship.ENTITY_TRUST_TYPE.send &&
trustRelationship.request_type === requestType &&
trustRelationship.target_entity_id === targetWallet.getId() &&
trustRelationship.originator_entity_id === this._id &&
trustRelationship.actor_entity_id === actorWallet._id
)
})){
throw new HttpError(403, "The trust requested has existed");
}

//check if the orginator can control the actor
const hasControl = await this.hasControlOver(requesterWallet);
Expand All @@ -230,16 +215,61 @@ class Wallet{
await requesteeWallet.checkTrustRequestSentToMe(requestType, this.id);

//create this request
const result = await this.trustRepository.create({
const trustRelationship = {
type: TrustRelationship.getTrustTypeByRequestType(requestType),
request_type: requestType,
actor_entity_id: actorWallet.getId(),
originator_entity_id: this._id,
target_entity_id: targetWallet.getId(),
state: TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested,
});
}
await this.checkDuplicateRequest(trustRelationship);
const result = await this.trustRepository.create(trustRelationship);
return result;
}

//check if I (current wallet) can add a new trust like this
async checkDuplicateRequest(trustRelationship){
const trustRelationships = await this.getTrustRelationships();
if(
trustRelationship.type === TrustRelationship.ENTITY_TRUST_TYPE.send ||
trustRelationship.type === TrustRelationship.ENTITY_TRUST_TYPE.manage
){
if(
trustRelationships.some(e => {
if(
(
e.request_type === trustRelationship.request_type &&
(
e.state === TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested ||
e.state === TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted
) &&
e.actor_entity_id === trustRelationship.actor_entity_id &&
e.target_entity_id === trustRelationship.target_entity_id
) || (
e.request_type !== trustRelationship.request_type &&
(
e.state === TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested ||
e.state === TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted
) &&
e.actor_entity_id === trustRelationship.target_entity_id &&
e.target_entity_id === trustRelationship.actor_entity_id
)
){
return true;
}else{
return false;
}
})
){
log.debug("Has duplicated trust");
throw new HttpError(403, "The trust relationship has been requested or trusted");
}
}else{
throw HttpError(500, "Not supported type");
}
log.debug("Has no duplicated trust");
}

/*
* Check if a request sent to me is acceptable.
Expand Down
94 changes: 79 additions & 15 deletions server/models/Wallet.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,11 @@ describe("Wallet", () => {
}).rejects.toThrow("type");
});

it("request with trust which has existed should throw 403", async () => {
const wallet2 = new Wallet(2);
const fn = sinon.stub(Wallet.prototype, "getTrustRelationships").resolves([{
type: TrustRelationship.ENTITY_TRUST_TYPE.send,
actor_entity_id: wallet.getId(),
target_entity_id: wallet2.getId(),
request_type: TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.send,
originator_entity_id: wallet.getId(),
}]);
await jestExpect(async () => {
await wallet.requestTrustFromAWallet("send",wallet, wallet2);
}).rejects.toThrow(/existed/i);
fn.restore();
});

it("request successfully", async () => {
const wallet2 = new Wallet(2);
const fn2 = sinon.stub(TrustRepository.prototype, "get").resolves([]);
const fn3 = sinon.stub(TrustRepository.prototype, "create");
sinon.stub(Wallet.prototype, "checkDuplicateRequest");
await wallet.requestTrustFromAWallet(
TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.send,
wallet,
Expand Down Expand Up @@ -1113,4 +1099,82 @@ describe("Wallet", () => {
});
});

describe("checkDuplicateRequest", () => {

it("A send B trust has been requested, now request again, should throw error", async () => {
const walletA = new Wallet(1);
const walletB = new Wallet(2);
const trustRelationshipRequested = {
id: 1,
type: TrustRelationship.ENTITY_TRUST_TYPE.send,
request_type: TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.send,
actor_entity_id: walletA.getId(),
target_entity_id: walletB.getId(),
state: TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested,
}
const trustRelationshipRequesting = {
id: 1,
type: TrustRelationship.ENTITY_TRUST_TYPE.send,
request_type: TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.send,
actor_entity_id: walletA.getId(),
target_entity_id: walletB.getId(),
state: TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested,
}
sinon.stub(Wallet.prototype, "getTrustRelationships").resolves([trustRelationshipRequested]);
await jestExpect(async () => {
await wallet.checkDuplicateRequest(trustRelationshipRequesting);
}).rejects.toThrow(/has been/);
});

it("A send B trust has been trusted, now request B receive A, should throw error", async () => {
const walletA = new Wallet(1);
const walletB = new Wallet(2);
const trustRelationshipRequested = {
id: 1,
type: TrustRelationship.ENTITY_TRUST_TYPE.send,
request_type: TrustRelationship.ENTITY_TRUST_TYPE.send,
actor_entity_id: walletA.getId(),
target_entity_id: walletB.getId(),
state: TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted,
}
const trustRelationshipRequesting = {
id: 1,
type: TrustRelationship.ENTITY_TRUST_TYPE.send,
request_type: TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.receive,
actor_entity_id: walletB.getId(),
target_entity_id: walletA.getId(),
state: TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested,
}
sinon.stub(Wallet.prototype, "getTrustRelationships").resolves([trustRelationshipRequested]);
await jestExpect(async () => {
await wallet.checkDuplicateRequest(trustRelationshipRequesting);
}).rejects.toThrow(/has been/);
});

it("A manage B trust has been requested, now request B yield A, should throw error", async () => {
const walletA = new Wallet(1);
const walletB = new Wallet(2);
const trustRelationshipRequested = {
id: 1,
type: TrustRelationship.ENTITY_TRUST_TYPE.manage,
request_type: TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.manage,
actor_entity_id: walletA.getId(),
target_entity_id: walletB.getId(),
state: TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested,
}
const trustRelationshipRequesting = {
id: 1,
type: TrustRelationship.ENTITY_TRUST_TYPE.manage,
request_type: TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.yield,
actor_entity_id: walletB.getId(),
target_entity_id: walletA.getId(),
state: TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested,
}
sinon.stub(Wallet.prototype, "getTrustRelationships").resolves([trustRelationshipRequested]);
await jestExpect(async () => {
await wallet.checkDuplicateRequest(trustRelationshipRequesting);
}).rejects.toThrow(/has been/);
});
});

});

0 comments on commit e36d226

Please sign in to comment.