Skip to content
Open
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 @@ -19,6 +19,7 @@
import uk.gov.hmcts.darts.common.entity.UserAccountEntity;
import uk.gov.hmcts.darts.common.enums.SecurityGroupEnum;
import uk.gov.hmcts.darts.common.repository.SecurityGroupRepository;
import uk.gov.hmcts.darts.common.repository.SecurityRoleRepository;
import uk.gov.hmcts.darts.common.repository.UserAccountRepository;
import uk.gov.hmcts.darts.common.util.DateConverterUtil;
import uk.gov.hmcts.darts.test.common.data.UserAccountTestData;
Expand All @@ -35,7 +36,9 @@

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import static java.time.OffsetDateTime.now;
import static java.time.ZoneOffset.UTC;
Expand Down Expand Up @@ -67,6 +70,9 @@ class UserControllerIntTest extends IntegrationBase {

@Autowired
private SecurityGroupRepository securityGroupRepository;

@Autowired
private SecurityRoleRepository securityRoleRepository;

@Autowired
private UserAccountRepository userAccountRepository;
Expand All @@ -80,18 +86,20 @@ class UserControllerIntTest extends IntegrationBase {
private static final String SOME_CASE_ID = "1";
private static final OffsetDateTime YESTERDAY = now(UTC).minusDays(1).withHour(9).withMinute(0)
.withSecond(0).withNano(0);
private static final int TRANSCRIBER_SG_ID = -4;
private static final int REQUESTOR_SG_ID = -2;
private static final int APPROVER_SG_ID = -1;

@Test
void testDeactivateModifyWithSuperAdmin() throws Exception {
void deactivateUser_ShouldDeactivateUserAndRollBackTranscriptions_WhenTranscriber() throws Exception {
superAdminUserStub.givenSystemAdminIsAuthorised(userIdentity);

UserAccountEntity userAccountEntity = UserAccountTestData.minimalUserAccount();
userAccountEntity = userAccountRepository.save(userAccountEntity);

Optional<SecurityGroupEntity> groupEntity
= securityGroupRepository.findByGroupNameIgnoreCase(SecurityGroupEnum.SUPER_ADMIN.getName());
SecurityGroupEntity transcriberGroupEntity = securityGroupRepository.getReferenceById(TRANSCRIBER_SG_ID);
userAccountEntity.setSecurityGroupEntities(Set.of(transcriberGroupEntity));

userAccountEntity.getSecurityGroupEntities().add(groupEntity.get());
userAccountEntity = dartsDatabaseStub.save(userAccountEntity);

HearingEntity hearingEntity = dartsDatabase.givenTheDatabaseContainsCourtCaseWithHearingAndCourthouseWithRoom(
Expand Down Expand Up @@ -141,18 +149,19 @@ void testDeactivateModifyWithSuperAdmin() throws Exception {
}

@Test
void testDeactivateUserWithSuperUser() throws Exception {
superAdminUserStub.givenSystemUserIsAuthorised(userIdentity);
void deactivateUser_ShouldDeactivateUser_WhenSuperAdmin() throws Exception {
securityGroupStub.clearUsers(SecurityGroupEnum.SUPER_ADMIN);
superAdminUserStub.givenSystemAdminIsAuthorised(userIdentity);

UserAccountEntity userAccountEntity = UserAccountTestData.minimalUserAccount();
userAccountEntity = userAccountRepository.save(userAccountEntity);

// add user to the super user group
Optional<SecurityGroupEntity> groupEntity
= securityGroupRepository.findByGroupNameIgnoreCase(SecurityGroupEnum.SUPER_USER.getName());
= securityGroupRepository.findByGroupNameIgnoreCase(SecurityGroupEnum.SUPER_ADMIN.getName());
SecurityGroupEntity superAdminGroup = groupEntity.get();

userAccountEntity.getSecurityGroupEntities().add(superAdminGroup);

userAccountEntity.getSecurityGroupEntities().add(groupEntity.get());
userAccountEntity = dartsDatabaseStub.save(userAccountEntity);

HearingEntity hearingEntity = dartsDatabase.givenTheDatabaseContainsCourtCaseWithHearingAndCourthouseWithRoom(
Expand All @@ -168,7 +177,66 @@ void testDeactivateUserWithSuperUser() throws Exception {
// now run the test to disable the user
UserPatch userPatch = new UserPatch();
userPatch.setActive(false);
userPatch.setDescription("");
List<TranscriptionWorkflowEntity> workflowEntityBefore
= dartsDatabase.getTranscriptionWorkflowRepository().findByTranscriptionOrderByWorkflowTimestampDesc(transcription);
Assertions.assertFalse(containsApprovedWorkflow(workflowEntityBefore));

MvcResult mvcResult = mockMvc.perform(patch(ENDPOINT_URL + userAccountEntity.getId())
.header("Content-Type", "application/json")
.content(objectMapper.writeValueAsString(userPatch)))
.andExpect(status().is2xxSuccessful())
.andReturn();

Optional<UserAccountEntity> fndUserIdentity = dartsDatabase.getUserAccountRepository().findById(userAccountEntity.getId());
Assertions.assertTrue(fndUserIdentity.isPresent());

Assertions.assertFalse(securityGroupStub.isPartOfAnySecurityGroup(fndUserIdentity.get().getId()));

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());

UserWithIdAndTimestamps userWithIdAndTimestamps = mapper.readValue(mvcResult.getResponse().getContentAsString(),
UserWithIdAndTimestamps.class);

List<Long> rolledBackTranscription = userWithIdAndTimestamps.getRolledBackTranscriptRequests();

List<TranscriptionWorkflowEntity> workflowEntityAfter
= dartsDatabase.getTranscriptionWorkflowRepository().findByTranscriptionOrderByWorkflowTimestampDesc(transcription);

// transcription workflows should not be changed if user is not transcriber
Assertions.assertNull(rolledBackTranscription);
Assertions.assertEquals(workflowEntityBefore.size(), workflowEntityAfter.size());
}

@Test
void deactivateUser_ShouldDeactivateUser_WhenRequester() throws Exception {
superAdminUserStub.givenSystemAdminIsAuthorised(userIdentity);

UserAccountEntity userAccountEntity = UserAccountTestData.minimalUserAccount();
userAccountEntity = userAccountRepository.save(userAccountEntity);

SecurityGroupEntity requesterSecurityGroup = securityGroupRepository.getReferenceById(REQUESTOR_SG_ID);
userAccountEntity.setSecurityGroupEntities(Set.of(requesterSecurityGroup));

userAccountEntity = dartsDatabaseStub.save(userAccountEntity);

HearingEntity hearingEntity = dartsDatabase.givenTheDatabaseContainsCourtCaseWithHearingAndCourthouseWithRoom(
SOME_CASE_ID,
SOME_COURTHOUSE,
SOME_COURTROOM,
DateConverterUtil.toLocalDateTime(SOME_DATE_TIME));

var courtCase = authorisationStub.getCourtCaseEntity();
TranscriptionEntity transcription
= dartsDatabase.getTranscriptionStub().createAndSaveWithTranscriberTranscription(userAccountEntity, courtCase, hearingEntity, YESTERDAY, false);
// confirm user is requester of the transcription
Assertions.assertEquals(userAccountEntity.getId(), transcription.getRequestedBy().getId());

// now run the test to disable the user
UserPatch userPatch = new UserPatch();
userPatch.setActive(false);
userPatch.setDescription("");
List<TranscriptionWorkflowEntity> workflowEntityBefore
= dartsDatabase.getTranscriptionWorkflowRepository().findByTranscriptionOrderByWorkflowTimestampDesc(transcription);
Assertions.assertFalse(containsApprovedWorkflow(workflowEntityBefore));
Expand All @@ -195,14 +263,137 @@ void testDeactivateUserWithSuperUser() throws Exception {
List<TranscriptionWorkflowEntity> workflowEntityAfter
= dartsDatabase.getTranscriptionWorkflowRepository().findByTranscriptionOrderByWorkflowTimestampDesc(transcription);

Assertions.assertEquals(1, rolledBackTranscription.size());
Assertions.assertEquals(transcription.getId(), rolledBackTranscription.getFirst());
Assertions.assertEquals(workflowEntityBefore.size() + 1, workflowEntityAfter.size());
Assertions.assertTrue(containsApprovedWorkflow(workflowEntityAfter));
// transcription workflows should not be changed if user is not transcriber
Assertions.assertNull(rolledBackTranscription);
Assertions.assertEquals(workflowEntityBefore.size(), workflowEntityAfter.size());
}

@Test
void deactivateUser_ShouldDeactivateUser_WhenApprover() throws Exception {
superAdminUserStub.givenSystemAdminIsAuthorised(userIdentity);

UserAccountEntity userAccountEntity = UserAccountTestData.minimalUserAccount();
userAccountEntity = userAccountRepository.save(userAccountEntity);

SecurityGroupEntity approverSecurityGroup = securityGroupRepository.getReferenceById(APPROVER_SG_ID);
userAccountEntity.setSecurityGroupEntities(Set.of(approverSecurityGroup));

userAccountEntity = dartsDatabaseStub.save(userAccountEntity);

HearingEntity hearingEntity = dartsDatabase.givenTheDatabaseContainsCourtCaseWithHearingAndCourthouseWithRoom(
SOME_CASE_ID,
SOME_COURTHOUSE,
SOME_COURTROOM,
DateConverterUtil.toLocalDateTime(SOME_DATE_TIME));

var courtCase = authorisationStub.getCourtCaseEntity();
TranscriptionEntity transcription
= dartsDatabase.getTranscriptionStub().createAndSaveWithTranscriberTranscription(userAccountEntity, courtCase, hearingEntity, YESTERDAY, false);
// Set user as approver
TranscriptionWorkflowEntity workflowEntity = new TranscriptionWorkflowEntity();
workflowEntity.setTranscription(transcription);
workflowEntity.setTranscriptionStatus(dartsDatabase.getTranscriptionStub().getTranscriptionStatusByEnum(TranscriptionStatusEnum.APPROVED));
workflowEntity.setWorkflowActor(userAccountEntity);
workflowEntity.setWorkflowTimestamp(YESTERDAY.minusHours(1));
dartsDatabase.getTranscriptionWorkflowRepository().save(workflowEntity);
// confirm user is approver of the transcription
List<TranscriptionWorkflowEntity> workflowEntities
= dartsDatabase.getTranscriptionWorkflowRepository().findByTranscriptionOrderByWorkflowTimestampDesc(transcription);
Integer userId = userAccountEntity.getId();
Assertions.assertTrue(workflowEntities.stream().anyMatch(w -> Objects.equals(w.getTranscriptionStatus().getId(),
TranscriptionStatusEnum.APPROVED.getId())
&& w.getWorkflowActor() != null && w.getWorkflowActor().getId().equals(userId)));

// now run the test to disable the user
UserPatch userPatch = new UserPatch();
userPatch.setActive(false);
userPatch.setDescription("");
List<TranscriptionWorkflowEntity> workflowEntityBefore
= dartsDatabase.getTranscriptionWorkflowRepository().findByTranscriptionOrderByWorkflowTimestampDesc(transcription);

MvcResult mvcResult = mockMvc.perform(patch(ENDPOINT_URL + userAccountEntity.getId())
.header("Content-Type", "application/json")
.content(objectMapper.writeValueAsString(userPatch)))
.andExpect(status().is2xxSuccessful())
.andReturn();

Optional<UserAccountEntity> fndUserIdentity = dartsDatabase.getUserAccountRepository().findById(userAccountEntity.getId());
Assertions.assertTrue(fndUserIdentity.isPresent());

Assertions.assertFalse(securityGroupStub.isPartOfAnySecurityGroup(fndUserIdentity.get().getId()));

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());

UserWithIdAndTimestamps userWithIdAndTimestamps = mapper.readValue(mvcResult.getResponse().getContentAsString(),
UserWithIdAndTimestamps.class);

List<Long> rolledBackTranscription = userWithIdAndTimestamps.getRolledBackTranscriptRequests();

List<TranscriptionWorkflowEntity> workflowEntityAfter
= dartsDatabase.getTranscriptionWorkflowRepository().findByTranscriptionOrderByWorkflowTimestampDesc(transcription);

// transcription workflows should not be changed if user is not transcriber
Assertions.assertNull(rolledBackTranscription);
Assertions.assertEquals(workflowEntityBefore.size(), workflowEntityAfter.size());
}

@Test
void deactivateUser_ShouldDeactivateUser_WhenSuperUser() throws Exception {
superAdminUserStub.givenSystemUserIsAuthorised(userIdentity);

UserAccountEntity userAccountEntity = UserAccountTestData.minimalUserAccount();
userAccountEntity = userAccountRepository.save(userAccountEntity);

// add user to the super user group
Optional<SecurityGroupEntity> groupEntity
= securityGroupRepository.findByGroupNameIgnoreCase(SecurityGroupEnum.SUPER_USER.getName());
userAccountEntity.getSecurityGroupEntities().add(groupEntity.get());
userAccountEntity = dartsDatabaseStub.save(userAccountEntity);

HearingEntity hearingEntity = dartsDatabase.givenTheDatabaseContainsCourtCaseWithHearingAndCourthouseWithRoom(
SOME_CASE_ID,
SOME_COURTHOUSE,
SOME_COURTROOM,
DateConverterUtil.toLocalDateTime(SOME_DATE_TIME));

var courtCase = authorisationStub.getCourtCaseEntity();
TranscriptionEntity transcription
= dartsDatabase.getTranscriptionStub().createAndSaveWithTranscriberTranscription(userAccountEntity, courtCase, hearingEntity, YESTERDAY, false);

// now run the test to disable the user
UserPatch userPatch = new UserPatch();
userPatch.setActive(false);

List<TranscriptionWorkflowEntity> workflowEntityBefore
= dartsDatabase.getTranscriptionWorkflowRepository().findByTranscriptionOrderByWorkflowTimestampDesc(transcription);
Assertions.assertFalse(containsApprovedWorkflow(workflowEntityBefore));

MvcResult mvcResult = mockMvc.perform(patch(ENDPOINT_URL + userAccountEntity.getId())
.header("Content-Type", "application/json")
.content(objectMapper.writeValueAsString(userPatch)))
.andExpect(status().is2xxSuccessful())
.andReturn();

Optional<UserAccountEntity> fndUserIdentity = dartsDatabase.getUserAccountRepository().findById(userAccountEntity.getId());
Assertions.assertTrue(fndUserIdentity.isPresent());

Assertions.assertFalse(securityGroupStub.isPartOfAnySecurityGroup(fndUserIdentity.get().getId()));

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
UserWithIdAndTimestamps userWithIdAndTimestamps = mapper.readValue(mvcResult.getResponse().getContentAsString(),
UserWithIdAndTimestamps.class);

List<Long> rolledBackTranscription = userWithIdAndTimestamps.getRolledBackTranscriptRequests();
Assertions.assertNull(rolledBackTranscription);
List<TranscriptionWorkflowEntity> workflowEntityAfter
= dartsDatabase.getTranscriptionWorkflowRepository().findByTranscriptionOrderByWorkflowTimestampDesc(transcription);
Assertions.assertEquals(workflowEntityBefore.size(), workflowEntityAfter.size());
}

@Test
void testActivateModifyUserWithSuperAdmin() throws Exception {
void activateUser_ShouldActivateUser_WhenSuperAdmin() throws Exception {
superAdminUserStub.givenSystemAdminIsAuthorised(userIdentity);

UserAccountEntity userAccountEntity = UserAccountTestData.minimalUserAccount();
Expand Down Expand Up @@ -263,7 +454,7 @@ void testActivateModifyUserWithSuperAdmin() throws Exception {
}

@Test
void testActivateModifyUserWithSuperAdminAndFailWithNoEmailAddress() throws Exception {
void activateUser_ShouldFail_WhenSuperAdminAndNoEmailAddress() throws Exception {
superAdminUserStub.givenSystemAdminIsAuthorised(userIdentity);

UserAccountEntity userAccountEntity = UserAccountTestData.minimalUserAccount();
Expand Down Expand Up @@ -312,7 +503,7 @@ void testActivateModifyUserWithSuperAdminAndFailWithNoEmailAddress() throws Exce
}

@Test
void testActivateModifyUserWithSuperAdminAndFailWithNoFullNameAndNoEmailAddress() throws Exception {
void activateUser_ShouldFail_WhenSuperAdminAndNoFullNameOrEmailAddress() throws Exception {
superAdminUserStub.givenSystemAdminIsAuthorised(userIdentity);

UserAccountEntity userAccountEntity = UserAccountTestData.minimalUserAccount();
Expand Down Expand Up @@ -358,7 +549,7 @@ void testActivateModifyUserWithSuperAdminAndFailWithNoFullNameAndNoEmailAddress(
}

@Test
void testDeactivateFailureWhereUserIsLastInSuperAdminGroup() throws Exception {
void deactivateUser_ShouldFail_WhenUserIsLastInSuperAdminGroup() throws Exception {
superAdminUserStub.givenSystemAdminIsAuthorised(userIdentity);

UserAccountEntity userAccountEntity = UserAccountTestData.minimalUserAccount();
Expand Down Expand Up @@ -391,7 +582,7 @@ void testDeactivateFailureWhereUserIsLastInSuperAdminGroup() throws Exception {
}

@Test
void testDeactivateFailureFromSuperUserModifyChange() throws Exception {
void setDescription_ShouldFail_WhenSuperUser() throws Exception {
superAdminUserStub.givenSystemUserIsAuthorised(userIdentity);

UserAccountEntity userAccountEntity = UserAccountTestData.minimalUserAccount();
Expand Down Expand Up @@ -419,7 +610,7 @@ void testDeactivateFailureFromSuperUserModifyChange() throws Exception {
}

@Test
void testActivateFailureFromSuperUser() throws Exception {
void activateUser_ShouldFail_WhenSuperUser() throws Exception {
superAdminUserStub.givenSystemUserIsAuthorised(userIdentity);

UserAccountEntity userAccountEntity = UserAccountTestData.minimalUserAccount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import static java.util.Objects.isNull;
import static java.util.stream.Collectors.toSet;
import static uk.gov.hmcts.darts.common.enums.SecurityRoleEnum.TRANSCRIBER;
import static uk.gov.hmcts.darts.usermanagement.auditing.UserAccountUpdateAuditActivityProvider.auditActivitiesFor;

@Service
Expand Down Expand Up @@ -195,15 +196,22 @@ private List<Long> updateEntity(UserPatch userPatch, UserAccountEntity userAccou
}

Boolean active = userPatch.getActive();
// if we are disabling the user
if (Boolean.FALSE.equals(active)) {
// unassign from any groups they are part of and if they are a transcriber, roll back any transcriptions they are assigned to
boolean isTranscriber = userAccountRepository
.findByRoleAndUserId(TRANSCRIBER.getId(), userAccountEntity.getId())
.isPresent();

if (isTranscriber) {
rolledBackTranscriptionsList =
transcriptionService.rollbackUserTranscriptions(userAccountEntity);
}
unassignUserFromGroupsTheyArePartOf(userAccountEntity);
}
// set active status to new value if it is not null
if (active != null) {
userAccountEntity.setActive(active);

// if we are disabling the user then disable the transcriptions
// and remove user from security groups
if (active.equals(Boolean.FALSE)) {
unassignUserFromGroupsTheyArePartOf(userAccountEntity);
rolledBackTranscriptionsList = transcriptionService.rollbackUserTranscriptions(userAccountEntity);
}
}

if (BooleanUtils.isTrue(userAccountEntity.isActive())) {
Expand Down
Loading
Loading