Skip to content

Commit

Permalink
Fikset tester, setter userId første request også hvis userId mangler
Browse files Browse the repository at this point in the history
  • Loading branch information
blommish committed Jan 21, 2025
1 parent 62b2ec1 commit e176f14
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
8 changes: 4 additions & 4 deletions log/main/no/nav/tilleggsstonader/libs/log/filter/LogFilter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ class LogFilter(val sporBruker: Boolean) : HttpFilter() {
if (!sporBruker) {
return
}
val userId = resolveUserId(request)
var userId = resolveUserId(request)
if (userId.isNullOrEmpty()) {
// user-id tracking only works if the client is stateful and supports cookies.
// if no user-id is found, generate one for any following requests but do not use it on the
// current request to avoid generating large numbers of useless user-ids.
generateUserIdCookie(response)
userId = IdUtils.generateId()
generateUserIdCookie(response, userId)
}
MDC.put(MDC_USER_ID, userId)
}
Expand Down Expand Up @@ -95,8 +96,7 @@ class LogFilter(val sporBruker: Boolean) : HttpFilter() {
?: IdUtils.generateId()
}

private fun generateUserIdCookie(response: HttpServletResponse) {
val userId = IdUtils.generateId()
private fun generateUserIdCookie(response: HttpServletResponse, userId: String) {
val cookie = Cookie(RANDOM_USER_ID_COOKIE_NAME, userId).apply {
path = "/"
maxAge = ONE_MONTH_IN_SECONDS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.mockk.mockk
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import no.nav.tilleggsstonader.libs.log.NavHttpHeaders
import no.nav.tilleggsstonader.libs.log.mdc.MDCConstants
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand All @@ -14,7 +15,7 @@ class LogFilterTest {

private lateinit var httpServletRequest: HttpServletRequest
private lateinit var httpServletResponse: HttpServletResponse
private val logFilter = LogFilter()
private val logFilter = LogFilter(true)

@BeforeEach
fun setup() {
Expand Down Expand Up @@ -52,6 +53,22 @@ class LogFilterTest {
.isEqualTo(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)
}

@Test
fun `skal ikke logge bruker hvis sporBruker=true`() {
logFilter.doFilter(httpServletRequest, httpServletResponse) { _, _ ->
assertThat(MDC.get(MDCConstants.MDC_USER_ID)).isNotNull()
}
}

@Test
fun `skal ikke logge bruker hvis sporBruker=false`() {
val logFilter = LogFilter(sporBruker = false)

logFilter.doFilter(httpServletRequest, httpServletResponse) { _, _ ->
assertThat(MDC.get(MDCConstants.MDC_USER_ID)).isNull()
}
}

private val mockHttpServletRequest = mockk<HttpServletRequest>(relaxed = true).also {
every { it.method } returns "GET"
every { it.requestURI } returns "/test/path"
Expand Down

0 comments on commit e176f14

Please sign in to comment.