Skip to content

Commit

Permalink
test: 회원 삭제 controller 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Combi153 committed Feb 27, 2024
1 parent f79d065 commit 361f815
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class AuthController(
@Operation(summary = "회원 탈퇴 API", description = "회원 탈퇴를 합니다")
@ApiResponse(responseCode = "204", description = "회원 탈퇴 성공")
@SecurityRequirement(name = ACCESS_TOKEN_SECURITY_SCHEME_KEY)
@DeleteMapping
@DeleteMapping("/members")
fun delete(
@Auth loginMember: LoginMember
): ResponseEntity<Unit> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.petqua.presentation.auth

import io.restassured.module.kotlin.extensions.Extract
import io.restassured.module.kotlin.extensions.Given
import io.restassured.module.kotlin.extensions.Then
import io.restassured.module.kotlin.extensions.When
import io.restassured.response.Response

fun requestLogin(
code: String
): Response {
return Given {
log().all()
} When {
queryParam("code", code)
get("/auth/login/kakao")
} Then {
log().all()
} Extract {
response()
}
}

fun requestExtendLogin(
accessToken: String,
refreshToken: String,
): Response {
return Given {
log().all()
auth().preemptive().oauth2(accessToken)
cookie("refresh-token", refreshToken)
} When {
get("/auth/token")
} Then {
log().all()
} Extract {
response()
}
}

fun requestDeleteMember(
accessToken: String
): Response {
return Given {
log().all()
auth().preemptive().oauth2(accessToken)
} When {
delete("/auth/members")
} Then {
log().all()
} Extract {
response()
}
}
69 changes: 44 additions & 25 deletions src/test/kotlin/com/petqua/presentation/auth/AuthControllerTest.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package com.petqua.presentation.auth

import com.ninjasquad.springmockk.SpykBean
import com.petqua.application.auth.OauthService
import com.petqua.common.domain.findByIdOrThrow
import com.petqua.domain.auth.oauth.OauthServerType
import com.petqua.domain.auth.token.AuthTokenProvider
import com.petqua.domain.auth.token.RefreshToken
import com.petqua.domain.auth.token.RefreshTokenRepository
import com.petqua.domain.member.MemberRepository
import com.petqua.test.ApiTestConfig
import com.petqua.test.config.OauthTestConfig
import com.petqua.test.fixture.member
import io.kotest.assertions.assertSoftly
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import io.restassured.module.kotlin.extensions.Extract
import io.restassured.module.kotlin.extensions.Given
import io.restassured.module.kotlin.extensions.Then
import io.restassured.module.kotlin.extensions.When
import io.mockk.verify
import org.springframework.context.annotation.Import
import org.springframework.http.HttpHeaders.AUTHORIZATION
import org.springframework.http.HttpHeaders.SET_COOKIE
import org.springframework.http.HttpStatus.NO_CONTENT
import org.springframework.http.HttpStatus.OK
import java.util.Date

Expand All @@ -24,22 +27,14 @@ class AuthControllerTest(
private val memberRepository: MemberRepository,
private val refreshTokenRepository: RefreshTokenRepository,
private val authTokenProvider: AuthTokenProvider,
@SpykBean private val oauthService: OauthService,
) : ApiTestConfig() {

init {
Given("소셜 로그인을 할 때") {

When("카카오 로그인을 시도하면") {
val response = Given {
log().all()
} When {
queryParam("code", "accessCode")
get("/auth/login/kakao")
} Then {
log().all()
} Extract {
response()
}
val response = requestLogin(code = "accessCode")

Then("인증토큰이 반환된다.") {
val headers = response.headers()
Expand All @@ -63,17 +58,10 @@ class AuthControllerTest(
)

When("요청하면") {
val response = Given {
log().all()
auth().preemptive().oauth2(expiredAccessToken)
cookie("refresh-token", refreshToken)
} When {
get("/auth/token")
} Then {
log().all()
} Extract {
response()
}
val response = requestExtendLogin(
accessToken = expiredAccessToken,
refreshToken = refreshToken
)

Then("인증토큰이 반환된다.") {
val headers = response.headers()
Expand All @@ -84,5 +72,36 @@ class AuthControllerTest(
}
}
}

Given("회원 탈퇴를 요청할 때") {
val accessToken = signInAsMember().accessToken
val memberId = getMemberIdByAccessToken(accessToken)

When("유효한 액세스 토큰으로 회원이 요청하면") {
val response = requestDeleteMember(accessToken)

Then("회원이 삭제된다") {
val deletedMember = memberRepository.findByIdOrThrow(memberId)

assertSoftly(deletedMember) {
response.statusCode shouldBe NO_CONTENT.value()

it.isDeleted shouldBe true
it.oauthId shouldBe -1L
it.nickname shouldBe ""
it.profileImageUrl shouldBe null
it.oauthAccessToken shouldBe ""
it.expireAt shouldBe null
it.oauthRefreshToken shouldBe ""
}
}

Then("oauth 서버에 회원 정보 삭제를 요청한다") {
verify(exactly = 1) {
oauthService.disconnectBy(any(OauthServerType::class), any(String::class))
}
}
}
}
}
}

0 comments on commit 361f815

Please sign in to comment.