Skip to content

Commit b9d52c1

Browse files
committed
Add test for empty list of prisons has no access.
1 parent 70f58de commit b9d52c1

File tree

7 files changed

+30
-16
lines changed

7 files changed

+30
-16
lines changed

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/prison/PrisonController.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class PrisonController(
5454
)
5555
fun getPerson(
5656
@PathVariable hmppsId: String,
57-
@RequestAttribute filters: ConsumerFilters?
57+
@RequestAttribute filters: ConsumerFilters?,
5858
): DataResponse<Person?> {
5959
val response = getPersonService.getPrisoner(hmppsId, filters)
6060

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/roleconfig/ConsumerFilters.kt

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.roleconfig
22

33
data class ConsumerFilters(
4-
val prisons: List<String>?
4+
val prisons: List<String>?,
55
) {
6-
fun matchesPrison(
7-
prisonId: String?,
8-
): Boolean = matchesFilterList(prisons, prisonId)
6+
fun matchesPrison(prisonId: String?): Boolean = matchesFilterList(prisons, prisonId)
97

10-
private fun matchesFilterList(filterList: List<String>?, value: String?): Boolean {
8+
private fun matchesFilterList(
9+
filterList: List<String>?,
10+
value: String?,
11+
): Boolean {
1112
if (filterList == null) {
1213
return true
1314
}

src/main/resources/application-integration-test.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,8 @@ authorisation:
9999
filters:
100100
prisons:
101101
- ABC
102-
# TODO: Some sort of test that prisons: with no list blocks all prisons
102+
no-prisons:
103+
include:
104+
- "/v1/prison/prisoners/[^/]*$"
105+
filters:
106+
prisons:

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/prison/PrisonControllerTest.kt

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1.prison
33
import io.kotest.core.spec.style.DescribeSpec
44
import io.kotest.matchers.shouldBe
55
import org.mockito.Mockito
6-
import org.mockito.kotlin.any
76
import org.mockito.kotlin.anyOrNull
87
import org.mockito.kotlin.eq
98
import org.mockito.kotlin.times

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/extensions/FiltersExtractionFilterTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class FiltersExtractionFilterTest {
4343
val mockResponse = mock(HttpServletResponse::class.java)
4444
val mockChain = mock(FilterChain::class.java)
4545

46-
val expectedFilters = ConsumerFilters(prisons=listOf("filter-1", "filter-2"))
46+
val expectedFilters = ConsumerFilters(prisons = listOf("filter-1", "filter-2"))
4747
authorisationConfig.consumers = mapOf("consumer-name" to ConsumerConfig(include = null, filters = expectedFilters))
4848

4949
// Act
@@ -62,7 +62,7 @@ class FiltersExtractionFilterTest {
6262
val mockResponse = mock(HttpServletResponse::class.java)
6363
val mockChain = mock(FilterChain::class.java)
6464

65-
val expectedFilters = ConsumerFilters(prisons=listOf("filter-1", "filter-2"))
65+
val expectedFilters = ConsumerFilters(prisons = listOf("filter-1", "filter-2"))
6666
authorisationConfig.consumers = mapOf("consumer-name" to ConsumerConfig(include = null, filters = expectedFilters))
6767

6868
// Act

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/integration/prison/PrisonIntegrationTest.kt

+10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ class PrisonIntegrationTest : IntegrationTestBase() {
3131
.andExpect(status().isNotFound)
3232
}
3333

34+
@Test
35+
fun `return a 404 for if consumer has empty list of prisons`() {
36+
val headers = HttpHeaders()
37+
headers.set("subject-distinguished-name", "C=GB,ST=London,L=London,O=Home Office,CN=no-prisons")
38+
mockMvc.perform(
39+
get("$basePrisonPath/prisoners/$hmppsId").headers(headers),
40+
)
41+
.andExpect(status().isNotFound)
42+
}
43+
3444
@Test
3545
fun `return multiple prisoners when querying by complex parameters`() {
3646
callApi("$basePrisonPath/prisoners?first_name=$firstName&last_name=$lastName&dateOfBirth=$dateOfBirth")

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetPersonServiceTest.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal class GetPersonServiceTest(
4040

4141
val nomsNumber = "N1234PS"
4242
val prisoner = POSPrisoner(firstName = "Jim", lastName = "Brown", dateOfBirth = LocalDate.of(1992, 12, 3), prisonerNumber = nomsNumber)
43-
val blankConsumerFilters = ConsumerFilters(prisons=null)
43+
val blankConsumerFilters = ConsumerFilters(prisons = null)
4444

4545
beforeEach {
4646
Mockito.reset(prisonerOffenderSearchGateway)
@@ -197,7 +197,7 @@ internal class GetPersonServiceTest(
197197
whenever(prisonerOffenderSearchGateway.getPrisonOffender(wrongPrisonHmppsId))
198198
.thenReturn(Response(data = POSPrisoner(firstName = "Test", lastName = "Person", prisonId = "XYZ")))
199199

200-
val result = getPersonService.getPrisoner(wrongPrisonHmppsId, ConsumerFilters(prisons=listOf("ABC")))
200+
val result = getPersonService.getPrisoner(wrongPrisonHmppsId, ConsumerFilters(prisons = listOf("ABC")))
201201

202202
result.data.shouldBe(null)
203203
result.errors.shouldBe(listOf(UpstreamApiError(UpstreamApi.PRISONER_OFFENDER_SEARCH, UpstreamApiError.Type.ENTITY_NOT_FOUND, "Not found")))
@@ -210,7 +210,7 @@ internal class GetPersonServiceTest(
210210
whenever(prisonerOffenderSearchGateway.getPrisonOffender(correctPrisonHmppsId))
211211
.thenReturn(Response(data = posPrisoner))
212212

213-
val result = getPersonService.getPrisoner(correctPrisonHmppsId, ConsumerFilters(prisons=listOf(prisonId)))
213+
val result = getPersonService.getPrisoner(correctPrisonHmppsId, ConsumerFilters(prisons = listOf(prisonId)))
214214

215215
result.data.shouldBeTypeOf<Person>()
216216
result.data!!.firstName.shouldBe(posPrisoner.firstName)
@@ -226,7 +226,7 @@ internal class GetPersonServiceTest(
226226
Response(data = POSPrisoner(firstName = "Sam", lastName = "Mills")),
227227
)
228228

229-
val result = getPersonService.getPrisoner(validHmppsId, ConsumerFilters(prisons=null))
229+
val result = getPersonService.getPrisoner(validHmppsId, ConsumerFilters(prisons = null))
230230

231231
result.data.shouldBeTypeOf<Person>()
232232
result.data!!.firstName.shouldBe(person.firstName)
@@ -239,7 +239,7 @@ internal class GetPersonServiceTest(
239239
whenever(prisonerOffenderSearchGateway.getPrisonOffender(validHmppsId))
240240
.thenReturn(Response(data = POSPrisoner(firstName = "Test", lastName = "Person", prisonId = "XYZ")))
241241

242-
val result = getPersonService.getPrisoner(validHmppsId, ConsumerFilters(prisons=emptyList()))
242+
val result = getPersonService.getPrisoner(validHmppsId, ConsumerFilters(prisons = emptyList()))
243243

244244
result.data.shouldBe(null)
245245
result.errors.shouldBe(listOf(UpstreamApiError(UpstreamApi.PRISONER_OFFENDER_SEARCH, UpstreamApiError.Type.ENTITY_NOT_FOUND, "Not found")))
@@ -250,7 +250,7 @@ internal class GetPersonServiceTest(
250250
whenever(prisonerOffenderSearchGateway.getPrisonOffender(validHmppsId))
251251
.thenReturn(Response(data = POSPrisoner(firstName = "Test", lastName = "Person")))
252252

253-
val result = getPersonService.getPrisoner(validHmppsId, ConsumerFilters(prisons=listOf("ABC")))
253+
val result = getPersonService.getPrisoner(validHmppsId, ConsumerFilters(prisons = listOf("ABC")))
254254

255255
result.data.shouldBe(null)
256256
result.errors.shouldBe(listOf(UpstreamApiError(UpstreamApi.PRISONER_OFFENDER_SEARCH, UpstreamApiError.Type.ENTITY_NOT_FOUND, "Not found")))

0 commit comments

Comments
 (0)