package repository import ( "context" "testing" "time" "github.com/google/uuid" "github.com/phishingclub/phishingclub/database" "github.com/phishingclub/phishingclub/model" "github.com/phishingclub/phishingclub/vo" "gorm.io/gorm" ) // TestProgramAttentionCompletedPeople checks a running program whose people all // completed is active, not in need of attention, while a started program with // nobody enrolled or only people who left needs attention. The list filter, // the dashboard counts and the list summary agree. func TestProgramAttentionCompletedPeople(t *testing.T) { db := newProgramTestDB(t) ctx := context.Background() r := &Program{DB: db} now := time.Now().UTC() past := now.Add(-time.Hour) template := ptrUUID() if err := db.Create(&database.CampaignTemplate{ID: template, CreatedAt: ptrNow(), UpdatedAt: ptrNow(), Name: "t", URLIdentifierID: ptrUUID(), IsUsable: true}).Error; err != nil { t.Fatalf("template: %v", err) } completed := statusProgram(t, db, "completed", &past, &past, nil, nil) empty := statusProgram(t, db, "empty", &past, &past, nil, nil) left := statusProgram(t, db, "left", &past, &past, nil, nil) for _, id := range []*uuid.UUID{completed, empty, left} { createStep(t, db, id, template, 0) } enroll := func(programID *uuid.UUID, status string) { e := &database.ProgramEnrollment{ ID: ptrUUID(), CreatedAt: ptrNow(), UpdatedAt: ptrNow(), ProgramID: programID, RecipientID: ptrUUID(), Status: status, } if err := db.Create(e).Error; err != nil { t.Fatalf("enroll: %v", err) } } enroll(completed, model.ProgramEnrollmentCompleted) enroll(completed, model.ProgramEnrollmentCompleted) enroll(completed, model.ProgramEnrollmentExited) enroll(left, model.ProgramEnrollmentExited) ids := func(status string) map[uuid.UUID]bool { got, err := r.GetAll(ctx, nil, &ProgramOption{QueryArgs: &vo.QueryArgs{}, Status: status}, now) if err != nil { t.Fatalf("%s: %v", status, err) } out := map[uuid.UUID]bool{} for _, p := range got.Rows { out[p.ID.MustGet()] = true } return out } active, attention := ids(model.ProgramStatusActive), ids(model.ProgramStatusNeedsAttention) if !active[*completed] || attention[*completed] { t.Fatal("a program whose people all completed must be active") } if active[*empty] || !attention[*empty] || active[*left] || !attention[*left] { t.Fatal("a started program with nobody enrolled or only leavers must need attention") } counts, err := r.CountByStatus(ctx, nil, now) if err != nil { t.Fatalf("counts: %v", err) } if counts.Active != 1 || counts.NeedsAttention != 2 { t.Fatalf("counts: %+v", counts) } sums, err := r.GetListSummaries(ctx, []*uuid.UUID{completed, empty, left}) if err != nil { t.Fatalf("summaries: %v", err) } if sums[*completed].Enrolled != 0 || sums[*completed].People != 2 || sums[*empty].People != 0 || sums[*left].People != 0 { t.Fatalf("summaries: completed %+v empty %+v left %+v", sums[*completed], sums[*empty], sums[*left]) } } // TestProgramStalledNeedsAttention checks a running program needs attention // when its sends do not go out: a next send more than ProgramStallGrace // overdue, a queued send waiting more than that past its send time, or an // active person without a next send while nothing is queued. A program that // sent nothing for long but waits for a due time ahead, as over a weekend, a // skip range or the delay of a new member, is active. A running program with // nobody in it needs attention once marked started or past its start by more // than ProgramStartGrace. func TestProgramStalledNeedsAttention(t *testing.T) { db := newProgramTestDB(t) ctx := context.Background() r := &Program{DB: db} now := time.Now().UTC().Truncate(time.Second) longAgo := now.Add(-60 * 24 * time.Hour) day := 24 * time.Hour at := func(d time.Duration) *time.Time { v := now.Add(d); return &v } enroll := func(programID *uuid.UUID, due *time.Time) { e := &database.ProgramEnrollment{ ID: ptrUUID(), CreatedAt: ptrNow(), UpdatedAt: ptrNow(), ProgramID: programID, RecipientID: ptrUUID(), Status: model.ProgramEnrollmentActive, NextDueAt: due, } if err := db.Create(e).Error; err != nil { t.Fatalf("enroll: %v", err) } } failedAt := map[time.Time]bool{} send := func(programID *uuid.UUID, sendAt time.Time, sent bool) { campaignID := ptrUUID() c := &database.Campaign{ID: campaignID, CreatedAt: ptrNow(), UpdatedAt: ptrNow(), Name: "c " + campaignID.String(), ProgramID: programID} if err := db.Create(c).Error; err != nil { t.Fatalf("campaign: %v", err) } cr := &database.CampaignRecipient{ ID: ptrUUID(), CreatedAt: ptrNow(), UpdatedAt: ptrNow(), CampaignID: campaignID, RecipientID: ptrUUID(), SendAt: &sendAt, } if sent { cr.SentAt = &sendAt } if failedAt[sendAt] { cr.LastAttemptAt = &sendAt } if err := db.Create(cr).Error; err != nil { t.Fatalf("send: %v", err) } } running := func(name string) *uuid.UUID { return statusProgram(t, db, name, &longAgo, &longAgo, nil, nil) } // last sent on a Friday, the next due time is on Monday weekend := running("weekend") send(weekend, now.Add(-2*day), true) enroll(weekend, at(day)) // a new member waits the join delay, nothing was ever sent joinDelay := running("join delay") enroll(joinDelay, at(20*day)) // a long skip range, the next due time is after it skipRange := running("skip range") send(skipRange, now.Add(-20*day), true) enroll(skipRange, at(14*day)) // a due time just inside the grace is not yet stalled inGrace := running("in grace") enroll(inGrace, at(-day+time.Hour)) // a queued send without a due time is planned queuedOnly := running("queued only") enroll(queuedOnly, nil) send(queuedOnly, now.Add(time.Hour), false) // a program that started a few minutes ago with nobody in it yet justStarted := statusProgram(t, db, "just started", at(-10*time.Minute), nil, nil, nil) // the scheduler stopped advancing overdue := running("overdue") enroll(overdue, at(-3*day)) // the sender stopped sending queuedLate := running("queued late") enroll(queuedLate, at(5*day)) send(queuedLate, now.Add(-3*day), false) // an active person without a next send and nothing queued unplanned := running("unplanned") enroll(unplanned, nil) // started and empty emptyStarted := running("empty started") // past its start but never marked started, and empty emptyNotMarked := statusProgram(t, db, "empty not marked", at(-2*time.Hour), nil, nil, nil) // one send failed, for example an address that bounced, while the others // went out: the sender works bounced := running("bounced") enroll(bounced, at(5*day)) failedAt[now.Add(-3*day)] = true send(bounced, now.Add(-3*day), false) send(bounced, now.Add(-3*day+time.Minute), true) // every send failed in the last week and none went out deadSender := running("dead sender") enroll(deadSender, at(5*day)) failedAt[now.Add(-2*day)] = true send(deadSender, now.Add(-2*day), false) // a failure older than the window is no longer looked at oldFailure := running("old failure") enroll(oldFailure, at(5*day)) failedAt[now.Add(-10*day)] = true send(oldFailure, now.Add(-10*day), false) // paused programs are paused, not stalled paused := statusProgram(t, db, "paused", &longAgo, &longAgo, &now, nil) enroll(paused, at(-3*day)) wantAttention := []*uuid.UUID{overdue, queuedLate, unplanned, emptyStarted, emptyNotMarked, deadSender} wantActive := []*uuid.UUID{weekend, joinDelay, skipRange, inGrace, queuedOnly, justStarted, bounced, oldFailure} check := func(status string, want []*uuid.UUID) { t.Helper() list, err := r.GetAll(ctx, nil, &ProgramOption{QueryArgs: &vo.QueryArgs{}, Status: status}, now) if err != nil { t.Fatalf("%s: %v", status, err) } got := map[uuid.UUID]string{} for _, p := range list.Rows { got[p.ID.MustGet()] = p.Name.MustGet().String() } for _, id := range want { if _, ok := got[*id]; !ok { t.Errorf("%s: missing %v, got %v", status, *id, got) } } if len(got) != len(want) { t.Errorf("%s: expected %d programs, got %v", status, len(want), got) } } check(model.ProgramStatusNeedsAttention, wantAttention) check(model.ProgramStatusActive, wantActive) counts, err := r.CountByStatus(ctx, nil, now) if err != nil { t.Fatalf("counts: %v", err) } if counts.NeedsAttention != int64(len(wantAttention)) || counts.Active != int64(len(wantActive)) || counts.Paused != 1 { t.Fatalf("counts: %+v", counts) } sums, err := r.GetListSummaries(ctx, []*uuid.UUID{queuedLate, unplanned, queuedOnly, bounced, deadSender}) if err != nil { t.Fatalf("summaries: %v", err) } late := sums[*queuedLate] if late.Queued != 1 || late.OldestQueuedAt == nil || late.OldestQueuedAt.Sub(now.Add(-3*day)).Abs() > time.Second { t.Fatalf("queued late summary: %+v", late) } if sums[*unplanned].Unplanned != 1 || sums[*unplanned].Queued != 0 { t.Fatalf("unplanned summary: %+v", sums[*unplanned]) } if sums[*queuedOnly].Unplanned != 1 || sums[*queuedOnly].Queued != 1 { t.Fatalf("queued only summary: %+v", sums[*queuedOnly]) } // a failed send is not counted as overdue, its attempt and the newest send // that went out are given instead b := sums[*bounced] if b.OldestQueuedAt != nil || b.NewestFailedAt == nil || b.NewestSentAt == nil { t.Fatalf("bounced summary: %+v", b) } d := sums[*deadSender] if d.OldestQueuedAt != nil || d.NewestFailedAt == nil || d.NewestSentAt != nil { t.Fatalf("dead sender summary: %+v", d) } } // statusProgram stores a program with the given state func statusProgram(t *testing.T, db *gorm.DB, name string, startAt, startedAt, pausedAt, closedAt *time.Time) *uuid.UUID { t.Helper() id := ptrUUID() row := &database.Program{ ID: id, CreatedAt: ptrNow(), UpdatedAt: ptrNow(), Name: name, StartAt: startAt, StartedAt: startedAt, PausedAt: pausedAt, ClosedAt: closedAt, } if err := db.Create(row).Error; err != nil { t.Fatalf("create program: %v", err) } return id } // TestProgramStatusFilterAndCounts checks the list status filter and the // dashboard counts use the same rules: a paused program that has not started // is scheduled, a running program with a broken template or nobody active // needs attention func TestProgramStatusFilterAndCounts(t *testing.T) { db := newProgramTestDB(t) ctx := context.Background() r := &Program{DB: db} now := time.Now().UTC() past, future := now.Add(-time.Hour), now.Add(time.Hour) usable, broken := ptrUUID(), ptrUUID() for _, tpl := range []struct { id *uuid.UUID usable bool }{{usable, true}, {broken, false}} { row := &database.CampaignTemplate{ID: tpl.id, CreatedAt: ptrNow(), UpdatedAt: ptrNow(), Name: "t " + tpl.id.String(), URLIdentifierID: ptrUUID(), IsUsable: tpl.usable} if err := db.Create(row).Error; err != nil { t.Fatalf("template: %v", err) } } want := map[string]*uuid.UUID{ model.ProgramStatusDraft: statusProgram(t, db, "draft", nil, nil, nil, nil), model.ProgramStatusScheduled: statusProgram(t, db, "scheduled", &future, nil, &now, nil), model.ProgramStatusPaused: statusProgram(t, db, "paused", &past, &past, &now, nil), model.ProgramStatusActive: statusProgram(t, db, "active", &past, &past, nil, nil), model.ProgramStatusNeedsAttention: statusProgram(t, db, "attention", &past, &past, nil, nil), model.ProgramStatusClosed: statusProgram(t, db, "closed", &past, &past, nil, &now), } createStep(t, db, want[model.ProgramStatusActive], usable, 0) createStep(t, db, want[model.ProgramStatusNeedsAttention], broken, 0) enrollment := &database.ProgramEnrollment{ ID: ptrUUID(), CreatedAt: ptrNow(), UpdatedAt: ptrNow(), ProgramID: want[model.ProgramStatusActive], RecipientID: ptrUUID(), Status: model.ProgramEnrollmentActive, NextDueAt: &future, } if err := db.Create(enrollment).Error; err != nil { t.Fatalf("enroll: %v", err) } for status, id := range want { got, err := r.GetAll(ctx, nil, &ProgramOption{QueryArgs: &vo.QueryArgs{}, Status: status}, now) if err != nil { t.Fatalf("%s: %v", status, err) } if len(got.Rows) != 1 || got.Rows[0].ID.MustGet() != *id { t.Fatalf("%s: got %d rows", status, len(got.Rows)) } } if _, err := r.GetAll(ctx, nil, &ProgramOption{QueryArgs: &vo.QueryArgs{}, Status: "running"}, now); err == nil { t.Fatal("an unknown status must be refused") } counts, err := r.CountByStatus(ctx, nil, now) if err != nil { t.Fatalf("counts: %v", err) } if counts.Total != 6 || counts.Scheduled != 1 || counts.Paused != 1 || counts.Active != 1 || counts.NeedsAttention != 1 { t.Fatalf("counts: %+v", counts) } // the model status agrees with the filter scheduled, _ := r.GetByID(ctx, want[model.ProgramStatusScheduled], &ProgramOption{}) if scheduled.StatusAt(now) != model.ProgramStatusScheduled { t.Fatalf("a paused program before its start reads %s", scheduled.StatusAt(now)) } }