diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationController.java index b7ae6e4bd..0d7f715f3 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationController.java @@ -127,7 +127,8 @@ public class OneTimeDonationController { @Min(1) public long amount; - @Schema(description = "The level for the boost payment", defaultValue = "1", requiredMode = Schema.RequiredMode.REQUIRED) + @Schema(description = "The level for the boost payment", defaultValue = "1", minimum = "1", requiredMode = Schema.RequiredMode.REQUIRED) + @Min(1) public long level = 1; @Schema(description = "The payment method", defaultValue = "CARD") @@ -177,14 +178,20 @@ public class OneTimeDonationController { } /** - * Validates that the currency is supported by the {@code manager} and {@code request.paymentMethod} and that the - * amount meets minimum and maximum constraints. + * Validates that the request level is valid, the currency is supported by the {@code manager} and {@code request.paymentMethod}, + * and that the amount meets minimum and maximum constraints. * * @throws BadRequestException indicates validation failed. Inspect {@code response.error} for details */ private void validateRequestCurrencyAmount(final CreateBoostRequest request, final BigDecimal amount, final CustomerAwareSubscriptionPaymentProcessor manager) { + if (!(request.level == oneTimeDonationConfiguration.gift().level() + || request.level == oneTimeDonationConfiguration.boost().level())) { + throw new BadRequestException( + Response.status(Response.Status.BAD_REQUEST).entity(Map.of("error", "invalid_level")).build()); + } + if (!manager.getSupportedCurrenciesForPaymentMethod(request.paymentMethod) .contains(request.currency.toLowerCase(Locale.ROOT))) { throw new BadRequestException(Response.status(Response.Status.BAD_REQUEST) diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationControllerTest.java index 9549c5527..41230828a 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationControllerTest.java @@ -104,8 +104,7 @@ class OneTimeDonationControllerTest extends AbstractV1SubscriptionControllerTest .post(Entity.json(""" { "currency": "USD", - "amount": 249, - "level": null + "amount": 249 } """)); assertThat(response.getStatus()).isEqualTo(400); @@ -125,7 +124,6 @@ class OneTimeDonationControllerTest extends AbstractV1SubscriptionControllerTest { "currency": "EUR", "amount": 1000001, - "level": null, "paymentMethod": "SEPA_DEBIT" } """)); @@ -147,7 +145,6 @@ class OneTimeDonationControllerTest extends AbstractV1SubscriptionControllerTest { "currency": "USD", "amount": 3000, - "level": null, "paymentMethod": "SEPA_DEBIT" } """)); @@ -170,7 +167,10 @@ class OneTimeDonationControllerTest extends AbstractV1SubscriptionControllerTest final Response response = RESOURCE_EXTENSION.target("/v1/subscription/boost/create") .request() - .post(Entity.json("{\"currency\": \"USD\", \"amount\": 300, \"level\": null}")); + .post(Entity.json(""" + {"currency": "USD", "amount": 300} + """ + )); assertThat(response.getStatus()).isEqualTo(200); }