Skip to content

Commit

Permalink
PI-1528 add LAO endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj committed Oct 3, 2023
1 parent 9b313b8 commit 1cecc32
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions projects/approved-premises-and-delius/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ apply(plugin = "com.google.cloud.tools.jib")
dependencies {
implementation(project(":libs:audit"))
implementation(project(":libs:commons"))
implementation(project(":libs:limited-access"))
implementation(project(":libs:messaging"))
implementation(project(":libs:oauth-client"))
implementation(project(":libs:oauth-server"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uk.gov.justice.digital.hmpps.controller

import jakarta.validation.constraints.Size
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import uk.gov.justice.digital.hmpps.service.UserAccess
import uk.gov.justice.digital.hmpps.service.UserAccessService

@RestController
@RequestMapping("users")
class UserController(private val uas: UserAccessService) {
@PreAuthorize("hasRole('APPROVED_PREMISES_STAFF')")
@RequestMapping("access", method = [RequestMethod.GET, RequestMethod.POST])
fun userAccessCheck(
@RequestParam(required = false) username: String?,
@Size(min = 1, max = 500, message = "Please provide between 1 and 500 crns") @RequestBody crns: List<String>
): UserAccess = username?.let { uas.userAccessFor(it, crns) } ?: uas.checkLimitedAccessFor(crns)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package uk.gov.justice.digital.hmpps.controller

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
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.verify
import org.mockito.kotlin.whenever
import uk.gov.justice.digital.hmpps.service.CaseAccess
import uk.gov.justice.digital.hmpps.service.UserAccess
import uk.gov.justice.digital.hmpps.service.UserAccessService

@ExtendWith(MockitoExtension::class)
class UserControllerTest {
@Mock
internal lateinit var userAccessService: UserAccessService

@InjectMocks
internal lateinit var userController: UserController

@Test
fun `when username provided, calls service with username`() {
val username = "john-smith"
val crns = listOf("T123456", "T234567", "T345678")
whenever(userAccessService.userAccessFor(username, crns)).thenReturn(
UserAccess(
crns.map {
CaseAccess(
it,
userExcluded = false,
userRestricted = false
)
}
)
)

val res = userController.userAccessCheck(username, crns)
verify(userAccessService).userAccessFor(username, crns)
assertThat(res.access.size, equalTo(3))
}

@Test
fun `when no username provided, calls service without username`() {
val crns = listOf("N123456", "N234567", "N345678")
whenever(userAccessService.checkLimitedAccessFor(crns)).thenReturn(
UserAccess(
crns.map {
CaseAccess(
it,
userExcluded = false,
userRestricted = false
)
}
)
)

val res = userController.userAccessCheck(null, crns)
verify(userAccessService).checkLimitedAccessFor(crns)
assertThat(res.access.size, equalTo(3))
}
}

0 comments on commit 1cecc32

Please sign in to comment.