generated from hmcts/spring-boot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into PUB-1909-View-Audit-Logs
- Loading branch information
Showing
9 changed files
with
376 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...ionTest/java/uk/gov/hmcts/reform/pip/account/management/database/AuditRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...a/uk/gov/hmcts/reform/pip/account/management/database/MediaApplicationRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
165 changes: 165 additions & 0 deletions
165
...tionTest/java/uk/gov/hmcts/reform/pip/account/management/database/UserRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/integrationTest/resources/application-integration-jpa.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.