forked from Brain-up/brn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
103 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/com/epam/brn/auth/filter/RememberLastVisitFilter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.epam.brn.auth.filter | ||
|
||
import com.epam.brn.service.UserAccountService | ||
import org.apache.logging.log4j.kotlin.logger | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.filter.OncePerRequestFilter | ||
import javax.servlet.FilterChain | ||
import javax.servlet.http.HttpServletRequest | ||
import javax.servlet.http.HttpServletResponse | ||
|
||
@Component | ||
class RememberLastVisitFilter( | ||
private val userAccountService: UserAccountService, | ||
) : OncePerRequestFilter() { | ||
|
||
private val log = logger() | ||
|
||
override fun doFilterInternal( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
filterChain: FilterChain | ||
) { | ||
markVisit() | ||
filterChain.doFilter(request, response) | ||
} | ||
|
||
private fun markVisit() { | ||
try { | ||
if (SecurityContextHolder.getContext().authentication != null) userAccountService.markVisitForCurrentUser() | ||
} catch (e: Exception) { | ||
log.error("Error: ${e.message}", e) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/test/kotlin/com/epam/brn/auth/filter/RememberLastVisitFilterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.epam.brn.auth.filter | ||
|
||
import com.epam.brn.service.UserAccountService | ||
import io.mockk.impl.annotations.InjectMockKs | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.junit5.MockKExtension | ||
import io.mockk.justRun | ||
import io.mockk.verify | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
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.core.context.SecurityContextHolder | ||
import javax.servlet.FilterChain | ||
|
||
@ExtendWith(MockKExtension::class) | ||
@DisplayName("RememberLastVisitFilter test using MockK") | ||
internal class RememberLastVisitFilterTest { | ||
|
||
@InjectMockKs | ||
lateinit var rememberLastVisitFilter: RememberLastVisitFilter | ||
|
||
@MockK | ||
lateinit var userAccountService: UserAccountService | ||
|
||
@BeforeEach | ||
fun init() { | ||
SecurityContextHolder.clearContext() | ||
} | ||
|
||
@Test | ||
fun `should mark visit for current user`() { | ||
// 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 = 1) { userAccountService.markVisitForCurrentUser() } | ||
} | ||
} |