Skip to content

Commit

Permalink
Sharing a request with yourself shows a misleading error message (#280)
Browse files Browse the repository at this point in the history
* feat: improve error message when sharing a request with yourself

* chore: pr changes

* chore: pr changes

* chore: fix prettier

* test: add test for new error item

* chore: correct error code

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
sebbi08 and mergify[bot] authored Sep 23, 2024
1 parent 390ea01 commit 3ad1fa5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/consumption/src/consumption/ConsumptionCoreErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ class Requests {
return new ApplicationError("error.consumption.requests.validation.inheritedFromItem", message);
}

public cannotShareRequestWithYourself() {
return new CoreError("error.consumption.requests.cannotShareRequestWithYourself", "You cannot share a Request with yourself.");
}

private static readonly _decideValidation = class {
public invalidNumberOfItems(message: string) {
return new ApplicationError("error.consumption.requests.decide.validation.invalidNumberOfItems", message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export class OutgoingRequestsController extends ConsumptionBaseController {

public async canCreate(params: ICanCreateOutgoingRequestParameters): Promise<ValidationResult> {
const parsedParams = CanCreateOutgoingRequestParameters.from(params);

if (parsedParams.peer?.equals(this.identity.address)) {
return ValidationResult.error(ConsumptionCoreErrors.requests.cannotShareRequestWithYourself());
}

if (parsedParams.peer) {
const relationship = await this.relationshipResolver.getRelationshipToIdentity(parsedParams.peer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,32 @@ describe("OutgoingRequestsController", function () {
expect(validationResult.items[1].items).toHaveLength(1);
expect(validationResult.items[1].items[0].isError()).toBe(true);
});

test("returns a validation result that contains an error for requests to myself", async function () {
const validationResult = await When.iCallCanCreateForAnOutgoingRequest({
content: {
items: [
TestRequestItem.from({
mustBeAccepted: false
}),
RequestItemGroup.from({
items: [
TestRequestItem.from({
mustBeAccepted: false,
shouldFailAtCanCreateOutgoingRequestItem: true
})
]
})
]
},
peer: context.currentIdentity.address
});

expect(validationResult).errorValidationResult({
code: "error.consumption.requests.cannotShareRequestWithYourself",
message: "You cannot share a Request with yourself."
});
});
});

describe("Create (on active relationship)", function () {
Expand Down

0 comments on commit 3ad1fa5

Please sign in to comment.