From b30126e38056e60194daa2ecc780f379b342f184 Mon Sep 17 00:00:00 2001 From: Marcus Aspin Date: Tue, 30 Apr 2024 09:29:50 +0100 Subject: [PATCH] PI-2135 Add user access endpoint for Manage a Supervision (#3697) --- .../build.gradle.kts | 1 + ...serController.kt => CaseloadController.kt} | 2 +- .../api/controller/UserAccessController.kt | 17 +++++++++ ...ollerTest.kt => CaseloadControllerTest.kt} | 4 +- .../controller/UserAccessControllerTest.kt | 37 +++++++++++++++++++ 5 files changed, 58 insertions(+), 3 deletions(-) rename projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/{UserController.kt => CaseloadController.kt} (96%) create mode 100644 projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserAccessController.kt rename projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/{UserControllerTest.kt => CaseloadControllerTest.kt} (97%) create mode 100644 projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserAccessControllerTest.kt diff --git a/projects/manage-supervision-and-delius/build.gradle.kts b/projects/manage-supervision-and-delius/build.gradle.kts index f73e151b2d..41f234bb96 100644 --- a/projects/manage-supervision-and-delius/build.gradle.kts +++ b/projects/manage-supervision-and-delius/build.gradle.kts @@ -6,6 +6,7 @@ dependencies { implementation(project(":libs:audit")) implementation(project(":libs:commons")) implementation(project(":libs:oauth-server")) + implementation(project(":libs:limited-access")) implementation(project(":libs:document-management")) implementation("org.springframework.boot:spring-boot-starter-actuator") implementation("org.springframework.boot:spring-boot-starter-data-jpa") diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserController.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/CaseloadController.kt similarity index 96% rename from projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserController.kt rename to projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/CaseloadController.kt index 708b5084ce..71d4016078 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserController.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/CaseloadController.kt @@ -11,7 +11,7 @@ import uk.gov.justice.digital.hmpps.service.UserService @Tag(name = "Caseload Info") @RequestMapping("/caseload") @PreAuthorize("hasRole('PROBATION_API__MANAGE_A_SUPERVISION__CASE_DETAIL')") -class UserController(private val userService: UserService) { +class CaseloadController(private val userService: UserService) { @GetMapping("/user/{username}") @Operation(summary = "Gets caseloads for the user") diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserAccessController.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserAccessController.kt new file mode 100644 index 0000000000..3978f62ff5 --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserAccessController.kt @@ -0,0 +1,17 @@ +package uk.gov.justice.digital.hmpps.api.controller + +import io.swagger.v3.oas.annotations.tags.Tag +import org.springframework.security.access.prepost.PreAuthorize +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.RestController +import uk.gov.justice.digital.hmpps.service.UserAccessService + +@RestController +@Tag(name = "User access") +@PreAuthorize("hasRole('PROBATION_API__MANAGE_A_SUPERVISION__CASE_DETAIL')") +class UserAccessController(private val userAccessService: UserAccessService) { + @GetMapping("/user/{username}/access/{crn}") + fun checkAccess(@PathVariable username: String, @PathVariable crn: String) = + userAccessService.caseAccessFor(username, crn) +} \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserControllerTest.kt b/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/CaseloadControllerTest.kt similarity index 97% rename from projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserControllerTest.kt rename to projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/CaseloadControllerTest.kt index bacebcebf0..ed711b494e 100644 --- a/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserControllerTest.kt +++ b/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/CaseloadControllerTest.kt @@ -18,13 +18,13 @@ import uk.gov.justice.digital.hmpps.service.toStaffCase import uk.gov.justice.digital.hmpps.service.toTeamCase @ExtendWith(MockitoExtension::class) -internal class UserControllerTest { +internal class CaseloadControllerTest { @Mock lateinit var userService: UserService @InjectMocks - lateinit var controller: UserController + lateinit var controller: CaseloadController @Test fun `calls get user case load function `() { diff --git a/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserAccessControllerTest.kt b/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserAccessControllerTest.kt new file mode 100644 index 0000000000..93b6ac2eda --- /dev/null +++ b/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserAccessControllerTest.kt @@ -0,0 +1,37 @@ +package uk.gov.justice.digital.hmpps.api.controller + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.mockito.InjectMocks +import org.mockito.Mock +import org.mockito.junit.jupiter.MockitoExtension +import org.mockito.kotlin.whenever +import uk.gov.justice.digital.hmpps.service.CaseAccess +import uk.gov.justice.digital.hmpps.service.UserAccessService + +@ExtendWith(MockitoExtension::class) +internal class UserControllerTest { + @Mock + lateinit var userAccessService: UserAccessService + + @InjectMocks + lateinit var userAccessController: UserAccessController + + @Test + fun `check user access`() { + val caseAccess = CaseAccess( + crn = "crn", + userRestricted = false, + userExcluded = true, + exclusionMessage = "testing", + ) + + whenever(userAccessService.caseAccessFor("username", "crn")).thenReturn(caseAccess) + + val response = userAccessController.checkAccess("username", "crn") + + assertThat(response, equalTo(caseAccess)) + } +} \ No newline at end of file