Skip to content

Commit

Permalink
#2406 fix errors in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
naXa777 committed Oct 24, 2023
1 parent d428f58 commit a3a04f0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import io.mockk.every
import io.mockk.impl.annotations.InjectMockKs
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import io.mockk.justRun
import io.mockk.mockk
import io.mockk.verify
import org.amshove.kluent.shouldBeInstanceOf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.http.HttpMethod
import org.springframework.mock.web.MockHttpServletRequest
import org.springframework.mock.web.MockHttpServletResponse
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.context.SecurityContextHolder
import javax.servlet.FilterChain

Expand All @@ -26,6 +27,9 @@ internal class RememberLastVisitFilterTest {
@MockK
lateinit var userAccountService: UserAccountService

@MockK
lateinit var authentication: UsernamePasswordAuthenticationToken

@BeforeEach
fun init() {
SecurityContextHolder.clearContext()
Expand All @@ -39,6 +43,7 @@ internal class RememberLastVisitFilterTest {
requestMock.addHeader("Authorization", "Bearer $tokenMock")
val responseMock = MockHttpServletResponse()
val filterChain = FilterChain { _, _ -> }
SecurityContextHolder.getContext().authentication = authentication

justRun { userAccountService.markVisitForCurrentUser() }

Expand All @@ -48,4 +53,22 @@ internal class RememberLastVisitFilterTest {
// THEN
verify(exactly = 1) { userAccountService.markVisitForCurrentUser() }
}

@Test
fun `should not mark visit when request is anonymous`() {
// GIVEN
val requestMock = MockHttpServletRequest(HttpMethod.GET.name, "/test")
val tokenMock = "firebaseTokenMock"
requestMock.addHeader("Authorization", "Bearer $tokenMock")
val responseMock = MockHttpServletResponse()
val filterChain = FilterChain { _, _ -> }

justRun { userAccountService.markVisitForCurrentUser() }

// WHEN
rememberLastVisitFilter.doFilter(requestMock, responseMock, filterChain)

// THEN
verify(exactly = 0) { userAccountService.markVisitForCurrentUser() }
}
}

0 comments on commit a3a04f0

Please sign in to comment.