|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "github.com/stretchr/testify/suite" |
| 9 | +) |
| 10 | + |
| 11 | +type EvaluateInputCommandTestSuite struct { |
| 12 | + suite.Suite |
| 13 | +} |
| 14 | + |
| 15 | +func (suite *EvaluateInputCommandTestSuite) TestEvaluateInputCmd() { |
| 16 | + tests := []cmdTestCase{ |
| 17 | + { |
| 18 | + wantError: true, |
| 19 | + name: "missing --policy flag fails", |
| 20 | + cmd: "evaluate input", |
| 21 | + golden: "Error: required flag(s) \"policy\" not set\n", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "allow-all policy with input file returns ALLOWED", |
| 25 | + cmd: "evaluate input --input-file testdata/evaluate/trail-input.json --policy testdata/policies/allow-all.rego", |
| 26 | + goldenRegex: `RESULT:\s+ALLOWED`, |
| 27 | + }, |
| 28 | + { |
| 29 | + wantError: true, |
| 30 | + name: "deny-all policy with input file returns DENIED", |
| 31 | + cmd: "evaluate input --input-file testdata/evaluate/trail-input.json --policy testdata/policies/deny-all.rego", |
| 32 | + goldenRegex: `RESULT:\s+DENIED`, |
| 33 | + }, |
| 34 | + { |
| 35 | + wantError: true, |
| 36 | + name: "non-existent input file returns error", |
| 37 | + cmd: "evaluate input --input-file testdata/evaluate/no-such-file.json --policy testdata/policies/allow-all.rego", |
| 38 | + goldenRegex: `failed to read input file:`, |
| 39 | + }, |
| 40 | + { |
| 41 | + wantError: true, |
| 42 | + name: "invalid JSON input file returns error", |
| 43 | + cmd: "evaluate input --input-file testdata/policies/allow-all.rego --policy testdata/policies/allow-all.rego", |
| 44 | + goldenRegex: `failed to parse input:`, |
| 45 | + }, |
| 46 | + { |
| 47 | + wantError: true, |
| 48 | + name: "missing --input-file reads from stdin (empty stdin fails)", |
| 49 | + cmd: "evaluate input --policy testdata/policies/allow-all.rego", |
| 50 | + goldenRegex: `failed to parse input:`, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "JSON output with allow-all policy", |
| 54 | + cmd: "evaluate input --input-file testdata/evaluate/trail-input.json --policy testdata/policies/allow-all.rego --output json", |
| 55 | + goldenJson: []jsonCheck{ |
| 56 | + {"allow", true}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + wantError: true, |
| 61 | + name: "policy with wrong package returns error", |
| 62 | + cmd: "evaluate input --input-file testdata/evaluate/trail-input.json --policy testdata/policies/no-package-policy.rego", |
| 63 | + goldenRegex: `policy package must be 'package policy', got 'foo'`, |
| 64 | + }, |
| 65 | + { |
| 66 | + wantError: true, |
| 67 | + name: "policy missing allow rule returns error", |
| 68 | + cmd: "evaluate input --input-file testdata/evaluate/trail-input.json --policy testdata/policies/no-allow-rule.rego", |
| 69 | + goldenRegex: `policy must declare an 'allow' rule`, |
| 70 | + }, |
| 71 | + { |
| 72 | + wantError: true, |
| 73 | + name: "deny without violations rule returns DENIED with no violation messages", |
| 74 | + cmd: "evaluate input --input-file testdata/evaluate/trail-input.json --policy testdata/policies/deny-no-violations.rego", |
| 75 | + goldenRegex: `RESULT:\s+DENIED`, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "show-input includes input in JSON output", |
| 79 | + cmd: "evaluate input --input-file testdata/evaluate/trail-input.json --policy testdata/policies/allow-all.rego --output json --show-input", |
| 80 | + goldenJson: []jsonCheck{ |
| 81 | + {"allow", true}, |
| 82 | + {"input.trail.name", "test-trail"}, |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | + runTestCmd(suite.T(), tests) |
| 87 | +} |
| 88 | + |
| 89 | +func TestLoadInput(t *testing.T) { |
| 90 | + reader := strings.NewReader(`{"trail": {"name": "from-reader"}}`) |
| 91 | + input, err := loadInput(reader) |
| 92 | + require.NoError(t, err) |
| 93 | + trail, ok := input["trail"].(map[string]interface{}) |
| 94 | + require.True(t, ok) |
| 95 | + require.Equal(t, "from-reader", trail["name"]) |
| 96 | +} |
| 97 | + |
| 98 | +func TestLoadInputInvalidJSON(t *testing.T) { |
| 99 | + reader := strings.NewReader(`not json`) |
| 100 | + _, err := loadInput(reader) |
| 101 | + require.Error(t, err) |
| 102 | + require.Contains(t, err.Error(), "failed to parse input") |
| 103 | +} |
| 104 | + |
| 105 | +func TestEvaluateInputCommandTestSuite(t *testing.T) { |
| 106 | + suite.Run(t, new(EvaluateInputCommandTestSuite)) |
| 107 | +} |
0 commit comments