mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 01:53:42 +01:00
539 lines
21 KiB
Go
539 lines
21 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/oapi-codegen/nullable"
|
|
"github.com/phishingclub/phishingclub/embedded"
|
|
"github.com/phishingclub/phishingclub/model"
|
|
"github.com/phishingclub/phishingclub/vo"
|
|
)
|
|
|
|
func TestDurationText(t *testing.T) {
|
|
const day = 24 * 60
|
|
cases := map[int]string{
|
|
0: "-",
|
|
1: "1 minute",
|
|
90: "90 minutes",
|
|
60: "1 hour",
|
|
day: "1 day",
|
|
3 * day: "3 days",
|
|
7 * day: "1 week",
|
|
14 * day: "2 weeks",
|
|
30 * day: "1 month",
|
|
90 * day: "3 months",
|
|
365 * day: "1 year",
|
|
10 * day: "10 days",
|
|
day + 30: "1470 minutes",
|
|
2 * 60: "2 hours",
|
|
210 * day: "7 months",
|
|
730 * day: "2 years",
|
|
1095 * day: "3 years",
|
|
}
|
|
for minutes, want := range cases {
|
|
if got := durationText(minutes); got != want {
|
|
t.Fatalf("durationText(%d) = %q, want %q", minutes, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProgramReportDetails(t *testing.T) {
|
|
start := time.Date(2026, 1, 5, 9, 0, 0, 0, time.UTC)
|
|
program := &model.Program{
|
|
Status: "active",
|
|
IsAnonymous: nullable.NewNullableWithValue(true),
|
|
OrderMode: nullable.NewNullableWithValue(model.ProgramOrderSequential),
|
|
LoopMode: nullable.NewNullableWithValue(model.ProgramLoopLoop),
|
|
StepIntervalMinutes: nullable.NewNullableWithValue(30 * 24 * 60),
|
|
SpreadMinutes: nullable.NewNullableWithValue(2 * 24 * 60),
|
|
NewMemberDelayMinutes: nullable.NewNullableWithValue(7 * 24 * 60),
|
|
StartAt: nullable.NewNullableWithValue(start),
|
|
BlackoutDates: nullable.NewNullableWithValue([]model.ProgramBlackout{
|
|
{From: "2026-12-24", To: "2026-12-26"},
|
|
{From: "2026-05-01", To: "2026-05-01"},
|
|
}),
|
|
Steps: []*model.ProgramStep{{}, {}},
|
|
}
|
|
setup := &programReportSetup{TemplateNames: []string{"Invoice", "Parcel"}, GroupNames: []string{"Sales"}}
|
|
got := map[string]string{}
|
|
labels := []string{}
|
|
for _, l := range programReportDetails(program, setup) {
|
|
got[l.Label] = l.Value
|
|
labels = append(labels, l.Label)
|
|
}
|
|
want := map[string]string{
|
|
"Status": "Active",
|
|
"Anonymous": "Yes",
|
|
"Templates": "2: Invoice, Parcel",
|
|
"Template order": "Same order for everyone",
|
|
"After the last template": "Start over",
|
|
"Time between templates": "1 month",
|
|
"Spread sends over": "2 days",
|
|
"Sending days": "Any day",
|
|
"Sending hours": "Any time",
|
|
"Skip dates": "24 Dec 2026 to 26 Dec 2026, 1 May 2026",
|
|
"First template to new members": "After 1 week",
|
|
"Start": "5 Jan 2026 09:00",
|
|
"End": "Runs until closed",
|
|
"Groups": "Sales",
|
|
}
|
|
for k, v := range want {
|
|
if got[k] != v {
|
|
t.Fatalf("%s = %q, want %q", k, got[k], v)
|
|
}
|
|
}
|
|
for _, l := range labels {
|
|
if strings.Contains(strings.ToLower(l), "time zone") || strings.Contains(strings.ToLower(l), "simulation") {
|
|
t.Fatalf("unexpected label %q", l)
|
|
}
|
|
}
|
|
program.EndAt = nullable.NewNullableWithValue(start.AddDate(1, 0, 0))
|
|
program.IsAnonymous = nullable.NewNullableWithValue(false)
|
|
for _, l := range programReportDetails(program, setup) {
|
|
if l.Label == "End" && l.Value != "5 Jan 2027 09:00" {
|
|
t.Fatalf("End = %q", l.Value)
|
|
}
|
|
if l.Label == "Anonymous" && l.Value != "No" {
|
|
t.Fatalf("Anonymous = %q", l.Value)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestProgramReportSections checks the report shows the figures and names of
|
|
// the program page, with training cards only for a program with training
|
|
func TestProgramReportSections(t *testing.T) {
|
|
program := &model.Program{
|
|
Name: nullable.NewNullableWithValue(*vo.NewString64Must("Year one")),
|
|
Status: "active",
|
|
}
|
|
facts := &programFacts{templateNames: map[string]string{"t1": "Invoice"}}
|
|
for _, p := range people(6) {
|
|
facts.sends = append(facts.sends, send(p, 10, "clicked"))
|
|
}
|
|
r := buildProgramResults(facts, programResultsInput{loc: time.UTC, now: resultsNow, recipients: 8, repeatMonths: 12})
|
|
d := programReportFromResults(program, &programReportSetup{}, r, resultsNow, nil, nil)
|
|
if d.HasTraining || !d.HasSends {
|
|
t.Fatalf("flags: training %v, sends %v", d.HasTraining, d.HasSends)
|
|
}
|
|
if got := d.Templates[0]; len(got.Cells) != 5 || got.Cells[2] != "6" || got.Pcts[2] != "100%" {
|
|
t.Fatalf("template row: %+v", got)
|
|
}
|
|
wantOrder := []string{"Recipients", "Sent", "Clicked link", "Data submitted", "Reported", "Repeat offenders"}
|
|
if len(d.Results) != len(wantOrder) {
|
|
t.Fatalf("expected %d results, got %+v", len(wantOrder), d.Results)
|
|
}
|
|
for i, l := range d.Results {
|
|
if l.Label != wantOrder[i] {
|
|
t.Fatalf("result %d = %q, want %q", i, l.Label, wantOrder[i])
|
|
}
|
|
}
|
|
if d.Results[1].Value != "6" || d.Results[1].Sub != "" {
|
|
t.Fatalf("sent card: %+v", d.Results[1])
|
|
}
|
|
if d.Results[2].Value != "6" || d.Results[2].Sub != "100% of sent" {
|
|
t.Fatalf("clicked card: %+v", d.Results[2])
|
|
}
|
|
if d.Outcome[0].Label != "Clicked link" || d.Outcome[0].Rate != 100 {
|
|
t.Fatalf("outcome: %+v", d.Outcome[0])
|
|
}
|
|
facts.sends = append(facts.sends, &programSend{Person: uuid.New(), SentAt: at(5), Training: true})
|
|
r = results(facts, time.UTC, resultsNow, nil, nil)
|
|
d = programReportFromResults(program, &programReportSetup{}, r, resultsNow, nil, nil)
|
|
if !d.HasTraining {
|
|
t.Fatalf("expected the training flag")
|
|
}
|
|
if l := d.Results[len(d.Results)-2]; l.Label != "Training completed" {
|
|
t.Fatalf("expected training cards before repeat offenders, got %+v", d.Results)
|
|
}
|
|
var buf bytes.Buffer
|
|
if err := programReportTemplate.Execute(&buf, d); err != nil {
|
|
t.Fatalf("render: %v", err)
|
|
}
|
|
out := buf.String()
|
|
for _, h := range []string{"Program Details", "Results", "Clicked link", "Data submitted", "Repeat offenders", "Results by Template", "Trend by Month", "Training completed"} {
|
|
if !strings.Contains(out, h) {
|
|
t.Fatalf("missing heading %q", h)
|
|
}
|
|
}
|
|
for _, banned := range []string{"imulation", "Withheld", "Configuration", "Funnel", "Resilience", "ifficulty", "Hard to spot",
|
|
"Coverage", "Phish prone", "Repeat clickers", "Reports per click", "Median time", "30 days", "Send number", "mail"} {
|
|
if strings.Contains(out, banned) {
|
|
t.Fatalf("report still says %q", banned)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestProgramReportTablesBounded checks a long program keeps the month table
|
|
// to the last months, the template table to the most sent templates and the
|
|
// details to the first names, each with a note of how many there are
|
|
func TestProgramReportTablesBounded(t *testing.T) {
|
|
program := &model.Program{
|
|
Name: nullable.NewNullableWithValue(*vo.NewString64Must("Long run")),
|
|
Status: "active",
|
|
Steps: make([]*model.ProgramStep, 40),
|
|
}
|
|
r := &ProgramResults{Summary: &ProgramSummary{}}
|
|
for i := 0; i < 30; i++ {
|
|
sent := 100 - i
|
|
key := time.Date(2024, time.Month(1+i), 1, 0, 0, 0, 0, time.UTC).Format("2006-01")
|
|
r.Months = append(r.Months, &ProgramResultRow{Key: key, Name: key, Sent: intPtr(10)})
|
|
r.Templates = append(r.Templates, &ProgramResultRow{Key: key, Name: "template " + key, Sent: &sent})
|
|
}
|
|
d := programReportFromResults(program, &programReportSetup{}, r, resultsNow, nil, nil)
|
|
if len(d.Months) != programReportTableMonths || d.MonthsTotal != 30 {
|
|
t.Fatalf("expected the last %d of 30 months, got %d of %d", programReportTableMonths, len(d.Months), d.MonthsTotal)
|
|
}
|
|
if d.Months[len(d.Months)-1].Cells[0] != r.Months[29].Name || d.Months[0].Cells[0] != r.Months[18].Name {
|
|
t.Fatalf("expected the latest months, got %v to %v", d.Months[0].Cells[0], d.Months[len(d.Months)-1].Cells[0])
|
|
}
|
|
if len(d.Templates) != programReportTableTemplates || d.TemplatesTotal != 30 || d.Templates[0].Cells[0] != r.Templates[0].Name {
|
|
t.Fatalf("expected the %d most sent of 30 templates, got %d of %d", programReportTableTemplates, len(d.Templates), d.TemplatesTotal)
|
|
}
|
|
var buf bytes.Buffer
|
|
if err := programReportTemplate.Execute(&buf, d); err != nil {
|
|
t.Fatalf("render: %v", err)
|
|
}
|
|
out := buf.String()
|
|
for _, note := range []string{"The last 12 of 30 months of the period are shown.", "The 15 most sent of 30 templates are shown."} {
|
|
if !strings.Contains(out, note) {
|
|
t.Fatalf("missing note %q", note)
|
|
}
|
|
}
|
|
names := []string{}
|
|
for i := 0; i < 8; i++ {
|
|
names = append(names, "T"+strconv.Itoa(i))
|
|
}
|
|
for _, l := range programReportDetails(program, &programReportSetup{TemplateNames: names}) {
|
|
if l.Label == "Templates" && l.Value != "40: T0, T1, T2, T3, T4 and 3 more" {
|
|
t.Fatalf("Templates = %q", l.Value)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestProgramReportShareBase checks the share under a card is of the people
|
|
// sent to and never passes 100%, also when fewer people are enrolled than
|
|
// were sent to, as after a recipient is deleted
|
|
func TestProgramReportShareBase(t *testing.T) {
|
|
program := &model.Program{
|
|
Name: nullable.NewNullableWithValue(*vo.NewString64Must("Base")),
|
|
Status: "active",
|
|
}
|
|
r := &ProgramResults{Summary: &ProgramSummary{
|
|
Recipients: 2, Sent: intPtr(4), ClickedLink: intPtr(4), SubmittedData: intPtr(1), Reported: intPtr(2),
|
|
HasTraining: true, TrainingStarted: intPtr(2), TrainingCompleted: intPtr(3),
|
|
}}
|
|
d := programReportFromResults(program, &programReportSetup{}, r, resultsNow, nil, nil)
|
|
want := map[string]string{
|
|
"Sent": "", "Clicked link": "100% of sent", "Data submitted": "25% of sent", "Reported": "50% of sent",
|
|
"Training started": "", "Training completed": "100% of started", "Repeat offenders": "",
|
|
}
|
|
for _, l := range d.Results {
|
|
if w, ok := want[l.Label]; ok && l.Sub != w {
|
|
t.Fatalf("%s sub = %q, want %q", l.Label, l.Sub, w)
|
|
}
|
|
}
|
|
for _, l := range d.Outcome {
|
|
if l.Rate > 100 {
|
|
t.Fatalf("%s rate %v passes 100", l.Label, l.Rate)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProgramReportEscapes(t *testing.T) {
|
|
program := &model.Program{
|
|
Name: nullable.NewNullableWithValue(*vo.NewString64Must("<script>alert(1)</script>")),
|
|
Status: "active",
|
|
}
|
|
p := uuid.New()
|
|
facts := &programFacts{templateNames: map[string]string{"t1": "<img src=x onerror=alert(1)>"}}
|
|
facts.sends = []*programSend{send(p, 10, "clicked")}
|
|
d := programReportFromResults(program, &programReportSetup{}, results(facts, time.UTC, resultsNow, nil, nil), resultsNow, nil, nil)
|
|
var buf bytes.Buffer
|
|
if err := programReportTemplate.Execute(&buf, d); err != nil {
|
|
t.Fatalf("render: %v", err)
|
|
}
|
|
out := buf.String()
|
|
if strings.Contains(out, "<script>alert") || strings.Contains(out, "<img src=x") {
|
|
t.Fatalf("report must escape user text")
|
|
}
|
|
if !strings.Contains(out, "<script>") {
|
|
t.Fatalf("expected escaped name in report")
|
|
}
|
|
}
|
|
|
|
func TestProgramReportResultsWithhold(t *testing.T) {
|
|
program := &model.Program{Status: "active"}
|
|
facts := &programFacts{anonymous: true, templateNames: map[string]string{"t1": "Invoice"}}
|
|
ps := people(10)
|
|
for i, p := range ps {
|
|
s := send(p, 10, "ignored")
|
|
if i < 2 {
|
|
s = send(p, 10, "clicked")
|
|
}
|
|
facts.sends = append(facts.sends, s)
|
|
}
|
|
// six training sends, two completed
|
|
for i, p := range ps[:6] {
|
|
tr := &programSend{Person: p, Campaign: uuid.New(), SentAt: at(9), Training: true}
|
|
if i < 2 {
|
|
tr.TrainingCompletedAt = tp(at(8))
|
|
}
|
|
facts.sends = append(facts.sends, tr)
|
|
}
|
|
// the sends are in May, which has settled on 4 July
|
|
later := time.Date(2025, 7, 4, 0, 0, 0, 0, time.UTC)
|
|
r := buildProgramResults(facts, programResultsInput{loc: time.UTC, now: later, recipients: 10, repeatMonths: 12})
|
|
d := programReportFromResults(program, &programReportSetup{}, r, later, nil, nil)
|
|
got := map[string]string{}
|
|
for _, l := range d.Results {
|
|
got[l.Label] = l.Value
|
|
}
|
|
want := map[string]string{
|
|
"Recipients": "10",
|
|
"Sent": "10",
|
|
"Clicked link": programWithheldText,
|
|
// a zero is withheld, it tells what everyone did
|
|
"Data submitted": programWithheldText,
|
|
"Reported": programWithheldText,
|
|
"Training started": programWithheldText,
|
|
"Training completed": programWithheldText,
|
|
}
|
|
for k, v := range want {
|
|
if got[k] != v {
|
|
t.Fatalf("%s = %q, want %q", k, got[k], v)
|
|
}
|
|
}
|
|
// an anonymous program never counts repeat offenders, so the report has
|
|
// no such line
|
|
if _, ok := got["Repeat offenders"]; ok {
|
|
t.Fatalf("the report of an anonymous program must leave out repeat offenders: %v", got)
|
|
}
|
|
// 4 people withhold every outcome
|
|
facts.sends = facts.sends[6:10]
|
|
r = results(facts, time.UTC, later, nil, nil)
|
|
d = programReportFromResults(program, &programReportSetup{}, r, later, nil, nil)
|
|
for _, l := range d.Results {
|
|
if (l.Label == "Clicked link" || l.Label == "Reported") && !l.Withheld {
|
|
t.Fatalf("an outcome of 4 people must be withheld: %+v", l)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProgramReportPeriodNamed(t *testing.T) {
|
|
loc, err := time.LoadLocation("Europe/Copenhagen")
|
|
if err != nil {
|
|
t.Skip("no zone data")
|
|
}
|
|
p := &model.Program{TimeZone: nullable.NewNullableWithValue("Europe/Copenhagen")}
|
|
// date only values are read in the program zone and the last day is whole
|
|
from, to, err := programReportPeriod(p, "2026-03-10", "2026-03-10")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !from.Equal(time.Date(2026, 3, 10, 0, 0, 0, 0, loc)) || !to.Equal(time.Date(2026, 3, 11, 0, 0, 0, 0, loc)) {
|
|
t.Fatalf("got %v to %v", from, to)
|
|
}
|
|
// a time snaps to its day in the program zone: late in the UTC day is the
|
|
// next day in Copenhagen
|
|
from, to, err = programReportPeriod(p, "2026-03-10T23:30:00Z", "2026-03-12T10:00:00Z")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !from.Equal(time.Date(2026, 3, 11, 0, 0, 0, 0, loc)) || !to.Equal(time.Date(2026, 3, 13, 0, 0, 0, 0, loc)) {
|
|
t.Fatalf("got %v to %v", from, to)
|
|
}
|
|
// the last day across the spring forward change is still a whole day
|
|
_, to, err = programReportPeriod(p, "", "2026-03-29")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !to.Equal(time.Date(2026, 3, 30, 0, 0, 0, 0, loc)) {
|
|
t.Fatalf("got %v", to)
|
|
}
|
|
if f, tt, err := programReportPeriod(p, "", ""); err != nil || f != nil || tt != nil {
|
|
t.Fatal("open bounds stay open")
|
|
}
|
|
if _, _, err := programReportPeriod(p, "10/03/2026", ""); err == nil {
|
|
t.Fatal("a bad date must fail")
|
|
}
|
|
}
|
|
|
|
func TestProgramReportPeriodAnonymousWholeMonths(t *testing.T) {
|
|
loc, err := time.LoadLocation("Europe/Copenhagen")
|
|
if err != nil {
|
|
t.Skip("no zone data")
|
|
}
|
|
p := &model.Program{
|
|
TimeZone: nullable.NewNullableWithValue("Europe/Copenhagen"),
|
|
IsAnonymous: nullable.NewNullableWithValue(true),
|
|
}
|
|
from, to, err := programReportPeriod(p, "2026-10-01", "2026-11-01")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !from.Equal(time.Date(2026, 10, 1, 0, 0, 0, 0, loc)) || !to.Equal(time.Date(2026, 11, 1, 0, 0, 0, 0, loc)) {
|
|
t.Fatalf("got %v to %v", from, to)
|
|
}
|
|
// a month start given as a time in the program zone is fine
|
|
if _, _, err := programReportPeriod(p, "2026-09-01T00:00:00+02:00", "2026-11-01T00:00:00+01:00"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if f, tt, err := programReportPeriod(p, "", ""); err != nil || f != nil || tt != nil {
|
|
t.Fatal("all time must be allowed")
|
|
}
|
|
bad := [][2]string{
|
|
{"2026-10-02", "2026-11-01"},
|
|
{"2026-10-01", "2026-10-31"},
|
|
{"2026-10-01", ""},
|
|
{"", "2026-11-01"},
|
|
{"2026-11-01", "2026-10-01"},
|
|
{"2026-10-01", "2026-10-01"},
|
|
// midnight UTC is not the month start in Copenhagen
|
|
{"2026-10-01T00:00:00Z", "2026-11-01T00:00:00Z"},
|
|
}
|
|
for _, b := range bad {
|
|
_, _, err := programReportPeriod(p, b[0], b[1])
|
|
if err == nil || !strings.Contains(err.Error(), "whole calendar months") {
|
|
t.Fatalf("%v: want whole month error, got %v", b, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestProgramRowsMixedReportCells checks a mixed program report row lists the
|
|
// phishing cells then the training cells, a count the row does not hold as
|
|
// "-"
|
|
func TestProgramRowsMixedReportCells(t *testing.T) {
|
|
row := programReportResultRow("Course", &ProgramResultRow{
|
|
TrainingSent: intPtr(4), TrainingStarted: intPtr(2), TrainingCompleted: intPtr(1),
|
|
}, true, true)
|
|
want := []string{"Course", "-", "-", "-", "-", "4", "2", "1"}
|
|
if strings.Join(row.Cells, ",") != strings.Join(want, ",") {
|
|
t.Fatalf("cells %v, want %v", row.Cells, want)
|
|
}
|
|
if row.Pcts[6] != "50%" || row.Pcts[7] != "50%" || row.Pcts[2] != "" {
|
|
t.Fatalf("percents %v", row.Pcts)
|
|
}
|
|
// a phishing only report row keeps its 5 cells
|
|
row = programReportResultRow("Phish", &ProgramResultRow{Sent: intPtr(2), Clicked: intPtr(1), Submitted: intPtr(0), Reported: intPtr(1)}, true, false)
|
|
if strings.Join(row.Cells, ",") != "Phish,2,1,0,1" {
|
|
t.Fatalf("phishing cells %v", row.Cells)
|
|
}
|
|
}
|
|
|
|
// TestProgramAnonymousTrainingOnlyReport checks the report and the CSV export
|
|
// of an anonymous program that only sends training: the cards are Recipients,
|
|
// Sent from the training sends and the 2 training cards, there are no month
|
|
// or template rows, the PDF says the training results are in the totals only,
|
|
// and the CSV totals hold a Training sent line. A small program withholds Sent.
|
|
func TestProgramAnonymousTrainingOnlyReport(t *testing.T) {
|
|
program := &model.Program{Status: model.ProgramStatusActive, IsAnonymous: nullable.NewNullableWithValue(true)}
|
|
facts := &programFacts{anonymous: true, templateNames: map[string]string{"tr": "Course"}}
|
|
for i, p := range people(20) {
|
|
facts.sends = append(facts.sends, trainingSend(p, 10, "tr", i < 12, i < 6))
|
|
}
|
|
r := buildProgramResults(facts, programResultsInput{loc: time.UTC, now: anonymousNow, recipients: 20, repeatMonths: 12})
|
|
if r.Summary.HasPhishing || !r.Summary.HasTraining || !r.Summary.HasSent {
|
|
t.Fatalf("a sent training program: %+v", r.Summary)
|
|
}
|
|
if len(r.Months) != 0 || len(r.Templates) != 0 {
|
|
t.Fatalf("an anonymous program keeps training out of its rows, got %d months and %d templates", len(r.Months), len(r.Templates))
|
|
}
|
|
d := programReportFromResults(program, &programReportSetup{}, r, anonymousNow, nil, nil)
|
|
want := [][2]string{{"Recipients", "20"}, {"Sent", "20"}, {"Training started", "12"}, {"Training completed", "6"}}
|
|
if len(d.Results) != len(want) {
|
|
t.Fatalf("want %d cards, got %+v", len(want), d.Results)
|
|
}
|
|
for i, w := range want {
|
|
l := d.Results[i]
|
|
if l.Label != w[0] || l.Value != w[1] || l.Withheld {
|
|
t.Fatalf("card %d = %s %s, want %s %s", i, l.Label, l.Value, w[0], w[1])
|
|
}
|
|
// an anonymous program counts sends, so a card shows no share
|
|
if l.Sub != "" {
|
|
t.Fatalf("an anonymous card has no share: %+v", l)
|
|
}
|
|
}
|
|
if len(d.Outcome) != 0 || len(d.Trend) != 0 || len(d.Months) != 0 || len(d.Templates) != 0 {
|
|
t.Fatalf("no outcome, trend or tables: %+v", d)
|
|
}
|
|
html, err := renderProgramReportContent(embedded.DefaultProgramReportHTML, d)
|
|
if err != nil {
|
|
t.Fatalf("render: %v", err)
|
|
}
|
|
if !strings.Contains(html, "Anonymous programs show training results in the totals only") {
|
|
t.Fatalf("the report must say why there are no tables")
|
|
}
|
|
for _, gone := range []string{"Trend by Month", "Results by Template", "Nothing was sent in this period.", "Clicked link"} {
|
|
if strings.Contains(html, gone) {
|
|
t.Fatalf("the report must leave out %q", gone)
|
|
}
|
|
}
|
|
// the CSV holds the totals with Training sent and no rows
|
|
csv := programAggregateCSV(r)
|
|
keys := []string{}
|
|
for _, row := range csv[1:] {
|
|
if row[0] != "Result" {
|
|
t.Fatalf("an anonymous training program has no %s rows: %v", row[0], row)
|
|
}
|
|
keys = append(keys, row[1]+"="+row[3])
|
|
}
|
|
if got := strings.Join(keys, ","); got != "recipients=20,trainingSent=20,trainingStarted=12,trainingCompleted=6" {
|
|
t.Fatalf("csv totals %s", got)
|
|
}
|
|
|
|
// 4 people could be singled out, so every send count is withheld
|
|
facts.sends = facts.sends[:4]
|
|
r = buildProgramResults(facts, programResultsInput{loc: time.UTC, now: anonymousNow, recipients: 4, repeatMonths: 12})
|
|
d = programReportFromResults(program, &programReportSetup{}, r, anonymousNow, nil, nil)
|
|
if l := d.Results[1]; l.Label != "Sent" || !l.Withheld || l.Value != programWithheldText {
|
|
t.Fatalf("a small training program withholds Sent: %+v", l)
|
|
}
|
|
for _, row := range programAggregateCSV(r)[1:] {
|
|
if row[1] == "trainingSent" && (row[3] != "" || row[9] != "yes") {
|
|
t.Fatalf("a withheld training sent line has no value: %v", row)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestProgramReportPeriodRepeatOffenders checks a report period counts its own
|
|
// sends and reads the repeat offender window back from the end of the period
|
|
func TestProgramReportPeriodRepeatOffenders(t *testing.T) {
|
|
to := time.Date(2025, 5, 1, 0, 0, 0, 0, time.UTC)
|
|
now := time.Date(2025, 6, 1, 12, 0, 0, 0, time.UTC)
|
|
facts := &programFacts{templateNames: map[string]string{}}
|
|
for _, p := range people(10) {
|
|
// clicks nine months and ten days before the end
|
|
for _, sentAt := range []time.Time{to.AddDate(0, -9, 0), to.AddDate(0, 0, -10)} {
|
|
sd := &programSend{Person: p, Campaign: uuid.New(), SentAt: sentAt, Template: "t1"}
|
|
sd.ClickedAt = tp(sentAt.Add(time.Hour))
|
|
facts.sends = append(facts.sends, sd)
|
|
}
|
|
// a send after the end is not read
|
|
after := &programSend{Person: p, Campaign: uuid.New(), SentAt: to.AddDate(0, 0, 5), Template: "t1"}
|
|
after.ClickedAt = tp(after.SentAt.Add(time.Hour))
|
|
facts.sends = append(facts.sends, after)
|
|
}
|
|
in := programResultsInput{loc: time.UTC, now: now, to: &to, repeatMonths: 12}
|
|
r := buildProgramResults(facts, in)
|
|
if *r.Summary.RepeatOffenders != 10 || *r.Summary.Sent != 10 || r.Summary.SentTotal != 20 {
|
|
t.Fatalf("period summary: %+v", r.Summary)
|
|
}
|
|
// a 6 month window from the end of the period leaves one click each
|
|
in.repeatMonths = 6
|
|
if r := buildProgramResults(facts, in); *r.Summary.RepeatOffenders != 0 {
|
|
t.Fatalf("6 months: %+v", r.Summary)
|
|
}
|
|
// a period of the last week before the end holds no sends
|
|
from := to.AddDate(0, 0, -7)
|
|
in = programResultsInput{loc: time.UTC, now: now, from: &from, to: &to, repeatMonths: 12}
|
|
d := programReportFromResults(&model.Program{Status: "active"}, &programReportSetup{}, buildProgramResults(facts, in), now, &from, &to)
|
|
if d.HasSends || d.Period != "24 Apr 2025 to 30 Apr 2025" {
|
|
t.Fatalf("period = %q, sends %v", d.Period, d.HasSends)
|
|
}
|
|
}
|