Skip to content

Commit

Permalink
PRSD-358: Review markups
Browse files Browse the repository at this point in the history
  • Loading branch information
JasminConterioSW committed Oct 15, 2024
1 parent ff2faf7 commit 133c35e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ class ManageLocalAuthorityUsersController(
model: Model,
principal: Principal,
): String {
val currentUserLocalAuthority = localAuthorityDataService.getLocalAuthorityForUser(principal.name)
if (currentUserLocalAuthority?.id != null) {
// We should always get to here as only LA_ADMINs can access this page
val users = localAuthorityDataService.getLocalAuthorityUsersForLocalAuthority(currentUserLocalAuthority.id)
val usersJson = Json.encodeToString(users)
model.addAttribute("usersJson", usersJson)
}
val currentUserLocalAuthority = localAuthorityDataService.getLocalAuthorityForUser(principal.name)!!
currentUserLocalAuthority.id!!

val users = localAuthorityDataService.getLocalAuthorityUsersForLocalAuthority(currentUserLocalAuthority.id)
val usersJson = Json.encodeToString(users)
model.addAttribute("usersJson", usersJson)

model.addAttribute("contentHeader", "Manage Local Authority Users")
model.addAttribute("title", "Manage Local Authority Users")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class LocalAuthorityDataService(

fun getLocalAuthorityForUser(subjectId: String): LocalAuthority? {
val localAuthorityUser = localAuthorityUserRepository.findByBaseUser_Id(subjectId)
if (localAuthorityUser == null) {
return null
}

return localAuthorityUser.localAuthority
return localAuthorityUser?.localAuthority
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ class UserRolesService(
if (matchingLocalAuthorityUser != null) {
if (matchingLocalAuthorityUser.isManager) {
roles.add("ROLE_LA_ADMIN")
} else {
roles.add("ROLE_LA_USER")
}
roles.add("ROLE_LA_USER")
}

return roles
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package uk.gov.communities.prsdb.webapp.controllers

import org.mockito.Mockito
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.security.test.context.support.WithMockUser
import org.springframework.test.util.ReflectionTestUtils
import org.springframework.test.web.servlet.get
import org.springframework.web.context.WebApplicationContext
import uk.gov.communities.prsdb.webapp.database.entity.LocalAuthority
import kotlin.test.Test

@WebMvcTest(ManageLocalAuthorityUsersController::class)
Expand All @@ -31,6 +34,12 @@ class ManageLocalAuthorityUsersControllerTests(
@Test
@WithMockUser(roles = ["LA_ADMIN"])
fun `ManageLocalAuthorityUsersController returns 200 for authorized user`() {
val localAuthority = LocalAuthority()
ReflectionTestUtils.setField(localAuthority, "id", 123)
Mockito
.`when`(localAuthorityDataService.getLocalAuthorityForUser("user"))
.thenReturn(localAuthority)

mvc
.get("/manage-users")
.andExpect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ class UserRolesServiceTests {
val roles = userRolesService.getRolesForSubjectId("test-user-1")

// Assert
Assertions.assertEquals(1, roles.size)
Assertions.assertEquals(2, roles.size)
Assertions.assertEquals("ROLE_LA_ADMIN", roles[0])
Assertions.assertEquals("ROLE_LA_USER", roles[1])
}

@Test
Expand Down

0 comments on commit 133c35e

Please sign in to comment.