mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 01:53:42 +01:00
95 lines
3.6 KiB
Go
95 lines
3.6 KiB
Go
package service
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestParseReportedDate_Formats(t *testing.T) {
|
|
utc := func(s string) time.Time {
|
|
v, err := time.Parse(time.RFC3339Nano, s)
|
|
if err != nil {
|
|
t.Fatalf("bad want %q: %v", s, err)
|
|
}
|
|
return v
|
|
}
|
|
cases := map[string]time.Time{
|
|
// iso 8601, with and without fractional seconds and zone
|
|
"2024-03-05T10:20:30.1234567Z": utc("2024-03-05T10:20:30.1234567Z"),
|
|
"2024-03-05T10:20:30.5+05:00": utc("2024-03-05T05:20:30.5Z"),
|
|
"2024-03-05T10:20:30.999-07:00": utc("2024-03-05T17:20:30.999Z"),
|
|
"2024-03-05T10:20:30Z": utc("2024-03-05T10:20:30Z"),
|
|
"2024-03-05T10:20:30+02:00": utc("2024-03-05T08:20:30Z"),
|
|
"2024-03-05T10:20:30+01:00": utc("2024-03-05T09:20:30Z"),
|
|
"2024-03-05T10:20:30-07:00": utc("2024-03-05T17:20:30Z"),
|
|
"2024-03-05T10:20:30": utc("2024-03-05T10:20:30Z"),
|
|
"2024-03-05T10:20:30.123": utc("2024-03-05T10:20:30.123Z"),
|
|
"2024-03-05 10:20:30": utc("2024-03-05T10:20:30Z"),
|
|
"2024-03-05 10:20": utc("2024-03-05T10:20:00Z"),
|
|
"2024-03-05": utc("2024-03-05T00:00:00Z"),
|
|
// month first
|
|
"03/05/2024 10:20:30": utc("2024-03-05T10:20:30Z"),
|
|
"03/05/2024": utc("2024-03-05T00:00:00Z"),
|
|
"3/5/2024 9:05": utc("2024-03-05T09:05:00Z"),
|
|
"3/5/2024": utc("2024-03-05T00:00:00Z"),
|
|
// day first
|
|
"05-03-2024 10:20:30": utc("2024-03-05T10:20:30Z"),
|
|
"05-03-2024": utc("2024-03-05T00:00:00Z"),
|
|
"5-3-2024 10:20": utc("2024-03-05T10:20:00Z"),
|
|
"2024/03/05 10:20:30": utc("2024-03-05T10:20:30Z"),
|
|
"2024/03/05 10:20": utc("2024-03-05T10:20:00Z"),
|
|
"2024/03/05": utc("2024-03-05T00:00:00Z"),
|
|
}
|
|
for in, want := range cases {
|
|
got, err := parseReportedDate(in)
|
|
if err != nil {
|
|
t.Errorf("%q: unexpected error %v", in, err)
|
|
continue
|
|
}
|
|
if !got.Equal(want) {
|
|
t.Errorf("%q: got %s want %s", in, got.UTC().Format(time.RFC3339Nano), want.Format(time.RFC3339Nano))
|
|
}
|
|
}
|
|
for _, in := range []string{
|
|
"", "yesterday", "13/02/2024", "2024-03-05T10:20:30+0200", "05.03.2024", "2024-13-01",
|
|
} {
|
|
if _, err := parseReportedDate(in); err == nil {
|
|
t.Errorf("%q: expected an error", in)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestReadReportedCSVRowPickedDate checks a row with no date in a picked date
|
|
// column fails, also where the date is optional, and that a file without a
|
|
// date column still reads rows without a date
|
|
func TestReadReportedCSVRowPickedDate(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
fields []string
|
|
dateColumn int
|
|
dateRequired bool
|
|
ok bool
|
|
}{
|
|
{"optional date picked and empty", []string{"bob@example.com", ""}, 1, false, false},
|
|
{"optional date picked and blank", []string{"bob@example.com", " "}, 1, false, false},
|
|
{"optional date picked and missing", []string{"bob@example.com"}, 1, false, false},
|
|
{"optional date picked and set", []string{"bob@example.com", "2026-02-01"}, 1, false, true},
|
|
{"no date column", []string{"bob@example.com", ""}, -1, false, true},
|
|
{"required date empty", []string{"bob@example.com", ""}, 1, true, false},
|
|
{"required date set", []string{"bob@example.com", "2026-02-01"}, 1, true, true},
|
|
}
|
|
for _, c := range cases {
|
|
result := &ReportedCSVResult{}
|
|
cell, ok := readReportedCSVRow(result, ReportedCSVRow{Line: 2, Fields: c.fields}, 0, c.dateColumn, c.dateRequired)
|
|
if ok != c.ok {
|
|
t.Fatalf("%s: ok = %v, want %v", c.name, ok, c.ok)
|
|
}
|
|
if !ok && (result.Failed != 1 || len(result.Errors) != 1 || result.Errors[0].Line != 2) {
|
|
t.Fatalf("%s: a failed row must be recorded with its line: %+v", c.name, result)
|
|
}
|
|
if ok && cell.email != "bob@example.com" {
|
|
t.Fatalf("%s: email = %q", c.name, cell.email)
|
|
}
|
|
}
|
|
}
|