Skip to content
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-2523 - Updated audit log #431

Merged
merged 2 commits into from
Aug 9, 2024
Merged
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 @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.zonky.test.db.AutoConfigureEmbeddedDatabase;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -24,6 +25,7 @@
import uk.gov.hmcts.reform.pip.model.enums.AuditAction;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -145,6 +147,74 @@ void testCreateAuditLog() throws Exception {
assertEquals(AUDIT_DETAILS, auditLog.getDetails(), "Failed to create audit log");
}

@Test
void testCreateAuditLogWhenAuditDetailLengthIsBelowMinimum() throws Exception {
AuditLog belowMinimumAuditLog = new AuditLog(
USER_ID,
EMAIL,
ROLES,
USER_PROVENANCE,
AUDIT_ACTION,
""
);

MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders
.post(ROOT_URL)
.content(OBJECT_MAPPER.writeValueAsString(belowMinimumAuditLog))
.contentType(MediaType.APPLICATION_JSON);

MvcResult mvcResult = mockMvc.perform(mockHttpServletRequestBuilder)
.andExpect(status().isBadRequest()).andReturn();
assertTrue(mvcResult.getResponse().getContentAsString().contains(
"details should be between 1 and 255 characters"),
"Audit log details should be between 1 and 255 characters");
}

@Test
void testCreateAuditLogWhenAuditDetailLengthIsNull() throws Exception {
AuditLog belowMinimumAuditLog = new AuditLog(
USER_ID,
EMAIL,
ROLES,
USER_PROVENANCE,
AUDIT_ACTION,
null
);

MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders
.post(ROOT_URL)
.content(OBJECT_MAPPER.writeValueAsString(belowMinimumAuditLog))
.contentType(MediaType.APPLICATION_JSON);

MvcResult mvcResult = mockMvc.perform(mockHttpServletRequestBuilder)
.andExpect(status().isBadRequest()).andReturn();
assertTrue(mvcResult.getResponse().getContentAsString().contains("details must be provided"),
"The audit details field must be provided");
}

@Test
void testCreateAuditLogWhenAuditDetailLengthIsAboveMaximum() throws Exception {
AuditLog aboveMaximumAuditLog = new AuditLog(
USER_ID,
EMAIL,
ROLES,
USER_PROVENANCE,
AUDIT_ACTION,
RandomStringUtils.random(256, true, false)
);

MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders
.post(ROOT_URL)
.content(OBJECT_MAPPER.writeValueAsString(aboveMaximumAuditLog))
.contentType(MediaType.APPLICATION_JSON);

MvcResult mvcResult = mockMvc.perform(mockHttpServletRequestBuilder)
.andExpect(status().isBadRequest()).andReturn();
assertTrue(mvcResult.getResponse().getContentAsString().contains(
"details should be between 1 and 255 characters"),
"Audit log details should be between 1 and 255 characters");
}

@Test
@WithMockUser(username = UNAUTHORIZED_USERNAME, authorities = {UNAUTHORIZED_ROLE})
void testUnauthorizedCreateAuditLog() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -54,7 +55,8 @@ public class AuditLog {
@NotNull(message = "action shouldn't be null")
private AuditAction action;

@NotBlank(message = "details shouldn't be blank or null")
@NotNull(message = "details must be provided")
@Size(min = 1, max = 255, message = "details should be between 1 and 255 characters")
private String details;

@CreatedDate
Expand Down