Skip to content

Commit

Permalink
refactor: (#78) Policy 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
khcho0125 committed Sep 27, 2022
1 parent 8338db1 commit 43b5d72
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -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

/**
Expand All @@ -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
)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 = "[email protected]"
Expand Down Expand Up @@ -58,8 +55,7 @@ class CheckAuthCodeUseCaseTests {

@BeforeEach
fun setUp() {
checkAuthCodePolicy = CheckAuthCodePolicy(queryAuthCodePort)
checkAuthCodeUseCase = CheckAuthCodeUseCase(checkAuthCodePolicy, commandAuthCodeLimitPort)
checkAuthCodeUseCase = CheckAuthCodeUseCase(commandAuthCodeLimitPort, queryAuthCodePort)
}

@Test
Expand All @@ -68,9 +64,6 @@ class CheckAuthCodeUseCaseTests {
given(queryAuthCodePort.queryAuthCodeByEmail(email))
.willReturn(authCodeStub)

given(commandAuthCodeLimitPort.save(verifiedAuthCodeLimitStub))
.willReturn(verifiedAuthCodeLimitStub)

// when
checkAuthCodeUseCase.execute(email, code)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -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
)
}

}

0 comments on commit 43b5d72

Please sign in to comment.