Skip to content

Commit

Permalink
[KAN-107] 필터 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkyoungdeok committed May 30, 2024
1 parent d0f0ce3 commit f09cffc
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
12 changes: 12 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,15 @@ tasks {
tasks.jar { enabled = false }

tasks.bootJar { enabled = true }

koverReport {
filters {
excludes {
classes(
"com.restaurant.be.common.exception.GlobalExceptionHandler",
"com.restaurant.be.common.jwt.JwtFilter",
"com.restaurant.be.common.jwt.TokenProvider",
)
}
}
}
100 changes: 100 additions & 0 deletions src/test/kotlin/com/restaurant/be/common/jwt/JwtFilterTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.restaurant.be.common.jwt

import com.restaurant.be.common.response.Token
import io.kotest.core.spec.style.DescribeSpec
import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import java.security.SignatureException
import java.util.Date
import javax.servlet.FilterChain
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.springframework.security.core.Authentication
import org.springframework.security.core.context.SecurityContext
import org.springframework.security.core.context.SecurityContextHolder

class JwtFilterTest : DescribeSpec({

val tokenProvider = mockk<TokenProvider>()
val jwtUserRepository = mockk<JwtUserRepository>()
val request = mockk<HttpServletRequest>(relaxed = true)
val response = mockk<HttpServletResponse>(relaxed = true)
val filterChain = mockk<FilterChain>(relaxed = true)
val jwtFilter = JwtFilter(tokenProvider, jwtUserRepository)

beforeEach {
clearAllMocks()
val securityContext = mockk<SecurityContext>()
SecurityContextHolder.setContext(securityContext)
}

describe("doFilterInternal") {

it("should set authentication when access token is valid and user is valid") {
val accessToken = "validAccessToken"
every { request.getHeader(JwtFilter.AUTHORIZATION_HEADER) } returns accessToken
every { request.getHeader(JwtFilter.REFRESH_TOKEN_HEADER) } returns null
every { tokenProvider.resolveToken(accessToken) } returns accessToken
every { tokenProvider.validateToken(accessToken) } returns true
every { tokenProvider.getEmailFromToken(accessToken) } returns "[email protected]"
every { jwtUserRepository.validTokenByEmail("[email protected]") } returns true
val authentication = mockk<Authentication>()
every { tokenProvider.getAuthentication(accessToken) } returns authentication

jwtFilter.doFilterInternal(request, response, filterChain)

verify { SecurityContextHolder.getContext().authentication = authentication }
verify { filterChain.doFilter(request, response) }
}

it("should not set authentication when access token is invalid") {
val accessToken = "invalidAccessToken"
every { request.getHeader(JwtFilter.AUTHORIZATION_HEADER) } returns accessToken
every { request.getHeader(JwtFilter.REFRESH_TOKEN_HEADER) } returns null
every { tokenProvider.resolveToken(accessToken) } returns accessToken
every { tokenProvider.validateToken(accessToken) } returns false

jwtFilter.doFilterInternal(request, response, filterChain)

verify { request.setAttribute("exception", any<SignatureException>()) }
verify { filterChain.doFilter(request, response) }
}

it("should reissue token when refresh token is present") {
val refreshToken = "validRefreshToken"
val accessToken = "oldAccessToken"
every { request.getHeader(JwtFilter.AUTHORIZATION_HEADER) } returns accessToken
every { request.getHeader(JwtFilter.REFRESH_TOKEN_HEADER) } returns refreshToken
every { tokenProvider.resolveToken(accessToken) } returns accessToken
every { tokenProvider.resolveToken(refreshToken) } returns refreshToken
every { tokenProvider.validateToken(refreshToken) } returns true
every { tokenProvider.tokenReissue(accessToken, refreshToken) } returns Token(
"newAccessToken",
"newRefreshToken",
"Bearer",
1000,
Date.from(Date().toInstant())
)
every { response.getHeader(JwtFilter.AUTHORIZATION_HEADER) } returns null

jwtFilter.doFilterInternal(request, response, filterChain)

verify { response.addHeader(JwtFilter.AUTHORIZATION_HEADER, "newAccessToken") }
verify { filterChain.doFilter(request, response) }
}

it("should log and return when access token is missing but refresh token is present") {
val refreshToken = "validRefreshToken"
every { request.getHeader(JwtFilter.AUTHORIZATION_HEADER) } returns null
every { request.getHeader(JwtFilter.REFRESH_TOKEN_HEADER) } returns refreshToken
every { tokenProvider.resolveToken(refreshToken) } returns refreshToken
every { tokenProvider.validateToken(refreshToken) } returns true

jwtFilter.doFilterInternal(request, response, filterChain)

verify(exactly = 0) { filterChain.doFilter(request, response) }
}
}
})

0 comments on commit f09cffc

Please sign in to comment.