mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 01:53:42 +01:00
121 lines
5.2 KiB
Go
121 lines
5.2 KiB
Go
package service
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/oapi-codegen/nullable"
|
|
"github.com/phishingclub/phishingclub/database"
|
|
"github.com/phishingclub/phishingclub/model"
|
|
)
|
|
|
|
// TestProgramDataSavingStepCampaigns checks a hidden campaign gets data saving
|
|
// from its program when it is created and follows a change while it is open,
|
|
// while a closed campaign keeps what it had
|
|
func TestProgramDataSavingStepCampaigns(t *testing.T) {
|
|
p := newProgramDataSavingTest(t)
|
|
session := authorizedSession(t)
|
|
|
|
offID := p.createDataSavingProgram("off", false, false)
|
|
if p.campaignSavesData(p.stepCampaignOf(offID, 0)) {
|
|
t.Fatal("a program without data saving must create campaigns that do not save data")
|
|
}
|
|
|
|
programID := p.createDataSavingProgram("on", false, true)
|
|
open := p.stepCampaignOf(programID, 0)
|
|
if !p.campaignSavesData(open) {
|
|
t.Fatal("the hidden campaign must save data when the program does")
|
|
}
|
|
closed := p.stepCampaignOf(programID, 1)
|
|
if err := p.db.Model(&database.Campaign{}).Where("id = ?", closed).Update("closed_at", time.Now().UTC()).Error; err != nil {
|
|
t.Fatalf("close: %v", err)
|
|
}
|
|
|
|
off := &model.Program{Name: p.loadProgram(programID).Name, SaveSubmittedData: nullable.NewNullableWithValue(false)}
|
|
if err := p.svc.UpdateByID(p.ctx, session, &programID, off); err != nil {
|
|
t.Fatalf("turn off: %v", err)
|
|
}
|
|
if p.campaignSavesData(open) {
|
|
t.Fatal("an open hidden campaign must stop saving data when the program does")
|
|
}
|
|
if !p.campaignSavesData(closed) {
|
|
t.Fatal("a closed hidden campaign must be left as it was")
|
|
}
|
|
|
|
on := &model.Program{Name: p.loadProgram(programID).Name, SaveSubmittedData: nullable.NewNullableWithValue(true)}
|
|
if err := p.svc.UpdateByID(p.ctx, session, &programID, on); err != nil {
|
|
t.Fatalf("turn on: %v", err)
|
|
}
|
|
if !p.campaignSavesData(open) {
|
|
t.Fatal("an open hidden campaign must save data again when the program does")
|
|
}
|
|
}
|
|
|
|
// TestTickRepairsStaleCampaignSettings checks the settings of a program are
|
|
// copied to an open hidden campaign whose lists, webhooks, pages or data
|
|
// saving differ, for example after the copy of an edit failed, and that a
|
|
// campaign that matches, a closed campaign and a program being closed are
|
|
// left alone
|
|
func TestTickRepairsStaleCampaignSettings(t *testing.T) {
|
|
p := newProgramLifecycleTest(t)
|
|
programID := p.program(model.ProgramLoopComplete)
|
|
stepID := p.step(programID, 0, true)
|
|
stale := p.stepCampaign(programID, stepID, 0)
|
|
closedCampaign := p.stepCampaign(programID, stepID, 1)
|
|
p.db.Model(&database.Campaign{}).Where("id = ?", closedCampaign).Update("closed_at", time.Now().UTC())
|
|
list, oldList, hook, page := uuid.New(), uuid.New(), uuid.New(), uuid.New()
|
|
p.create(&database.ProgramAllowDeny{ProgramID: &programID, AllowDenyID: &list})
|
|
p.create(&database.ProgramWebhook{ProgramID: &programID, WebhookID: &hook, WebhookIncludeData: "full", WebhookEvents: 3})
|
|
p.setProgram(programID, map[string]any{"deny_page_id": page})
|
|
// the campaign kept the list of before the edit
|
|
p.create(&database.CampaignAllowDeny{CampaignID: &stale, AllowDenyID: &oldList})
|
|
p.create(&database.CampaignAllowDeny{CampaignID: &closedCampaign, AllowDenyID: &oldList})
|
|
|
|
// a matching program is not listed
|
|
other := p.program(model.ProgramLoopComplete)
|
|
otherStep := p.step(other, 0, true)
|
|
p.stepCampaign(other, otherStep, 0)
|
|
|
|
found, err := p.svc.ProgramRepository.GetProgramsWithStaleCampaignSettings(p.ctx, 10)
|
|
if err != nil || len(found) != 1 || *found[0] != programID {
|
|
t.Fatalf("expected only the stale program listed, got %v %v", found, err)
|
|
}
|
|
// a program being closed is left to its close
|
|
p.setProgram(programID, map[string]any{"closing_at": time.Now().UTC()})
|
|
if found, err := p.svc.ProgramRepository.GetProgramsWithStaleCampaignSettings(p.ctx, 10); err != nil || len(found) != 0 {
|
|
t.Fatalf("a program being closed must not be listed, got %v %v", found, err)
|
|
}
|
|
p.setProgram(programID, map[string]any{"closing_at": nil})
|
|
|
|
p.svc.lastRetentionAt = time.Now()
|
|
if err := p.svc.Tick(p.ctx, authorizedSession(t)); err != nil {
|
|
t.Fatalf("tick: %v", err)
|
|
}
|
|
p.waitForCloses(t)
|
|
var lists []database.CampaignAllowDeny
|
|
p.db.Where("campaign_id = ?", stale).Find(&lists)
|
|
if len(lists) != 1 || *lists[0].AllowDenyID != list {
|
|
t.Fatalf("expected the program list on the campaign, got %v", lists)
|
|
}
|
|
var hooks []database.CampaignWebhook
|
|
p.db.Where("campaign_id = ?", stale).Find(&hooks)
|
|
if len(hooks) != 1 || *hooks[0].WebhookID != hook || hooks[0].WebhookEvents != 3 {
|
|
t.Fatalf("expected the program webhook on the campaign, got %v", hooks)
|
|
}
|
|
if row := p.campaignRow(stale); row.DenyPageID == nil || *row.DenyPageID != page {
|
|
t.Fatalf("expected the deny page copied, got %v", row.DenyPageID)
|
|
}
|
|
var closedLists []database.CampaignAllowDeny
|
|
p.db.Where("campaign_id = ?", closedCampaign).Find(&closedLists)
|
|
if len(closedLists) != 1 || *closedLists[0].AllowDenyID != oldList {
|
|
t.Fatalf("a closed campaign keeps its settings, got %v", closedLists)
|
|
}
|
|
if found, err := p.svc.ProgramRepository.GetProgramsWithStaleCampaignSettings(p.ctx, 10); err != nil || len(found) != 0 {
|
|
t.Fatalf("expected nothing left to repair, got %v %v", found, err)
|
|
}
|
|
if !lockIsFree(p.svc) {
|
|
t.Fatal("the repair must release the lock")
|
|
}
|
|
}
|