Skip to content

Commit

Permalink
merge: (#111) 회원 가입 리팩토링 (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
khcho0125 authored Oct 14, 2022
2 parents d85853e + 4dd0332 commit 17dc102
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package team.comit.simtong.domain.user.usecase

import team.comit.simtong.domain.auth.dto.TokenResponse
import team.comit.simtong.domain.user.policy.SignUpPolicy
import team.comit.simtong.domain.auth.exception.RequiredNewEmailAuthenticationException
import team.comit.simtong.domain.auth.exception.UncertifiedEmailException
import team.comit.simtong.domain.auth.exception.UsedEmailException
import team.comit.simtong.domain.spot.exception.SpotNotFoundException
import team.comit.simtong.domain.team.exception.TeamNotFoundException
import team.comit.simtong.domain.user.dto.SignUpRequest
import team.comit.simtong.domain.user.model.Authority
import team.comit.simtong.domain.user.model.User
import team.comit.simtong.domain.user.spi.CommandUserPort
import team.comit.simtong.domain.user.spi.QueryUserPort
import team.comit.simtong.domain.user.spi.UserJwtPort
import team.comit.simtong.domain.user.dto.SignUpRequest
import team.comit.simtong.domain.user.spi.UserQueryAuthCodeLimitPort
import team.comit.simtong.domain.user.spi.UserQuerySpotPort
import team.comit.simtong.domain.user.spi.UserQueryTeamPort
import team.comit.simtong.domain.user.spi.UserSecurityPort
import team.comit.simtong.global.annotation.UseCase

/**
Expand All @@ -20,18 +31,48 @@ import team.comit.simtong.global.annotation.UseCase
class SignUpUseCase(
private val userJwtPort: UserJwtPort,
private val commandUserPort: CommandUserPort,
private val signUpPolicy: SignUpPolicy
private val queryUserPort: QueryUserPort,
private val userQueryAuthCodeLimitPort: UserQueryAuthCodeLimitPort,
private val userQuerySpotPort: UserQuerySpotPort,
private val userQueryTeamPort: UserQueryTeamPort,
// private val nickNamePort: NickNamePort,
private val userSecurityPort: UserSecurityPort
) {

fun execute(request: SignUpRequest): TokenResponse {
val (name, email, password, nickname, employeeNumber, profileImagePath) = request

if (queryUserPort.existsUserByEmail(email)) {
throw UsedEmailException.EXCEPTION
}

val authCodeLimit = userQueryAuthCodeLimitPort.queryAuthCodeLimitByEmail(email)
?: throw RequiredNewEmailAuthenticationException.EXCEPTION

if (!authCodeLimit.isVerified) {
throw UncertifiedEmailException.EXCEPTION
}

// TODO 비즈니스 로직 직접 구현
// 임직원 확인

val spot = userQuerySpotPort.querySpotByName("test spotName")
?: throw SpotNotFoundException.EXCEPTION

val team = userQueryTeamPort.queryTeamByName("test teamName")
?: throw TeamNotFoundException.EXCEPTION

val user = commandUserPort.save(
signUpPolicy.implement(
nickname = request.nickname,
name = request.name,
email = request.email,
password = request.password,
employeeNumber = request.employeeNumber,
profileImagePath = request.profileImagePath
User(
nickname = nickname ?: "", // nickNamePort.random()
name = name,
email = email,
password = userSecurityPort.encode(password),
employeeNumber = employeeNumber,
authority = Authority.ROLE_COMMON,
spotId = spot.id,
teamId = team.id,
profileImagePath = profileImagePath ?: User.defaultImage
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.BDDMockito.given
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.junit.jupiter.SpringExtension
import team.comit.simtong.domain.auth.dto.TokenResponse
import team.comit.simtong.domain.auth.exception.RequiredNewEmailAuthenticationException
import team.comit.simtong.domain.auth.exception.UncertifiedEmailException
import team.comit.simtong.domain.auth.exception.UsedEmailException
import team.comit.simtong.domain.auth.model.AuthCodeLimit
import team.comit.simtong.domain.auth.dto.TokenResponse
import team.comit.simtong.domain.spot.exception.SpotNotFoundException
import team.comit.simtong.domain.spot.model.Spot
import team.comit.simtong.domain.team.exception.TeamNotFoundException
import team.comit.simtong.domain.team.model.Team
import team.comit.simtong.domain.user.dto.SignUpRequest
import team.comit.simtong.domain.user.model.Authority
import team.comit.simtong.domain.user.model.User
import team.comit.simtong.domain.user.policy.SignUpPolicy
import team.comit.simtong.domain.user.spi.CommandUserPort
import team.comit.simtong.domain.user.spi.QueryUserPort
import team.comit.simtong.domain.user.spi.UserJwtPort
import team.comit.simtong.domain.user.spi.UserQueryAuthCodeLimitPort
import team.comit.simtong.domain.user.spi.UserQuerySpotPort
import team.comit.simtong.domain.user.spi.UserQueryTeamPort
import team.comit.simtong.domain.user.spi.UserSecurityPort
import team.comit.simtong.domain.user.dto.SignUpRequest
import java.util.*

@ExtendWith(SpringExtension::class)
Expand All @@ -53,8 +53,6 @@ class SignUpUseCaseTests {
@MockBean
private lateinit var userQueryAuthCodeLimitPort: UserQueryAuthCodeLimitPort

private lateinit var signUpPolicy: SignUpPolicy

private lateinit var signUpUseCase: SignUpUseCase

private val employeeNumber: Int = 1234567891
Expand Down Expand Up @@ -102,20 +100,6 @@ class SignUpUseCaseTests {
)
}

private val userStub: User by lazy {
User(
nickname = nickname,
name = name,
email = email,
password = "encode test password",
employeeNumber = employeeNumber,
authority = Authority.ROLE_COMMON,
spotId = spotStub.id,
teamId = teamStub.id,
profileImagePath = profileImagePath
)
}

private val authCodeLimitStub: AuthCodeLimit by lazy {
AuthCodeLimit(
key = email,
Expand All @@ -125,15 +109,6 @@ class SignUpUseCaseTests {
)
}

private val unVerifiedAuthCodeLimitStub: AuthCodeLimit by lazy {
AuthCodeLimit(
key = email,
expirationTime = 12345,
attemptCount = 5,
isVerified = false
)
}

private val requestStub: SignUpRequest by lazy {
SignUpRequest(
nickname = nickname,
Expand All @@ -155,19 +130,84 @@ class SignUpUseCaseTests {

@BeforeEach
fun setUp() {
signUpPolicy = SignUpPolicy(
userQueryTeamPort,
userQuerySpotPort,
userQueryAuthCodeLimitPort,
signUpUseCase = SignUpUseCase(
userJwtPort,
commandUserPort,
queryUserPort,
userQueryAuthCodeLimitPort,
userQuerySpotPort,
userQueryTeamPort,
userSecurityPort
)
signUpUseCase = SignUpUseCase(userJwtPort, commandUserPort, signUpPolicy)
}

@Test
fun `회원가입 성공`() {
// given
val userStub = User(
nickname = nickname,
name = name,
email = email,
password = "encode test password",
employeeNumber = employeeNumber,
authority = Authority.ROLE_COMMON,
spotId = spotStub.id,
teamId = teamStub.id,
profileImagePath = profileImagePath
)

given(userQueryAuthCodeLimitPort.queryAuthCodeLimitByEmail(requestStub.email))
.willReturn(authCodeLimitStub)

given(queryUserPort.existsUserByEmail(requestStub.email))
.willReturn(false)

given(userSecurityPort.encode(requestStub.password))
.willReturn(userStub.password)

given(userQuerySpotPort.querySpotByName(spotName))
.willReturn(spotStub)

given(userQueryTeamPort.queryTeamByName(teamName))
.willReturn(teamStub)

given(commandUserPort.save(userStub))
.willReturn(saveUserStub)

given(userJwtPort.receiveToken(saveUserStub.id, saveUserStub.authority))
.willReturn(responseStub)

// when
val result = signUpUseCase.execute(requestStub)

// then
assertThat(result).isEqualTo(responseStub)
}

@Test
fun `회원가입 성공 OPTINAL`() {
// given
val requestStub = SignUpRequest(
nickname = null,
name = name,
email = email,
password = "test password",
employeeNumber = employeeNumber,
profileImagePath = null
)

val userStub = User(
nickname = "",
name = name,
email = email,
password = "encode test password",
employeeNumber = employeeNumber,
authority = Authority.ROLE_COMMON,
spotId = spotStub.id,
teamId = teamStub.id,
profileImagePath = User.defaultImage
)

given(userQueryAuthCodeLimitPort.queryAuthCodeLimitByEmail(requestStub.email))
.willReturn(authCodeLimitStub)

Expand Down Expand Up @@ -199,6 +239,13 @@ class SignUpUseCaseTests {
@Test
fun `인증되지 않은 이메일`() {
// given
val unVerifiedAuthCodeLimitStub = AuthCodeLimit(
key = email,
expirationTime = 12345,
attemptCount = 5,
isVerified = false
)

given(userQueryAuthCodeLimitPort.queryAuthCodeLimitByEmail(requestStub.email))
.willReturn(unVerifiedAuthCodeLimitStub)

Expand All @@ -209,13 +256,13 @@ class SignUpUseCaseTests {
}

@Test
fun `이메일 인증 객체 찾기 실패`() {
fun `인증되지 못한 이메일`() {
// given
given(userQueryAuthCodeLimitPort.queryAuthCodeLimitByEmail(requestStub.email))
.willReturn(null)

// when & then
assertThrows<UncertifiedEmailException> {
assertThrows<RequiredNewEmailAuthenticationException> {
signUpUseCase.execute(requestStub)
}
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.springframework.context.annotation.ComponentScan.Filter
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.FilterType
import team.comit.simtong.global.annotation.DomainService
import team.comit.simtong.global.annotation.Policy
import team.comit.simtong.global.annotation.ReadOnlyUseCase
import team.comit.simtong.global.annotation.UseCase

Expand All @@ -26,7 +25,6 @@ import team.comit.simtong.global.annotation.UseCase
classes = [
UseCase::class,
ReadOnlyUseCase::class,
Policy::class,
DomainService::class
]
)
Expand Down

0 comments on commit 17dc102

Please sign in to comment.