mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 10:03:46 +01:00
96 lines
3.7 KiB
Go
96 lines
3.7 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/phishingclub/phishingclub/database"
|
|
"github.com/phishingclub/phishingclub/errs"
|
|
"github.com/phishingclub/phishingclub/model"
|
|
"github.com/phishingclub/phishingclub/repository"
|
|
"github.com/phishingclub/phishingclub/vo"
|
|
)
|
|
|
|
func TestProgramGetCalendarEntries(t *testing.T) {
|
|
p := newProgramServiceTest(t)
|
|
if err := p.db.AutoMigrate(&database.Company{}); err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
session := authorizedSession(t)
|
|
now := time.Now().UTC()
|
|
at := func(d time.Duration) *time.Time { v := now.Add(d); return &v }
|
|
day := 24 * time.Hour
|
|
companyA, companyB := uuid.New(), uuid.New()
|
|
p.create(&database.Company{ID: companyA, CreatedAt: &now, UpdatedAt: &now, Name: "Company A", AssetsKey: "a"})
|
|
|
|
setEnd := func(id uuid.UUID, end *time.Time) {
|
|
p.setProgram(id, map[string]any{"end_at": end})
|
|
}
|
|
running := p.statsProgram(&companyA, at(-30*day), at(-30*day), nil, nil)
|
|
scheduledIn := p.statsProgram(&companyA, at(3*day), nil, nil, nil)
|
|
p.statsProgram(&companyA, at(30*day), nil, nil, nil) // starts after the range
|
|
p.statsProgram(&companyA, nil, nil, nil, nil) // draft
|
|
p.statsProgram(&companyA, at(-60*day), at(-60*day), nil, at(-20*day))
|
|
closedIn := p.statsProgram(&companyA, at(-60*day), at(-60*day), nil, at(-2*day))
|
|
endedBefore := p.statsProgram(&companyA, at(-60*day), at(-60*day), nil, nil)
|
|
setEnd(endedBefore, at(-10*day))
|
|
endsIn := p.statsProgram(&companyA, at(-60*day), at(-60*day), nil, nil)
|
|
setEnd(endsIn, at(2*day))
|
|
paused := p.statsProgram(&companyA, at(-30*day), at(-30*day), at(-day), nil)
|
|
other := p.statsProgram(&companyB, at(-30*day), at(-30*day), nil, nil)
|
|
|
|
options := &repository.ProgramOption{QueryArgs: &vo.QueryArgs{Limit: 100, OrderBy: "`programs`.`name`"}}
|
|
got := func(companyID *uuid.UUID) map[uuid.UUID]*model.ProgramCalendarEntry {
|
|
t.Helper()
|
|
res, err := p.svc.GetCalendarEntries(p.ctx, session, companyID, now.Add(-7*day), now.Add(7*day), options)
|
|
if err != nil {
|
|
t.Fatalf("calendar: %v", err)
|
|
}
|
|
out := map[uuid.UUID]*model.ProgramCalendarEntry{}
|
|
for _, row := range res.Rows {
|
|
out[row.ID] = row
|
|
}
|
|
return out
|
|
}
|
|
|
|
a := got(&companyA)
|
|
want := []uuid.UUID{running, scheduledIn, closedIn, endsIn, paused}
|
|
if len(a) != len(want) {
|
|
t.Fatalf("company A: got %d entries, want %d", len(a), len(want))
|
|
}
|
|
for _, id := range want {
|
|
if a[id] == nil {
|
|
t.Fatalf("company A: missing %s", id)
|
|
}
|
|
}
|
|
if a[paused].Status != model.ProgramStatusPaused || a[scheduledIn].Status != model.ProgramStatusScheduled ||
|
|
a[closedIn].Status != model.ProgramStatusClosed || a[running].Status != model.ProgramStatusActive {
|
|
t.Fatalf("unexpected statuses: %+v", a)
|
|
}
|
|
if a[running].CompanyName != "Company A" {
|
|
t.Fatalf("company name: %q", a[running].CompanyName)
|
|
}
|
|
if b := got(&companyB); len(b) != 1 || b[other] == nil {
|
|
t.Fatalf("company B: %+v", b)
|
|
}
|
|
if all := got(nil); len(all) != len(want)+1 {
|
|
t.Fatalf("every company: got %d entries", len(all))
|
|
}
|
|
|
|
// paging is bounded like the campaign calendar
|
|
paged, err := p.svc.GetCalendarEntries(p.ctx, session, &companyA, now.Add(-7*day), now.Add(7*day),
|
|
&repository.ProgramOption{QueryArgs: &vo.QueryArgs{Limit: 2, OrderBy: "`programs`.`name`"}})
|
|
if err != nil || len(paged.Rows) != 2 || !paged.HasNextPage {
|
|
t.Fatalf("paging: %v %+v", err, paged)
|
|
}
|
|
|
|
if _, err := p.svc.GetCalendarEntries(p.ctx, unauthorizedSession(t), nil, now, now, options); !errors.Is(err, errs.ErrAuthorizationFailed) {
|
|
t.Fatalf("expected a refused read, got %v", err)
|
|
}
|
|
if _, err := p.svc.GetCalendarEntries(p.ctx, session, nil, now, now.Add(-day), options); err == nil {
|
|
t.Fatalf("an end before the start must be refused")
|
|
}
|
|
}
|