generated from hmcts/spring-boot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PUB-2576 - Add functional tests for audit logs #467
Open
NatashaAlker
wants to merge
26
commits into
master
Choose a base branch
from
PUB-2576
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+399
−23
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
86e4180
Replace constructor with lombok annotation
NatashaAlker b9b2e7d
Add delete method to clean up after audit functional test
NatashaAlker 8aff3eb
Add delete method to clean up after audit functional test
NatashaAlker dab2f88
Replace constructor with lombok annotation
NatashaAlker bc081cc
Add get method for audit functional tests
NatashaAlker f57ebf6
Add get method for audit functional tests
NatashaAlker 6dd6539
Add functional tests for audit endpoints
NatashaAlker e980853
Merge branch 'master' into PUB-2576
NatashaAlker c605cbc
Fix PMD issue
NatashaAlker 27825b8
Add unit test for testing support delete audit method
NatashaAlker 205d3d2
Remove whitespace
NatashaAlker ff77fa9
Add unit test for functional test helper method
NatashaAlker 39a6b75
Merge branch 'freeze' into PUB-2576
NatashaAlker a67f2b2
Remove unused method
NatashaAlker 97c3089
Filter returned audits for those created within the test only
NatashaAlker 6bab7eb
Update error message to also apply to audit logs
NatashaAlker a08bf94
Add integration tests for testing support delete endpoint
NatashaAlker 8ebaf69
Fix checkstyle and pmd fails
NatashaAlker 33ac555
Merge branch 'master' into PUB-2576
NatashaAlker b28c068
test commits being picked up by jenkins
NatashaAlker 3ef2f98
Update get request to include test generated email
NatashaAlker 3106714
Add helper functionality to update the timestamp on an audit log
NatashaAlker 6a2faad
Add tests for helper functionality
NatashaAlker d40d85f
Add tests for helper functions
NatashaAlker 81f2b70
Add functional test for deletion of out of date audits
NatashaAlker 2cdb0b7
Merge branch 'master' into PUB-2576
ChrisS1512 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
179 changes: 179 additions & 0 deletions
179
...alTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditCreationTest.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,179 @@ | ||
package uk.gov.hmcts.reform.pip.account.management.controllers; | ||
|
||
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpHeaders; | ||
import io.restassured.response.Response; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import uk.gov.hmcts.reform.pip.account.management.model.AuditLog; | ||
import uk.gov.hmcts.reform.pip.account.management.utils.FunctionalTestBase; | ||
import uk.gov.hmcts.reform.pip.account.management.utils.OAuthClient; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.springframework.http.HttpStatus.NOT_FOUND; | ||
import static org.springframework.http.HttpStatus.OK; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
@ActiveProfiles(profiles = "functional") | ||
@SpringBootTest(classes = {OAuthClient.class}) | ||
class AuditCreationTest extends FunctionalTestBase { | ||
private static final UUID USER_ID = UUID.randomUUID(); | ||
private static final String TEST_EMAIL_PREFIX = String.format( | ||
"pip-am-test-email-%s", ThreadLocalRandom.current().nextInt(1000, 9999)); | ||
private static final String TEST_EMAIL = TEST_EMAIL_PREFIX + "@justice.gov.uk"; | ||
private static final String ROLES = "SYSTEM_ADMIN"; | ||
private static final String USER_PROVENANCE = "PI_AAD"; | ||
private static final String ACTION = "PUBLICATION_UPLOAD"; | ||
private static final String DETAILS = "Publication with artefact id %s successfully uploaded"; | ||
private static final Integer PAGE_NUMBER = 0; | ||
private static final Integer PAGE_SIZE = 2; | ||
|
||
private static final String AUDIT_URL = "/audit"; | ||
private static final String GET_AUDIT_URL = "/audit/%s"; | ||
private static final String TESTING_SUPPORT_AUDIT_URL = "/testing-support/audit/"; | ||
private static final String BEARER = "Bearer "; | ||
private static final String CONTENT = "content"; | ||
|
||
private Map<String, String> bearer; | ||
|
||
@BeforeAll | ||
public void startUp() { | ||
bearer = Map.of(HttpHeaders.AUTHORIZATION, BEARER + accessToken); | ||
} | ||
|
||
@AfterAll | ||
public void teardown() { | ||
doDeleteRequest(TESTING_SUPPORT_AUDIT_URL + TEST_EMAIL_PREFIX, bearer); | ||
} | ||
|
||
private AuditLog createAuditLog() { | ||
|
||
UUID artefactId = UUID.randomUUID(); | ||
String responseBody = """ | ||
{ | ||
"userId": "%s", | ||
"userEmail": "%s", | ||
"roles": "%s", | ||
"userProvenance": "%s", | ||
"action": "%s", | ||
"details": "%s" | ||
} | ||
""".formatted(USER_ID, TEST_EMAIL, ROLES, USER_PROVENANCE, ACTION, String.format(DETAILS, artefactId)); | ||
|
||
Response response = doPostRequest(AUDIT_URL, bearer, responseBody); | ||
|
||
assertThat(response.getStatusCode()).isEqualTo(OK.value()); | ||
|
||
return response.getBody().as(AuditLog.class); | ||
} | ||
|
||
private void deleteAuditLog() { | ||
Response response = doDeleteRequest(AUDIT_URL, bearer); | ||
|
||
assertThat(response.getStatusCode()).isEqualTo(OK.value()); | ||
} | ||
|
||
@Test | ||
void shouldBeAbleToCreateAndGetAnAuditRecord() { | ||
AuditLog auditLog = createAuditLog(); | ||
|
||
Response getResponse = doGetRequest(String.format(GET_AUDIT_URL, auditLog.getId()), bearer); | ||
assertThat(getResponse.getStatusCode()).isEqualTo(OK.value()); | ||
|
||
AuditLog retrievedAuditLog = getResponse.getBody().as(AuditLog.class); | ||
assertThat(retrievedAuditLog.getId()).isEqualTo(auditLog.getId()); | ||
assertThat(retrievedAuditLog.getUserEmail()).isEqualTo(auditLog.getUserEmail()); | ||
} | ||
|
||
@Test | ||
void shouldBeAbleToGetAllAuditLogsUsingDefaultDisplayParameters() { | ||
AuditLog auditLog = createAuditLog(); | ||
|
||
Response getResponse = doGetRequestWithQueryParameters(String.format(AUDIT_URL), bearer, | ||
"", "", TEST_EMAIL_PREFIX); | ||
|
||
assertThat(getResponse.getStatusCode()).isEqualTo(OK.value()); | ||
assertThat(getResponse.jsonPath().getInt("pageable.pageNumber")).isEqualTo(0); | ||
assertThat(getResponse.jsonPath().getInt("pageable.pageSize")).isEqualTo(25); | ||
|
||
List<AuditLog> retrievedAuditLogs = getResponse.jsonPath().getList(CONTENT, AuditLog.class); | ||
|
||
assertThat(retrievedAuditLogs).isNotNull(); | ||
assertThat(retrievedAuditLogs.size()).isEqualTo(2); | ||
assertThat(retrievedAuditLogs.getFirst().getId()).isEqualTo(auditLog.getId()); | ||
} | ||
|
||
@Test | ||
void shouldBeAbleToGetAllAuditLogsUsingCustomDisplayParameters() { | ||
AuditLog auditLog = createAuditLog(); | ||
|
||
Response getResponse = doGetRequestWithQueryParameters(String.format(AUDIT_URL), bearer, | ||
PAGE_NUMBER.toString(), PAGE_SIZE.toString(), TEST_EMAIL_PREFIX); | ||
|
||
assertThat(getResponse.getStatusCode()).isEqualTo(OK.value()); | ||
assertThat(getResponse.jsonPath().getInt("pageable.pageNumber")).isEqualTo(0); | ||
assertThat(getResponse.jsonPath().getInt("pageable.pageSize")).isEqualTo(2); | ||
assertThat(getResponse.jsonPath().getInt("totalPages")).isEqualTo(2); | ||
assertThat(getResponse.jsonPath().getInt("totalElements")).isEqualTo(4); | ||
|
||
List<AuditLog> retrievedAuditLogs = getResponse.jsonPath().getList(CONTENT, AuditLog.class); | ||
|
||
assertThat(retrievedAuditLogs).isNotNull(); | ||
assertThat(retrievedAuditLogs.getFirst().getId()).isEqualTo(auditLog.getId()); | ||
assertThat(retrievedAuditLogs.size()).isEqualTo(2); | ||
} | ||
|
||
@Test | ||
void shouldReturnErrorWhenAuditRecordDoesNotExist() { | ||
Response getResponse = doGetRequest(String.format(GET_AUDIT_URL, USER_ID), bearer); | ||
|
||
assertThat(getResponse.getStatusCode()).isEqualTo(NOT_FOUND.value()); | ||
} | ||
|
||
@Test | ||
void shouldReturnOkForDeleteAuditLogsIfAllInDate() { | ||
AuditLog auditLog = createAuditLog(); | ||
|
||
deleteAuditLog(); | ||
|
||
Response getResponse = doGetRequestWithQueryParameters(String.format(AUDIT_URL), bearer, | ||
"", "", TEST_EMAIL_PREFIX); | ||
|
||
List<AuditLog> retrievedAuditLogs = getResponse.jsonPath().getList(CONTENT, AuditLog.class); | ||
|
||
assertThat(retrievedAuditLogs).isNotNull(); | ||
assertThat(retrievedAuditLogs.getFirst().getId()).isEqualTo(auditLog.getId()); | ||
assertThat(retrievedAuditLogs.size()).isEqualTo(3); | ||
} | ||
|
||
@Test | ||
void shouldBeAbleToDeleteAnOutOfDateAudit() { | ||
Response getResponse = doGetRequestWithQueryParameters(String.format(AUDIT_URL), bearer, | ||
"", "", TEST_EMAIL_PREFIX); | ||
List<AuditLog> retrievedAuditLogs = getResponse.jsonPath().getList(CONTENT, AuditLog.class); | ||
assertThat(retrievedAuditLogs.size()).isEqualTo(4); | ||
|
||
AuditLog auditLog = createAuditLog(); | ||
doPutRequest(TESTING_SUPPORT_AUDIT_URL + auditLog.getId(), bearer); | ||
|
||
getResponse = doGetRequestWithQueryParameters(String.format(AUDIT_URL), bearer, "", "", TEST_EMAIL_PREFIX); | ||
retrievedAuditLogs = getResponse.jsonPath().getList(CONTENT, AuditLog.class); | ||
assertThat(retrievedAuditLogs.size()).isEqualTo(5); | ||
|
||
deleteAuditLog(); | ||
|
||
getResponse = doGetRequestWithQueryParameters(String.format(AUDIT_URL), bearer, "", "", TEST_EMAIL_PREFIX); | ||
retrievedAuditLogs = getResponse.jsonPath().getList(CONTENT, AuditLog.class); | ||
assertThat(retrievedAuditLogs.size()).isEqualTo(4); | ||
} | ||
|
||
} |
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'll run into the issues with the below when it runs on the master pipeline due to master linking up to the Staging DB and the numbers below not matching.
To fix this, I'd suggest using the functionality in the recently merged PUB-1909, to filter based on email alongside the page sizes below to ensure it will always return a consistent result