Files
Redflag/aggregator-server/internal/database/migrations/migration018_test.go
jpetree331 ab676c3b83 test(database): B-1 pre-fix tests for migration and schema bugs
Pre-fix test suite documenting 9 database migration and schema
integrity bugs. Tests FAIL where they assert correct post-fix
behavior, PASS where they document current buggy state.

Tests added:
- F-B1-11 P0: main.go swallows migration errors (3 tests)
- F-B1-13: Duplicate migration numbers 009/012 (2 tests)
- F-B1-1: Migration 024 self-insert into schema_migrations (2 tests)
- F-B1-2: Migration 024 references non-existent column (2 tests)
- F-B1-3: Migration 018 wrong file suffix (2 tests)
- F-B1-4: Migration 018 GRANT to wrong role (1 test)
- F-B1-15: 7+ migrations not idempotent (2 tests)
- F-B1-5: Missing agent_commands sent_at index (2 tests)
- F-B1-6: N+1 query in GetDashboardStats (2 tests)
- F-B1-10: No background refresh token cleanup (2 tests)

Current state: 10 PASS, 10 FAIL, 0 SKIP.
All A-series tests continue to pass (no regressions).
See docs/B1_PreFix_Tests.md for full inventory.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 06:42:19 -04:00

111 lines
3.7 KiB
Go

package migrations_test
// migration018_test.go — Pre-fix tests for migration 018 filename bug.
//
// F-B1-3 HIGH: 018_create_scanner_config_table.sql has no .up.sql suffix.
// The migration runner only processes *.up.sql files (db.go:59).
// scanner_config table is NEVER created by the migration runner.
//
// F-B1-4 HIGH: GRANT references role `redflag_user` which does not exist.
// The default database user is `redflag`.
//
// Run: cd aggregator-server && go test ./internal/database/migrations/... -v -run TestMigration018
import (
"os"
"strings"
"testing"
)
// ---------------------------------------------------------------------------
// Test 3.1 — scanner_config migration has wrong file suffix (documents F-B1-3)
//
// Category: PASS-NOW (documents the bug)
// ---------------------------------------------------------------------------
func TestMigration018ScannerConfigHasWrongSuffix(t *testing.T) {
files, err := os.ReadDir(".")
if err != nil {
t.Fatalf("failed to read migrations directory: %v", err)
}
hasWrongSuffix := false
hasCorrectSuffix := false
for _, f := range files {
if f.Name() == "018_create_scanner_config_table.sql" {
hasWrongSuffix = true
}
if f.Name() == "018_create_scanner_config_table.up.sql" {
hasCorrectSuffix = true
}
}
if !hasWrongSuffix {
t.Error("[ERROR] [server] [database] F-B1-3 already fixed: " +
"018_create_scanner_config_table.sql no longer exists")
}
if hasCorrectSuffix {
t.Error("[ERROR] [server] [database] F-B1-3 already fixed: " +
"018_create_scanner_config_table.up.sql now exists")
}
t.Log("[INFO] [server] [database] F-B1-3 confirmed: scanner_config migration has .sql suffix (not .up.sql)")
t.Log("[INFO] [server] [database] the migration runner skips this file; scanner_config table is never created")
}
// ---------------------------------------------------------------------------
// Test 3.2 — scanner_config migration should have correct suffix (assert fix)
//
// Category: FAIL-NOW / PASS-AFTER-FIX
// ---------------------------------------------------------------------------
func TestMigration018ScannerConfigHasCorrectSuffix(t *testing.T) {
files, err := os.ReadDir(".")
if err != nil {
t.Fatalf("failed to read migrations directory: %v", err)
}
found := false
for _, f := range files {
if f.Name() == "018_create_scanner_config_table.up.sql" {
found = true
break
}
}
if !found {
t.Errorf("[ERROR] [server] [database] 018_create_scanner_config_table.up.sql not found.\n"+
"F-B1-3: scanner_config migration must have .up.sql suffix for the runner to process it.\n"+
"After fix: rename 018_create_scanner_config_table.sql to 018_create_scanner_config_table.up.sql")
}
}
// ---------------------------------------------------------------------------
// Test 3.3 — scanner_config migration should not GRANT to wrong role (F-B1-4)
//
// Category: FAIL-NOW / PASS-AFTER-FIX
// ---------------------------------------------------------------------------
func TestMigration018ScannerConfigHasNoGrantToWrongRole(t *testing.T) {
// Try both possible filenames
var content []byte
var err error
content, err = os.ReadFile("018_create_scanner_config_table.up.sql")
if err != nil {
content, err = os.ReadFile("018_create_scanner_config_table.sql")
if err != nil {
t.Fatalf("failed to read scanner_config migration (tried both suffixes): %v", err)
}
}
src := string(content)
if strings.Contains(src, "redflag_user") {
t.Errorf("[ERROR] [server] [database] scanner_config migration GRANTs to non-existent role `redflag_user`.\n"+
"F-B1-4: the default database user is `redflag`, not `redflag_user`.\n"+
"After fix: use correct role name or remove the GRANT statement.")
}
}