From 4f95265872599b3c5967e4ce0c12c514cafbf585 Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Sat, 13 Sep 2025 19:56:25 +0200 Subject: [PATCH] infinity money bug :D This is here to show a small miss with big problems. - add missing test to - TransactionEndpoints - TransactionControllerTest - update Transfer with validation for sender receiver - add SenderReceiverValidator - add SenderReceiver --- .../ltd/lulz/annotation/SenderReceiver.kt | 15 +++++++++++++++ .../validator/SenderReceiverValidator.kt | 14 ++++++++++++++ src/main/kotlin/ltd/lulz/model/Transfer.kt | 2 ++ .../kotlin/ltd/lulz/TransactionEndpoints.kt | 19 +++++++++++++++++++ .../controller/TransactionControllerTest.kt | 18 ++++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 src/main/kotlin/ltd/lulz/annotation/SenderReceiver.kt create mode 100644 src/main/kotlin/ltd/lulz/annotation/validator/SenderReceiverValidator.kt diff --git a/src/main/kotlin/ltd/lulz/annotation/SenderReceiver.kt b/src/main/kotlin/ltd/lulz/annotation/SenderReceiver.kt new file mode 100644 index 0000000..d608370 --- /dev/null +++ b/src/main/kotlin/ltd/lulz/annotation/SenderReceiver.kt @@ -0,0 +1,15 @@ +package ltd.lulz.annotation + +import jakarta.validation.Constraint +import jakarta.validation.Payload +import kotlin.reflect.KClass +import ltd.lulz.annotation.validator.SenderReceiverValidator + +@Constraint(validatedBy = [SenderReceiverValidator::class]) +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +annotation class SenderReceiver( + val message: String = "Receiver and Sender cant be the same account", + val groups: Array> = [], + val payload: Array> = [], +) diff --git a/src/main/kotlin/ltd/lulz/annotation/validator/SenderReceiverValidator.kt b/src/main/kotlin/ltd/lulz/annotation/validator/SenderReceiverValidator.kt new file mode 100644 index 0000000..273f06f --- /dev/null +++ b/src/main/kotlin/ltd/lulz/annotation/validator/SenderReceiverValidator.kt @@ -0,0 +1,14 @@ +package ltd.lulz.annotation.validator + +import jakarta.validation.ConstraintValidator +import jakarta.validation.ConstraintValidatorContext +import ltd.lulz.annotation.SenderReceiver +import ltd.lulz.model.Transfer.Request + +class SenderReceiverValidator : ConstraintValidator { + + override fun isValid( + request: Request, + context: ConstraintValidatorContext, + ): Boolean = request.receiver != request.account +} diff --git a/src/main/kotlin/ltd/lulz/model/Transfer.kt b/src/main/kotlin/ltd/lulz/model/Transfer.kt index 26b206b..2d6a73f 100644 --- a/src/main/kotlin/ltd/lulz/model/Transfer.kt +++ b/src/main/kotlin/ltd/lulz/model/Transfer.kt @@ -3,9 +3,11 @@ package ltd.lulz.model import jakarta.validation.constraints.DecimalMin import java.math.BigDecimal import java.util.UUID +import ltd.lulz.annotation.SenderReceiver object Transfer { + @SenderReceiver data class Request( val account: UUID, val receiver: UUID, diff --git a/src/test-integration/kotlin/ltd/lulz/TransactionEndpoints.kt b/src/test-integration/kotlin/ltd/lulz/TransactionEndpoints.kt index e60dc55..0c6a9bb 100644 --- a/src/test-integration/kotlin/ltd/lulz/TransactionEndpoints.kt +++ b/src/test-integration/kotlin/ltd/lulz/TransactionEndpoints.kt @@ -195,6 +195,25 @@ class TransactionEndpoints { result.expectStatus().isEqualTo(CREATED) } + @Test + fun `deposit - fail same account`() { + // given + val request = Transfer.Request( + account = UUID.fromString("00000000-0000-7000-0000-000000000001"), + receiver = UUID.fromString("00000000-0000-7000-0000-000000000001"), + amount = BigDecimal.valueOf(1.00), + ) + + // when + val result = webClient.post() + .uri("/transfer") + .bodyValue(request) + .exchange() + + // then + result.expectStatus().isEqualTo(BAD_REQUEST) + } + @Test fun `deposit - fail amount to small`() { // given diff --git a/src/test/kotlin/ltd/lulz/controller/TransactionControllerTest.kt b/src/test/kotlin/ltd/lulz/controller/TransactionControllerTest.kt index b2762cd..44f0df9 100644 --- a/src/test/kotlin/ltd/lulz/controller/TransactionControllerTest.kt +++ b/src/test/kotlin/ltd/lulz/controller/TransactionControllerTest.kt @@ -215,6 +215,24 @@ class TransactionControllerTest { result.expectStatus().isCreated } + @Test + fun `transfer fail same accounts`() { + // given + val request = Transfer.Request(uuid, uuid, amount) + + every { transactionService.transfer(any(), any(), any()) } returns Mono.empty() + + // when + val result = webTestClient.post() + .uri("/transfer") + .contentType(APPLICATION_JSON) + .bodyValue(request) + .exchange() + + // then + result.expectStatus().isBadRequest + } + @Test fun `transfer fail amount to small`() { // given