|
| 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 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.config.AuthorisationConfig |
| 12 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.roleconfig.ConsumerConfig |
| 13 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.roleconfig.ConsumerFilters |
| 14 | + |
| 15 | +class AuthorisationFilterTest { |
| 16 | + private var authorisationConfig: AuthorisationConfig = AuthorisationConfig() |
| 17 | + private var authorisationFilter: AuthorisationFilter = AuthorisationFilter(authorisationConfig) |
| 18 | + private var examplePath: String = "/v1/persons" |
| 19 | + private var exampleConsumer: String = "consumer-name" |
| 20 | + |
| 21 | + @Test |
| 22 | + fun `calls the onward chain`() { |
| 23 | + // Arrange |
| 24 | + val mockRequest = mock(HttpServletRequest::class.java) |
| 25 | + whenever(mockRequest.requestURI).thenReturn(examplePath) |
| 26 | + whenever(mockRequest.getAttribute("clientName")).thenReturn(exampleConsumer) |
| 27 | + val mockResponse = mock(HttpServletResponse::class.java) |
| 28 | + val mockChain = mock(FilterChain::class.java) |
| 29 | + |
| 30 | + authorisationConfig.consumers = mapOf(exampleConsumer to ConsumerConfig(include = listOf(examplePath), filters = ConsumerFilters(emptyMap()))) |
| 31 | + |
| 32 | + // Act |
| 33 | + authorisationFilter.doFilter(mockRequest, mockResponse, mockChain) |
| 34 | + |
| 35 | + // Assert |
| 36 | + verify(mockChain, times(1)).doFilter(mockRequest, mockResponse) |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun `generates error when consumer is unauthorised for requested path`() { |
| 41 | + val mockRequest = mock(HttpServletRequest::class.java) |
| 42 | + whenever(mockRequest.requestURI).thenReturn(examplePath) |
| 43 | + whenever(mockRequest.getAttribute("clientName")).thenReturn(exampleConsumer) |
| 44 | + val mockResponse = mock(HttpServletResponse::class.java) |
| 45 | + val mockChain = mock(FilterChain::class.java) |
| 46 | + |
| 47 | + authorisationConfig.consumers = mapOf(exampleConsumer to ConsumerConfig(include = emptyList(), filters = ConsumerFilters(emptyMap()))) |
| 48 | + |
| 49 | + val authorisationFilter = AuthorisationFilter(authorisationConfig) |
| 50 | + |
| 51 | + // Act |
| 52 | + authorisationFilter.doFilter(mockRequest, mockResponse, mockChain) |
| 53 | + |
| 54 | + // Assert |
| 55 | + verify(mockResponse, times(1)).sendError(403, "Unable to authorise /v1/persons for consumer-name") |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `generates error when subject distinguished name is null in the request`() { |
| 60 | + val mockRequest = mock(HttpServletRequest::class.java) |
| 61 | + whenever(mockRequest.requestURI).thenReturn(examplePath) |
| 62 | + whenever(mockRequest.getAttribute("clientName")).thenReturn(null) |
| 63 | + val mockResponse = mock(HttpServletResponse::class.java) |
| 64 | + val mockChain = mock(FilterChain::class.java) |
| 65 | + |
| 66 | + authorisationFilter.doFilter(mockRequest, mockResponse, mockChain) |
| 67 | + |
| 68 | + verify(mockResponse, times(1)).sendError(403, "No subject-distinguished-name header provided for authorisation") |
| 69 | + } |
| 70 | +} |
0 commit comments