mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 01:53:42 +01:00
114 lines
3.9 KiB
Go
114 lines
3.9 KiB
Go
package service
|
|
|
|
import (
|
|
"math/rand"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/oapi-codegen/nullable"
|
|
"github.com/phishingclub/phishingclub/database"
|
|
"github.com/phishingclub/phishingclub/model"
|
|
"github.com/phishingclub/phishingclub/repository"
|
|
)
|
|
|
|
// TestProgramMixBatchCounts checks picks made earlier in a batch count for
|
|
// the later people of the same value, and other values stay apart
|
|
func TestProgramMixBatchCounts(t *testing.T) {
|
|
program := &model.Program{OrderMode: nullable.NewNullableWithValue(model.ProgramOrderShuffle)}
|
|
steps := []*model.ProgramStep{}
|
|
for i := 0; i < 4; i++ {
|
|
steps = append(steps, &model.ProgramStep{ID: nullable.NewNullableWithValue(uuid.New()), Position: nullable.NewNullableWithValue(i)})
|
|
}
|
|
mix := &programMix{values: map[uuid.UUID]string{}, counts: repository.ProgramMixCounts{}}
|
|
people := []uuid.UUID{}
|
|
for i := 0; i < 8; i++ {
|
|
id := uuid.New()
|
|
mix.values[id] = "sales"
|
|
people = append(people, id)
|
|
}
|
|
other := uuid.New()
|
|
mix.values[other] = "it"
|
|
rng := rand.New(rand.NewSource(1))
|
|
picked := map[uuid.UUID]int{}
|
|
for _, id := range people {
|
|
pick := programPickStepMixed(program, steps, rng, mix.countsFor(&id))
|
|
mix.add(&id, pick.ID.MustGet())
|
|
picked[pick.ID.MustGet()]++
|
|
}
|
|
for _, st := range steps {
|
|
if picked[st.ID.MustGet()] != 2 {
|
|
t.Fatalf("expected each template twice, got %v", picked)
|
|
}
|
|
}
|
|
if len(mix.countsFor(&other)) != 0 {
|
|
t.Fatal("another department must not see the sales picks")
|
|
}
|
|
// nil counts and same order pick as before
|
|
if programPickStepMixed(program, steps, rng, nil) == nil {
|
|
t.Fatal("expected a pick without counts")
|
|
}
|
|
seq := &model.Program{OrderMode: nullable.NewNullableWithValue(model.ProgramOrderSequential)}
|
|
if programPickStepMixed(seq, steps, rng, map[uuid.UUID]int{steps[0].ID.MustGet(): 9}) != steps[0] {
|
|
t.Fatal("same order takes the first step whatever the counts")
|
|
}
|
|
var none *programMix
|
|
if none.countsFor(&other) != nil {
|
|
t.Fatal("a nil mix has no counts")
|
|
}
|
|
none.add(&other, steps[0].ID.MustGet())
|
|
}
|
|
|
|
// TestMixKeepsPickOfStoredSend checks a pick whose send was stored right away
|
|
// stays in the mix counts when the rest of the person's writes fail
|
|
func TestMixKeepsPickOfStoredSend(t *testing.T) {
|
|
stepID := uuid.New()
|
|
stored, failed := uuid.New(), uuid.New()
|
|
rid := uuid.New()
|
|
mix := &programMix{
|
|
values: map[uuid.UUID]string{rid: "sales"},
|
|
counts: map[string]map[uuid.UUID]int{},
|
|
from: time.Now().Add(-time.Hour),
|
|
to: time.Now().Add(time.Hour),
|
|
}
|
|
now := time.Now()
|
|
mix.record(&stored, &rid, stepID, now)
|
|
mix.record(&failed, &rid, stepID, now)
|
|
e1 := &database.ProgramEnrollment{ID: &stored}
|
|
e2 := &database.ProgramEnrollment{ID: &failed}
|
|
w := &programWrites{people: []*programPersonWrites{{e: e1, sendStored: true}, {e: e2}}}
|
|
mixFailed := programMixFailed(w, map[uuid.UUID]bool{stored: true, failed: true})
|
|
mix.settle(mixFailed)
|
|
if got := mix.counts["sales"][stepID]; got != 1 {
|
|
t.Fatalf("expected the stored send counted once, got %d", got)
|
|
}
|
|
}
|
|
|
|
// TestProgramMixSettle checks the picks of a batch stay counted for the next
|
|
// batch of the tick only when stored and inside the window
|
|
func TestProgramMixSettle(t *testing.T) {
|
|
now := time.Now()
|
|
stepID := uuid.New()
|
|
kept, failed, late := uuid.New(), uuid.New(), uuid.New()
|
|
rid := uuid.New()
|
|
mix := &programMix{
|
|
values: map[uuid.UUID]string{rid: "sales"},
|
|
counts: repository.ProgramMixCounts{},
|
|
from: now.Add(-time.Hour),
|
|
to: now.Add(time.Hour),
|
|
}
|
|
mix.record(&kept, &rid, stepID, now)
|
|
mix.record(&failed, &rid, stepID, now)
|
|
mix.record(&late, &rid, stepID, now.Add(48*time.Hour))
|
|
if got := mix.counts["sales"][stepID]; got != 3 {
|
|
t.Fatalf("the batch must see all its picks, got %d", got)
|
|
}
|
|
mix.settle(map[uuid.UUID]bool{failed: true})
|
|
if got := mix.counts["sales"][stepID]; got != 1 {
|
|
t.Fatalf("only the stored pick inside the window stays, got %d", got)
|
|
}
|
|
var none *programMix
|
|
none.record(&kept, &rid, stepID, now)
|
|
none.settle(nil)
|
|
}
|