mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 01:53:42 +01:00
334 lines
12 KiB
Go
334 lines
12 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/phishingclub/phishingclub/cache"
|
|
"github.com/phishingclub/phishingclub/data"
|
|
"github.com/phishingclub/phishingclub/database"
|
|
"github.com/phishingclub/phishingclub/model"
|
|
"github.com/phishingclub/phishingclub/repository"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// TestSharedExportListsGlobalPrograms checks the shared export lists the
|
|
// global programs, a named one with its events and an anonymous one without,
|
|
// and its audit event counts them
|
|
func TestSharedExportListsGlobalPrograms(t *testing.T) {
|
|
p := newProgramServiceTest(t)
|
|
buf := p.withAuditBuffer()
|
|
now := time.Now().UTC()
|
|
alice := p.recipient("alice@example.com")
|
|
companyID := uuid.New()
|
|
mk := func(name string, company *uuid.UUID, anonymous bool) {
|
|
programID := p.program(model.ProgramLoopComplete)
|
|
p.db.Model(&database.Program{}).Where("id = ?", programID).Updates(map[string]any{
|
|
"name": name, "company_id": company, "is_anonymous": anonymous,
|
|
})
|
|
campaignID := p.stepCampaign(programID, p.step(programID, 0, true), 0)
|
|
p.send(campaignID, alice, true)
|
|
eventID := uuid.New()
|
|
p.create(&database.CampaignEvent{
|
|
ID: &eventID, CreatedAt: &now, UpdatedAt: &now,
|
|
CampaignID: &campaignID, RecipientID: &alice, EventID: &eventID,
|
|
})
|
|
}
|
|
mk("Global named", nil, false)
|
|
mk("Global anonymous", nil, true)
|
|
mk("Company named", &companyID, false)
|
|
|
|
sections, err := p.svc.CampaignService.GetProgramEventsExport(p.ctx, authorizedSession(t), nil)
|
|
if err != nil {
|
|
t.Fatalf("shared export: %v", err)
|
|
}
|
|
if len(sections) != 2 {
|
|
t.Fatalf("expected the two global programs, got %d", len(sections))
|
|
}
|
|
for _, s := range sections {
|
|
switch s.ProgramName {
|
|
case "Global named":
|
|
if s.Anonymous || len(exportEvents(t, p.ctx, s)) != 1 {
|
|
t.Fatalf("named global program: %+v", s)
|
|
}
|
|
case "Global anonymous":
|
|
if !s.Anonymous || len(exportEvents(t, p.ctx, s)) != 0 {
|
|
t.Fatalf("an anonymous program must have no events: %+v", s)
|
|
}
|
|
default:
|
|
t.Fatalf("unexpected program %q", s.ProgramName)
|
|
}
|
|
}
|
|
|
|
company := &Company{Common: p.svc.Common, CampaignService: p.svc.CampaignService}
|
|
for _, id := range []*uuid.UUID{nil, &companyID} {
|
|
done, err := company.AuthorizeExport(p.ctx, authorizedSession(t), id)
|
|
if err != nil {
|
|
t.Fatalf("authorize: %v", err)
|
|
}
|
|
done(true)
|
|
}
|
|
ev := auditEventsNamed(t, buf.String(), "Company.Export")
|
|
if len(ev) != 2 {
|
|
t.Fatalf("expected two export events, got %v", ev)
|
|
}
|
|
shared, own := eventDetails(t, ev[0]), eventDetails(t, ev[1])
|
|
if shared["programs"] != float64(2) || shared["programsWithEvents"] != float64(1) {
|
|
t.Fatalf("shared export details: %v", shared)
|
|
}
|
|
if own["programs"] != float64(1) || own["programsWithEvents"] != float64(1) {
|
|
t.Fatalf("company export details: %v", own)
|
|
}
|
|
}
|
|
|
|
// TestExportProgramData checks the recipient export reads only program sends
|
|
// of the recipient's company or global programs, never an anonymous program,
|
|
// and the company export gives events for a named program only
|
|
func TestExportProgramData(t *testing.T) {
|
|
p := newProgramServiceTest(t)
|
|
now := time.Now().UTC()
|
|
companyID, otherCompany := uuid.New(), uuid.New()
|
|
alice := p.recipient("alice@example.com")
|
|
p.db.Model(&database.Recipient{}).Where("id = ?", alice).Update("company_id", companyID)
|
|
|
|
mk := func(name string, company *uuid.UUID, anonymous bool) (uuid.UUID, uuid.UUID) {
|
|
programID := p.program(model.ProgramLoopComplete)
|
|
p.db.Model(&database.Program{}).Where("id = ?", programID).Updates(map[string]any{
|
|
"name": name, "company_id": company, "is_anonymous": anonymous,
|
|
})
|
|
step := p.step(programID, 0, true)
|
|
campaignID := p.stepCampaign(programID, step, 0)
|
|
// a hidden campaign sends the template of its step
|
|
var stepRow database.ProgramStep
|
|
p.db.Where("id = ?", step).First(&stepRow)
|
|
p.db.Model(&database.Campaign{}).Where("id = ?", campaignID).Update("campaign_template_id", stepRow.CampaignTemplateID)
|
|
p.send(campaignID, alice, true)
|
|
eventID := uuid.New()
|
|
p.create(&database.CampaignEvent{
|
|
ID: &eventID, CreatedAt: &now, UpdatedAt: &now,
|
|
CampaignID: &campaignID, RecipientID: &alice, EventID: &eventID,
|
|
})
|
|
return programID, campaignID
|
|
}
|
|
mk("Own", &companyID, false)
|
|
mk("Shared", nil, false)
|
|
mk("Elsewhere", &otherCompany, false)
|
|
mk("Anonymous", &companyID, true)
|
|
|
|
recipients := &Recipient{
|
|
Common: Common{Logger: zap.NewNop().Sugar()},
|
|
RecipientRepository: &repository.Recipient{DB: p.db},
|
|
}
|
|
sends, events, err := recipients.GetProgramExport(p.ctx, authorizedSession(t), &alice)
|
|
if err != nil {
|
|
t.Fatalf("recipient export: %v", err)
|
|
}
|
|
names := map[string]bool{}
|
|
for _, s := range sends {
|
|
names["send "+s.ProgramName] = true
|
|
}
|
|
for _, e := range events {
|
|
names["event "+e.ProgramName] = true
|
|
}
|
|
for _, want := range []string{"send Own", "send Shared", "event Own", "event Shared"} {
|
|
if !names[want] {
|
|
t.Fatalf("missing %s in %v", want, names)
|
|
}
|
|
}
|
|
if len(names) != 4 {
|
|
t.Fatalf("unexpected program data %v", names)
|
|
}
|
|
if _, _, err := recipients.GetProgramExport(p.ctx, unauthorizedSession(t), &alice); err == nil {
|
|
t.Fatal("expected an unauthorized session to be refused")
|
|
}
|
|
|
|
sections, err := p.svc.CampaignService.GetProgramEventsExport(p.ctx, authorizedSession(t), &companyID)
|
|
if err != nil {
|
|
t.Fatalf("company export: %v", err)
|
|
}
|
|
if len(sections) != 2 {
|
|
t.Fatalf("sections = %d, want the two company programs", len(sections))
|
|
}
|
|
for _, s := range sections {
|
|
switch s.ProgramName {
|
|
case "Anonymous":
|
|
if !s.Anonymous || len(exportEvents(t, p.ctx, s)) != 0 {
|
|
t.Fatalf("an anonymous program must have no events: %+v", s)
|
|
}
|
|
case "Own":
|
|
events := exportEvents(t, p.ctx, s)
|
|
if len(events) != 1 || !events[0].HasRecipient {
|
|
t.Fatalf("named program events = %+v", events)
|
|
}
|
|
if name := s.TemplateByCampaign[events[0].CampaignID]; name == "" {
|
|
t.Fatal("the event template is not named")
|
|
}
|
|
default:
|
|
t.Fatalf("unexpected program %q", s.ProgramName)
|
|
}
|
|
}
|
|
}
|
|
|
|
// exportEvents reads every event of a program section of the company export
|
|
func exportEvents(t *testing.T, ctx context.Context, s *ProgramEventsExport) []repository.ProgramEventExportRow {
|
|
t.Helper()
|
|
out := []repository.ProgramEventExportRow{}
|
|
err := s.EachEvent(ctx, true, func(row *repository.ProgramEventExportRow) error {
|
|
out = append(out, *row)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("export events: %v", err)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// TestProgramSendCSVFromFacts checks the per send export gives one row per
|
|
// send oldest first, with the outcomes, kind and template of each
|
|
func TestProgramSendCSVFromFacts(t *testing.T) {
|
|
f := newResultsFixture(newProgramServiceTest(t))
|
|
now := time.Now().UTC().Truncate(time.Second)
|
|
second := f.sentTo(now.Add(-time.Hour))
|
|
first := f.sentTo(now.Add(-2 * time.Hour))
|
|
f.event(first, data.EVENT_CAMPAIGN_RECIPIENT_PAGE_VISITED)
|
|
f.event(first, data.EVENT_CAMPAIGN_RECIPIENT_REPORTED)
|
|
rows := [][]string{}
|
|
err := f.svc.programSendCSV(context.Background(), f.loadProgram(f.programID), func(row []string) error {
|
|
rows = append(rows, row)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("export: %v", err)
|
|
}
|
|
if len(rows) != 3 || strings.Join(rows[0], ",") != strings.Join(programSendCSVHeader, ",") {
|
|
t.Fatalf("rows %d: %v", len(rows), rows)
|
|
}
|
|
emailOf := func(id uuid.UUID) string {
|
|
var r database.Recipient
|
|
f.db.Where("id = ?", id).First(&r)
|
|
return *r.Email
|
|
}
|
|
if rows[1][0] != emailOf(first) || rows[2][0] != emailOf(second) {
|
|
t.Fatal("the sends must be oldest first")
|
|
}
|
|
if rows[1][3] != ProgramSendKindPhishing || rows[1][4] != now.Add(-2*time.Hour).Format(time.RFC3339) {
|
|
t.Fatalf("first row %v", rows[1])
|
|
}
|
|
if rows[1][5] == "" || rows[1][7] == "" || rows[1][6] != "" || rows[2][5] != "" {
|
|
t.Fatalf("outcomes %v %v", rows[1], rows[2])
|
|
}
|
|
if rows[1][2] != programDeletedTemplateName {
|
|
t.Fatalf("template %q", rows[1][2])
|
|
}
|
|
}
|
|
|
|
// TestProgramSendCSVTrainingColumns checks the per send export of a named
|
|
// program with phishing and training: a training send has the training kind,
|
|
// its started and completed times and no phishing outcome, and a phishing
|
|
// send has no training times
|
|
func TestProgramSendCSVTrainingColumns(t *testing.T) {
|
|
f := newResultsFixture(newProgramServiceTest(t))
|
|
now := time.Now().UTC().Truncate(time.Second)
|
|
phished := f.sentTo(now.Add(-3 * time.Hour))
|
|
f.event(phished, data.EVENT_CAMPAIGN_RECIPIENT_PAGE_VISITED)
|
|
|
|
// a training step with its hidden training campaign
|
|
templateID := f.trainingTemplate()
|
|
stepID := uuid.New()
|
|
f.create(&database.ProgramStep{
|
|
ID: &stepID, CreatedAt: &now, UpdatedAt: &now,
|
|
ProgramID: &f.programID, CampaignTemplateID: &templateID, Position: 1,
|
|
})
|
|
trainingID := uuid.New()
|
|
f.create(&database.Campaign{
|
|
ID: &trainingID, CreatedAt: &now, UpdatedAt: &now, Name: "training " + trainingID.String(),
|
|
ProgramID: &f.programID, IsTraining: true, CampaignTemplateID: &templateID,
|
|
})
|
|
linkID := uuid.New()
|
|
f.create(&database.ProgramCampaign{
|
|
ID: &linkID, CreatedAt: &now,
|
|
ProgramID: &f.programID, ProgramStepID: &stepID, CampaignID: &trainingID,
|
|
})
|
|
trained := f.recipient(uuid.NewString() + "@example.com")
|
|
sendID := f.send(trainingID, trained, true)
|
|
sentAt := now.Add(-2 * time.Hour)
|
|
if err := f.db.Model(&database.CampaignRecipient{}).Where("id = ?", sendID).Update("sent_at", sentAt).Error; err != nil {
|
|
t.Fatalf("set sent_at: %v", err)
|
|
}
|
|
startedAt, completedAt := now.Add(-90*time.Minute), now.Add(-time.Hour)
|
|
for name, at := range map[string]time.Time{
|
|
data.EVENT_CAMPAIGN_RECIPIENT_TRAINING_STARTED: startedAt,
|
|
data.EVENT_CAMPAIGN_RECIPIENT_TRAINING_COMPLETED: completedAt,
|
|
// a visit on a training send is no click on a phish
|
|
data.EVENT_CAMPAIGN_RECIPIENT_PAGE_VISITED: startedAt,
|
|
} {
|
|
if cache.EventIDByName[name] == nil {
|
|
id := uuid.New()
|
|
cache.EventIDByName[name] = &id
|
|
}
|
|
id := uuid.New()
|
|
f.create(&database.CampaignEvent{
|
|
ID: &id, CreatedAt: &at, UpdatedAt: &at,
|
|
CampaignID: &trainingID, RecipientID: &trained, EventID: cache.EventIDByName[name],
|
|
})
|
|
}
|
|
|
|
rows := [][]string{}
|
|
err := f.svc.programSendCSV(context.Background(), f.loadProgram(f.programID), func(row []string) error {
|
|
rows = append(rows, row)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("export: %v", err)
|
|
}
|
|
if len(rows) != 3 {
|
|
t.Fatalf("want the header and 2 sends, got %d: %v", len(rows), rows)
|
|
}
|
|
phish, training := rows[1], rows[2]
|
|
if phish[3] != ProgramSendKindPhishing || phish[5] == "" || phish[8] != "" || phish[9] != "" {
|
|
t.Fatalf("phishing row %v", phish)
|
|
}
|
|
if training[3] != ProgramSendKindTraining || training[4] != sentAt.Format(time.RFC3339) {
|
|
t.Fatalf("training row %v", training)
|
|
}
|
|
if training[5] != "" || training[6] != "" || training[7] != "" {
|
|
t.Fatalf("a training row has no phishing outcome: %v", training)
|
|
}
|
|
if training[8] != startedAt.Format(time.RFC3339) || training[9] != completedAt.Format(time.RFC3339) {
|
|
t.Fatalf("training times %q %q, want %q %q", training[8], training[9],
|
|
startedAt.Format(time.RFC3339), completedAt.Format(time.RFC3339))
|
|
}
|
|
if !strings.HasPrefix(training[2], "training ") {
|
|
t.Fatalf("training template name %q", training[2])
|
|
}
|
|
}
|
|
|
|
// TestProgramSendExportLanguage checks the per send export of a named program
|
|
// names the language of each send
|
|
func TestProgramSendExportLanguage(t *testing.T) {
|
|
f := newResultsFixture(newProgramServiceTest(t))
|
|
f.withTemplateLanguage("en")
|
|
now := time.Now().UTC()
|
|
f.languageSend("de", now)
|
|
f.languageSend("", now)
|
|
rows := [][]string{}
|
|
err := f.svc.programSendCSV(f.ctx, f.loadProgram(f.programID), func(row []string) error {
|
|
rows = append(rows, append([]string{}, row...))
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(rows) != 3 || rows[0][len(rows[0])-1] != "Language" {
|
|
t.Fatalf("export: %v", rows)
|
|
}
|
|
got := []string{rows[1][10], rows[2][10]}
|
|
joined := strings.Join(got, ",")
|
|
if joined != "de,en" && joined != "en,de" {
|
|
t.Fatalf("languages in export: %v", got)
|
|
}
|
|
}
|