mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"skillshare/internal/audit"
|
|
)
|
|
|
|
func TestIsSecurityError(t *testing.T) {
|
|
tests := []struct {
|
|
err error
|
|
want bool
|
|
}{
|
|
{nil, false},
|
|
{fmt.Errorf("connection timeout"), false},
|
|
{fmt.Errorf("skill not found"), false},
|
|
{fmt.Errorf("has uncommitted changes"), false},
|
|
|
|
// Sentinel-based detection
|
|
{fmt.Errorf("security audit failed: scan error: %w", audit.ErrBlocked), true},
|
|
{fmt.Errorf("post-update audit failed: scan error — rolled back: %w", audit.ErrBlocked), true},
|
|
{fmt.Errorf("rollback failed: permission denied: %w", audit.ErrBlocked), true},
|
|
{audit.ErrBlocked, true},
|
|
|
|
// Wrapped sentinel
|
|
{fmt.Errorf("outer: %w", fmt.Errorf("inner: %w", audit.ErrBlocked)), true},
|
|
|
|
// Non-sentinel errors with similar text should NOT match
|
|
{fmt.Errorf("security audit something"), false},
|
|
{fmt.Errorf("was rolled back successfully"), false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
name := "nil"
|
|
if tt.err != nil {
|
|
name = tt.err.Error()
|
|
if len(name) > 60 {
|
|
name = name[:60]
|
|
}
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
got := isSecurityError(tt.err)
|
|
if got != tt.want {
|
|
t.Errorf("isSecurityError(%v) = %v, want %v", tt.err, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseUpdateArgs_ThresholdAlias(t *testing.T) {
|
|
opts, showHelp, err := parseUpdateArgs([]string{"--all", "-T", "h"})
|
|
if err != nil {
|
|
t.Fatalf("parseUpdateArgs returned error: %v", err)
|
|
}
|
|
if showHelp {
|
|
t.Fatal("expected showHelp=false")
|
|
}
|
|
if opts.threshold != audit.SeverityHigh {
|
|
t.Fatalf("expected threshold=%s, got %s", audit.SeverityHigh, opts.threshold)
|
|
}
|
|
}
|
|
|
|
func TestParseUpdateArgs_InvalidThreshold(t *testing.T) {
|
|
_, showHelp, err := parseUpdateArgs([]string{"--all", "--threshold", "urgent"})
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid threshold")
|
|
}
|
|
if showHelp {
|
|
t.Fatal("expected showHelp=false on invalid threshold")
|
|
}
|
|
}
|