Skip to content

Commit

Permalink
Merge branch 'master' into PUB-1909-View-Audit-Logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisS1512 authored Nov 29, 2024
2 parents d1d87ea + ef67e10 commit 83d1a4d
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ The service can also be adapted using the yaml files found in the following loca
- [src/main/resources/application-dev.yaml](./src/main/resources/application-dev.yaml) for changes to the behaviour of the service when running locally.
- [src/test/resources/application-test.yaml](./src/test/resources/application-test.yaml) for changes to other test types (e.g. unit tests).
- [src/integrationTest/resources/application-integration.yaml](./src/integrationTest/resources/application-integration.yaml) for changes to the application when it's running integration tests.
- [src/integrationTest/resources/application-integration-jpa.yaml](./src/integrationTest/resources/application-integration-jpa.yaml) for changes to the application when it's running repository integration tests.
- [src/integrationTest/resources/application-functional.yaml](./src/functionalTest/resources/application-functional.yaml) for changes to the application when its running functional tests.

### Fortify
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package uk.gov.hmcts.reform.pip.account.management.database;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.test.context.ActiveProfiles;
import uk.gov.hmcts.reform.pip.account.management.model.AuditLog;
import uk.gov.hmcts.reform.pip.model.account.Roles;
import uk.gov.hmcts.reform.pip.model.account.UserProvenances;
import uk.gov.hmcts.reform.pip.model.enums.AuditAction;

