Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sharing a request with yourself shows a misleading error message #280

Merged
Show file tree
Hide file tree
Changes from all 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: 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