From da2de341c338473d3ab185857ff1349fe511f960 Mon Sep 17 00:00:00 2001 From: easternkite Date: Thu, 20 Feb 2025 20:02:09 +0900 Subject: [PATCH] =?UTF-8?q?fortune=20->=20=EC=83=9D=EB=85=84=EC=9B=94?= =?UTF-8?q?=EC=9D=BC=20=EC=9C=A0=ED=9A=A8=EC=84=B1=20=EC=B2=B4=ED=81=AC=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80,=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../remain/features/fortune/FortuneRoute.kt | 43 +++++++++++------ .../features/fortune/FortuneRouteKtTest.kt | 48 +++++++++++++++++++ 2 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 src/test/kotlin/com/easternkite/remain/features/fortune/FortuneRouteKtTest.kt diff --git a/src/main/kotlin/com/easternkite/remain/features/fortune/FortuneRoute.kt b/src/main/kotlin/com/easternkite/remain/features/fortune/FortuneRoute.kt index 1628f8e..8fd6c1d 100644 --- a/src/main/kotlin/com/easternkite/remain/features/fortune/FortuneRoute.kt +++ b/src/main/kotlin/com/easternkite/remain/features/fortune/FortuneRoute.kt @@ -61,26 +61,25 @@ fun Application.fortuneRoute() { } ) ) - val birth = body - .text - .takeIf { it.length == 6 } - ?.takeIf { it.matches("[0-9]+".toRegex()) } - ?: run { - val errorResponse = DrResponse( - text = "${HttpStatusCode.BadRequest} - 생년월일은 숫자로 이루어진 6자리여야 합니다.", - responseType = "ephemeral" // 작성자한테만 표시 - ) + val birth = body.text + if (!validateBirth(birth)) { + val errorResponse = DrResponse( + text = "${HttpStatusCode.BadRequest} - 생년월일은 숫자로 이루어진 6자리여야 합니다.", + responseType = "ephemeral" // 작성자한테만 표시 + ) + + call.respondText( + text = Json.encodeToString(errorResponse), + contentType = ContentType.Application.Json + ) + return@post + } - call.respondText( - text = Json.encodeToString(errorResponse), - contentType = ContentType.Application.Json - ) - return@post - } val userTag = DoorayTag.create( tenantId = body.tenantId, userId = body.userId ) + val response = chat.sendMessage("생년월일 : ${birth}, 기준 날짜 : $currentTimeFormatted") val fortuneJson = response.text?.let { text -> println(text) @@ -126,6 +125,20 @@ fun Application.fortuneRoute() { } } +fun validateBirth(birth: String): Boolean { + if (birth.length != 6) return false + + val (_, month, day) = runCatching { + birth.chunked(2) { it.toString().toInt() } + }.getOrNull() ?: return false + + val isPossibleMonth = month in 1..12 + val isPossibleDay = day in 1 .. 31 + + return isPossibleMonth && isPossibleDay +} + + data class FortuneField( val title: String, val section: Section, diff --git a/src/test/kotlin/com/easternkite/remain/features/fortune/FortuneRouteKtTest.kt b/src/test/kotlin/com/easternkite/remain/features/fortune/FortuneRouteKtTest.kt new file mode 100644 index 0000000..0d9164b --- /dev/null +++ b/src/test/kotlin/com/easternkite/remain/features/fortune/FortuneRouteKtTest.kt @@ -0,0 +1,48 @@ +package com.easternkite.remain.features.fortune + +import org.junit.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class ValidateBirthTest { + + @Test + fun `생년월일이 6자 이상일 때 false`() { + assertFalse(validateBirth("9709088")) + } + + @Test + fun `생년월일중, 태어난 달이 12월을 초과할 때 false`() { + assertFalse(validateBirth("971308")) + } + + @Test + fun `생년월일중, 태어난 일이 31일을 초과할 때 false`() { + assertFalse(validateBirth("970132")) + } + + @Test + fun `태어난 월, 일이 0일 때 false`() { + assertFalse(validateBirth("000000")) + } + + @Test + fun `태어난 월, 일이 99일 때 false`() { + assertFalse(validateBirth("999999")) + } + + @Test + fun `99년 12월 31일 경계값 true`() { + assertTrue(validateBirth("991231")) + } + + @Test + fun `00년 01월 01일 경계값 true`() { + assertTrue(validateBirth("000101")) + } + + @Test + fun `한글, 특수문자 들어가 있는 경우 false`() { + assertFalse(validateBirth("__--얍얍")) + } +} \ No newline at end of file