generated from pricelees/issue-pr-template
test: AuthServiceTest 코틀린 전환 및 일부 테스트 추가
This commit is contained in:
parent
26f7c554e7
commit
4a014b9a6e
@ -1,66 +1,83 @@
|
||||
package roomescape.system.auth.business;
|
||||
package roomescape.system.auth.business
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import io.kotest.assertions.assertSoftly
|
||||
import io.kotest.assertions.throwables.shouldThrow
|
||||
import io.kotest.core.spec.style.BehaviorSpec
|
||||
import io.kotest.matchers.shouldBe
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import roomescape.common.JwtFixture
|
||||
import roomescape.common.MemberFixture
|
||||
import roomescape.member.business.MemberService
|
||||
import roomescape.member.infrastructure.persistence.Member
|
||||
import roomescape.member.infrastructure.persistence.MemberRepository
|
||||
import roomescape.system.auth.infrastructure.jwt.JwtHandler
|
||||
import roomescape.system.auth.service.AuthService
|
||||
import roomescape.system.exception.ErrorType
|
||||
import roomescape.system.exception.RoomEscapeException
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import roomescape.member.business.MemberService;
|
||||
import roomescape.member.infrastructure.persistence.Member;
|
||||
import roomescape.member.infrastructure.persistence.MemberRepository;
|
||||
import roomescape.member.infrastructure.persistence.Role;
|
||||
import roomescape.system.auth.service.AuthService;
|
||||
import roomescape.system.auth.web.LoginRequest;
|
||||
import roomescape.system.auth.infrastructure.jwt.JwtHandler;
|
||||
import roomescape.system.auth.infrastructure.jwt.TokenDto;
|
||||
import roomescape.system.exception.RoomEscapeException;
|
||||
class AuthServiceTest : BehaviorSpec({
|
||||
val memberRepository: MemberRepository = mockk()
|
||||
val memberService: MemberService = MemberService(memberRepository)
|
||||
val jwtHandler: JwtHandler = JwtFixture.create()
|
||||
|
||||
@SpringBootTest
|
||||
@Import({AuthService.class, JwtHandler.class, MemberService.class})
|
||||
class AuthServiceTest {
|
||||
val authService = AuthService(memberService, jwtHandler)
|
||||
val user: Member = MemberFixture.user()
|
||||
|
||||
@Autowired
|
||||
private AuthService authService;
|
||||
@Autowired
|
||||
private MemberRepository memberRepository;
|
||||
Given("로그인 요청을 받으면") {
|
||||
When("이메일과 비밀번호로 회원을 찾고") {
|
||||
val request = MemberFixture.userLoginRequest()
|
||||
|
||||
@Test
|
||||
@DisplayName("로그인 성공시 JWT accessToken 을 반환한다.")
|
||||
void loginSuccess() {
|
||||
// given
|
||||
Member member = memberRepository.save(new Member(null, "이름", "test@test.com", "12341234", Role.MEMBER));
|
||||
Then("회원이 있다면 JWT 토큰을 생성한 뒤 반환한다.") {
|
||||
every {
|
||||
memberRepository.findByEmailAndPassword(request.email, request.password)
|
||||
} returns user
|
||||
|
||||
// when
|
||||
TokenDto response = authService.login(new LoginRequest(member.getEmail(), member.getPassword()));
|
||||
val accessToken: String = authService.login(request).accessToken
|
||||
|
||||
// then
|
||||
assertThat(response.accessToken()).isNotNull();
|
||||
accessToken.isNotBlank() shouldBe true
|
||||
jwtHandler.getMemberIdFromToken(accessToken) shouldBe user.id
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("존재하지 않는 회원 email 또는 password로 로그인하면 예외가 발생한다.")
|
||||
void loginFailByNotExistMemberInfo() {
|
||||
// given
|
||||
String notExistEmail = "invalid@test.com";
|
||||
String notExistPassword = "invalid1234";
|
||||
Then("회원이 없다면 예외를 던진다.") {
|
||||
every {
|
||||
memberRepository.findByEmailAndPassword(request.email, request.password)
|
||||
} returns null
|
||||
|
||||
// when & then
|
||||
Assertions.assertThatThrownBy(() -> authService.login(new LoginRequest(notExistEmail, notExistPassword)))
|
||||
.isInstanceOf(RoomEscapeException.class);
|
||||
val exception = shouldThrow<RoomEscapeException> {
|
||||
authService.login(request)
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("존재하지 않는 회원의 memberId로 로그인 여부를 체크하면 예외가 발생한다.")
|
||||
void checkLoginFailByNotExistMemberInfo() {
|
||||
// given
|
||||
Long notExistMemberId = (long)(memberRepository.findAll().size() + 1);
|
||||
|
||||
// when & then
|
||||
Assertions.assertThatThrownBy(() -> authService.checkLogin(notExistMemberId))
|
||||
.isInstanceOf(RoomEscapeException.class);
|
||||
exception.errorType shouldBe ErrorType.MEMBER_NOT_FOUND
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Given("로그인 확인 요청을 받으면") {
|
||||
When("회원 ID로 회원을 찾고") {
|
||||
val userId: Long = user.id!!
|
||||
|
||||
Then("회원이 있다면 회원의 이름을 반환한다.") {
|
||||
every { memberRepository.findByIdOrNull(userId) } returns user
|
||||
|
||||
val response = authService.checkLogin(userId)
|
||||
|
||||
assertSoftly(response) {
|
||||
this.name shouldBe user.name
|
||||
}
|
||||
}
|
||||
|
||||
Then("회원이 없다면 예외를 던진다.") {
|
||||
every { memberRepository.findByIdOrNull(userId) } returns null
|
||||
|
||||
val exception = shouldThrow<RoomEscapeException> {
|
||||
authService.checkLogin(userId)
|
||||
}
|
||||
|
||||
exception.errorType shouldBe ErrorType.MEMBER_NOT_FOUND
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user