Skip to content

Commit

Permalink
PI-1577 improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj committed Oct 19, 2023
1 parent a3e6bfa commit b2aca86
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class LimitedAccessUser(
)

interface UserAccessRepository : JpaRepository<LimitedAccessUser, Long> {
@Query("select u from LimitedAccessUser u where upper(u.username) = upper(:username) ")
fun findByUsername(username: String): LimitedAccessUser?

@Query(
"""
select p.crn as crn, '' as exclusionMessage, p.restrictionMessage as restrictionMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import uk.gov.justice.digital.hmpps.entity.isRestricted
@Service
class UserAccessService(private val uar: UserAccessRepository) {
fun userAccessFor(username: String, crns: List<String>): UserAccess {
val limitations: Map<String, List<PersonAccess>> = uar.getAccessFor(username, crns).groupBy { it.crn }
return UserAccess(crns.map { limitations[it].combined(it) })
val user = uar.findByUsername(username)

val limitations: List<PersonAccess> =
user?.let { uar.getAccessFor(it.username, crns) } ?: uar.checkLimitedAccessFor(crns)
return UserAccess(crns.map { limitations.groupBy { it.crn }[it].combined(it) })
}

fun checkLimitedAccessFor(crns: List<String>): UserAccess {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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.entity.LimitedAccessUser
import uk.gov.justice.digital.hmpps.entity.PersonAccess
import uk.gov.justice.digital.hmpps.entity.UserAccessRepository

Expand All @@ -22,6 +23,7 @@ internal class UserAccessServiceTest {
@Test
fun `user limited access is correctly returned`() {
val pas = givenLimitedAccessResults()
whenever(uar.findByUsername("john-smith")).thenReturn(LimitedAccessUser("john-smith", 1))
whenever(uar.getAccessFor("john-smith", listOf("E123456", "R123456", "B123456", "N123456")))
.thenReturn(pas)

Expand All @@ -31,6 +33,18 @@ internal class UserAccessServiceTest {
assertThat(res, equalTo(userAccess()))
}

@Test
fun `user limited access is correctly returned when user doesn't exist`() {
val pas = givenLimitedAccessResults()
whenever(uar.checkLimitedAccessFor(listOf("E123456", "R123456", "B123456", "N123456")))
.thenReturn(pas)

val res = userAccessService.userAccessFor("jane-smith", listOf("E123456", "R123456", "B123456", "N123456"))

assertThat(res.access.size, equalTo(4))
assertThat(res, equalTo(userAccess()))
}

@Test
fun `limited access is correctly returned`() {
val pas = givenLimitedAccessResults()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,54 @@ internal class LimitedAccessTest {
)
)
}

@Test
fun `limited access controls are correctly returned with full access`() {
val res = mockMvc.perform(
MockMvcRequestBuilders.post("/users/access?username=${LimitedAccessGenerator.FULL_ACCESS_USER.username}")
.withOAuth2Token(wireMockserver)
.contentType(MediaType.APPLICATION_JSON)
.content(
objectMapper.writeValueAsString(
listOf(
LimitedAccessGenerator.EXCLUDED_CASE.crn,
LimitedAccessGenerator.RESTRICTED_CASE.crn,
LimitedAccessGenerator.UNLIMITED_ACCESS.crn
)
)
)
).andReturn().response.contentAsString

val result = objectMapper.readValue<UserAccess>(res)
assertThat(
result.access.first { it.crn == LimitedAccessGenerator.EXCLUDED_CASE.crn },
equalTo(
CaseAccess(
LimitedAccessGenerator.EXCLUDED_CASE.crn,
userExcluded = false,
userRestricted = false
)
)
)
assertThat(
result.access.first { it.crn == LimitedAccessGenerator.RESTRICTED_CASE.crn },
equalTo(
CaseAccess(
LimitedAccessGenerator.RESTRICTED_CASE.crn,
userExcluded = false,
userRestricted = false
)
)
)
assertThat(
result.access.first { it.crn == LimitedAccessGenerator.UNLIMITED_ACCESS.crn },
equalTo(
CaseAccess(
LimitedAccessGenerator.UNLIMITED_ACCESS.crn,
userExcluded = false,
userRestricted = false
)
)
)
}
}

0 comments on commit b2aca86

Please sign in to comment.