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;
|
class AuthServiceTest : BehaviorSpec({
|
||||||
import roomescape.member.infrastructure.persistence.Member;
|
val memberRepository: MemberRepository = mockk()
|
||||||
import roomescape.member.infrastructure.persistence.MemberRepository;
|
val memberService: MemberService = MemberService(memberRepository)
|
||||||
import roomescape.member.infrastructure.persistence.Role;
|
val jwtHandler: JwtHandler = JwtFixture.create()
|
||||||
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;
|
|
||||||
|
|
||||||
@SpringBootTest
|
val authService = AuthService(memberService, jwtHandler)
|
||||||
@Import({AuthService.class, JwtHandler.class, MemberService.class})
|
val user: Member = MemberFixture.user()
|
||||||
class AuthServiceTest {
|
|
||||||
|
|
||||||
@Autowired
|
Given("로그인 요청을 받으면") {
|
||||||
private AuthService authService;
|
When("이메일과 비밀번호로 회원을 찾고") {
|
||||||
@Autowired
|
val request = MemberFixture.userLoginRequest()
|
||||||
private MemberRepository memberRepository;
|
|
||||||
|
|
||||||
@Test
|
Then("회원이 있다면 JWT 토큰을 생성한 뒤 반환한다.") {
|
||||||
@DisplayName("로그인 성공시 JWT accessToken 을 반환한다.")
|
every {
|
||||||
void loginSuccess() {
|
memberRepository.findByEmailAndPassword(request.email, request.password)
|
||||||
// given
|
} returns user
|
||||||
Member member = memberRepository.save(new Member(null, "이름", "test@test.com", "12341234", Role.MEMBER));
|
|
||||||
|
|
||||||
// when
|
val accessToken: String = authService.login(request).accessToken
|
||||||
TokenDto response = authService.login(new LoginRequest(member.getEmail(), member.getPassword()));
|
|
||||||
|
|
||||||
// then
|
accessToken.isNotBlank() shouldBe true
|
||||||
assertThat(response.accessToken()).isNotNull();
|
jwtHandler.getMemberIdFromToken(accessToken) shouldBe user.id
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
Then("회원이 없다면 예외를 던진다.") {
|
||||||
@DisplayName("존재하지 않는 회원 email 또는 password로 로그인하면 예외가 발생한다.")
|
every {
|
||||||
void loginFailByNotExistMemberInfo() {
|
memberRepository.findByEmailAndPassword(request.email, request.password)
|
||||||
// given
|
} returns null
|
||||||
String notExistEmail = "invalid@test.com";
|
|
||||||
String notExistPassword = "invalid1234";
|
|
||||||
|
|
||||||
// when & then
|
val exception = shouldThrow<RoomEscapeException> {
|
||||||
Assertions.assertThatThrownBy(() -> authService.login(new LoginRequest(notExistEmail, notExistPassword)))
|
authService.login(request)
|
||||||
.isInstanceOf(RoomEscapeException.class);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
exception.errorType shouldBe ErrorType.MEMBER_NOT_FOUND
|
||||||
@DisplayName("존재하지 않는 회원의 memberId로 로그인 여부를 체크하면 예외가 발생한다.")
|
}
|
||||||
void checkLoginFailByNotExistMemberInfo() {
|
}
|
||||||
// given
|
}
|
||||||
Long notExistMemberId = (long)(memberRepository.findAll().size() + 1);
|
|
||||||
|
|
||||||
// when & then
|
Given("로그인 확인 요청을 받으면") {
|
||||||
Assertions.assertThatThrownBy(() -> authService.checkLogin(notExistMemberId))
|
When("회원 ID로 회원을 찾고") {
|
||||||
.isInstanceOf(RoomEscapeException.class);
|
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