diff --git a/simtong-application/src/main/kotlin/team/comit/simtong/domain/auth/usecase/CheckAuthCodeUseCase.kt b/simtong-application/src/main/kotlin/team/comit/simtong/domain/auth/usecase/CheckAuthCodeUseCase.kt index 3ba8a6df..fd0a4d6c 100644 --- a/simtong-application/src/main/kotlin/team/comit/simtong/domain/auth/usecase/CheckAuthCodeUseCase.kt +++ b/simtong-application/src/main/kotlin/team/comit/simtong/domain/auth/usecase/CheckAuthCodeUseCase.kt @@ -1,7 +1,9 @@ package team.comit.simtong.domain.auth.usecase -import team.comit.simtong.domain.auth.policy.CheckAuthCodePolicy +import team.comit.simtong.domain.auth.exception.AuthCodeMismatchException +import team.comit.simtong.domain.auth.model.AuthCodeLimit import team.comit.simtong.domain.auth.spi.CommandAuthCodeLimitPort +import team.comit.simtong.domain.auth.spi.QueryAuthCodePort import team.comit.simtong.global.annotation.UseCase /** @@ -14,13 +16,24 @@ import team.comit.simtong.global.annotation.UseCase **/ @UseCase class CheckAuthCodeUseCase( - private val checkAuthCodePolicy: CheckAuthCodePolicy, - private val commandAuthCodeLimitPort: CommandAuthCodeLimitPort + private val commandAuthCodeLimitPort: CommandAuthCodeLimitPort, + private val queryAuthCodePort: QueryAuthCodePort ) { fun execute(email: String, code: String) { + val authCode = queryAuthCodePort.queryAuthCodeByEmail(email) + + if (authCode?.code != code) { + throw AuthCodeMismatchException.EXCEPTION + } + commandAuthCodeLimitPort.save( - checkAuthCodePolicy.implement(email, code) + AuthCodeLimit( + key = email, + expirationTime = AuthCodeLimit.VERIFIED_EXPIRED, + attemptCount = 0, + isVerified = true + ) ) } diff --git a/simtong-application/src/main/kotlin/team/comit/simtong/domain/auth/usecase/SendAuthCodeUseCase.kt b/simtong-application/src/main/kotlin/team/comit/simtong/domain/auth/usecase/SendAuthCodeUseCase.kt index ca0b6b89..e013d2f2 100644 --- a/simtong-application/src/main/kotlin/team/comit/simtong/domain/auth/usecase/SendAuthCodeUseCase.kt +++ b/simtong-application/src/main/kotlin/team/comit/simtong/domain/auth/usecase/SendAuthCodeUseCase.kt @@ -1,5 +1,7 @@ package team.comit.simtong.domain.auth.usecase +import net.bytebuddy.utility.RandomString +import team.comit.simtong.domain.auth.model.AuthCode import team.comit.simtong.domain.auth.policy.SendAuthCodePolicy import team.comit.simtong.domain.auth.spi.CommandAuthCodeLimitPort import team.comit.simtong.domain.auth.spi.CommandAuthCodePort @@ -24,11 +26,15 @@ class SendAuthCodeUseCase( fun execute(email: String) { commandAuthCodeLimitPort.save( - sendAuthCodePolicy.restriction(email) + sendAuthCodePolicy.implement(email) ) val authCode = commandAuthCodePort.save( - sendAuthCodePolicy.implement(email) + AuthCode( + key = email, + code = RandomString(6).nextString(), + expirationTime = AuthCode.EXPIRED + ) ) sendEmailPort.sendAuthCode(authCode.code, email) diff --git a/simtong-application/src/test/kotlin/team/comit/simtong/domain/auth/usecase/CheckAuthCodeUseCaseTests.kt b/simtong-application/src/test/kotlin/team/comit/simtong/domain/auth/usecase/CheckAuthCodeUseCaseTests.kt index 37d3b0d5..00307d39 100644 --- a/simtong-application/src/test/kotlin/team/comit/simtong/domain/auth/usecase/CheckAuthCodeUseCaseTests.kt +++ b/simtong-application/src/test/kotlin/team/comit/simtong/domain/auth/usecase/CheckAuthCodeUseCaseTests.kt @@ -10,7 +10,6 @@ import org.springframework.test.context.junit.jupiter.SpringExtension import team.comit.simtong.domain.auth.exception.AuthCodeMismatchException import team.comit.simtong.domain.auth.model.AuthCode import team.comit.simtong.domain.auth.model.AuthCodeLimit -import team.comit.simtong.domain.auth.policy.CheckAuthCodePolicy import team.comit.simtong.domain.auth.spi.CommandAuthCodeLimitPort import team.comit.simtong.domain.auth.spi.QueryAuthCodePort @@ -23,8 +22,6 @@ class CheckAuthCodeUseCaseTests { @MockBean private lateinit var commandAuthCodeLimitPort: CommandAuthCodeLimitPort - private lateinit var checkAuthCodePolicy: CheckAuthCodePolicy - private lateinit var checkAuthCodeUseCase: CheckAuthCodeUseCase private val email = "test@test.com" @@ -58,8 +55,7 @@ class CheckAuthCodeUseCaseTests { @BeforeEach fun setUp() { - checkAuthCodePolicy = CheckAuthCodePolicy(queryAuthCodePort) - checkAuthCodeUseCase = CheckAuthCodeUseCase(checkAuthCodePolicy, commandAuthCodeLimitPort) + checkAuthCodeUseCase = CheckAuthCodeUseCase(commandAuthCodeLimitPort, queryAuthCodePort) } @Test @@ -68,9 +64,6 @@ class CheckAuthCodeUseCaseTests { given(queryAuthCodePort.queryAuthCodeByEmail(email)) .willReturn(authCodeStub) - given(commandAuthCodeLimitPort.save(verifiedAuthCodeLimitStub)) - .willReturn(verifiedAuthCodeLimitStub) - // when checkAuthCodeUseCase.execute(email, code) } diff --git a/simtong-application/src/test/kotlin/team/comit/simtong/domain/auth/usecase/SendAuthCodeUseCaseTests.kt b/simtong-application/src/test/kotlin/team/comit/simtong/domain/auth/usecase/SendAuthCodeUseCaseTests.kt index 07c7ae5a..f03d39ee 100644 --- a/simtong-application/src/test/kotlin/team/comit/simtong/domain/auth/usecase/SendAuthCodeUseCaseTests.kt +++ b/simtong-application/src/test/kotlin/team/comit/simtong/domain/auth/usecase/SendAuthCodeUseCaseTests.kt @@ -42,15 +42,6 @@ class SendAuthCodeUseCaseTests { private val code = "123456" - private val authCodeLimitStub by lazy { - AuthCodeLimit( - key = email, - expirationTime = AuthCodeLimit.EXPIRED, - attemptCount = 1, - isVerified = false - ) - } - private val verifiedAuthCodeLimitStub by lazy { AuthCodeLimit( key = email, @@ -94,16 +85,13 @@ class SendAuthCodeUseCaseTests { given(queryAuthCodeLimitPort.queryAuthCodeLimitByEmail(email)) .willReturn(null) - given(commandAuthCodeLimitPort.save(any())) - .willReturn(authCodeLimitStub) - given(commandAuthCodePort.save(any())) .willReturn(authCodeStub) willDoNothing().given(sendEmailPort).sendAuthCode(code, email) // when - val result = sendAuthCodeUseCase.execute(email) + sendAuthCodeUseCase.execute(email) } @Test diff --git a/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/policy/CheckAuthCodePolicy.kt b/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/policy/CheckAuthCodePolicy.kt deleted file mode 100644 index 6ca0e054..00000000 --- a/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/policy/CheckAuthCodePolicy.kt +++ /dev/null @@ -1,36 +0,0 @@ -package team.comit.simtong.domain.auth.policy - -import team.comit.simtong.domain.auth.exception.AuthCodeMismatchException -import team.comit.simtong.domain.auth.model.AuthCodeLimit -import team.comit.simtong.domain.auth.spi.QueryAuthCodePort -import team.comit.simtong.global.annotation.Policy - -/** - * - * 이메일 인증 코드 확인 정책을 관리하는 CheckAuthCodePolicy - * - * @author Chokyunghyeon - * @date 2022/09/25 - * @version 1.0.0 - **/ -@Policy -class CheckAuthCodePolicy( - private val queryAuthCodePort: QueryAuthCodePort -) { - - fun implement(email: String, code: String): AuthCodeLimit { - val authCode = queryAuthCodePort.queryAuthCodeByEmail(email) - - if (authCode?.code != code) { - throw AuthCodeMismatchException.EXCEPTION - } - - return AuthCodeLimit( - key = email, - expirationTime = AuthCodeLimit.VERIFIED_EXPIRED, - attemptCount = 0, - isVerified = true - ) - } - -} \ No newline at end of file diff --git a/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/policy/SendAuthCodePolicy.kt b/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/policy/SendAuthCodePolicy.kt index 5024d705..8614bfb6 100644 --- a/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/policy/SendAuthCodePolicy.kt +++ b/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/policy/SendAuthCodePolicy.kt @@ -1,9 +1,7 @@ package team.comit.simtong.domain.auth.policy -import net.bytebuddy.utility.RandomString import team.comit.simtong.domain.auth.exception.CertifiedEmailException import team.comit.simtong.domain.auth.exception.ExceededSendAuthCodeRequestException -import team.comit.simtong.domain.auth.model.AuthCode import team.comit.simtong.domain.auth.model.AuthCodeLimit import team.comit.simtong.domain.auth.spi.QueryAuthCodeLimitPort import team.comit.simtong.global.annotation.Policy @@ -21,7 +19,7 @@ class SendAuthCodePolicy( private val queryAuthCodeLimitPort: QueryAuthCodeLimitPort ) { - fun restriction(email: String): AuthCodeLimit { + fun implement(email: String): AuthCodeLimit { val authCodeLimit = queryAuthCodeLimitPort.queryAuthCodeLimitByEmail(email) ?: AuthCodeLimit( key = email, @@ -41,12 +39,4 @@ class SendAuthCodePolicy( return authCodeLimit.sendAuthCode() } - fun implement(email: String): AuthCode { - return AuthCode( - key = email, - code = RandomString(6).nextString(), - expirationTime = AuthCode.EXPIRED - ) - } - } \ No newline at end of file