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