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