generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b313b8
commit 1cecc32
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...ises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/UserController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
64 changes: 64 additions & 0 deletions
64
...-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/controller/UserControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |