feat(platform): add the notification inbox and deterministic match search
This commit is contained in:
1 parent
5edff21d2d
commit
0b11021b16
169 files changed
+17083
-443
No files matched your search
@@ -0,0 +1,135 @@
|
||||
//go:build integration
|
||||
|
||||
package integration_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
postgresadapter "lincodex/backend/internal/adapter/secondary/postgres"
|
||||
"lincodex/backend/internal/domain"
|
||||
)
|
||||
|
||||
const (
|
||||
// syntheticNotificationMigrationVersion adds the synthetic event type to
|
||||
// the inbox allowlist.
|
||||
syntheticNotificationMigrationVersion = 36
|
||||
checkViolation = "23514"
|
||||
notificationTypeConstraint = "notification_inbox_events_type_known"
|
||||
)
|
||||
|
||||
func TestSyntheticNotificationMigrationAllowlistAndGuardedRollback(t *testing.T) {
|
||||
env := bootstrap(t, postgresService)
|
||||
ctx, cancel := context.WithTimeout(t.Context(), migrationTestTimeout)
|
||||
defer cancel()
|
||||
databaseURL := env.createIsolatedDatabase(ctx, t)
|
||||
migrator := newMigrator(t, databaseURL)
|
||||
pool := newPoolFor(ctx, t, databaseURL)
|
||||
if _, err := migrator.Up(ctx); err != nil {
|
||||
t.Fatalf("Up() error = %v", err)
|
||||
}
|
||||
rollBackAboveVersion(ctx, t, migrator, syntheticNotificationMigrationVersion)
|
||||
|
||||
now := time.Now().UTC().Truncate(time.Microsecond)
|
||||
identity := identityFixture{ctx: ctx, pool: pool, store: postgresadapter.NewIdentityStore(pool), now: now}
|
||||
candidate := identity.createUser(t, "inbox-migration@example.com", domain.ScenarioCandidate, now.Add(-time.Hour))
|
||||
organizations := organizationFixture{
|
||||
ctx: ctx, pool: pool, identity: identity, transactor: postgresadapter.NewTransactor(pool), now: now,
|
||||
}
|
||||
owner := organizations.employer(t, "inbox-migration-owner@example.com")
|
||||
organization := organizations.create(t, owner, organizations.completeProfile(t, "Example Company"), now)
|
||||
|
||||
insert := func(eventType, sourceType string, schemaVersion int) error {
|
||||
_, err := pool.Exec(ctx, `INSERT INTO notification_inbox_events (
|
||||
recipient_user_id, event_id, organization_id, event_type, schema_version, source_type, source_id, occurred_at
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
|
||||
candidate.ID, uuid.New(), organization.ID(), eventType, schemaVersion, sourceType, organization.ID(), now)
|
||||
return err
|
||||
}
|
||||
if err := insert("invitation.delivered", "invitation", 1); err != nil {
|
||||
t.Fatalf("insert invitation event: %v", err)
|
||||
}
|
||||
if err := insert("notification.synthetic_delivered", "organization", 1); err != nil {
|
||||
t.Fatalf("insert synthetic event: %v", err)
|
||||
}
|
||||
for name, args := range map[string][3]any{
|
||||
"synthetic event with the invitation source": {"notification.synthetic_delivered", "invitation", 1},
|
||||
"invitation event with the organization": {"invitation.delivered", "organization", 1},
|
||||
"unknown event type": {"notification.mail_requested", "organization", 1},
|
||||
"unsupported schema version": {"notification.synthetic_delivered", "organization", 2},
|
||||
} {
|
||||
err := insert(args[0].(string), args[1].(string), args[2].(int))
|
||||
assertPgError(t, name, err, checkViolation, notificationTypeConstraint)
|
||||
}
|
||||
|
||||
// A rollback refuses to orphan a synthetic row and leaves the migration
|
||||
// applied.
|
||||
_, err := migrator.DownOne(ctx)
|
||||
assertPgError(t, "DownOne() with a synthetic inbox event", err, privacyIntegrityViolation, notificationTypeConstraint)
|
||||
statuses, err := migrator.Status(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("Status() error = %v", err)
|
||||
}
|
||||
for _, status := range statuses {
|
||||
if status.Version == syntheticNotificationMigrationVersion && !status.Applied {
|
||||
t.Fatal("the migration was rolled back despite a synthetic inbox event")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyntheticNotificationMigrationRollsBackWithoutSyntheticRows(t *testing.T) {
|
||||
env := bootstrap(t, postgresService)
|
||||
ctx, cancel := context.WithTimeout(t.Context(), migrationTestTimeout)
|
||||
defer cancel()
|
||||
databaseURL := env.createIsolatedDatabase(ctx, t)
|
||||
migrator := newMigrator(t, databaseURL)
|
||||
pool := newPoolFor(ctx, t, databaseURL)
|
||||
if _, err := migrator.Up(ctx); err != nil {
|
||||
t.Fatalf("Up() error = %v", err)
|
||||
}
|
||||
rollBackAboveVersion(ctx, t, migrator, syntheticNotificationMigrationVersion)
|
||||
result, err := migrator.DownOne(ctx)
|
||||
if err != nil || result.Version != syntheticNotificationMigrationVersion {
|
||||
t.Fatalf("DownOne() = %+v, %v, want version %d rolled back", result, err, syntheticNotificationMigrationVersion)
|
||||
}
|
||||
var constraint string
|
||||
if err := pool.QueryRow(ctx, `SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = $1`, notificationTypeConstraint).Scan(&constraint); err != nil {
|
||||
t.Fatalf("read the restored constraint: %v", err)
|
||||
}
|
||||
if constraint != "CHECK (((event_type = 'invitation.delivered'::text) AND (schema_version = 1) AND (source_type = 'invitation'::text)))" {
|
||||
t.Fatalf("restored constraint = %s, want the invitation-only allowlist", constraint)
|
||||
}
|
||||
if _, err := migrator.Up(ctx); err != nil {
|
||||
t.Fatalf("Up() after the rollback error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// rollBackAboveVersion rolls back every migration newer than version, so a
|
||||
// test of one migration keeps working when later migrations exist.
|
||||
func rollBackAboveVersion(ctx context.Context, t *testing.T, migrator *postgresadapter.Migrator, version int64) {
|
||||
t.Helper()
|
||||
for {
|
||||
statuses, err := migrator.Status(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("Status() while preparing migration %d: %v", version, err)
|
||||
}
|
||||
latest := int64(0)
|
||||
for _, status := range statuses {
|
||||
if status.Applied && status.Version > latest {
|
||||
latest = status.Version
|
||||
}
|
||||
}
|
||||
if latest < version {
|
||||
t.Fatalf("migration %d is not applied", version)
|
||||
}
|
||||
if latest == version {
|
||||
return
|
||||
}
|
||||
if _, err := migrator.DownOne(ctx); err != nil {
|
||||
t.Fatalf("DownOne() while preparing migration %d: %v", version, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user