|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/boostsecurityio/poutine/models" |
| 7 | + "github.com/boostsecurityio/poutine/results" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestErrViolationsFound(t *testing.T) { |
| 13 | + require.EqualError(t, ErrViolationsFound, "poutine: violations found") |
| 14 | + assert.Implements(t, (*error)(nil), ErrViolationsFound) |
| 15 | +} |
| 16 | + |
| 17 | +func TestExitCodeViolations(t *testing.T) { |
| 18 | + assert.Equal(t, 10, exitCodeViolations) |
| 19 | +} |
| 20 | + |
| 21 | +func TestFailOnViolationFlag(t *testing.T) { |
| 22 | + flag := RootCmd.PersistentFlags().Lookup("fail-on-violation") |
| 23 | + require.NotNil(t, flag, "--fail-on-violation flag should be registered") |
| 24 | + assert.Equal(t, "false", flag.DefValue, "--fail-on-violation should default to false") |
| 25 | +} |
| 26 | + |
| 27 | +func TestCheckViolations(t *testing.T) { |
| 28 | + pkgWithFindings := &models.PackageInsights{ |
| 29 | + FindingsResults: results.FindingsResult{ |
| 30 | + Findings: []results.Finding{ |
| 31 | + {RuleId: "injection", Purl: "pkg:github/example/repo"}, |
| 32 | + }, |
| 33 | + }, |
| 34 | + } |
| 35 | + pkgNoFindings := &models.PackageInsights{ |
| 36 | + FindingsResults: results.FindingsResult{ |
| 37 | + Findings: []results.Finding{}, |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + t.Run("returns nil when failOnViolation is false", func(t *testing.T) { |
| 42 | + failOnViolation = false |
| 43 | + defer func() { failOnViolation = false }() |
| 44 | + |
| 45 | + assert.NoError(t, checkViolations(pkgWithFindings)) |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("returns ErrViolationsFound when findings exist", func(t *testing.T) { |
| 49 | + failOnViolation = true |
| 50 | + defer func() { failOnViolation = false }() |
| 51 | + |
| 52 | + assert.ErrorIs(t, checkViolations(pkgWithFindings), ErrViolationsFound) |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("returns nil when no findings", func(t *testing.T) { |
| 56 | + failOnViolation = true |
| 57 | + defer func() { failOnViolation = false }() |
| 58 | + |
| 59 | + assert.NoError(t, checkViolations(pkgNoFindings)) |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("returns nil for nil package", func(t *testing.T) { |
| 63 | + failOnViolation = true |
| 64 | + defer func() { failOnViolation = false }() |
| 65 | + |
| 66 | + assert.NoError(t, checkViolations(nil)) |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("returns nil when called with no args", func(t *testing.T) { |
| 70 | + failOnViolation = true |
| 71 | + defer func() { failOnViolation = false }() |
| 72 | + |
| 73 | + assert.NoError(t, checkViolations()) |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +func TestCheckViolationsOrg(t *testing.T) { |
| 78 | + pkgWithFindings := &models.PackageInsights{ |
| 79 | + FindingsResults: results.FindingsResult{ |
| 80 | + Findings: []results.Finding{ |
| 81 | + {RuleId: "injection", Purl: "pkg:github/example/repo"}, |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | + pkgNoFindings := &models.PackageInsights{} |
| 86 | + |
| 87 | + t.Run("returns ErrViolationsFound when any package has findings", func(t *testing.T) { |
| 88 | + failOnViolation = true |
| 89 | + defer func() { failOnViolation = false }() |
| 90 | + |
| 91 | + err := checkViolations(pkgNoFindings, pkgWithFindings) |
| 92 | + assert.ErrorIs(t, err, ErrViolationsFound) |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("returns nil when no packages have findings", func(t *testing.T) { |
| 96 | + failOnViolation = true |
| 97 | + defer func() { failOnViolation = false }() |
| 98 | + |
| 99 | + err := checkViolations(pkgNoFindings, pkgNoFindings) |
| 100 | + assert.NoError(t, err) |
| 101 | + }) |
| 102 | +} |
0 commit comments