Skip to content

Commit

Permalink
[#117] Fixed mustache tests vulnerability to date formats
Browse files Browse the repository at this point in the history
  • Loading branch information
susanw1 committed Oct 9, 2023
1 parent 247b954 commit 03e0569
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Test-2 (classpath): Test mustache file: receipt is {{receipt}} on {{date}}
Test-2 (classpath): Test mustache file: receipt is {{receipt}} for {{#customer}}{{first_name}} {{family_name}}{{/customer}}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Test-4 (classpath, partials): Test mustache file: receipt is {{receipt}} on {{date}}
Test-4 (classpath, partials): Test mustache file: receipt is {{receipt}} for {{#customer}}{{first_name}} {{family_name}}{{/customer}}
Testing partials:
{{> foo-4.mustache}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Test-1: Test mustache file: receipt is {{receipt}} on {{date}}
Test-1: Test mustache file: receipt is {{receipt}} for {{#customer}}{{first_name}} {{family_name}}{{/customer}}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Test-3: Test mustache file: receipt is {{receipt}} on {{date}}
Test-3: Test mustache file: receipt is {{receipt}} for {{#customer}}{{first_name}} {{family_name}}{{/customer}}
Partial#1: {{> partials/test-3a.mustache}}
Partial#2: {{> partials/test-3b.mustache}}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class Test1BasicTemplatingTest {
@Test
public void shouldCreateFirstOutputFile() throws IOException {
String expectedContent = "Test-1: Test mustache file: receipt is Oz-Ware Purchase Invoice on Mon Aug 06 01:00:00 BST 2012" + lineSeparator();
String expectedContent = "Test-1: Test mustache file: receipt is Oz-Ware Purchase Invoice for Dorothy Gale" + lineSeparator();
InputStream input = getClass().getResourceAsStream("/templates-out/test1/exampleA-1.txt");
assertThat(input).isNotNull();

Expand All @@ -26,7 +26,7 @@ public void shouldCreateFirstOutputFile() throws IOException {

@Test
public void shouldCreateSecondOutputFile() throws IOException {
String expectedContent = "Test-1: Test mustache file: receipt is Something Else on Sun Jan 06 00:00:00 GMT 2013" + lineSeparator();
String expectedContent = "Test-1: Test mustache file: receipt is Something Else for Joe Bloggs" + lineSeparator();
InputStream input = getClass().getResourceAsStream("/templates-out/test1/exampleB-1.txt");
assertThat(input).isNotNull();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class Test2ClasspathTest {
@Test
public void shouldCreateOutputFile() throws IOException {
String expectedContent = "Test-2 (classpath): Test mustache file: receipt is Classpath example on Thu Mar 16 00:00:00 GMT 2023" + lineSeparator();
String expectedContent = "Test-2 (classpath): Test mustache file: receipt is Classpath example for Joe Bloggs" + lineSeparator();
InputStream input = getClass().getResourceAsStream("/templates-out/test2/example-2.txt");
String content = IOUtils.toString(input, StandardCharsets.UTF_8);
assertThat(content).isEqualTo(expectedContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Test3LocalFileTemplatingPartialTest {

@Test
public void shouldCreateFileUsingPartials() throws IOException {
String expectedContent = "Test-3: Test mustache file: receipt is Oz-Ware Purchase Invoice on Mon Aug 06 01:00:00 BST 2012" + lineSeparator()
String expectedContent = "Test-3: Test mustache file: receipt is Oz-Ware Purchase Invoice for Dorothy Gale" + lineSeparator()
+ "Partial#1: Test-3a: Bill-to: 123 Tornado Alley
Suite 16
" + lineSeparator() + lineSeparator()
+ "Partial#2: Test-3b: - Ship-to: 123 Tornado Alley
Suite 16
" + lineSeparator() + lineSeparator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,23 @@
public class Test4MustachePartialTest {
@Test
public void shouldRenderClasspathTemplatesWithPartials() throws IOException {
final String expected = "Test-4 (classpath, partials): Test mustache file: receipt is Classpath example on Thu Mar 16 00:00:00 GMT 2023" + lineSeparator()
final String expected = "Test-4 (classpath, partials): Test mustache file: receipt is Classpath example for Joe Bloggs" + lineSeparator()
+ "Testing partials:" + lineSeparator()
+ "foo-4.mustache: - Ship-to: 31 Cat Road
Suite 16
" + lineSeparator() + lineSeparator();

// final String templateResource = "/template-cp/test-4-cp-part.mustache";
// final URL templateUrl = getClass().getResource(templateResource);

// final String templatePathStr
// = "jar:file:/home/users/bill/.m2/repository/net/zscript/templating-maven-plugin-tests/0.0.1-SNAPSHOT/templating-maven-plugin-tests-0.0.1-SNAPSHOT.jar"
// + "!/template-cp/test-4-cp-part.mustache";
// final String fooPathStr
// = "jar:file:/home/users/bill/.m2/repository/net/zscript/templating-maven-plugin-tests/0.0.1-SNAPSHOT/templating-maven-plugin-tests-0.0.1-SNAPSHOT.jar"
// + "!/template-cp/foo-4.mustache";

// final URL templateUrl = new URL(templatePathStr);
final URL contextUrl = getClass().getResource("/contexts-cp/example-2.yaml");
// System.out.println("templateUrl: " + templateUrl);
System.out.println("contextUrl : " + contextUrl);

// MustacheResolver resolver = new ClasspathResolver("template-cp");

// final InputStream templateStream = templateUrl.openStream();
// assertThat(templateStream).isNotNull();
final InputStream contextStream = contextUrl.openStream();
assertThat(contextStream).isNotNull();

final Yaml yamlMapper = new Yaml();

final DefaultMustacheFactory mustacheFactory = new DefaultMustacheFactory("template-cp");
try {
final Mustache mustache = mustacheFactory.compile("test-4-cp-part.mustache");
final StringWriter writer = new StringWriter();
final Map<?, ?> context = yamlMapper.load(contextStream);
mustache.execute(writer, context);

assertThat(writer.toString()).isEqualTo(expected);
} catch (Exception ex) {
ex.printStackTrace(System.out);
throw ex;
}
final Mustache mustache = mustacheFactory.compile("test-4-cp-part.mustache");
final StringWriter writer = new StringWriter();
final Map<?, ?> context = yamlMapper.load(contextStream);
mustache.execute(writer, context);

assertThat(writer.toString()).isEqualTo(expected);
}
}

0 comments on commit 03e0569

Please sign in to comment.