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

163 lines
6.3 KiB
Go

package service
import (
"strings"
"testing"
"time"
"github.com/phishingclub/phishingclub/database"
"github.com/phishingclub/phishingclub/model"
)
// TestTickAuditsAdvance checks a tick that enrolls, creates a hidden campaign
// and schedules a send writes one system style event with the counts, and a
// tick that changes nothing writes none. The advance of a tick plans from the
// time after its membership pass, so a person enrolled as due is advanced in
// the same tick.
func TestTickAuditsAdvance(t *testing.T) {
p := newProgramDataSavingTest(t)
buf := p.withAuditBuffer()
session := authorizedSession(t)
groupID := p.group("staff")
alice := p.recipient("alice@example.com")
p.create(&database.RecipientGroupRecipient{RecipientID: &alice, RecipientGroupID: &groupID})
programID := p.program(model.ProgramLoopComplete)
p.create(&database.ProgramRecipientGroup{ProgramID: &programID, RecipientGroupID: &groupID})
p.step(programID, 0, true)
// the membership pass enrolls alice and the advance plans her send
p.svc.lastRetentionAt = time.Now()
if err := p.svc.Tick(p.ctx, session); err != nil {
t.Fatalf("tick: %v", err)
}
enrollments, err := p.svc.ProgramRepository.GetEnrollmentPlansByProgramID(p.ctx, &programID)
if err != nil || len(enrollments) != 1 {
t.Fatalf("expected alice enrolled: %d %v", len(enrollments), err)
}
// nothing is due, nothing is written
p.svc.lastMembershipAt = time.Now()
if err := p.svc.Tick(p.ctx, session); err != nil {
t.Fatalf("tick: %v", err)
}
ev := auditEventsNamed(t, buf.String(), "Program.Advance")
if len(ev) != 1 {
t.Fatalf("expected one event per tick that changed something, got %d: %v", len(ev), ev)
}
if ev[0]["result"] != "success" {
t.Fatalf("expected success: %v", ev[0])
}
details := eventDetails(t, ev[0])
ids, _ := details["programIds"].([]any)
if len(ids) != 1 || ids[0] != programID.String() {
t.Fatalf("tick program ids: %v", details)
}
if details["enrolled"] != float64(1) || details["campaignsCreated"] != float64(1) ||
details["sendsScheduled"] != float64(1) || details["exited"] != float64(0) {
t.Fatalf("tick details: %v", details)
}
if strings.Contains(buf.String(), "alice@example.com") || strings.Contains(buf.String(), alice.String()) {
t.Fatal("the tick event must not name a person")
}
}
// TestTickAuditLeavesOutAnonymousPrograms checks the tick event never names
// an anonymous program, gives its counts or says one changed, while a named
// program changed in the same tick keeps its id and counts, and a tick that
// changed only an anonymous program writes no event
func TestTickAuditLeavesOutAnonymousPrograms(t *testing.T) {
p := newProgramDataSavingTest(t)
buf := p.withAuditBuffer()
session := authorizedSession(t)
groupID := p.group("staff")
alice := p.recipient("alice@example.com")
p.create(&database.RecipientGroupRecipient{RecipientID: &alice, RecipientGroupID: &groupID})
anonymousID := p.program(model.ProgramLoopComplete)
p.setProgram(anonymousID, map[string]any{"is_anonymous": true})
p.create(&database.ProgramRecipientGroup{ProgramID: &anonymousID, RecipientGroupID: &groupID})
p.step(anonymousID, 0, true)
namedID := p.program(model.ProgramLoopComplete)
p.create(&database.ProgramRecipientGroup{ProgramID: &namedID, RecipientGroupID: &groupID})
p.step(namedID, 0, true)
p.svc.lastRetentionAt = time.Now()
if err := p.svc.Tick(p.ctx, session); err != nil {
t.Fatalf("tick: %v", err)
}
ev := auditEventsNamed(t, buf.String(), "Program.Advance")
if len(ev) != 1 || ev[0]["result"] != "success" {
t.Fatalf("expected one success event, got %v", ev)
}
details := eventDetails(t, ev[0])
ids, _ := details["programIds"].([]any)
if len(ids) != 1 || ids[0] != namedID.String() {
t.Fatalf("only the named program may be named: %v", details)
}
if details["enrolled"] != float64(1) || details["sendsScheduled"] != float64(1) {
t.Fatalf("the counts must be those of the named program only: %v", details)
}
if _, ok := details["anonymousProgramsChanged"]; ok {
t.Fatalf("the event must not say an anonymous program changed: %v", details)
}
if strings.Contains(buf.String(), anonymousID.String()) {
t.Fatal("the tick event must not name the anonymous program")
}
// a tick that changes only the anonymous program writes no event
buf.Reset()
p.setProgram(namedID, map[string]any{"closed_at": time.Now().UTC()})
bob := p.recipient("bob@example.com")
p.create(&database.RecipientGroupRecipient{RecipientID: &bob, RecipientGroupID: &groupID})
p.svc.lastMembershipAt = time.Time{}
if err := p.svc.Tick(p.ctx, session); err != nil {
t.Fatalf("tick: %v", err)
}
enrolled, err := p.svc.ProgramRepository.GetEnrollmentPlansByProgramID(p.ctx, &anonymousID)
if err != nil || len(enrolled) != 2 {
t.Fatalf("expected bob enrolled in the anonymous program: %d %v", len(enrolled), err)
}
if ev := auditEventsNamed(t, buf.String(), "Program.Advance"); len(ev) != 0 {
t.Fatalf("an anonymous only tick must write no event, got %v", ev)
}
}
// TestTickAuditsFailureThrottled checks a failing tick writes a failed event
// once and holds back the same failure on the next ticks
func TestTickAuditsFailureThrottled(t *testing.T) {
p := newProgramServiceTest(t)
buf := p.withAuditBuffer()
p.svc.lastMembershipAt = time.Now()
p.svc.lastRetentionAt = time.Now()
if err := p.db.Migrator().DropTable(&database.ProgramEnrollment{}); err != nil {
t.Fatalf("drop: %v", err)
}
for i := 0; i < 3; i++ {
if err := p.svc.Tick(p.ctx, authorizedSession(t)); err == nil {
t.Fatal("expected the tick to fail")
}
}
ev := auditEventsNamed(t, buf.String(), "Program.Advance")
if len(ev) != 1 || ev[0]["result"] != "failed" {
t.Fatalf("expected one failed event, got %v", ev)
}
}
// TestProgramTaskDeniedAudit checks the scheduler and the report sender write
// a denied event for a session without the permission
func TestProgramTaskDeniedAudit(t *testing.T) {
p := newProgramServiceTest(t)
buf := p.withAuditBuffer()
if err := p.svc.Tick(p.ctx, unauthorizedSession(t)); err == nil {
t.Fatal("expected the tick to be refused")
}
if err := p.svc.SendDueReports(p.ctx, unauthorizedSession(t)); err == nil {
t.Fatal("expected the report pass to be refused")
}
for _, name := range []string{"Program.Advance", "Program.SendDueReports"} {
ev := auditEventsNamed(t, buf.String(), name)
if len(ev) != 1 || ev[0]["result"] != "denied" {
t.Fatalf("%s: expected one denied event, got %v", name, ev)
}
}
}