Skip to content

Commit

Permalink
PI-2557 Update staff endpoints to treat username as case-insensitive (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl authored Oct 2, 2024
1 parent 2d08a04 commit 036a93a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ internal class IntegrationTest {
.andExpect(jsonPath("$.telephoneNumber", equalTo("10101010101")))
}

@Test
fun `username is case-insensitive`() {
val username = StaffGenerator.DEFAULT_STAFF_USER.username.uppercase()
mockMvc
.perform(get("/staff/$username").withToken())
.andExpect(status().isOk)
.andExpect(jsonPath("$.username", equalTo("john-smith")))
.andExpect(jsonPath("$.email", equalTo("[email protected]")))
.andExpect(jsonPath("$.telephoneNumber", equalTo("10101010101")))
}

@Test
fun `returns pdu heads`() {
val boroughCode = ProviderGenerator.DEFAULT_BOROUGH.code
Expand Down Expand Up @@ -140,6 +151,29 @@ internal class IntegrationTest {
)
}

@Test
fun `usernames are case-insensitive`() {
val usernames = listOf(
StaffGenerator.DEFAULT_PDUSTAFF_USER.username.uppercase(),
StaffGenerator.DEFAULT_STAFF_USER.username.uppercase()
)

val staffNames = mockMvc
.perform(post("/staff").withToken().withJson(usernames))
.andExpect(status().isOk)
.andReturn().response.contentAsJson<List<StaffName>>()

assertThat(
staffNames,
equalTo(
listOf(
StaffGenerator.PDUHEAD.asStaffName(),
StaffGenerator.DEFAULT.asStaffName()
)
)
)
}

@Test
fun `returns staff by id `() {
mockMvc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class StaffUser(

interface StaffRepository : JpaRepository<Staff, Long> {
@EntityGraph(attributePaths = ["user", "teams"])
fun findByUserUsername(username: String): Staff?
fun findByUserUsernameIgnoreCase(username: String): Staff?

@EntityGraph(attributePaths = ["user", "teams"])
fun findByCode(code: String): Staff?
Expand All @@ -77,5 +77,5 @@ interface StaffRepository : JpaRepository<Staff, Long> {
override fun findById(id: Long): Optional<Staff>

@EntityGraph(attributePaths = ["user"])
fun findByUserUsernameIn(usernames: List<String>): List<Staff>
fun findByUserUsernameInIgnoreCase(usernames: List<String>): List<Staff>
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class StaffService(
private val boroughRepository: BoroughRepository,
private val caseloadRepository: CaseloadRepository,
) {
fun findStaff(username: String): Staff = staffRepository.findByUserUsername(username)
fun findStaff(username: String): Staff = staffRepository.findByUserUsernameIgnoreCase(username)
?.let { ldapTemplate.populateUserDetails(it).asStaff() }
?: throw NotFoundException("Staff", "username", username)

Expand All @@ -39,7 +39,7 @@ class StaffService(
?: listOf()

fun findStaffForUsernames(usernames: List<String>): List<StaffName> =
staffRepository.findByUserUsernameIn(usernames).map { it.asStaffName() }
staffRepository.findByUserUsernameInIgnoreCase(usernames).map { it.asStaffName() }

fun getManagedOffendersByStaffId(id: Long): List<ManagedOffender> =
caseloadRepository.findByStaffIdAndRoleCode(
Expand Down

0 comments on commit 036a93a

Please sign in to comment.