Skip to content

Commit

Permalink
test: (#103) 비밀번호 변경 TestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
khcho0125 committed Oct 15, 2022
1 parent 35213a6 commit c8ee4fa
Showing 1 changed file with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package team.comit.simtong.domain.user.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
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.junit.jupiter.SpringExtension
import team.comit.simtong.domain.user.dto.ChangePasswordRequest
import team.comit.simtong.domain.user.exception.DifferentPasswordException
import team.comit.simtong.domain.user.exception.UserNotFoundException
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.UserSecurityPort
import java.util.UUID

@ExtendWith(SpringExtension::class)
class ChangePasswordUseCaseTests {

@MockBean
private lateinit var queryUserPort: QueryUserPort

@MockBean
private lateinit var userSecurityPort: UserSecurityPort

@MockBean
private lateinit var commandUserPort: CommandUserPort

private lateinit var changePasswordUseCase: ChangePasswordUseCase

private val id = UUID.randomUUID()

private val userStub: User by lazy {
User(
id = id,
nickname = "test nickname",
name = "test name",
email = "[email protected]",
password = "test password",
employeeNumber = 1234567890,
authority = Authority.ROLE_COMMON,
spotId = UUID.randomUUID(),
teamId = UUID.randomUUID(),
profileImagePath = "test profile image path"
)
}

private val requestStub: ChangePasswordRequest by lazy {
ChangePasswordRequest(
password = "test password",
newPassword = "test new password"
)
}

@BeforeEach
fun setUp() {
changePasswordUseCase = ChangePasswordUseCase(
queryUserPort,
userSecurityPort,
commandUserPort
)
}

@Test
fun `비밀번호 변경 성공`() {
// given
given(userSecurityPort.getCurrentUserId())
.willReturn(id)

given(queryUserPort.queryUserById(id))
.willReturn(userStub)

given(userSecurityPort.compare(requestStub.password, userStub.password))
.willReturn(true)

given(userSecurityPort.encode(requestStub.newPassword))
.willReturn(requestStub.newPassword)

// when & then
assertDoesNotThrow {
changePasswordUseCase.execute(requestStub)
}
}

@Test
fun `유저 찾기 실패`() {
// given
given(userSecurityPort.getCurrentUserId())
.willReturn(id)

given(queryUserPort.queryUserById(id))
.willReturn(null)

// when & then
assertThrows<UserNotFoundException> {
changePasswordUseCase.execute(requestStub)
}
}

@Test
fun `비밀번호 불일치`() {
// given
given(userSecurityPort.getCurrentUserId())
.willReturn(id)

given(queryUserPort.queryUserById(id))
.willReturn(userStub)

given(userSecurityPort.compare(requestStub.password, userStub.password))
.willReturn(false)

// when & then
assertThrows<DifferentPasswordException> {
changePasswordUseCase.execute(requestStub)
}
}

}

0 comments on commit c8ee4fa

Please sign in to comment.