From 8cb08f0183847944e3996d95ecf97d9cc8dee872 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Sat, 13 Apr 2024 18:26:19 -0500 Subject: [PATCH 1/3] Add tests for template variables --- cmd/dmarc-report-converter/output_test.go | 130 ++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 cmd/dmarc-report-converter/output_test.go diff --git a/cmd/dmarc-report-converter/output_test.go b/cmd/dmarc-report-converter/output_test.go new file mode 100644 index 0000000..27b9d04 --- /dev/null +++ b/cmd/dmarc-report-converter/output_test.go @@ -0,0 +1,130 @@ +package main + +import ( + "bytes" + "html/template" + "testing" + "time" + + "github.com/tierpod/dmarc-report-converter/pkg/dmarc" +) + +func TestExternalTemplate(t *testing.T) { + r := ` + + + Org 1 + foo@bar.baz + 1712279633.907274 + + 1712188800 + 1712275199 + + + + report.test + r + r +

none

+ 100 +
+ + + 1.2.3.4 + 1 + + none + pass + fail + + + + headerfrom.test + + + + auth.test + 1000073432 + pass + + + cust.test + 2020263919 + pass + + + spf.test + pass + + + +
+` + + report, err := dmarc.Parse([]byte(r), false) + if err != nil { + t.Errorf("unexpected error parsing XML: %s", err) + } + + tmpl := `AssetsPath: {{ .AssetsPath }} +# Report +XMLName: {{ .Report.XMLName.Local }} +ReportMetadata: {{ .Report.ReportMetadata }} +PolicyPublished: {{ .Report.PolicyPublished }} +## Records +{{- range .Report.Records }} +- {{ . }} +{{ end -}} +## MessagesStats +{{ .Report.MessagesStats }} + +// Deprecated +XMLName: {{ .XMLName.Local }} +ReportMetadata: {{ .ReportMetadata }} +PolicyPublished: {{ .PolicyPublished }} +{{ .MessagesStats }} +` + + conf := config{ + Output: Output{ + AssetsPath: "/foo", + template: template.Must(template.New("report").Funcs( + template.FuncMap{ + "now": func(fmt string) string { + return time.Now().Format(fmt) + }, + }, + ).Parse(tmpl)), + }, + } + + var buf bytes.Buffer + out := newOutput(&conf) + out.w = &buf + + err = out.template(report) + if err != nil { + t.Errorf("unexpected error building template: %s", err) + } + + expect := `AssetsPath: /foo +# Report +XMLName: feedback +ReportMetadata: {Org 1 foo@bar.baz 1712279633.907274 {2024-04-03 19:00:00 -0500 CDT 2024-04-04 18:59:59 -0500 CDT}} +PolicyPublished: {report.test r r none 100} +## Records +- {{1.2.3.4 1 {none pass fail} } {headerfrom.test } {{cust.test pass 2020263919} {spf.test pass }}} +## MessagesStats +{1 0 1 100} + +// Deprecated +XMLName: feedback +ReportMetadata: {Org 1 foo@bar.baz 1712279633.907274 {2024-04-03 19:00:00 -0500 CDT 2024-04-04 18:59:59 -0500 CDT}} +PolicyPublished: {report.test r r none 100} +{1 0 1 100} +` + + if buf.String() != expect { + t.Errorf("Oops!\nWANT:\n%s\nGOT:\n%s", expect, buf.String()) + } +} From 3c09e05dbd2bb029c4a13156d23b3ceffaae33fb Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Sun, 14 Apr 2024 13:51:41 -0500 Subject: [PATCH 2/3] Update tests Update tests since the data model changed after PR #48 was merged. --- cmd/dmarc-report-converter/output_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/dmarc-report-converter/output_test.go b/cmd/dmarc-report-converter/output_test.go index 27b9d04..eae26f2 100644 --- a/cmd/dmarc-report-converter/output_test.go +++ b/cmd/dmarc-report-converter/output_test.go @@ -61,7 +61,7 @@ func TestExternalTemplate(t *testing.T) { ` - report, err := dmarc.Parse([]byte(r), false) + report, err := dmarc.Parse([]byte(r), false, 1) if err != nil { t.Errorf("unexpected error parsing XML: %s", err) } @@ -113,7 +113,7 @@ XMLName: feedback ReportMetadata: {Org 1 foo@bar.baz 1712279633.907274 {2024-04-03 19:00:00 -0500 CDT 2024-04-04 18:59:59 -0500 CDT}} PolicyPublished: {report.test r r none 100} ## Records -- {{1.2.3.4 1 {none pass fail} } {headerfrom.test } {{cust.test pass 2020263919} {spf.test pass }}} +- {{1.2.3.4 1 {none pass fail} } {headerfrom.test } {[{auth.test pass 1000073432} {cust.test pass 2020263919}] [{spf.test pass }]}} ## MessagesStats {1 0 1 100} From 068948cc015be814ef7d8c340f8b90c7efe452a4 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Mon, 15 Apr 2024 20:40:31 -0500 Subject: [PATCH 3/3] Refactor output tests and fix timezone bug --- cmd/dmarc-report-converter/output_test.go | 138 +++++++++++++--------- 1 file changed, 83 insertions(+), 55 deletions(-) diff --git a/cmd/dmarc-report-converter/output_test.go b/cmd/dmarc-report-converter/output_test.go index eae26f2..66cb5c4 100644 --- a/cmd/dmarc-report-converter/output_test.go +++ b/cmd/dmarc-report-converter/output_test.go @@ -61,70 +61,98 @@ func TestExternalTemplate(t *testing.T) { ` - report, err := dmarc.Parse([]byte(r), false, 1) - if err != nil { - t.Errorf("unexpected error parsing XML: %s", err) - } - - tmpl := `AssetsPath: {{ .AssetsPath }} -# Report -XMLName: {{ .Report.XMLName.Local }} -ReportMetadata: {{ .Report.ReportMetadata }} -PolicyPublished: {{ .Report.PolicyPublished }} -## Records -{{- range .Report.Records }} -- {{ . }} -{{ end -}} -## MessagesStats -{{ .Report.MessagesStats }} - -// Deprecated -XMLName: {{ .XMLName.Local }} -ReportMetadata: {{ .ReportMetadata }} -PolicyPublished: {{ .PolicyPublished }} -{{ .MessagesStats }} -` + tests := map[string]struct { + tmpl string + expect string + }{ + ".AssetsPath": { + `{{ .AssetsPath }}`, + `/foo`, + }, + ".Report.XMLName.Local": { + `{{ .Report.XMLName.Local }}`, + `feedback`, + }, + ".Report.ReportMetadata": { + `{{ .Report.ReportMetadata }}`, + `{Org 1 foo@bar.baz 1712279633.907274 {2024-04-04 00:00:00 +0000 UTC 2024-04-04 23:59:59 +0000 UTC}}`, + }, + ".Report.PolicyPublished": { + `{{ .Report.PolicyPublished }}`, + `{report.test r r none 100}`, + }, + ".Report.Records": { + "{{ range .Report.Records }}\n- {{ . }}\n{{ end -}}", + "\n- {{1.2.3.4 1 {none pass fail} } {headerfrom.test } {[{auth.test pass 1000073432} {cust.test pass 2020263919}] [{spf.test pass }]}}\n", + }, + ".Report.MessagesStats": { + `{{ .Report.MessagesStats }}`, + `{1 0 1 100}`, + }, - conf := config{ - Output: Output{ - AssetsPath: "/foo", - template: template.Must(template.New("report").Funcs( - template.FuncMap{ - "now": func(fmt string) string { - return time.Now().Format(fmt) - }, - }, - ).Parse(tmpl)), + // Deprecated + ".XMLName.Local": { + `{{ .XMLName.Local }}`, + `feedback`, + }, + ".ReportMetadata": { + `{{ .ReportMetadata }}`, + `{Org 1 foo@bar.baz 1712279633.907274 {2024-04-04 00:00:00 +0000 UTC 2024-04-04 23:59:59 +0000 UTC}}`, + }, + ".PolicyPublished": { + `{{ .PolicyPublished }}`, + `{report.test r r none 100}`, + }, + ".MessagesStats": { + `{{ .MessagesStats }}`, + `{1 0 1 100}`, }, } - var buf bytes.Buffer - out := newOutput(&conf) - out.w = &buf + // Set the timezone so that timestamps match regardless of local system time + origLocal := time.Local + + loc, err := time.LoadLocation("UTC") + if err != nil { + t.Errorf("unable to load UTC timezone: %s", err) + } + time.Local = loc + defer func() { + // Reset timezone + time.Local = origLocal + }() - err = out.template(report) + report, err := dmarc.Parse([]byte(r), false, 1) if err != nil { - t.Errorf("unexpected error building template: %s", err) + t.Fatalf("unexpected error parsing XML: %s", err) } - expect := `AssetsPath: /foo -# Report -XMLName: feedback -ReportMetadata: {Org 1 foo@bar.baz 1712279633.907274 {2024-04-03 19:00:00 -0500 CDT 2024-04-04 18:59:59 -0500 CDT}} -PolicyPublished: {report.test r r none 100} -## Records -- {{1.2.3.4 1 {none pass fail} } {headerfrom.test } {[{auth.test pass 1000073432} {cust.test pass 2020263919}] [{spf.test pass }]}} -## MessagesStats -{1 0 1 100} + for name, test := range tests { + conf := config{ + Output: Output{ + AssetsPath: "/foo", + template: template.Must(template.New("report").Funcs( + template.FuncMap{ + "now": func(fmt string) string { + return time.Now().Format(fmt) + }, + }, + ).Parse(test.tmpl)), + }, + } + + var buf bytes.Buffer + out := newOutput(&conf) + out.w = &buf -// Deprecated -XMLName: feedback -ReportMetadata: {Org 1 foo@bar.baz 1712279633.907274 {2024-04-03 19:00:00 -0500 CDT 2024-04-04 18:59:59 -0500 CDT}} -PolicyPublished: {report.test r r none 100} -{1 0 1 100} -` + err = out.template(report) + if err != nil { + t.Fatalf("%s: unexpected error building template: %s", name, err) + } + + if buf.String() != test.expect { + t.Errorf("%s\nWANT:\n%s\nGOT:\n%s", name, test.expect, buf.String()) + } - if buf.String() != expect { - t.Errorf("Oops!\nWANT:\n%s\nGOT:\n%s", expect, buf.String()) } }