Skip to content

Commit

Permalink
Refactor output tests and fix timezone bug
Browse files Browse the repository at this point in the history
  • Loading branch information
moorereason committed Apr 16, 2024
1 parent 3c09e05 commit 068948c
Showing 1 changed file with 83 additions and 55 deletions.
138 changes: 83 additions & 55 deletions cmd/dmarc-report-converter/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,70 +61,98 @@ func TestExternalTemplate(t *testing.T) {
</feedback>
`

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 [email protected] 1712279633.907274 {2024-04-04 00:00:00 &#43;0000 UTC 2024-04-04 23:59:59 &#43;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 [email protected] 1712279633.907274 {2024-04-04 00:00:00 &#43;0000 UTC 2024-04-04 23:59:59 &#43;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 [email protected] 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 [email protected] 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())
}
}

0 comments on commit 068948c

Please sign in to comment.