From 60c68c98e1e5a907e8eef040aed30d22a59d01ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A0=95=ED=98=B8?= Date: Sun, 7 Apr 2024 21:57:17 +0900 Subject: [PATCH] add: signin unit test --- .../trip/safe/user/service/UserServiceTest.kt | 47 ++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/src/test/kotlin/com/trip/safe/user/service/UserServiceTest.kt b/src/test/kotlin/com/trip/safe/user/service/UserServiceTest.kt index 86b89f6..aabbba6 100644 --- a/src/test/kotlin/com/trip/safe/user/service/UserServiceTest.kt +++ b/src/test/kotlin/com/trip/safe/user/service/UserServiceTest.kt @@ -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 @@ -19,25 +23,56 @@ class UserServiceTest : DescribeSpec( val jwtTokenProvider = mockk(relaxed = true) val passwordEncoder = mockk(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 { userService.signUp(requestStub) } + assertThrows { 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 { userService.signIn(signInRequestStub) } + } + } + + context("잘못된 비밀번호라면") { + coEvery { userRepository.findByAccountId(signInRequestStub.accountId) } returns userStub + coEvery { passwordEncoder.matches(any(), any()) } returns false + it("PasswordMisMatchException을 던진다.") { + assertThrows { userService.signIn(signInRequestStub) } + } + } + + context("올바른 아이디, 비밀번호라면") { + coEvery { userRepository.findByAccountId(signInRequestStub.accountId) } returns userStub + coEvery { passwordEncoder.matches(any(), any()) } returns true + it("로그인에 성공한다.") { + assertDoesNotThrow { userService.signIn(signInRequestStub) } + } + } + } } )