|
| 1 | +package uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions |
| 2 | + |
| 3 | +import jakarta.servlet.FilterChain |
| 4 | +import jakarta.servlet.http.HttpServletRequest |
| 5 | +import jakarta.servlet.http.HttpServletResponse |
| 6 | +import org.junit.jupiter.api.Test |
| 7 | +import org.mockito.Mockito.mock |
| 8 | +import org.mockito.Mockito.times |
| 9 | +import org.mockito.Mockito.verify |
| 10 | +import org.mockito.kotlin.whenever |
| 11 | + |
| 12 | +class ConsumerNameExtractionFilterTest { |
| 13 | + private var consumerNameExtractionFilter: ConsumerNameExtractionFilter = ConsumerNameExtractionFilter() |
| 14 | + |
| 15 | + @Test |
| 16 | + fun `calls the onward chain`() { |
| 17 | + // Arrange |
| 18 | + val mockRequest = mock(HttpServletRequest::class.java) |
| 19 | + whenever(mockRequest.getAttribute("clientName")).thenReturn("consumer-name") |
| 20 | + |
| 21 | + val mockResponse = mock(HttpServletResponse::class.java) |
| 22 | + val mockChain = mock(FilterChain::class.java) |
| 23 | + |
| 24 | + // Act |
| 25 | + consumerNameExtractionFilter.doFilter(mockRequest, mockResponse, mockChain) |
| 26 | + |
| 27 | + // Assert |
| 28 | + verify(mockChain, times(1)).doFilter(mockRequest, mockResponse) |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun `can get subject distinguished name from request and set as consumer name `() { |
| 33 | + // Arrange |
| 34 | + val mockRequest = mock(HttpServletRequest::class.java) |
| 35 | + whenever(mockRequest.getHeader("subject-distinguished-name")).thenReturn(",CN=consumer-name") |
| 36 | + |
| 37 | + val mockResponse = mock(HttpServletResponse::class.java) |
| 38 | + val mockChain = mock(FilterChain::class.java) |
| 39 | + // Act |
| 40 | + consumerNameExtractionFilter.doFilter(mockRequest, mockResponse, mockChain) |
| 41 | + |
| 42 | + // Assert |
| 43 | + verify(mockRequest, times(1)).setAttribute("clientName", "consumer-name") |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `does not set a clientName from request if it does not match the regex `() { |
| 48 | + // Arrange |
| 49 | + val mockRequest = mock(HttpServletRequest::class.java) |
| 50 | + whenever(mockRequest.getHeader("subject-distinguished-name")).thenReturn("CN=consumer-name") |
| 51 | + |
| 52 | + val mockResponse = mock(HttpServletResponse::class.java) |
| 53 | + val mockChain = mock(FilterChain::class.java) |
| 54 | + // Act |
| 55 | + consumerNameExtractionFilter.doFilter(mockRequest, mockResponse, mockChain) |
| 56 | + |
| 57 | + // Assert |
| 58 | + verify(mockRequest, times(1)).setAttribute("clientName", null) |
| 59 | + } |
| 60 | +} |
0 commit comments