import java.time.LocalDateTime;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@ActiveProfiles("integration-jpa")
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@EnableJpaAuditing
class AuditRepositoryTest {
private static final String USER_ID1 = "123";
private static final String USER_ID2 = "124";
private static final String USER_ID3 = "125";
private static final String EMAIL1 = "TestUser1@justice.gov.uk";
private static final String EMAIL2 = "TestUser2@justice.gov.uk";
private static final String EMAIL3 = "TestUser3@justice.gov.uk";
private static final String DETAILS1 = "Details 1";
private static final String DETAILS2 = "Details 2";
private static final String DETAILS3 = "Details 3";

private static final String AUDIT_LOG_MATCHED_MESSAGE = "Audit log does not match";
private static final String AUDIT_LOG_EMPTY_MESSAGE = "Audit log is not empty";

@Autowired
AuditRepository auditRepository;

@BeforeEach
void setup() {
AuditLog auditLog1 = new AuditLog();
auditLog1.setUserId(USER_ID1);
auditLog1.setUserEmail(EMAIL1);
auditLog1.setRoles(Roles.SYSTEM_ADMIN);
auditLog1.setUserProvenance(UserProvenances.SSO);
auditLog1.setAction(AuditAction.MANAGE_USER);
auditLog1.setDetails(DETAILS1);

AuditLog auditLog2 = new AuditLog();
auditLog2.setUserId(USER_ID2);
auditLog2.setUserEmail(EMAIL2);
auditLog2.setRoles(Roles.INTERNAL_ADMIN_CTSC);
auditLog2.setUserProvenance(UserProvenances.SSO);
auditLog2.setAction(AuditAction.REFERENCE_DATA_UPLOAD);
auditLog2.setDetails(DETAILS2);

AuditLog auditLog3 = new AuditLog();
auditLog3.setUserId(USER_ID3);
auditLog3.setUserEmail(EMAIL3);
auditLog3.setRoles(Roles.INTERNAL_ADMIN_LOCAL);
auditLog3.setUserProvenance(UserProvenances.SSO);
auditLog3.setAction(AuditAction.DELETE_PUBLICATION);
auditLog3.setDetails(DETAILS3);

auditRepository.saveAll(List.of(auditLog1, auditLog2, auditLog3));
}

@AfterEach
void shutdown() {
auditRepository.deleteAll();
}

@Test
void shouldDeleteAllByTimestampBefore() {
auditRepository.deleteAllByTimestampBefore(LocalDateTime.now().plusMinutes(10));
assertThat(auditRepository.findAll())
.as(AUDIT_LOG_EMPTY_MESSAGE)
.isEmpty();
}

@Test
void shouldNotDeleteAllByTimestamp() {
auditRepository.deleteAllByTimestampBefore(LocalDateTime.now().minusMinutes(10));
assertThat(auditRepository.findAll())
.as(AUDIT_LOG_MATCHED_MESSAGE)
.hasSize(3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package uk.gov.hmcts.reform.pip.account.management.database;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.ActiveProfiles;
import uk.gov.hmcts.reform.pip.account.management.model.MediaApplication;
import uk.gov.hmcts.reform.pip.account.management.model.MediaApplicationStatus;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@ActiveProfiles("integration-jpa")
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class MediaApplicationRepositoryTest {
private static final String NAME1 = "Test name 1";
private static final String NAME2 = "Test name 2";
private static final String NAME3 = "Test name 3";
private static final String NAME4 = "Test name 4";
private static final String EMAIL1 = "TestUser1@justice.gov.uk";
private static final String EMAIL2 = "TestUser2@justice.gov.uk";
private static final String EMAIL3 = "testuser3@justice.gov.uk";
private static final String EMAIL4 = "AnotherTestUser@justice.gov.uk";
private static final String EMPLOYER = "Test Employer";

private static final String MEDIA_APPLICATION_MATCHED_MESSAGE = "Media application does not match";
private static final String MEDIA_APPLICATION_EMPTY_MESSAGE = "Media application is not empty";

@Autowired
MediaApplicationRepository mediaApplicationRepository;

@BeforeAll
void setup() {
MediaApplication mediaApplication1 = new MediaApplication();
mediaApplication1.setFullName(NAME1);
mediaApplication1.setEmail(EMAIL1);
mediaApplication1.setEmployer(EMPLOYER);
mediaApplication1.setStatus(MediaApplicationStatus.PENDING);

MediaApplication mediaApplication2 = new MediaApplication();
mediaApplication2.setFullName(NAME2);
mediaApplication2.setEmail(EMAIL2);
mediaApplication2.setEmployer(EMPLOYER);
mediaApplication2.setStatus(MediaApplicationStatus.APPROVED);

MediaApplication mediaApplication3 = new MediaApplication();
mediaApplication3.setFullName(NAME3);
mediaApplication3.setEmail(EMAIL3);
mediaApplication3.setEmployer(EMPLOYER);
mediaApplication3.setStatus(MediaApplicationStatus.REJECTED);

MediaApplication mediaApplication4 = new MediaApplication();
mediaApplication4.setFullName(NAME4);
mediaApplication4.setEmail(EMAIL4);
mediaApplication4.setEmployer(EMPLOYER);
mediaApplication4.setStatus(MediaApplicationStatus.PENDING);

mediaApplicationRepository.saveAll(
List.of(mediaApplication1, mediaApplication2, mediaApplication3, mediaApplication4)
);
}

@AfterAll
void shutdown() {
mediaApplicationRepository.deleteAll();
}

@Test
void shouldFindAllMediaApplicationsByEmailStartingWithPrefix() {
assertThat(mediaApplicationRepository.findAllByEmailStartingWithIgnoreCase("testUser"))
.as(MEDIA_APPLICATION_MATCHED_MESSAGE)
.hasSize(3)
.extracting(MediaApplication::getEmail)
.containsExactlyInAnyOrder(EMAIL1, EMAIL2, EMAIL3);
}

@Test
void shouldFindAllMediaApplicationsByEmailIfPrefixNotMatched() {
assertThat(mediaApplicationRepository.findAllByEmailStartingWithIgnoreCase("InvalidPrefix"))
.as(MEDIA_APPLICATION_EMPTY_MESSAGE)
.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package uk.gov.hmcts.reform.pip.account.management.database;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.ActiveProfiles;
import uk.gov.hmcts.reform.pip.account.management.model.PiUser;
import uk.gov.hmcts.reform.pip.model.account.Roles;
import uk.gov.hmcts.reform.pip.model.account.UserProvenances;

import java.time.LocalDateTime;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;

@ActiveProfiles("integration-jpa")
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class UserRepositoryTest {
private static final String PROVENANCE_USER_ID1 = UUID.randomUUID().toString();
private static final String PROVENANCE_USER_ID2 = UUID.randomUUID().toString();
private static final String PROVENANCE_USER_ID3 = UUID.randomUUID().toString();
private static final String PROVENANCE_USER_ID4 = UUID.randomUUID().toString();
private static final String PROVENANCE_USER_ID5 = UUID.randomUUID().toString();
private static final String EMAIL1 = "TestUser1@justice.gov.uk";
private static final String EMAIL2 = "TestUser2@justice.gov.uk";
private static final String EMAIL3 = "TestUser3@justice.gov.uk";
private static final String EMAIL4 = "TestUser4@justice.gov.uk";
private static final String EMAIL5 = "TestUser5@justice.gov.uk";

private static final LocalDateTime TIMESTAMP_NOW = LocalDateTime.now();
private static final int DAYS = 5;

private static final String USER_MATCHED_MESSAGE = "User does not match";
private static final String USER_EMPTY_MESSAGE = "User is not empty";

private UUID userId1;
private UUID userId2;
private UUID userId3;
private UUID userId4;
private UUID userId5;

@Autowired
UserRepository userRepository;

@BeforeAll
void setup() {
PiUser user1 = new PiUser();
user1.setEmail(EMAIL1);
user1.setProvenanceUserId(PROVENANCE_USER_ID1);
user1.setUserProvenance(UserProvenances.PI_AAD);
user1.setRoles(Roles.VERIFIED);
user1.setLastVerifiedDate(TIMESTAMP_NOW.minusDays(DAYS));
userId1 = userRepository.save(user1).getUserId();

PiUser user2 = new PiUser();
user2.setEmail(EMAIL2);
user2.setProvenanceUserId(PROVENANCE_USER_ID2);
user2.setUserProvenance(UserProvenances.PI_AAD);
user2.setRoles(Roles.INTERNAL_ADMIN_CTSC);
user2.setLastSignedInDate(TIMESTAMP_NOW.minusDays(DAYS));
userId2 = userRepository.save(user2).getUserId();

PiUser user3 = new PiUser();
user3.setEmail(EMAIL3);
user3.setProvenanceUserId(PROVENANCE_USER_ID3);
user3.setUserProvenance(UserProvenances.SSO);
user3.setRoles(Roles.INTERNAL_ADMIN_CTSC);
user3.setLastSignedInDate(TIMESTAMP_NOW.minusDays(DAYS));
userId3 = userRepository.save(user3).getUserId();

PiUser user4 = new PiUser();
user4.setEmail(EMAIL4);
user4.setProvenanceUserId(PROVENANCE_USER_ID4);
user4.setUserProvenance(UserProvenances.CFT_IDAM);
user4.setRoles(Roles.VERIFIED);
user4.setLastSignedInDate(TIMESTAMP_NOW.minusDays(DAYS));
userId4 = userRepository.save(user4).getUserId();

PiUser user5 = new PiUser();
user5.setEmail(EMAIL5);
user5.setProvenanceUserId(PROVENANCE_USER_ID5);
user5.setUserProvenance(UserProvenances.CRIME_IDAM);
user5.setRoles(Roles.VERIFIED);
user5.setLastSignedInDate(TIMESTAMP_NOW.minusDays(DAYS));
userId5 = userRepository.save(user5).getUserId();
}

@AfterAll
void shutdown() {
userRepository.deleteAll();
}

@Test
void shouldFindUserByProvenanceId() {
assertThat(userRepository.findExistingByProvenanceId(PROVENANCE_USER_ID1, UserProvenances.PI_AAD.name()))
.as(USER_MATCHED_MESSAGE)
.hasSize(1)
.extracting(PiUser::getUserId)
.containsExactly(userId1);
}

@Test
void shouldNotFindUserByProvenanceIdIfUserProvenanceMismatch() {
assertThat(userRepository.findExistingByProvenanceId(PROVENANCE_USER_ID1, UserProvenances.CFT_IDAM.name()))
.as(USER_EMPTY_MESSAGE)
.isEmpty();
}

@Test
void shouldFindVerifiedUsersByLastVerifiedDate() {
assertThat(userRepository.findVerifiedUsersByLastVerifiedDate(DAYS))
.as(USER_MATCHED_MESSAGE)
.hasSize(1)
.extracting(PiUser::getUserId)
.containsExactly(userId1);
}

@Test
void shouldFindAdminUsersFortNotificationByLastSignedInDate() {
assertThat(userRepository.findAdminUsersFortNotificationByLastSignedInDate(DAYS))
.as(USER_MATCHED_MESSAGE)
.hasSize(1)
.extracting(PiUser::getUserId)
.containsExactly(userId2);
}

@Test
void shouldFindAdminUsersForDeletionByLastSignedInDate() {
assertThat(userRepository.findAdminUsersForDeletionByLastSignedInDate(DAYS, DAYS))
.as(USER_MATCHED_MESSAGE)
.hasSize(2)
.extracting(PiUser::getUserId)
.containsExactlyInAnyOrder(userId2, userId3);
}

@Test
void shouldFindIdamUsersByLastSignedInDate() {
assertThat(userRepository.findIdamUsersByLastSignedInDate(DAYS, DAYS))
.as(USER_MATCHED_MESSAGE)
.hasSize(2)
.extracting(PiUser::getUserId)
.containsExactlyInAnyOrder(userId4, userId5);
}

@Test
void shouldFindByUserIdPageable() {
Pageable pageable = PageRequest.of(0, 25);
Page<PiUser> page = userRepository.findByUserIdPageable(userId1.toString(), pageable);

assertThat(page.getContent())
.hasSize(1)
.first()
.extracting(PiUser::getUserId)
.isEqualTo(userId1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring:
datasource:
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
url: jdbc:tc:postgresql://localhost/pip
jpa:
hibernate:
ddl-auto: create
flyway:
enabled: true
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.Arrays;

@Configuration
@Profile("!test & !integration & !functional")
@Profile("!test & !integration & !integration-jpa & !functional")
public class AzureBlobConfiguration {
private static final String BLOB_ENDPOINT = "https://%s.blob.core.windows.net/";
private static final String DEV_PROFILE = "blobStorageDev";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Configuration class used to initialise beans to talk to Azure graph.
*/
@Profile("!test & !integration & !functional")
@Profile("!test & !integration & !integration-jpa & !functional")
@Configuration
public class AzureConfigurationClient {

Expand Down
Loading

0 comments on commit 83d1a4d

Please sign in to comment.