Skip to content

Commit

Permalink
Merge pull request #2562 from ministryofjustice/chore/remove-unused-c…
Browse files Browse the repository at this point in the history
…ode-from-UserAccessService

Chore/remove unused code from user access service
  • Loading branch information
daveawc authored Nov 22, 2024
2 parents a4a630e + 454d512 commit ba92e7a
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,6 @@ class UserAccessService(
else -> false
}

fun getTemporaryAccommodationApplicationAccessLevelForCurrentUser(): TemporaryAccommodationApplicationAccessLevel =
getTemporaryAccommodationApplicationAccessLevelForUser(userService.getUserForRequest())

fun getTemporaryAccommodationApplicationAccessLevelForUser(user: UserEntity): TemporaryAccommodationApplicationAccessLevel = when {
user.hasRole(UserRole.CAS3_ASSESSOR) -> TemporaryAccommodationApplicationAccessLevel.SUBMITTED_IN_REGION
user.hasRole(UserRole.CAS3_REFERRER) -> TemporaryAccommodationApplicationAccessLevel.SELF
Expand Down Expand Up @@ -181,24 +178,17 @@ class UserAccessService(
application.submittedAt != null
}

fun currentUserCanReallocateTask() = userCanReallocateTask(userService.getUserForRequest())

fun userCanReallocateTask(user: UserEntity): Boolean = when (requestContextService.getServiceForRequest()) {
ServiceName.temporaryAccommodation -> user.hasRole(UserRole.CAS3_ASSESSOR)
ServiceName.approvedPremises -> user.hasPermission(UserPermission.CAS1_VIEW_MANAGE_TASKS)
else -> false
}

fun currentUserCanDeallocateTask() = userCanDeallocateTask(userService.getUserForRequest())

fun userCanDeallocateTask(user: UserEntity): Boolean = when (requestContextService.getServiceForRequest()) {
ServiceName.temporaryAccommodation -> user.hasRole(UserRole.CAS3_ASSESSOR)
else -> false
}

fun currentUserCanViewAssessment(assessment: AssessmentEntity): Boolean =
userCanViewAssessment(userService.getUserForRequest(), assessment)

fun userCanViewAssessment(user: UserEntity, assessment: AssessmentEntity): Boolean = when (assessment) {
is ApprovedPremisesAssessmentEntity ->
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,25 +961,6 @@ class UserAccessServiceTest {
assertThat(userAccessService.getTemporaryAccommodationApplicationAccessLevelForUser(user)).isEqualTo(TemporaryAccommodationApplicationAccessLevel.NONE)
}

@Test
fun `getTemporaryAccommodationApplicationAccessLevelForCurrentUser returns SUBMITTED_IN_REGION if the user has the CAS3_ASSESSOR role`() {
user.addRoleForUnitTest(UserRole.CAS3_ASSESSOR)

assertThat(userAccessService.getTemporaryAccommodationApplicationAccessLevelForCurrentUser()).isEqualTo(TemporaryAccommodationApplicationAccessLevel.SUBMITTED_IN_REGION)
}

@Test
fun `getTemporaryAccommodationApplicationAccessLevelForCurrentUser returns SELF if the user has the CAS3_REFERRER role`() {
user.addRoleForUnitTest(UserRole.CAS3_REFERRER)

assertThat(userAccessService.getTemporaryAccommodationApplicationAccessLevelForCurrentUser()).isEqualTo(TemporaryAccommodationApplicationAccessLevel.SELF)
}

@Test
fun `getTemporaryAccommodationApplicationAccessLevelForCurrentUser returns NONE if the user has no suitable role`() {
assertThat(userAccessService.getTemporaryAccommodationApplicationAccessLevelForCurrentUser()).isEqualTo(TemporaryAccommodationApplicationAccessLevel.NONE)
}

@Test
fun `userCanViewApplication returns true if the user created the application for Approved Premises`() {
val newestJsonSchema = ApprovedPremisesApplicationJsonSchemaEntityFactory()
Expand Down Expand Up @@ -1233,45 +1214,6 @@ class UserAccessServiceTest {
assertThat(userAccessService.userCanReallocateTask(user)).isFalse
}

@Test
fun `currentUserCanReallocateTask returns true if the current request has 'X-Service-Name' header with value 'temporary-accommodation' and the user has the CAS3_ASSESSOR role`() {
currentRequestIsFor(ServiceName.temporaryAccommodation)

user.addRoleForUnitTest(UserRole.CAS3_ASSESSOR)

assertThat(userAccessService.currentUserCanReallocateTask()).isTrue
}

@Test
fun `currentUserCanReallocateTask returns false otherwise if the current request has 'X-Service-Name' header with value 'temporary-accommodation'`() {
currentRequestIsFor(ServiceName.temporaryAccommodation)

assertThat(userAccessService.currentUserCanReallocateTask()).isFalse
}

@Test
fun `currentUserCanReallocateTask returns true if the current request has 'X-Service-Name' header with value 'approved-premises' and the user has a role with the CAS1_VIEW_MANAGE_TASKS permission`() {
currentRequestIsFor(ServiceName.approvedPremises)

user.addRoleForUnitTest(CAS1_WORKFLOW_MANAGER)

assertThat(userAccessService.currentUserCanReallocateTask()).isTrue
}

@Test
fun `currentUserCanReallocateTask returns false otherwise if the current request has 'X-Service-Name' header with value 'approved-premises'`() {
currentRequestIsFor(ServiceName.approvedPremises)

assertThat(userAccessService.currentUserCanReallocateTask()).isFalse
}

@Test
fun `currentUserCanReallocateTask returns false by default`() {
currentRequestIsForArbitraryService()

assertThat(userAccessService.currentUserCanReallocateTask()).isFalse
}

@Test
fun `userCanDeallocateTask returns true if the current request has 'X-Service-Name' header with value 'temporary-accommodation' and the user has the CAS3_ASSESSOR role`() {
currentRequestIsFor(ServiceName.temporaryAccommodation)
Expand All @@ -1295,29 +1237,6 @@ class UserAccessServiceTest {
assertThat(userAccessService.userCanDeallocateTask(user)).isFalse
}

@Test
fun `currentUserCanDeallocateTask returns true if the current request has 'X-Service-Name' header with value 'temporary-accommodation' and the user has the CAS3_ASSESSOR role`() {
currentRequestIsFor(ServiceName.temporaryAccommodation)

user.addRoleForUnitTest(UserRole.CAS3_ASSESSOR)

assertThat(userAccessService.currentUserCanDeallocateTask()).isTrue
}

@Test
fun `currentUserCanDeallocateTask returns false otherwise if the current request has 'X-Service-Name' header with value 'temporary-accommodation'`() {
currentRequestIsFor(ServiceName.temporaryAccommodation)

assertThat(userAccessService.currentUserCanDeallocateTask()).isFalse
}

@Test
fun `currentUserCanDeallocateTask returns false by default`() {
currentRequestIsForArbitraryService()

assertThat(userAccessService.currentUserCanDeallocateTask()).isFalse
}

@Test
fun `userCanViewAssessment returns true for a Temporary Accommodation assessment if the user has the CAS3_ASSESSOR role and the assessment is in the same region`() {
currentRequestIsFor(ServiceName.temporaryAccommodation)
Expand Down Expand Up @@ -1406,92 +1325,6 @@ class UserAccessServiceTest {
assertThat(userAccessService.userCanViewAssessment(user, assessment)).isTrue
}

@Test
fun `currentUserCanViewAssessment returns true for a Temporary Accommodation assessment if the user has the CAS3_ASSESSOR role and the assessment is in the same region`() {
currentRequestIsFor(ServiceName.temporaryAccommodation)

val application = TemporaryAccommodationApplicationEntityFactory()
.withCreatedByUser(anotherUserInRegion)
.withProbationRegion(probationRegion)
.produce()

val assessment = TemporaryAccommodationAssessmentEntityFactory()
.withApplication(application)
.produce()

user.addRoleForUnitTest(UserRole.CAS3_ASSESSOR)

assertThat(userAccessService.currentUserCanViewAssessment(assessment)).isTrue
}

@Test
fun `currentUserCanViewAssessment returns false for a Temporary Accommodation assessment if the user has the CAS3_ASSESSOR role but the assessment is not in the same region`() {
currentRequestIsFor(ServiceName.temporaryAccommodation)

val application = TemporaryAccommodationApplicationEntityFactory()
.withCreatedByUser(anotherUserNotInRegion)
.withProbationRegion(anotherProbationRegion)
.produce()

val assessment = TemporaryAccommodationAssessmentEntityFactory()
.withApplication(application)
.produce()

user.addRoleForUnitTest(UserRole.CAS3_ASSESSOR)

assertThat(userAccessService.currentUserCanViewAssessment(assessment)).isFalse
}

@Test
fun `currentUserCanViewAssessment returns false for a Temporary Accommodation assessment if the user does not have the CAS3_ASSESSOR role`() {
currentRequestIsFor(ServiceName.temporaryAccommodation)

val application = TemporaryAccommodationApplicationEntityFactory()
.withCreatedByUser(anotherUserInRegion)
.withProbationRegion(probationRegion)
.produce()

val assessment = TemporaryAccommodationAssessmentEntityFactory()
.withApplication(application)
.produce()

assertThat(userAccessService.currentUserCanViewAssessment(assessment)).isFalse
}

@Test
fun `currentUserCanViewAssessment returns true for an Approved Premises assessment if the user has the CAS1_WORKFLOW_MANAGER role`() {
currentRequestIsFor(ServiceName.approvedPremises)

val application = ApprovedPremisesApplicationEntityFactory()
.withCreatedByUser(anotherUserNotInRegion)
.produce()

val assessment = ApprovedPremisesAssessmentEntityFactory()
.withApplication(application)
.withAllocatedToUser(anotherUserNotInRegion)
.produce()

user.addRoleForUnitTest(CAS1_WORKFLOW_MANAGER)

assertThat(userAccessService.currentUserCanViewAssessment(assessment)).isTrue
}

@Test
fun `currentUserCanViewAssessment returns true for an Approved Premises assessment if the assessment is assigned to the user`() {
currentRequestIsFor(ServiceName.approvedPremises)

val application = ApprovedPremisesApplicationEntityFactory()
.withCreatedByUser(anotherUserInRegion)
.produce()

val assessment = ApprovedPremisesAssessmentEntityFactory()
.withApplication(application)
.withAllocatedToUser(user)
.produce()

assertThat(userAccessService.currentUserCanViewAssessment(assessment)).isTrue
}

@Test
fun `reporterUserCanViewReport returns true if the current request has 'X-Service-Name' header with value 'temporary-accommodation' and the user has the CAS3_REPORTER role`() {
currentRequestIsFor(ServiceName.temporaryAccommodation)
Expand Down

0 comments on commit ba92e7a

Please sign in to comment.