Skip to content

Commit

Permalink
refactor: (#78) 생성자 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
khcho0125 committed Sep 29, 2022
1 parent d116bf2 commit a07848b
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -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.service.ConstructAuthCodeLimitService
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
Expand All @@ -17,7 +17,6 @@ import team.comit.simtong.global.annotation.UseCase
@UseCase
class CheckAuthCodeUseCase(
private val commandAuthCodeLimitPort: CommandAuthCodeLimitPort,
private val constructAuthCodeLimitService: ConstructAuthCodeLimitService,
private val queryAuthCodePort: QueryAuthCodePort
) {

Expand All @@ -29,7 +28,7 @@ class CheckAuthCodeUseCase(
}

commandAuthCodeLimitPort.save(
constructAuthCodeLimitService.verified(email)
AuthCodeLimit.verified(email)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.service.ConstructAuthCodeLimitService
import team.comit.simtong.domain.auth.service.ConstructAuthCodeService
import team.comit.simtong.domain.auth.model.AuthCode
import team.comit.simtong.domain.auth.model.AuthCodeLimit
import team.comit.simtong.domain.auth.spi.CommandAuthCodeLimitPort
import team.comit.simtong.domain.auth.spi.CommandAuthCodePort
import team.comit.simtong.domain.auth.spi.QueryAuthCodeLimitPort
Expand All @@ -22,24 +22,20 @@ 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)
?: constructAuthCodeLimitService.construct(email)
?: AuthCodeLimit.default(email)

if(authCodeLimit.isVerified) {
throw CertifiedEmailException.EXCEPTION
}

commandAuthCodeLimitPort.save(authCodeLimit.increaseCount())

val authCode = commandAuthCodePort.save(
constructAuthCodeService.construct(email)
)
val authCode = commandAuthCodePort.save(AuthCode.default(email))

sendEmailPort.sendAuthCode(authCode.code, email)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ 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

Expand All @@ -21,9 +19,6 @@ class CheckAuthCodeUseCaseTests {
@MockBean
private lateinit var queryAuthCodePort: QueryAuthCodePort

@MockBean
private lateinit var constructAuthCodeLimitService: ConstructAuthCodeLimitService

@MockBean
private lateinit var commandAuthCodeLimitPort: CommandAuthCodeLimitPort

Expand All @@ -33,15 +28,6 @@ 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,
Expand All @@ -62,7 +48,6 @@ class CheckAuthCodeUseCaseTests {
fun setUp() {
checkAuthCodeUseCase = CheckAuthCodeUseCase(
commandAuthCodeLimitPort,
constructAuthCodeLimitService,
queryAuthCodePort
)
}
Expand All @@ -73,9 +58,6 @@ class CheckAuthCodeUseCaseTests {
given(queryAuthCodePort.queryAuthCodeByEmail(email))
.willReturn(authCodeStub)

given(constructAuthCodeLimitService.verified(email))
.willReturn(verifiedAuthCodeLimitStub)

// when & then
assertDoesNotThrow {
checkAuthCodeUseCase.execute(email, code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ 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
Expand All @@ -35,27 +34,12 @@ 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 = "[email protected]"

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,
Expand Down Expand Up @@ -88,8 +72,6 @@ class SendAuthCodeUseCaseTests {
commandAuthCodeLimitPort,
commandAuthCodePort,
queryAuthCodeLimitPort,
constructAuthCodeLimitService,
constructAuthCodeService,
sendEmailPort
)
}
Expand All @@ -100,13 +82,7 @@ class SendAuthCodeUseCaseTests {
given(queryAuthCodeLimitPort.queryAuthCodeLimitByEmail(email))
.willReturn(null)

given(constructAuthCodeLimitService.construct(email))
.willReturn(authCodeLimitStub)

given(constructAuthCodeService.construct(email))
.willReturn(authCodeStub)

given(commandAuthCodePort.save(authCodeStub))
given(commandAuthCodePort.save(any()))
.willReturn(authCodeStub)

willDoNothing().given(sendEmailPort).sendAuthCode(code, email)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package team.comit.simtong.domain.auth.model

import net.bytebuddy.utility.RandomString
import team.comit.simtong.global.annotation.Aggregate

/**
Expand All @@ -21,6 +22,12 @@ class AuthCode(

companion object {
const val EXPIRED = 180

fun default(email: String) = AuthCode(
key = email,
code = RandomString(6).nextString(),
expirationTime = EXPIRED
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,24 @@ class AuthCodeLimit(
companion object {
// TODO 환경 변수 관리

const val EXPIRED = 1800
const val MAX_ATTEMPT_COUNT: Short = 5
const val EXPIRED = 1800
const val VERIFIED_EXPIRED = 2700

fun verified(email: String) = AuthCodeLimit(
key = email,
expirationTime = VERIFIED_EXPIRED,
attemptCount = 0,
isVerified = true
)

fun default(email: String) = AuthCodeLimit(
key = email,
expirationTime = EXPIRED,
attemptCount = 0,
isVerified = false
)

}

fun increaseCount(): AuthCodeLimit {
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit a07848b

Please sign in to comment.