mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 18:13:40 +01:00
124 lines
4.5 KiB
Go
124 lines
4.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/phishingclub/phishingclub/database"
|
|
"github.com/phishingclub/phishingclub/testdb"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// lateEventWalk is the interleaving of the anonymize walk with a late commit:
|
|
// an event insert takes a seq and stays open, a second event with a higher
|
|
// seq commits, and the walk runs while the first is still open. The walk
|
|
// passes the open seq, so only the wait for the inserts in flight and the
|
|
// pass after it reach the event.
|
|
func lateEventWalk(t *testing.T, walk func(r *Program, campaignID *uuid.UUID) error) (*gorm.DB, *uuid.UUID) {
|
|
t.Helper()
|
|
db := newProgramTestDB(t)
|
|
camp, eventID := ptrUUID(), ptrUUID()
|
|
createHiddenCampaign(t, db, camp, nil)
|
|
for i := 0; i < 3; i++ {
|
|
addEventFor(t, db, camp, ptrUUID(), eventID)
|
|
}
|
|
late := ptrUUID()
|
|
err := whileHeld(t, db, func(tx *gorm.DB) error {
|
|
// takes the lower seq and holds the event insert lock shared
|
|
addEventFor(t, tx, camp, late, eventID)
|
|
// a higher seq that commits first
|
|
addEventFor(t, db, camp, ptrUUID(), eventID)
|
|
return nil
|
|
}, func() error {
|
|
return walk(&Program{DB: db}, camp)
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("walk: %v", err)
|
|
}
|
|
return db, camp
|
|
}
|
|
|
|
// TestMoveEventsToPseudonymsLateCommit checks Anonymize All of a program
|
|
// campaign reaches an event that commits after the walk passed its seq
|
|
func TestMoveEventsToPseudonymsLateCommit(t *testing.T) {
|
|
testdb.SkipUnlessPostgres(t)
|
|
db, camp := lateEventWalk(t, func(r *Program, campaignID *uuid.UUID) error {
|
|
return r.MoveEventsToPseudonyms(context.Background(), campaignID)
|
|
})
|
|
if n := eventsLeftWith(t, db, camp, "recipient_id"); n != 0 {
|
|
t.Fatalf("%d events still name a person", n)
|
|
}
|
|
if n := eventsLeftWith(t, db, camp, "ip_address"); n != 0 {
|
|
t.Fatalf("%d events still hold an IP address", n)
|
|
}
|
|
}
|
|
|
|
// TestScrubCampaignEventDataLateCommit is the same for Anonymize Data
|
|
func TestScrubCampaignEventDataLateCommit(t *testing.T) {
|
|
testdb.SkipUnlessPostgres(t)
|
|
db, camp := lateEventWalk(t, func(r *Program, campaignID *uuid.UUID) error {
|
|
return r.ScrubCampaignEventData(context.Background(), campaignID)
|
|
})
|
|
if n := eventsLeftWith(t, db, camp, "ip_address"); n != 0 {
|
|
t.Fatalf("%d events still hold an IP address", n)
|
|
}
|
|
var kept int64
|
|
if err := db.Model(&database.CampaignEvent{}).
|
|
Where("campaign_id = ? AND data <> ?", camp, "anonymized").Count(&kept).Error; err != nil {
|
|
t.Fatalf("count: %v", err)
|
|
}
|
|
if kept != 0 {
|
|
t.Fatalf("%d events kept their data", kept)
|
|
}
|
|
// the person stays, only the data goes
|
|
if n := eventsLeftWith(t, db, camp, "recipient_id"); n != 5 {
|
|
t.Fatalf("events with a person %d, want 5", n)
|
|
}
|
|
}
|
|
|
|
// TestPacedWalkIndexes checks on postgres the paced anonymize walks read an
|
|
// index in walk order from the cursor on: the events by campaign, event and
|
|
// seq, the unlinked sends with a code by campaign and id, so no chunk reads
|
|
// the rest of the campaign again
|
|
func TestPacedWalkIndexes(t *testing.T) {
|
|
testdb.SkipUnlessPostgres(t)
|
|
db := newProgramTestDB(t)
|
|
if err := (database.CampaignRecipient{}).Migrate(db); err != nil {
|
|
t.Fatalf("migrate sends: %v", err)
|
|
}
|
|
camp, eventID := ptrUUID(), ptrUUID()
|
|
addEvents(t, db, camp, eventID, 50)
|
|
addSends(t, db, camp, 50)
|
|
plan := func(query string, args ...any) string {
|
|
t.Helper()
|
|
var lines []string
|
|
err := db.Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Exec("SET LOCAL enable_seqscan = off").Error; err != nil {
|
|
return err
|
|
}
|
|
if err := tx.Exec("SET LOCAL enable_sort = off").Error; err != nil {
|
|
return err
|
|
}
|
|
return tx.Raw("EXPLAIN "+query, args...).Scan(&lines).Error
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("explain: %v", err)
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
events := plan(`SELECT seq AS row_id, event_id FROM campaign_events
|
|
WHERE campaign_id = ? AND (event_id, seq) > (?, ?)
|
|
ORDER BY event_id, seq LIMIT 500`, camp, eventID.String(), 7)
|
|
if !strings.Contains(events, "idx_campaign_events_campaign_event_seq") || strings.Contains(events, "Sort") {
|
|
t.Fatalf("the event walk does not read idx_campaign_events_campaign_event_seq in order:\n%s", events)
|
|
}
|
|
sends := plan(`SELECT id FROM campaign_recipients
|
|
WHERE campaign_id = ? AND (recipient_id IS NULL AND (lure_code IS NOT NULL OR message_id IS NOT NULL)) AND id > ?
|
|
ORDER BY id LIMIT 500`, camp, uuid.Nil)
|
|
if !strings.Contains(sends, "idx_campaign_recipients_unlinked_code") || strings.Contains(sends, "Sort") {
|
|
t.Fatalf("the unlinked send walk does not read idx_campaign_recipients_unlinked_code in order:\n%s", sends)
|
|
}
|
|
}
|