Files
lincodex/backend/integration_test/notification_subject_migration_test.go
T

114 lines
5.9 KiB
Go

//go:build integration
package integration_test
import (
"context"
"crypto/sha256"
"strings"
"testing"
"time"
"github.com/google/uuid"
)
// notificationSubjectMigrationVersion adds the subject columns of notifications;
// productNoticeMigrationVersion adds the rights reasons, the body clearing of a
// cancelled notice, and the product due index on top of it.
const (
notificationSubjectMigrationVersion = 44
productNoticeMigrationVersion = 45
)
// TestNotificationSubjectMigrations checks the constraints that the rights
// checks rely on, and that the rollback of the two migrations refuses while
// rows name a subject or carry a rights cancellation, and runs once they are gone.
func TestNotificationSubjectMigrations(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)
}
// Migration 00046 (verification codes) is newer than the subject migration
// under test. Roll it back first, so the rollback checks below act on 00045.
if result, err := migrator.DownOne(ctx); err != nil || result.Version != 46 {
t.Fatalf("roll back the newest migration: result %+v, error = %v; want migration 46", result, err)
}
now := time.Date(2026, 10, 9, 12, 0, 0, 0, time.UTC)
userID, deliveryID, invitationID, organizationID := uuid.New(), uuid.New(), uuid.New(), uuid.New()
exec := func(statement string, args ...any) error {
t.Helper()
_, err := pool.Exec(ctx, statement, args...)
return err
}
if err := exec(`INSERT INTO users (id, email, email_normalized, password_hash, scenario, onboarding_step, email_verified_at,
registration_version, last_registered_at, version, created_at, updated_at)
VALUES ($1, 'notice@example.com', 'notice@example.com', $2, 'candidate', 'candidate_consents', $3, 1, $3, 1, $3, $3)`,
userID, identityTestPasswordHash, now); err != nil {
t.Fatalf("insert the account: %v", err)
}
// A cancelled notice that names its invitation and has no bodies is what
// the sending worker stores after a rights check.
if err := exec(`INSERT INTO deliveries (id, recipient_user_id, recipient_email, kind, template_version, subject, text_body, html_body,
content_sha256, provider, provider_account, idempotency_key, status, attempt_count, next_attempt_at, version,
created_at, updated_at, cancel_reason, body_redacted_at, subject_kind, subject_id, subject_organization_id)
VALUES ($1, $2, 'notice@example.com', 'notification', 'notification.v1', 's', '', '', $3, 'resend', 'main',
'delivery/notice', 'cancelled', 0, NULL, 2, $4, $4, 'rights_revoked', $4, 'invitation', $5, $6)`,
deliveryID, userID, make([]byte, sha256.Size), now, invitationID, organizationID); err != nil {
t.Fatalf("store a cancelled notice with its subject: %v", err)
}
// A subject belongs to a notification only, and the three subject columns
// are set together.
if err := exec(`INSERT INTO deliveries (id, recipient_user_id, recipient_email, kind, template_version, subject, text_body, html_body,
content_sha256, provider, provider_account, idempotency_key, status, attempt_count, next_attempt_at, version,
created_at, updated_at, subject_kind, subject_id, subject_organization_id)
VALUES ($1, $2, 'notice@example.com', 'verification', 'verification.v1', 's', 't', '', $3, 'resend', 'main',
'delivery/subject-verification', 'pending', 0, $4, 1, $4, $4, 'invitation', $5, $6)`,
uuid.New(), userID, make([]byte, sha256.Size), now, invitationID, organizationID); err == nil || !strings.Contains(err.Error(), "deliveries_subject_complete") {
t.Fatalf("a subject on a verification mail: error = %v, want deliveries_subject_complete", err)
}
// Bodies are cleared only for a cancelled notice or a terminal token mail.
if err := exec(`INSERT INTO deliveries (id, recipient_user_id, recipient_email, kind, template_version, subject, text_body, html_body,
content_sha256, provider, provider_account, idempotency_key, status, attempt_count, next_attempt_at, version,
created_at, updated_at, body_redacted_at)
VALUES ($1, $2, 'notice@example.com', 'notification', 'notification.v1', 's', '', '', $3, 'resend', 'main',
'delivery/cleared-pending', 'pending', 0, $4, 1, $4, $4, $4)`,
uuid.New(), userID, make([]byte, sha256.Size), now); err == nil || !strings.Contains(err.Error(), "deliveries_body_present") {
t.Fatalf("a pending notice without bodies: error = %v, want deliveries_body_present", err)
}
// The subject is fixed with the delivery, like its content.
// The trigger reports its refusal by message, not by constraint name.
if err := exec(`UPDATE deliveries SET subject_id = gen_random_uuid() WHERE id = $1`, deliveryID); err == nil ||
!strings.Contains(err.Error(), "content and recipient are immutable") {
t.Fatalf("changing the subject: error = %v, want the content refusal", err)
}
// The rollback refuses while the schema holds rights cancellations.
if _, err := migrator.DownOne(ctx); err == nil || !strings.Contains(err.Error(), "rights check") {
t.Fatalf("DownOne() with a rights cancellation stored: error = %v, want the refusal that names the rights check", err)
}
// Once the account is gone, its notices go with it, and the rollback runs.
if err := exec(`DELETE FROM users WHERE id = $1`, userID); err != nil {
t.Fatalf("delete the account: %v", err)
}
for _, version := range []int64{productNoticeMigrationVersion, notificationSubjectMigrationVersion} {
result, err := migrator.DownOne(ctx)
if err != nil {
t.Fatalf("DownOne() to below %d error = %v", version, err)
}
if result.Version != version {
t.Fatalf("DownOne() rolled back %d, want %d", result.Version, version)
}
}
if _, err := migrator.Up(ctx); err != nil {
t.Fatalf("Up() after the rollback error = %v", err)
}
}