Skip to content

Commit

Permalink
PI-1827 Add user access endpoint for Tier UI (#3143)
Browse files Browse the repository at this point in the history
* PI-1827 Add user access endpoint for Tier UI

* Add test
  • Loading branch information
marcus-bcl authored Jan 30, 2024
1 parent af11f44 commit 705e93a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import uk.gov.justice.digital.hmpps.entity.isRestricted

@Service
class UserAccessService(private val uar: UserAccessRepository) {
fun caseAccessFor(username: String, crn: String) =
userAccessFor(username, listOf(crn)).access.first { it.crn == crn }

fun userAccessFor(username: String, crns: List<String>): UserAccess {
val user = uar.findByUsername(username)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ internal class UserAccessServiceTest {
assertThat(res, equalTo(userAccess()))
}

@Test
fun `single case access is correctly returned`() {
val pas = givenLimitedAccessResults()
whenever(uar.findByUsername("john-smith")).thenReturn(LimitedAccessUser("john-smith", 1))
whenever(uar.getAccessFor("john-smith", listOf("E123456"))).thenReturn(pas)

val res = userAccessService.caseAccessFor("john-smith", "E123456")

assertThat(res, equalTo(userAccess().access[0]))
}

private fun givenLimitedAccessResults() =
listOf(
object : PersonAccess {
Expand Down
1 change: 1 addition & 0 deletions projects/tier-to-delius/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies {
implementation(project(":libs:messaging"))
implementation(project(":libs:oauth-client"))
implementation(project(":libs:oauth-server"))
implementation(project(":libs:limited-access"))

implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.gov.justice.digital.hmpps.controller

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.RequestMapping
import org.springframework.web.bind.annotation.RestController
import uk.gov.justice.digital.hmpps.service.CaseAccess
import uk.gov.justice.digital.hmpps.service.UserAccessService

@RestController
@RequestMapping("users")
@PreAuthorize("hasRole('TIER_DETAILS')")
class UserController(private val userAccessService: UserAccessService) {

@GetMapping("/{username}/access/{crn}")
fun userAccessCheck(
@PathVariable username: String,
@PathVariable crn: String,
): CaseAccess = userAccessService.caseAccessFor(username, crn)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package uk.gov.justice.digital.hmpps.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 userController: UserController

@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 = userController.userAccessCheck("username", "crn")

assertThat(response, equalTo(caseAccess))
}
}

0 comments on commit 705e93a

Please sign in to comment.