Skip to content

Commit

Permalink
add: signin unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongho1209 committed Apr 7, 2024
1 parent bdd0c38 commit 60c68c9
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/test/kotlin/com/trip/safe/user/service/UserServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package com.trip.safe.user.service
import com.trip.safe.common.security.jwt.JwtTokenProvider
import com.trip.safe.user.domain.User
import com.trip.safe.user.domain.UserRepository
import com.trip.safe.user.exception.PasswordMisMatchException
import com.trip.safe.user.exception.UserExistException
import com.trip.safe.user.exception.UserNotFoundException
import com.trip.safe.user.presentation.dto.request.UserSignInRequest
import com.trip.safe.user.presentation.dto.request.UserSignUpRequest
import io.kotest.core.spec.style.DescribeSpec
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows
import org.springframework.security.crypto.password.PasswordEncoder
Expand All @@ -19,25 +23,56 @@ class UserServiceTest : DescribeSpec(
val jwtTokenProvider = mockk<JwtTokenProvider>(relaxed = true)
val passwordEncoder = mockk<PasswordEncoder>(relaxed = true)
val userService = UserService(userRepository, jwtTokenProvider, passwordEncoder)
val requestStub = UserSignUpRequest("testAccountId", "testPassword", 20)
val signUpRequestStub = UserSignUpRequest("testAccountId", "testPassword", 20)
val signInRequestStub = UserSignInRequest("testAccountId", "testPassword")
val userStub = User(
accountId = "userAccountId",
password = "userPassword",
age = 20,
)

describe("유저가 회원가입을 하는 경우") {
context("이미 존재하는 유저이면") {
coEvery { userRepository.existsByAccountId(requestStub.accountId) } returns true
coEvery { userRepository.existsByAccountId(signUpRequestStub.accountId) } returns true
it("UserExistException을 던진다.") {
assertThrows<UserExistException> { userService.signUp(requestStub) }
assertThrows<UserExistException> { userService.signUp(signUpRequestStub) }
coVerify(exactly = 0) { userRepository.save(any()) }
}
}

context("존재하지 않는 유저이면") {
coEvery { userRepository.existsByAccountId(requestStub.accountId) } returns false
coEvery { passwordEncoder.encode(requestStub.password) } returns requestStub.password
coEvery { userRepository.existsByAccountId(signUpRequestStub.accountId) } returns false
coEvery { passwordEncoder.encode(signUpRequestStub.password) } returns signUpRequestStub.password
it("회원가입에 성공한다.") {
assertDoesNotThrow { userService.signUp(requestStub) }
assertDoesNotThrow { userService.signUp(signUpRequestStub) }
coVerify(exactly = 1) { userRepository.save(any()) }
}
}
}

describe("유저가 로그인을 하는 경우") {
context("잘못된 아이디라면") {
coEvery { userRepository.findByAccountId(signInRequestStub.accountId) } returns null
it("UserNotFoundException을 던진다.") {
assertThrows<UserNotFoundException> { userService.signIn(signInRequestStub) }
}
}

context("잘못된 비밀번호라면") {
coEvery { userRepository.findByAccountId(signInRequestStub.accountId) } returns userStub
coEvery { passwordEncoder.matches(any(), any()) } returns false
it("PasswordMisMatchException을 던진다.") {
assertThrows<PasswordMisMatchException> { userService.signIn(signInRequestStub) }
}
}

context("올바른 아이디, 비밀번호라면") {
coEvery { userRepository.findByAccountId(signInRequestStub.accountId) } returns userStub
coEvery { passwordEncoder.matches(any(), any()) } returns true
it("로그인에 성공한다.") {
assertDoesNotThrow { userService.signIn(signInRequestStub) }
}
}
}
}
)

0 comments on commit 60c68c9

Please sign in to comment.