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 fb5ee0b2..b69440ac 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,7 @@ package team.comit.simtong.domain.auth.usecase import team.comit.simtong.domain.auth.exception.AuthCodeMismatchException -import team.comit.simtong.domain.auth.model.AuthCodeLimit +import team.comit.simtong.domain.auth.service.ConstructAuthCodeLimitService import team.comit.simtong.domain.auth.spi.CommandAuthCodeLimitPort import team.comit.simtong.domain.auth.spi.QueryAuthCodePort import team.comit.simtong.global.annotation.UseCase @@ -17,6 +17,7 @@ import team.comit.simtong.global.annotation.UseCase @UseCase class CheckAuthCodeUseCase( private val commandAuthCodeLimitPort: CommandAuthCodeLimitPort, + private val constructAuthCodeLimitService: ConstructAuthCodeLimitService, private val queryAuthCodePort: QueryAuthCodePort ) { @@ -28,12 +29,7 @@ class CheckAuthCodeUseCase( } commandAuthCodeLimitPort.save( - AuthCodeLimit( - key = email, - expirationTime = AuthCodeLimit.VERIFIED_EXPIRED, - attemptCount = 0, - isVerified = true - ) + constructAuthCodeLimitService.verified(email) ) } 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 302fe43d..db5ff660 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,8 +1,8 @@ package team.comit.simtong.domain.auth.usecase import team.comit.simtong.domain.auth.exception.CertifiedEmailException -import team.comit.simtong.domain.auth.model.AuthCode -import team.comit.simtong.domain.auth.model.AuthCodeLimit +import team.comit.simtong.domain.auth.service.ConstructAuthCodeLimitService +import team.comit.simtong.domain.auth.service.ConstructAuthCodeService import team.comit.simtong.domain.auth.spi.CommandAuthCodeLimitPort import team.comit.simtong.domain.auth.spi.CommandAuthCodePort import team.comit.simtong.domain.auth.spi.QueryAuthCodeLimitPort @@ -22,12 +22,14 @@ class SendAuthCodeUseCase( private val commandAuthCodeLimitPort: CommandAuthCodeLimitPort, private val commandAuthCodePort: CommandAuthCodePort, private val queryAuthCodeLimitPort: QueryAuthCodeLimitPort, + private val constructAuthCodeLimitService: ConstructAuthCodeLimitService, + private val constructAuthCodeService: ConstructAuthCodeService, private val sendEmailPort: SendEmailPort ) { fun execute(email: String) { val authCodeLimit = queryAuthCodeLimitPort.queryAuthCodeLimitByEmail(email) - ?: AuthCodeLimit(email) + ?: constructAuthCodeLimitService.construct(email) if(authCodeLimit.isVerified) { throw CertifiedEmailException.EXCEPTION @@ -35,7 +37,9 @@ class SendAuthCodeUseCase( commandAuthCodeLimitPort.save(authCodeLimit.increaseCount()) - val authCode = commandAuthCodePort.save(AuthCode(email)) + val authCode = commandAuthCodePort.save( + constructAuthCodeService.construct(email) + ) 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 3697b3eb..7ba539d6 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 @@ -2,6 +2,7 @@ package team.comit.simtong.domain.auth.usecase import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertDoesNotThrow import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.extension.ExtendWith import org.mockito.BDDMockito.given @@ -9,6 +10,8 @@ import org.springframework.boot.test.mock.mockito.MockBean 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.service.ConstructAuthCodeLimitService import team.comit.simtong.domain.auth.spi.CommandAuthCodeLimitPort import team.comit.simtong.domain.auth.spi.QueryAuthCodePort @@ -18,6 +21,9 @@ class CheckAuthCodeUseCaseTests { @MockBean private lateinit var queryAuthCodePort: QueryAuthCodePort + @MockBean + private lateinit var constructAuthCodeLimitService: ConstructAuthCodeLimitService + @MockBean private lateinit var commandAuthCodeLimitPort: CommandAuthCodeLimitPort @@ -27,6 +33,15 @@ class CheckAuthCodeUseCaseTests { private val code = "123456" + private val verifiedAuthCodeLimitStub: AuthCodeLimit by lazy { + AuthCodeLimit( + key = email, + expirationTime = AuthCodeLimit.VERIFIED_EXPIRED, + attemptCount = 0, + isVerified = true + ) + } + private val authCodeStub: AuthCode by lazy { AuthCode( key = email, @@ -45,7 +60,11 @@ class CheckAuthCodeUseCaseTests { @BeforeEach fun setUp() { - checkAuthCodeUseCase = CheckAuthCodeUseCase(commandAuthCodeLimitPort, queryAuthCodePort) + checkAuthCodeUseCase = CheckAuthCodeUseCase( + commandAuthCodeLimitPort, + constructAuthCodeLimitService, + queryAuthCodePort + ) } @Test @@ -54,8 +73,13 @@ class CheckAuthCodeUseCaseTests { given(queryAuthCodePort.queryAuthCodeByEmail(email)) .willReturn(authCodeStub) - // when - checkAuthCodeUseCase.execute(email, code) + given(constructAuthCodeLimitService.verified(email)) + .willReturn(verifiedAuthCodeLimitStub) + + // when & then + assertDoesNotThrow { + checkAuthCodeUseCase.execute(email, code) + } } @Test 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 1635e018..e9df12aa 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 @@ -7,13 +7,14 @@ import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.extension.ExtendWith import org.mockito.BDDMockito.given import org.mockito.BDDMockito.willDoNothing -import org.mockito.kotlin.any import org.springframework.boot.test.mock.mockito.MockBean import org.springframework.test.context.junit.jupiter.SpringExtension 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.service.ConstructAuthCodeLimitService +import team.comit.simtong.domain.auth.service.ConstructAuthCodeService import team.comit.simtong.domain.auth.spi.CommandAuthCodeLimitPort import team.comit.simtong.domain.auth.spi.CommandAuthCodePort import team.comit.simtong.domain.auth.spi.QueryAuthCodeLimitPort @@ -34,12 +35,27 @@ class SendAuthCodeUseCaseTests { @MockBean private lateinit var sendEmailPort: SendEmailPort + @MockBean + private lateinit var constructAuthCodeLimitService: ConstructAuthCodeLimitService + + @MockBean + private lateinit var constructAuthCodeService: ConstructAuthCodeService + private lateinit var sendAuthCodeUseCase: SendAuthCodeUseCase private val email = "test@test.com" private val code = "123456" + private val authCodeLimitStub by lazy { + AuthCodeLimit( + key = email, + expirationTime = AuthCodeLimit.EXPIRED, + attemptCount = 0, + isVerified = false + ) + } + private val verifiedAuthCodeLimitStub by lazy { AuthCodeLimit( key = email, @@ -72,6 +88,8 @@ class SendAuthCodeUseCaseTests { commandAuthCodeLimitPort, commandAuthCodePort, queryAuthCodeLimitPort, + constructAuthCodeLimitService, + constructAuthCodeService, sendEmailPort ) } @@ -82,7 +100,13 @@ class SendAuthCodeUseCaseTests { given(queryAuthCodeLimitPort.queryAuthCodeLimitByEmail(email)) .willReturn(null) - given(commandAuthCodePort.save(any())) + given(constructAuthCodeLimitService.construct(email)) + .willReturn(authCodeLimitStub) + + given(constructAuthCodeService.construct(email)) + .willReturn(authCodeStub) + + given(commandAuthCodePort.save(authCodeStub)) .willReturn(authCodeStub) willDoNothing().given(sendEmailPort).sendAuthCode(code, email) diff --git a/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/model/AuthCode.kt b/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/model/AuthCode.kt index 88cc4437..e97f95a9 100644 --- a/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/model/AuthCode.kt +++ b/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/model/AuthCode.kt @@ -1,6 +1,5 @@ package team.comit.simtong.domain.auth.model -import net.bytebuddy.utility.RandomString import team.comit.simtong.global.annotation.Aggregate /** @@ -24,10 +23,4 @@ class AuthCode( const val EXPIRED = 180 } - constructor(email: String) : this( - key = email, - code = RandomString(6).nextString(), - expirationTime = EXPIRED - ) - } \ No newline at end of file diff --git a/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/model/AuthCodeLimit.kt b/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/model/AuthCodeLimit.kt index b8cc546c..b78deea7 100644 --- a/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/model/AuthCodeLimit.kt +++ b/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/model/AuthCodeLimit.kt @@ -31,13 +31,6 @@ class AuthCodeLimit( const val VERIFIED_EXPIRED = 2700 } - constructor(email: String) : this( - key = email, - expirationTime = EXPIRED, - attemptCount = 0, - isVerified = false - ) - fun increaseCount(): AuthCodeLimit { if (attemptCount >= MAX_ATTEMPT_COUNT) { throw ExceededSendAuthCodeRequestException.EXCEPTION diff --git a/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/service/ConstructAuthCodeLimitService.kt b/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/service/ConstructAuthCodeLimitService.kt new file mode 100644 index 00000000..be03e343 --- /dev/null +++ b/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/service/ConstructAuthCodeLimitService.kt @@ -0,0 +1,35 @@ +package team.comit.simtong.domain.auth.service + +import team.comit.simtong.domain.auth.model.AuthCodeLimit +import team.comit.simtong.global.annotation.DomainService + +/** + * + * AuthCodeLimit을 생성하는 ConstructAuthCodeLimitService + * + * @author Chokyunghyeon + * @date 2022/09/29 + * @version 1.0.0 + **/ +@DomainService +class ConstructAuthCodeLimitService { + + fun construct(email: String): AuthCodeLimit { + return AuthCodeLimit( + key = email, + expirationTime = AuthCodeLimit.EXPIRED, + attemptCount = 0, + isVerified = false + ) + } + + fun verified(email: String): AuthCodeLimit { + return AuthCodeLimit( + key = email, + expirationTime = AuthCodeLimit.VERIFIED_EXPIRED, + attemptCount = AuthCodeLimit.MAX_ATTEMPT_COUNT, + isVerified = true + ) + } + +} \ No newline at end of file diff --git a/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/service/ConstructAuthCodeService.kt b/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/service/ConstructAuthCodeService.kt new file mode 100644 index 00000000..4d27b443 --- /dev/null +++ b/simtong-domain/src/main/kotlin/team/comit/simtong/domain/auth/service/ConstructAuthCodeService.kt @@ -0,0 +1,26 @@ +package team.comit.simtong.domain.auth.service + +import net.bytebuddy.utility.RandomString +import team.comit.simtong.domain.auth.model.AuthCode +import team.comit.simtong.global.annotation.DomainService + +/** + * + * AuthCode를 생성하는 ConstructAuthCodeService + * + * @author Chokyunghyeon + * @date 2022/09/29 + * @version 1.0.0 + **/ +@DomainService +class ConstructAuthCodeService { + + fun construct(email: String): AuthCode { + return AuthCode( + key = email, + code = RandomString(6).nextString(), + expirationTime = AuthCode.EXPIRED + ) + } + +} \ No newline at end of file