Skip to content

Commit

Permalink
PRMT-4462 (#60)
Browse files Browse the repository at this point in the history
* [PRMT-4462] Disabled HTML escaping.

* [PRMT-4462] Added unit tests to verify that HTML is escaped.

* [PRMT-4462] Unit test structure improvements, focusing on DRY code.

* [PRMT-4462] Removed whitespace.
  • Loading branch information
martin-nhs authored Feb 1, 2024
1 parent 7cc17fb commit 5d86d89
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public ActiveSuspensionsMessage(@JsonProperty("nhsNumber") String nhsNumber,
}

public String toJsonString() {
return new GsonBuilder().disableHtmlEscaping().create().toJson(this);
return new GsonBuilder().create().toJson(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class AuditMessage {
private String status;

public String toJsonString() {
return new GsonBuilder().disableHtmlEscaping().create()
.toJson(this);
return new GsonBuilder().create().toJson(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public ReRegistrationEvent(@JsonProperty("nhsNumber") String nhsNumber,
}

public String toJsonString() {
return new GsonBuilder().disableHtmlEscaping().create()
.toJson(this);
return new GsonBuilder().create().toJson(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package uk.nhs.prm.repo.re_registration.model;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static uk.nhs.prm.repo.re_registration.utility.TestDataUtility.NHS_NUMBER;
import static uk.nhs.prm.repo.re_registration.utility.TestDataUtility.UNESCAPED_HTML;
import static uk.nhs.prm.repo.re_registration.utility.TestDataUtility.getRandomTimestamp;

class ActiveSuspensionsMessageTest {
@Test
void Given_ActiveSuspensionsMessageContainingUnescapedHtml_When_ToJsonStringIsCalled_Then_ReturnValueShouldNotContainUnescapedHtml() {
// Given
final String timestamp = getRandomTimestamp();
final ActiveSuspensionsMessage activeSuspensionsMessage = new ActiveSuspensionsMessage(
NHS_NUMBER,
UNESCAPED_HTML,
timestamp
);

// When
final String json = activeSuspensionsMessage.toJsonString();

// Then
assertFalse(json.contains(UNESCAPED_HTML));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package uk.nhs.prm.repo.re_registration.model;

import org.junit.jupiter.api.Test;

import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static uk.nhs.prm.repo.re_registration.utility.TestDataUtility.UNESCAPED_HTML;

class AuditMessageTest {
@Test
void Given_AuditMessageContainingUnescapedHtml_When_ToJsonStringIsCalled_Then_ReturnValueShouldNotContainUnescapedHtml() {
// Given
final String nemsMessageId = UUID.randomUUID().toString();
final AuditMessage auditMessage = new AuditMessage(
nemsMessageId,
UNESCAPED_HTML
);

// When
final String json = auditMessage.toJsonString();

// Then
assertFalse(json.contains(UNESCAPED_HTML));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package uk.nhs.prm.repo.re_registration.model;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static uk.nhs.prm.repo.re_registration.utility.TestDataUtility.NHS_NUMBER;
import static uk.nhs.prm.repo.re_registration.utility.TestDataUtility.UNESCAPED_HTML;
import static uk.nhs.prm.repo.re_registration.utility.TestDataUtility.getRandomOdsCode;
import static uk.nhs.prm.repo.re_registration.utility.TestDataUtility.getRandomTimestamp;

class ReRegistrationEventTest {
@Test
void Given_ReRegistrationEventContainingUnescapedHtml_When_ToJsonStringIsCalled_Then_ReturnValueShouldNotContainUnescapedHtml() {
// Given
final String timestamp = getRandomTimestamp();
final String odsCode = getRandomOdsCode();
final ReRegistrationEvent reRegistrationEvent = new ReRegistrationEvent(
NHS_NUMBER,
odsCode,
UNESCAPED_HTML,
timestamp
);

// When
final String json = reRegistrationEvent.toJsonString();

// Then
assertFalse(json.contains(UNESCAPED_HTML));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.nhs.prm.repo.re_registration.utility;


import org.joda.time.DateTime;
import wiremock.org.apache.commons.lang3.RandomStringUtils;

public final class TestDataUtility {
public static String UNESCAPED_HTML = "<!DOCTYPE html><html lang='en'><head></head><body></body></html>";

public static String NHS_NUMBER = "9745812541";

public static String getRandomTimestamp() {
return DateTime.now().toDateTimeISO().toString();
}

public static String getRandomOdsCode() {
return RandomStringUtils.randomAlphanumeric(6);
}

private TestDataUtility() { }
}

0 comments on commit 5d86d89

Please sign in to comment.