package store import ( "os" "regexp" "strings" "testing" ) // TestResultQueriesFilterByOrg guards the tables that cannot use row-level // security: every read of check results must be restricted to one org. func TestResultQueriesFilterByOrg(t *testing.T) { src, err := os.ReadFile("queries/check_results.sql") if err != nil { t.Fatal(err) } blocks := regexp.MustCompile(`(?m)^-- name: `).Split(string(src), -1)[1:] if len(blocks) == 0 { t.Fatal("no queries found") } for _, b := range blocks { name, _, _ := strings.Cut(b, " ") if strings.Contains(b, ":copyfrom") { continue } // The organization is the first parameter, positional or named. if !regexp.MustCompile(`(?i)WHERE[^;]*\borg_id\s*=\s*(\$1|sqlc\.arg\(org_id\))`).MatchString(b) { t.Errorf("query %s does not filter by org_id = $1", name) } } }