Skip to content

Commit 4b273ef

Browse files
authored
MAN-192: Add endpoint to check user access for list of crns (#4478)
1 parent c2305f0 commit 4b273ef

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/LaoCaseloadIntegrationTest.kt

+42-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
99
import org.springframework.boot.test.context.SpringBootTest
1010
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
1111
import org.springframework.test.web.servlet.MockMvc
12+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
1213
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
1314
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
1415
import uk.gov.justice.digital.hmpps.api.model.user.StaffCaseload
1516
import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.LIMITED_ACCESS_USER
1617
import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator.EXCLUSION
18+
import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator.PERSONAL_DETAILS
1719
import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator.RESTRICTION
1820
import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator.RESTRICTION_EXCLUSION
21+
import uk.gov.justice.digital.hmpps.service.UserAccess
1922
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
23+
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withJson
2024
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken
2125

2226
@AutoConfigureMockMvc
@@ -27,7 +31,6 @@ internal class LaoCaseloadIntegrationTest {
2731

2832
@Test
2933
fun `all caseload activity for an lao user`() {
30-
3134
val person = LIMITED_ACCESS_USER
3235
val res = mockMvc
3336
.perform(get("/caseload/user/${person.username}").withToken())
@@ -65,4 +68,42 @@ internal class LaoCaseloadIntegrationTest {
6568
assertThat(caseload[3].limitedAccess, equalTo(false))
6669
assertNotEquals(caseload[3].caseName, null)
6770
}
71+
72+
@Test
73+
fun `check lao access for a user with list of crns`() {
74+
val person = LIMITED_ACCESS_USER
75+
val crns = listOf(RESTRICTION_EXCLUSION.crn, EXCLUSION.crn, RESTRICTION.crn, PERSONAL_DETAILS.crn)
76+
val res = mockMvc
77+
.perform(
78+
MockMvcRequestBuilders.post("/user/${person.username}/access").withToken()
79+
.withJson(crns)
80+
)
81+
.andExpect(status().isOk)
82+
.andReturn().response.contentAsJson<UserAccess>()
83+
84+
val userAccess = res.access.sortedBy { it.crn }
85+
86+
assertThat(userAccess[0].userExcluded, equalTo(true))
87+
assertThat(userAccess[0].userRestricted, equalTo(true))
88+
89+
assertThat(userAccess[1].userExcluded, equalTo(true))
90+
assertThat(userAccess[1].userRestricted, equalTo(false))
91+
92+
assertThat(userAccess[2].userExcluded, equalTo(false))
93+
assertThat(userAccess[2].userRestricted, equalTo(true))
94+
95+
assertThat(userAccess[3].userExcluded, equalTo(false))
96+
assertThat(userAccess[3].userRestricted, equalTo(false))
97+
}
98+
99+
@Test
100+
fun `check lao access returns 400 when no crns are provided`() {
101+
val person = LIMITED_ACCESS_USER
102+
mockMvc
103+
.perform(
104+
MockMvcRequestBuilders.post("/user/${person.username}/access").withToken()
105+
.withJson(emptyList<String>())
106+
)
107+
.andExpect(status().isBadRequest)
108+
}
68109
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package uk.gov.justice.digital.hmpps.api.controller
22

33
import io.swagger.v3.oas.annotations.tags.Tag
4+
import jakarta.validation.constraints.Size
45
import org.springframework.security.access.prepost.PreAuthorize
5-
import org.springframework.web.bind.annotation.GetMapping
6-
import org.springframework.web.bind.annotation.PathVariable
7-
import org.springframework.web.bind.annotation.RestController
6+
import org.springframework.web.bind.annotation.*
87
import uk.gov.justice.digital.hmpps.service.UserAccessService
98

109
@RestController
@@ -14,4 +13,11 @@ class UserAccessController(private val userAccessService: UserAccessService) {
1413
@GetMapping("/user/{username}/access/{crn}")
1514
fun checkAccess(@PathVariable username: String, @PathVariable crn: String) =
1615
userAccessService.caseAccessFor(username, crn)
16+
17+
@PostMapping("/user/{username}/access")
18+
fun checkUserAccess(
19+
@PathVariable username: String,
20+
@Size(min = 1, max = 500, message = "Please provide between 1 and 500 crns")
21+
@RequestBody crns: List<String>
22+
) = userAccessService.userAccessFor(username, crns)
1723
}

0 commit comments

Comments
 (0)