Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PI-1577 improvements #2420

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
)
)
}
}