generated from pricelees/issue-pr-template
refactor: 기존 테스트에 변경된 인증 API 반영
This commit is contained in:
parent
498e8c8e75
commit
9b13448abd
@ -10,18 +10,20 @@ import io.restassured.response.ValidatableResponse
|
||||
import org.hamcrest.CoreMatchers.equalTo
|
||||
import org.springframework.http.HttpStatus
|
||||
import roomescape.admin.exception.AdminErrorCode
|
||||
import roomescape.auth.business.CLAIM_ADMIN_TYPE_KEY
|
||||
import roomescape.auth.business.CLAIM_PERMISSION_KEY
|
||||
import roomescape.auth.business.CLAIM_STORE_ID_KEY
|
||||
import roomescape.auth.exception.AuthErrorCode
|
||||
import roomescape.auth.infrastructure.jwt.JwtUtils
|
||||
import roomescape.auth.infrastructure.persistence.LoginHistoryRepository
|
||||
import roomescape.auth.web.LoginRequest
|
||||
import roomescape.common.dto.PrincipalType
|
||||
import roomescape.user.exception.UserErrorCode
|
||||
import roomescape.user.infrastructure.persistence.UserEntity
|
||||
import roomescape.supports.AdminFixture
|
||||
import roomescape.supports.FunSpecSpringbootTest
|
||||
import roomescape.supports.UserFixture
|
||||
import roomescape.supports.runTest
|
||||
import roomescape.user.exception.UserErrorCode
|
||||
import roomescape.user.infrastructure.persistence.UserEntity
|
||||
|
||||
class AuthApiTest(
|
||||
@SpykBean private val jwtUtils: JwtUtils,
|
||||
@ -31,17 +33,25 @@ class AuthApiTest(
|
||||
init {
|
||||
context("로그인을 시도한다.") {
|
||||
context("성공 응답") {
|
||||
test("관리자") {
|
||||
val admin = authUtil.createAdmin(AdminFixture.default)
|
||||
runLoginSuccessTest(
|
||||
id = admin.id,
|
||||
account = admin.account,
|
||||
password = admin.password,
|
||||
type = PrincipalType.ADMIN,
|
||||
) {
|
||||
val token: String = it.extract().path("data.accessToken")
|
||||
jwtUtils.extractSubject(token) shouldBe admin.id.toString()
|
||||
jwtUtils.extractClaim(token, CLAIM_PERMISSION_KEY) shouldBe admin.permissionLevel.name
|
||||
listOf(
|
||||
AdminFixture.storeDefault,
|
||||
AdminFixture.hqDefault
|
||||
).forEach {
|
||||
test("${it.type} 타입 관리자") {
|
||||
val admin = authUtil.createAdmin(it)
|
||||
|
||||
runLoginSuccessTest(
|
||||
id = admin.id,
|
||||
account = admin.account,
|
||||
password = admin.password,
|
||||
type = PrincipalType.ADMIN,
|
||||
) {
|
||||
val token: String = it.extract().path("data.accessToken")
|
||||
jwtUtils.extractSubject(token) shouldBe admin.id.toString()
|
||||
jwtUtils.extractClaim(token, CLAIM_STORE_ID_KEY) shouldBe admin.storeId?.toString()
|
||||
jwtUtils.extractClaim(token, CLAIM_ADMIN_TYPE_KEY) shouldBe admin.type.name
|
||||
jwtUtils.extractClaim(token, CLAIM_PERMISSION_KEY) shouldBe admin.permissionLevel.name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,52 +71,54 @@ class AuthApiTest(
|
||||
}
|
||||
|
||||
context("실패 응답") {
|
||||
test("비밀번호가 틀린 경우") {
|
||||
val admin = authUtil.createAdmin(AdminFixture.default)
|
||||
val request = LoginRequest(admin.account, "wrong_password", PrincipalType.ADMIN)
|
||||
context("계정이 맞으면 로그인 실패 이력을 남긴다.") {
|
||||
test("비밀번호가 틀린 경우") {
|
||||
val admin = authUtil.createAdmin(AdminFixture.default)
|
||||
val request = LoginRequest(admin.account, "wrong_password", PrincipalType.ADMIN)
|
||||
|
||||
runTest(
|
||||
using = {
|
||||
body(request)
|
||||
},
|
||||
on = {
|
||||
post("/auth/login")
|
||||
},
|
||||
expect = {
|
||||
statusCode(HttpStatus.UNAUTHORIZED.value())
|
||||
body("code", equalTo(AuthErrorCode.LOGIN_FAILED.errorCode))
|
||||
}
|
||||
).also {
|
||||
assertSoftly(loginHistoryRepository.findByPrincipalId(admin.id)[0]) {
|
||||
it.success shouldBe false
|
||||
it.principalType shouldBe PrincipalType.ADMIN
|
||||
runTest(
|
||||
using = {
|
||||
body(request)
|
||||
},
|
||||
on = {
|
||||
post("/auth/login")
|
||||
},
|
||||
expect = {
|
||||
statusCode(HttpStatus.UNAUTHORIZED.value())
|
||||
body("code", equalTo(AuthErrorCode.LOGIN_FAILED.errorCode))
|
||||
}
|
||||
).also {
|
||||
assertSoftly(loginHistoryRepository.findByPrincipalId(admin.id)[0]) {
|
||||
it.success shouldBe false
|
||||
it.principalType shouldBe PrincipalType.ADMIN
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test("토큰 생성 과정에서 오류가 발생하는 경우") {
|
||||
val admin = authUtil.createAdmin(AdminFixture.default)
|
||||
val request = LoginRequest(admin.account, admin.password, PrincipalType.ADMIN)
|
||||
test("토큰 생성 과정에서 오류가 발생하는 경우") {
|
||||
val admin = authUtil.createAdmin(AdminFixture.default)
|
||||
val request = LoginRequest(admin.account, admin.password, PrincipalType.ADMIN)
|
||||
|
||||
every {
|
||||
jwtUtils.createToken(any(), any())
|
||||
} throws RuntimeException("토큰 생성 실패")
|
||||
every {
|
||||
jwtUtils.createToken(any(), any())
|
||||
} throws RuntimeException("토큰 생성 실패")
|
||||
|
||||
runTest(
|
||||
using = {
|
||||
body(request)
|
||||
},
|
||||
on = {
|
||||
post("/auth/login")
|
||||
},
|
||||
expect = {
|
||||
statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
|
||||
body("code", equalTo(AuthErrorCode.TEMPORARY_AUTH_ERROR.errorCode))
|
||||
}
|
||||
).also {
|
||||
assertSoftly(loginHistoryRepository.findByPrincipalId(admin.id)[0]) {
|
||||
it.success shouldBe false
|
||||
it.principalType shouldBe PrincipalType.ADMIN
|
||||
runTest(
|
||||
using = {
|
||||
body(request)
|
||||
},
|
||||
on = {
|
||||
post("/auth/login")
|
||||
},
|
||||
expect = {
|
||||
statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
|
||||
body("code", equalTo(AuthErrorCode.TEMPORARY_AUTH_ERROR.errorCode))
|
||||
}
|
||||
).also {
|
||||
assertSoftly(loginHistoryRepository.findByPrincipalId(admin.id)[0]) {
|
||||
it.success shouldBe false
|
||||
it.principalType shouldBe PrincipalType.ADMIN
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -162,39 +174,6 @@ class AuthApiTest(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
context("로그인 상태를 확인한다.") {
|
||||
test("성공 응답") {
|
||||
val token = authUtil.defaultUserLogin()
|
||||
|
||||
runTest(
|
||||
token = token,
|
||||
on = {
|
||||
get("/auth/login/check")
|
||||
},
|
||||
expect = {
|
||||
statusCode(HttpStatus.OK.value())
|
||||
}
|
||||
).also {
|
||||
val name: String = it.extract().path("data.name")
|
||||
val type: String = it.extract().path("data.type")
|
||||
|
||||
name.isBlank() shouldBe false
|
||||
type shouldBe PrincipalType.USER.name
|
||||
}
|
||||
}
|
||||
|
||||
test("로그인 상태가 아니면 실패한다.") {
|
||||
runTest(
|
||||
on = {
|
||||
get("/auth/login/check")
|
||||
},
|
||||
expect = {
|
||||
statusCode(HttpStatus.UNAUTHORIZED.value())
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun runLoginSuccessTest(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user