Files
phishingclub/backend/service/programResultsAnonymous_test.go
2026-12-01 21:25:01 +01:00

774 lines
33 KiB
Go

package service
import (
"fmt"
"math/rand"
"testing"
"time"
"github.com/google/uuid"
"github.com/oapi-codegen/nullable"
"github.com/phishingclub/phishingclub/model"
)
// TestProgramSummaryComplement checks the anonymous result tiles hide a count
// that leaves fewer than programMinGroup people out, hide zero and keep a
// count with enough people on both sides
func TestProgramSummaryComplement(t *testing.T) {
facts := &programFacts{anonymous: true, templateNames: map[string]string{}}
for i, p := range people(20) {
outcome := "reported"
if i < 15 {
outcome = "clickedReported"
}
facts.sends = append(facts.sends, send(p, 10, outcome))
}
anon := results(facts, time.UTC, anonymousNow, nil, nil).Summary
if !anon.Suppressed || anon.Reported != nil {
t.Fatalf("counts that leave under %d out must be null: %+v", programMinGroup, anon)
}
if anon.ClickedLink == nil || *anon.ClickedLink != 15 {
t.Fatalf("counts with enough people on both sides stay: %+v", anon)
}
if anon.SubmittedData != nil {
t.Fatalf("zero is withheld: %+v", anon)
}
facts.anonymous = false
named := results(facts, time.UTC, resultsNow, nil, nil).Summary
if named.Suppressed || named.Reported == nil || *named.Reported != 20 {
t.Fatalf("a named program keeps every count: %+v", named)
}
}
// anonMonthSends builds sends in a month of 2026, n people of which the first
// clicked clicked
func anonMonthSends(ps []uuid.UUID, month time.Month, clicked int) []*programSend {
out := []*programSend{}
for i, p := range ps {
sd := &programSend{Person: p, Campaign: uuid.New(), SentAt: time.Date(2026, month, 12, 10, 0, 0, 0, time.UTC), Template: "t1"}
if i < clicked {
sd.ClickedAt = tp(sd.SentAt.Add(time.Hour))
}
out = append(out, sd)
}
return out
}
// TestProgramAnonymousNoDifferencing is the case of a report period that
// would give away one person: 20 people sent to in January, 6 clicked, and
// one new person in March who clicked. January to February shows 6 clicks;
// January to March must not show 7, as the difference names the March
// recipient. Neither may March alone or the program page ranges that hold it.
func TestProgramAnonymousNoDifferencing(t *testing.T) {
now := time.Date(2026, 6, 15, 12, 0, 0, 0, time.UTC)
facts := &programFacts{anonymous: true, templateNames: map[string]string{"t1": "Template"}}
facts.sends = append(facts.sends, anonMonthSends(people(20), time.January, 6)...)
facts.sends = append(facts.sends, anonMonthSends(people(1), time.March, 1)...)
month := func(m time.Month) *time.Time {
t := time.Date(2026, m, 1, 0, 0, 0, 0, time.UTC)
return &t
}
period := func(from, to time.Month) *ProgramSummary {
return results(facts, time.UTC, now, month(from), month(to)).Summary
}
if s := period(time.January, time.March); s.ClickedLink == nil || *s.ClickedLink != 6 || *s.Sent != 20 {
t.Fatalf("January to February: %+v", s)
}
for _, s := range []*ProgramSummary{
period(time.January, time.April),
period(time.March, time.April),
period(time.February, time.June),
results(facts, time.UTC, now, nil, nil).Summary,
} {
if s.ClickedLink != nil || s.SubmittedData != nil || s.Reported != nil || !s.Suppressed {
t.Fatalf("a period that holds March must withhold its outcomes: %+v", s)
}
}
// the send total of a period that holds March is withheld too, as 21
// against the 20 of January to February would show March sent to 1 person
for _, s := range []*ProgramSummary{period(time.January, time.April), period(time.March, time.April)} {
if s.Sent != nil {
t.Fatalf("a period that holds March must withhold its send total: %+v", s)
}
}
in := programResultsInput{now: now, enrollment: programEnrollmentCounts{total: 21, active: 21}}
for _, key := range []string{ProgramTimeRange90Days, ProgramTimeRange12Months, ProgramTimeRangeAll} {
if tl := tilesFor(facts, in, key, false); tl.ClickedLink != nil || tl.Sent != nil || !tl.Suppressed {
t.Fatalf("%s holds March and must withhold clicks: %+v", key, tl)
}
}
}
// TestProgramAnonymousViewsAreSumsOfPassingMonths checks the rule behind the
// anonymous results on random data: every view an operator can read, any run
// of whole months and every time range on any day, shows an outcome only when
// each of its months shows it alone, and then shows their sum. Two views then
// differ by whole months that pass on their own.
func TestProgramAnonymousViewsAreSumsOfPassingMonths(t *testing.T) {
rng := rand.New(rand.NewSource(3))
facts := &programFacts{anonymous: true, templateNames: map[string]string{"t1": "Template"}}
ps := people(40)
for m := time.January; m <= time.September; m++ {
n := []int{3, 6, 12, 25, 40}[rng.Intn(5)]
group := append([]uuid.UUID{}, ps...)
rng.Shuffle(len(group), func(i, j int) { group[i], group[j] = group[j], group[i] })
for _, sd := range anonMonthSends(group[:n], m, rng.Intn(n+1)) {
sd.SentAt = sd.SentAt.Add(time.Duration(rng.Intn(15*24)) * time.Hour)
if rng.Intn(4) == 0 {
sd.ReportedAt = tp(sd.SentAt.Add(2 * time.Hour))
}
facts.sends = append(facts.sends, sd)
}
}
now := time.Date(2026, 10, 20, 12, 0, 0, 0, time.UTC)
month := func(m time.Month) *time.Time {
t := time.Date(2026, m, 1, 0, 0, 0, 0, time.UTC)
return &t
}
alone := map[time.Month]*ProgramSummary{}
for m := time.January; m <= time.September; m++ {
alone[m] = results(facts, time.UTC, now, month(m), month(m+1)).Summary
}
check := func(label string, from, to time.Month, sent, clicked, reported *int) {
t.Helper()
wantSent, wantClicked, wantReported := 0, 0, 0
sentOK, clickedOK, reportedOK := true, true, true
for m := from; m < to; m++ {
if alone[m].Sent == nil {
sentOK = false
} else {
wantSent += *alone[m].Sent
}
if alone[m].ClickedLink == nil {
clickedOK = false
} else {
wantClicked += *alone[m].ClickedLink
}
if alone[m].Reported == nil {
reportedOK = false
} else {
wantReported += *alone[m].Reported
}
}
if sentOK != (sent != nil) || (sent != nil && *sent != wantSent) {
t.Fatalf("%s: sent %v, want %d shown %v", label, sent, wantSent, sentOK)
}
if clickedOK != (clicked != nil) || (clicked != nil && *clicked != wantClicked) {
t.Fatalf("%s: clicked %v, want %d shown %v", label, clicked, wantClicked, clickedOK)
}
if reportedOK != (reported != nil) || (reported != nil && *reported != wantReported) {
t.Fatalf("%s: reported %v, want %d shown %v", label, reported, wantReported, reportedOK)
}
}
for from := time.January; from <= time.September; from++ {
for to := from + 1; to <= time.October; to++ {
s := results(facts, time.UTC, now, month(from), month(to)).Summary
check(fmt.Sprintf("%v to %v", from, to), from, to, s.Sent, s.ClickedLink, s.Reported)
}
}
// the time ranges on every day of a month read the same whole months
in := programResultsInput{enrollment: programEnrollmentCounts{total: 40, active: 40}}
for d := 0; d < 31; d++ {
in.now = time.Date(2026, 10, 4, 6, 0, 0, 0, time.UTC).AddDate(0, 0, d)
for key, n := range map[string]int{ProgramTimeRange30Days: 1, ProgramTimeRange90Days: 3, ProgramTimeRange12Months: 9} {
tl := tilesFor(facts, in, key, false)
check(fmt.Sprintf("day %d %s", d, key), time.October-time.Month(n), time.October, tl.Sent, tl.ClickedLink, tl.Reported)
}
}
}
// TestProgramSummaryAnonymous checks an anonymous program counts sends and
// withholds small and nearly total counts
func TestProgramSummaryAnonymous(t *testing.T) {
facts := &programFacts{anonymous: true, templateNames: map[string]string{}}
for i, p := range people(12) {
outcome := "ignored"
switch {
case i < 2:
outcome = "submitted"
case i < 8:
outcome = "reported"
}
facts.sends = append(facts.sends, send(p, 10, outcome), send(p, 20, outcome))
}
s := results(facts, time.UTC, anonymousNow, nil, nil).Summary
if !s.Suppressed || s.ClickedLink != nil || s.SubmittedData != nil || s.RepeatOffenders != nil {
t.Fatalf("2 of 12 must be withheld: %+v", s)
}
// 6 of 12 people reported, shown as the 12 sends they reported
if s.Sent == nil || *s.Sent != 24 || s.Reported == nil || *s.Reported != 12 {
t.Fatalf("24 sent and 12 reported are shown: %+v", s)
}
// fewer than 5 people withhold every outcome and the send total too, so a
// one month range cannot single the month out
facts.sends = facts.sends[:8]
s = results(facts, time.UTC, anonymousNow, nil, nil).Summary
if s.Reported != nil || s.ClickedLink != nil || s.Sent != nil {
t.Fatalf("4 people withhold every outcome and the send total: %+v", s)
}
}
// TestProgramSummaryAnonymousSmallMonthHidesSent checks the send total of an
// anonymous program is withheld for a range that holds a month with fewer than
// the minimum people, so a one month range or report cannot single it out,
// while a range of only large months shows the send total
func TestProgramSummaryAnonymousSmallMonthHidesSent(t *testing.T) {
now := time.Date(2025, 7, 10, 12, 0, 0, 0, time.UTC)
monthStart := func(m time.Month) time.Time { return time.Date(2025, m, 1, 0, 0, 0, 0, time.UTC) }
sentIn := func(p uuid.UUID, m time.Month) *programSend {
return &programSend{Person: p, SentAt: monthStart(m).Add(9 * 24 * time.Hour), Template: "t1"}
}
facts := &programFacts{anonymous: true, templateNames: map[string]string{}}
// May and June each have 10 people, a settled month
for _, m := range []time.Month{time.May, time.June} {
for _, p := range people(10) {
facts.sends = append(facts.sends, sentIn(p, m))
}
}
// a small April month of 3 people
for _, p := range people(3) {
facts.sends = append(facts.sends, sentIn(p, time.April))
}
// the whole range holds the small April month, so the send total is withheld
s := results(facts, time.UTC, now, nil, nil).Summary
if s.Sent != nil {
t.Fatalf("a range with a small month must withhold the send total: %+v", s)
}
// the total of every template follows, so subtracting the shown months
// from it can not give the small month, while HasSent still says sends went out
if s.SentTotal != 0 || !s.SentTotalHidden || !s.HasSent {
t.Fatalf("a range with a small month must withhold the total too: %+v", s)
}
// a range of May and June only shows the summed send total
from := monthStart(time.May)
s = results(facts, time.UTC, now, &from, nil).Summary
if s.Sent == nil || *s.Sent != 20 {
t.Fatalf("a range of large months shows the send total: %+v", s)
}
if s.SentTotal != 20 || s.SentTotalHidden || !s.HasSent {
t.Fatalf("a range of large months shows the total: %+v", s)
}
}
// TestProgramSummaryAnonymousSmallTrainingHidesTotal checks the total of every
// template is withheld when the training sends of a month are too few, so
// subtracting Sent from it can not give them
func TestProgramSummaryAnonymousSmallTrainingHidesTotal(t *testing.T) {
now := time.Date(2025, 7, 10, 12, 0, 0, 0, time.UTC)
sentAt := time.Date(2025, time.May, 10, 0, 0, 0, 0, time.UTC)
facts := &programFacts{anonymous: true, templateNames: map[string]string{}}
ps := people(20)
for _, p := range ps {
facts.sends = append(facts.sends, &programSend{Person: p, SentAt: sentAt, Template: "t1"})
}
// one person got a training send in the same month
facts.sends = append(facts.sends, &programSend{Person: ps[0], SentAt: sentAt, Template: "t2", Training: true})
s := results(facts, time.UTC, now, nil, nil).Summary
if s.Sent == nil || *s.Sent != 20 {
t.Fatalf("20 phishing sends show: %+v", s)
}
if s.SentTotal != 0 || !s.SentTotalHidden || !s.HasSent {
t.Fatalf("a small training month must withhold the total: %+v", s)
}
}
// TestProgramAnonymousTemplateRowHoldsSmallMonth checks a template row is
// withheld when a month of the view sent that template to too few people, so
// its total can not be compared with the month rows or another view
func TestProgramAnonymousTemplateRowHoldsSmallMonth(t *testing.T) {
now := time.Date(2025, 7, 10, 12, 0, 0, 0, time.UTC)
jan := time.Date(2025, time.January, 10, 0, 0, 0, 0, time.UTC)
feb := time.Date(2025, time.February, 10, 0, 0, 0, 0, time.UTC)
facts := &programFacts{anonymous: true, templateNames: map[string]string{}}
for _, p := range people(10) {
facts.sends = append(facts.sends, &programSend{Person: p, SentAt: jan, Template: "t1"})
}
for _, p := range people(2) {
facts.sends = append(facts.sends, &programSend{Person: p, SentAt: feb, Template: "t1"})
}
both := results(facts, time.UTC, now, nil, nil)
if len(both.Templates) != 1 || both.Templates[0].Sent != nil || !both.Templates[0].Suppressed {
t.Fatalf("a view holding the small February must withhold the template row: %+v", both.Templates)
}
to := time.Date(2025, time.February, 1, 0, 0, 0, 0, time.UTC)
janOnly := results(facts, time.UTC, now, nil, &to)
if len(janOnly.Templates) != 1 || janOnly.Templates[0].Sent == nil || *janOnly.Templates[0].Sent != 10 {
t.Fatalf("a view of January only shows the template row: %+v", janOnly.Templates)
}
}
// anonGapMonth returns the January sends of 20 people where the first clicked
// people clicked, the first submitted of them also submitted and the first
// reported reported
func anonGapMonth(ps []uuid.UUID, clicked, submitted, reported int) []*programSend {
out := anonMonthSends(ps, time.January, clicked)
for i, sd := range out {
if i < submitted {
sd.SubmittedAt = tp(sd.SentAt.Add(2 * time.Hour))
}
if i < reported {
sd.ReportedAt = tp(sd.SentAt.Add(3 * time.Hour))
}
}
return out
}
// TestProgramAnonymousWithholdsZero checks an anonymous program withholds an
// outcome nobody had in a month, in the summary, the tiles and every view
// that holds the month, while the other outcomes still show
func TestProgramAnonymousWithholdsZero(t *testing.T) {
now := time.Date(2026, 6, 15, 12, 0, 0, 0, time.UTC)
facts := &programFacts{anonymous: true, templateNames: map[string]string{"t1": "Template"}}
facts.sends = anonGapMonth(people(20), 8, 0, 6)
jan := time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC)
feb := jan.AddDate(0, 1, 0)
for _, s := range []*ProgramSummary{
results(facts, time.UTC, now, &jan, &feb).Summary,
results(facts, time.UTC, now, nil, nil).Summary,
} {
if s.SubmittedData != nil || !s.Suppressed {
t.Fatalf("a submitted count of zero must be withheld: %+v", s)
}
if intOr(s.ClickedLink) != 8 || intOr(s.Reported) != 6 || intOr(s.Sent) != 20 {
t.Fatalf("the other outcomes must show: %+v", s)
}
}
in := programResultsInput{now: now, enrollment: programEnrollmentCounts{total: 20, active: 20}}
tl := tilesFor(facts, in, ProgramTimeRangeAll, false)
if tl.SubmittedData != nil || intOr(tl.ClickedLink) != 8 || !tl.Suppressed {
t.Fatalf("tiles must withhold the zero: %+v", tl)
}
// the PDF and the CSV read the same summary
csv := programAggregateCSV(results(facts, time.UTC, now, nil, nil))
for _, row := range csv {
if len(row) > 9 && row[2] == "Data submitted" && (row[3] != "" || row[9] != "yes") {
t.Fatalf("the CSV must withhold the zero: %v", row)
}
}
}
// TestProgramAnonymousRelatedGap checks clicked and submitted, and training
// started and completed, are both withheld when they differ by fewer than
// programMinGroup people in a month, as the difference would name them, and
// that a view holding that month withholds them too
func TestProgramAnonymousRelatedGap(t *testing.T) {
now := time.Date(2026, 6, 15, 12, 0, 0, 0, time.UTC)
jan := time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC)
feb := jan.AddDate(0, 1, 0)
summary := func(facts *programFacts) *ProgramSummary {
return results(facts, time.UTC, now, &jan, &feb).Summary
}
// 8 clicked and 6 of them submitted: the 2 who only clicked would show
gap := &programFacts{anonymous: true, templateNames: map[string]string{"t1": "Template"}}
gap.sends = anonGapMonth(people(20), 8, 6, 6)
s := summary(gap)
if s.ClickedLink != nil || s.SubmittedData != nil || !s.Suppressed {
t.Fatalf("a clicked and submitted gap of 2 must withhold both: %+v", s)
}
if intOr(s.Reported) != 6 {
t.Fatalf("reported is not related and must show: %+v", s)
}
in := programResultsInput{now: now, enrollment: programEnrollmentCounts{total: 20, active: 20}}
if tl := tilesFor(gap, in, ProgramTimeRangeAll, false); tl.ClickedLink != nil || tl.SubmittedData != nil || !tl.Suppressed {
t.Fatalf("tiles must withhold the gap: %+v", tl)
}
// a later month that passes on its own does not bring them back, as the
// view would differ from February alone by January
ps := people(20)
later := anonMonthSends(ps, time.February, 12)
for i, sd := range later {
if i < 6 {
sd.SubmittedAt = tp(sd.SentAt.Add(2 * time.Hour))
}
}
gap.sends = append(gap.sends, later...)
mar := feb.AddDate(0, 1, 0)
if s := results(gap, time.UTC, now, &jan, &mar).Summary; s.ClickedLink != nil || s.SubmittedData != nil {
t.Fatalf("a view holding January must withhold both: %+v", s)
}
if s := results(gap, time.UTC, now, &feb, &mar).Summary; intOr(s.ClickedLink) != 12 || intOr(s.SubmittedData) != 6 {
t.Fatalf("February alone passes: %+v", s)
}
// a gap of 5 is shown
wide := &programFacts{anonymous: true, templateNames: map[string]string{"t1": "Template"}}
wide.sends = anonGapMonth(people(20), 11, 6, 6)
if s := summary(wide); intOr(s.ClickedLink) != 11 || intOr(s.SubmittedData) != 6 {
t.Fatalf("a gap of 5 must show: %+v", s)
}
// training started and completed differ by 2
training := &programFacts{anonymous: true, templateNames: map[string]string{"t1": "Template"}}
for i, p := range people(20) {
sd := &programSend{Person: p, Campaign: uuid.New(), SentAt: time.Date(2026, time.January, 12, 10, 0, 0, 0, time.UTC), Template: "t1", Training: true}
if i < 10 {
sd.TrainingStartedAt = tp(sd.SentAt.Add(time.Hour))
}
if i < 8 {
sd.TrainingCompletedAt = tp(sd.SentAt.Add(2 * time.Hour))
}
training.sends = append(training.sends, sd)
}
if s := summary(training); s.TrainingStarted != nil || s.TrainingCompleted != nil {
t.Fatalf("a training gap of 2 must withhold both: %+v", s)
}
}
// TestProgramAnonymousRowsSizesOnly checks an anonymous row shows its size
// only and withholds it for fewer than the minimum people, while a named row
// shows every count
func TestProgramAnonymousRowsSizesOnly(t *testing.T) {
now := time.Date(2026, 6, 20, 12, 0, 0, 0, time.UTC)
may := time.Date(2026, 5, 10, 9, 0, 0, 0, time.UTC)
facts := &programFacts{anonymous: true, templateNames: map[string]string{"t1": "Template", "t2": "Small"}}
// three people with four sends each are 12 sends but only 3 people
for _, p := range people(3) {
for i := 0; i < 4; i++ {
facts.sends = append(facts.sends, &programSend{Person: p, SentAt: may.Add(time.Duration(i) * time.Hour), Template: "t2",
ClickedAt: tp(may.Add(time.Duration(i)*time.Hour + time.Minute))})
}
}
// ten people, one clicked six times
ps := people(10)
for i := 0; i < 6; i++ {
facts.sends = append(facts.sends, &programSend{Person: ps[0], SentAt: may, Template: "t1", ClickedAt: tp(may.Add(time.Hour))})
}
for _, p := range ps[1:] {
facts.sends = append(facts.sends, &programSend{Person: p, SentAt: may, Template: "t1"})
}
r := results(facts, time.UTC, now, nil, nil)
if len(r.Templates) != 2 {
t.Fatalf("templates: %+v", r.Templates)
}
for _, row := range r.Templates {
if row.Clicked != nil || row.Submitted != nil || row.Reported != nil || !row.OutcomesHidden {
t.Fatalf("an anonymous row must show sends only: %+v", row)
}
switch row.Key {
case "t1":
if row.Suppressed || row.Sent == nil || *row.Sent != 15 {
t.Fatalf("a row of 10 people shows its size: %+v", row)
}
case "t2":
if !row.Suppressed || row.Sent != nil {
t.Fatalf("a row of 3 people must be withheld: %+v", row)
}
}
}
facts.anonymous = false
r = results(facts, time.UTC, now, nil, nil)
for _, row := range r.Templates {
if row.OutcomesHidden || row.Suppressed || row.Clicked == nil {
t.Fatalf("a named row shows every count: %+v", row)
}
if row.Key == "t1" && *row.Clicked != 6 {
t.Fatalf("named clicks: %+v", row)
}
}
}
// TestProgramAnonymousResultsNoBackSolve checks that no month or template
// outcome of an anonymous program can be learned by subtracting shown rows
// from each other or from a total. One small month is hidden; the template
// total minus the shown months would give its clicks if outcomes were shown
// per row.
func TestProgramAnonymousResultsNoBackSolve(t *testing.T) {
now := time.Date(2026, 6, 20, 12, 0, 0, 0, time.UTC)
facts := &programFacts{anonymous: true, templateNames: map[string]string{"t1": "Template"}}
add := func(month time.Month, persons []uuid.UUID, clickers int) {
for i, p := range persons {
sd := &programSend{Person: p, Campaign: uuid.New(), SentAt: time.Date(2026, month, 10, 9, 0, 0, 0, time.UTC), Template: "t1"}
if i < clickers {
sd.ClickedAt = tp(sd.SentAt.Add(time.Hour))
}
facts.sends = append(facts.sends, sd)
}
}
all := people(12)
// March has 10 people, 5 clicked. April has 2 people, one clicked, too
// few to show
add(time.March, all[:10], 5)
add(time.April, all[10:], 1)
r := results(facts, time.UTC, now, nil, nil)
if len(r.Months) != 2 || len(r.Templates) != 1 {
t.Fatalf("rows: %d months, %d templates", len(r.Months), len(r.Templates))
}
for _, row := range append(append([]*ProgramResultRow{}, r.Months...), r.Templates...) {
if row.Clicked != nil || row.Submitted != nil || row.Reported != nil || !row.OutcomesHidden {
t.Fatalf("row %s shows an outcome: %+v", row.Name, row)
}
}
if !r.Months[1].Suppressed || r.Months[1].Sent != nil {
t.Fatalf("the small month must be withheld: %+v", r.Months[1])
}
// the small month withholds the outcomes of any period that holds it
if r.Summary.ClickedLink != nil || !r.Summary.Suppressed {
t.Fatalf("summary clicks with a small month: %+v", r.Summary)
}
// the outcomes are in the summary only, here of March alone
march := time.Date(2026, time.March, 1, 0, 0, 0, 0, time.UTC)
april := march.AddDate(0, 1, 0)
if m := results(facts, time.UTC, now, &march, &april); m.Summary.ClickedLink == nil || *m.Summary.ClickedLink != 5 {
t.Fatalf("March clicks: %+v", m.Summary)
}
// the CSV export carries the same rows, with no outcome per row
for _, line := range programAggregateCSV(r) {
if line[0] == "Month" || line[0] == "Template" {
if line[5] != "" || line[6] != "" || line[7] != "" {
t.Fatalf("csv row shows an outcome: %v", line)
}
}
}
// the report table and trend show no outcome either
d := programReportFromResults(&model.Program{IsAnonymous: nullable.NewNullableWithValue(true)}, &programReportSetup{}, r, now, nil, nil)
for _, row := range append(append([]programReportRow{}, d.Months...), d.Templates...) {
for _, cell := range row.Cells[2:] {
if cell != programWithheldText {
t.Fatalf("report row shows an outcome: %v", row.Cells)
}
}
}
for _, m := range d.Trend {
if m.Clicked.Height != 0 || m.Reported.Height != 0 {
t.Fatalf("trend shows an outcome: %+v", m)
}
}
}
// TestProgramResultsAnonymousMonthSnap checks an anonymous program counts a
// month only once it has ended and settled, at any time of the day
func TestProgramResultsAnonymousMonthSnap(t *testing.T) {
loc, err := time.LoadLocation("Europe/Copenhagen")
if err != nil {
t.Skip("no time zone data")
}
facts := &programFacts{anonymous: true, templateNames: map[string]string{}}
day := time.Date(2025, 5, 20, 0, 0, 0, 0, loc)
// five sends early in the day and five late, all one day
for i, p := range people(10) {
sentAt := day.Add(time.Hour)
if i >= 5 {
sentAt = day.Add(22 * time.Hour)
}
facts.sends = append(facts.sends, &programSend{Person: p, Campaign: uuid.New(), SentAt: sentAt, Template: "t1"})
}
settled := time.Date(2025, 6, 4, 0, 0, 0, 0, loc)
for _, hour := range []int{1, 12, 23} {
// the month ended on 1 June and settles 3 days later
for _, now := range []time.Time{day.AddDate(0, 0, 4), settled.AddDate(0, 0, -1)} {
now = now.Add(time.Duration(hour) * time.Hour)
r := results(facts, loc, now, nil, nil)
if *r.Summary.Sent != 0 || len(r.Months) != 0 || r.LastSentAt != nil {
t.Fatalf("%v: a month that has not settled must not count: %+v", now, r.Summary)
}
}
now := settled.Add(time.Duration(hour) * time.Hour)
r := results(facts, loc, now, nil, nil)
if *r.Summary.Sent != 10 || *r.Months[0].Sent != 10 {
t.Fatalf("hour %d: the whole month must be counted: %+v", hour, r.Summary)
}
if month := time.Date(day.Year(), day.Month(), 1, 0, 0, 0, 0, loc); !r.LastSentAt.Equal(month) {
t.Fatalf("last sent must be the start of the month, got %v", r.LastSentAt)
}
if !r.CountedTo.Equal(time.Date(2025, 6, 1, 0, 0, 0, 0, loc)) {
t.Fatalf("counted to %v", r.CountedTo)
}
}
// a normal program keeps exact times and counts young sends right away
facts.anonymous = false
now := day.AddDate(0, 0, 1).Add(12 * time.Hour)
r := results(facts, loc, now, nil, nil)
if *r.Summary.Sent != 10 || !r.LastSentAt.Equal(day.Add(22*time.Hour)) || r.CountedTo != nil {
t.Fatalf("normal program wrong: sent=%v last=%v", r.Summary.Sent, r.LastSentAt)
}
}
// TestProgramAnonymousSettledOutcomes checks an anonymous month counts the
// outcomes that happened before it settled, and keeps the same counts however
// late it is read
func TestProgramAnonymousSettledOutcomes(t *testing.T) {
loc, err := time.LoadLocation("Europe/Copenhagen")
if err != nil {
t.Skip("no time zone data")
}
day := time.Date(2026, 10, 20, 0, 0, 0, 0, loc)
settles := time.Date(2026, 11, 4, 0, 0, 0, 0, loc)
facts := &programFacts{anonymous: true, templateNames: map[string]string{}}
// twelve October sends: five clicked that day, one clicked on 3 November
// before the month settled, one on 4 November after it settled
for i, p := range people(12) {
sd := &programSend{Person: p, Campaign: uuid.New(), SentAt: day.Add(9 * time.Hour), Template: "t1"}
switch {
case i < 5:
sd.ClickedAt = tp(day.Add(10 * time.Hour))
case i == 5:
sd.ClickedAt = tp(settles.Add(-time.Hour))
case i == 6:
sd.ClickedAt = tp(settles.Add(time.Hour))
}
facts.sends = append(facts.sends, sd)
}
// a November send is never in the October counts
facts.sends = append(facts.sends, &programSend{Person: uuid.New(), Campaign: uuid.New(), SentAt: settles.Add(-2 * time.Hour), Template: "t1"})
if r := results(facts, loc, settles.Add(-time.Minute), nil, nil); len(r.Months) != 0 || *r.Summary.Sent != 0 {
t.Fatalf("October must not count before it settles: %+v", r.Summary)
}
for _, now := range []time.Time{settles.Add(time.Minute), settles.AddDate(0, 0, 20), settles.AddDate(0, 0, 40)} {
r := results(facts, loc, now, nil, nil)
if len(r.Months) == 0 || *r.Months[0].Sent != 12 || r.Months[0].Clicked != nil {
t.Fatalf("%v: an anonymous month shows its size only: %+v", now, r.Months)
}
october := time.Date(2026, 10, 1, 0, 0, 0, 0, loc)
november := october.AddDate(0, 1, 0)
r = results(facts, loc, now, &october, &november)
if r.Summary.ClickedLink == nil || *r.Summary.ClickedLink != 6 || *r.Summary.Sent != 12 {
t.Fatalf("%v: October counts the clicks before it settled: %+v", now, r.Summary)
}
}
// a named program counts everything right away
facts.anonymous = false
r := results(facts, loc, settles, nil, nil)
if *r.Months[0].Sent != 12 || *r.Months[0].Clicked != 7 {
t.Fatalf("named program wrong: %+v", r.Months)
}
}
// TestProgramAnonymousRepeatOffendersFollowClicks checks an anonymous program
// hides repeat offenders when clicked or submitted is hidden, as the repeat
// count would show how many clicked
func TestProgramAnonymousRepeatOffendersFollowClicks(t *testing.T) {
now := time.Date(2026, 6, 20, 12, 0, 0, 0, time.UTC)
facts := &programFacts{anonymous: true, templateNames: map[string]string{"t1": "Template"}}
all := people(12)
for round, day := range []int{3, 10} {
for i, p := range all {
sd := &programSend{Person: p, Campaign: uuid.New(), SentAt: time.Date(2026, time.May, day, 9, 0, 0, 0, time.UTC), Template: "t1"}
// 11 of 12 click the first send, 6 of them click the second too
if (round == 0 && i < 11) || (round == 1 && i < 6) {
sd.ClickedAt = tp(sd.SentAt.Add(time.Hour))
}
facts.sends = append(facts.sends, sd)
}
}
r := results(facts, time.UTC, now, nil, nil)
if r.Summary.ClickedLink != nil {
t.Fatalf("11 of 12 clicked must be withheld: %+v", r.Summary)
}
if r.Summary.RepeatOffenders != nil {
t.Fatalf("repeat offenders must be withheld when clicks are: %d", *r.Summary.RepeatOffenders)
}
}
// TestProgramAnonymousTemplateNotSubtractedFromMonth checks a template that
// went to too few people in a month can not be found by subtracting the shown
// template rows from the month row or the summary. January sent template A to
// 6 people and B to 2: showing January as 8 and A as 6 would give B as 2. The
// month row, the summary and the tiles of a range holding January withhold
// their send count, while February alone shows it.
func TestProgramAnonymousTemplateNotSubtractedFromMonth(t *testing.T) {
now := time.Date(2026, 4, 20, 12, 0, 0, 0, time.UTC)
facts := &programFacts{anonymous: true, templateNames: map[string]string{"a": "A", "b": "B"}}
add := func(month time.Month, persons []uuid.UUID, template string) {
for _, p := range persons {
facts.sends = append(facts.sends, &programSend{Person: p, Campaign: uuid.New(), SentAt: time.Date(2026, month, 10, 9, 0, 0, 0, time.UTC), Template: template})
}
}
ps := people(8)
add(time.January, ps[:6], "a")
add(time.January, ps[6:], "b")
add(time.February, ps[:6], "a")
r := results(facts, time.UTC, now, nil, nil)
byKey := map[string]*ProgramResultRow{}
for _, row := range append(append([]*ProgramResultRow{}, r.Months...), r.Templates...) {
byKey[row.Key] = row
}
if row := byKey["2026-01"]; row == nil || row.Sent != nil || !row.Suppressed {
t.Fatalf("January holds a withheld template and must withhold its sends: %+v", row)
}
if row := byKey["2026-02"]; row == nil || row.Sent == nil || *row.Sent != 6 {
t.Fatalf("February shows its sends: %+v", row)
}
if row := byKey["b"]; row == nil || row.Sent != nil || !row.Suppressed {
t.Fatalf("template B went to 2 people and must be withheld: %+v", row)
}
if row := byKey["a"]; row == nil || row.Sent == nil || *row.Sent != 12 {
t.Fatalf("template A shows its sends: %+v", row)
}
if s := r.Summary; s.Sent != nil || s.SentTotal != 0 || !s.SentTotalHidden || !s.HasSent {
t.Fatalf("a view holding January must withhold its send total: %+v", s)
}
month := func(m time.Month) *time.Time {
t := time.Date(2026, m, 1, 0, 0, 0, 0, time.UTC)
return &t
}
if s := results(facts, time.UTC, now, month(time.February), month(time.March)).Summary; s.Sent == nil || *s.Sent != 6 || s.SentTotalHidden {
t.Fatalf("February alone shows its send total: %+v", s)
}
in := programResultsInput{now: now, enrollment: programEnrollmentCounts{total: 8, active: 8}}
if tl := tilesFor(facts, in, ProgramTimeRangeAll, false); tl.Sent != nil {
t.Fatalf("the tiles of a range holding January must withhold the sends: %+v", tl)
}
}
// TestProgramRowsAnonymousHideTraining checks an anonymous program holds no
// training counts in its rows, as it holds no phishing outcomes there: the
// training outcomes are in the summary only, and its rows stay the phishing
// sizes they were
func TestProgramRowsAnonymousHideTraining(t *testing.T) {
facts := &programFacts{anonymous: true, templateNames: map[string]string{"t1": "Phish", "tr": "Course"}}
for i, p := range people(12) {
facts.sends = append(facts.sends, send(p, 10, "clicked"), trainingSend(p, 10, "tr", i < 8, i < 6))
}
r := results(facts, time.UTC, anonymousNow, nil, nil)
if !r.Summary.HasPhishing || !r.Summary.HasTraining {
t.Fatalf("the program has phishing and training: %+v", r.Summary)
}
for _, row := range append(append([]*ProgramResultRow{}, r.Months...), r.Templates...) {
if row.Key == "tr" {
t.Fatalf("an anonymous program shows no training template row")
}
if !row.OutcomesHidden {
t.Fatalf("an anonymous row hides its outcomes: %+v", row)
}
wantNull(t, "clicked", row.Clicked)
wantNull(t, "training sent", row.TrainingSent)
wantNull(t, "training started", row.TrainingStarted)
wantNull(t, "training completed", row.TrainingCompleted)
wantCount(t, "sent", row.Sent, 12)
}
if len(r.Months) != 1 || len(r.Templates) != 1 {
t.Fatalf("want the 1 phishing month and template, got %d and %d", len(r.Months), len(r.Templates))
}
// the report shows the training columns hidden, never a count
row := programReportResultRow("Jun 2025", r.Months[0], true, true)
for _, c := range row.Cells[2:] {
if c != programWithheldText {
t.Fatalf("an anonymous row cell must be hidden, got %v", row.Cells)
}
}
}
// TestInboxReportTimeKeepsShownMonths checks a report clamped by
// inboxReportTime never changes the months an anonymous program shows
func TestInboxReportTimeKeepsShownMonths(t *testing.T) {
loc, _ := time.LoadLocation("Europe/Copenhagen")
for _, now := range []time.Time{
time.Date(2026, 11, 3, 23, 0, 0, 0, loc),
time.Date(2026, 11, 4, 0, 30, 0, 0, loc),
time.Date(2026, 11, 30, 12, 0, 0, 0, loc),
time.Date(2026, 3, 29, 2, 30, 0, 0, time.UTC),
} {
fixed := programAnonymousFixedFrom(now, loc)
if fixed.After(now) {
t.Fatalf("now %v: fixed from %v is after now", now, fixed)
}
countedTo := programAnonymousCountedTo(now, loc)
// every shown month ends at or before countedTo and settles at or
// before fixed
last := countedTo.AddDate(0, -1, 0)
if settle := programMonthSettle(countedTo, loc); settle.After(fixed) {
t.Fatalf("now %v: month from %v settles %v after %v", now, last, settle, fixed)
}
}
}