github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/pkg/rego/result_test.go (about) 1 package rego 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func Test_parseResult(t *testing.T) { 10 var testCases = []struct { 11 name string 12 input interface{} 13 want regoResult 14 }{ 15 { 16 name: "unknown", 17 input: nil, 18 want: regoResult{ 19 Managed: true, 20 Message: "Rego policy resulted in DENY", 21 }, 22 }, 23 { 24 name: "string", 25 input: "message", 26 want: regoResult{ 27 Managed: true, 28 Message: "message", 29 }, 30 }, 31 { 32 name: "strings", 33 input: []interface{}{"message"}, 34 want: regoResult{ 35 Managed: true, 36 Message: "message", 37 }, 38 }, 39 { 40 name: "maps", 41 input: []interface{}{ 42 "message", 43 map[string]interface{}{ 44 "filepath": "a.out", 45 }, 46 }, 47 want: regoResult{ 48 Managed: true, 49 Message: "message", 50 Filepath: "a.out", 51 }, 52 }, 53 { 54 name: "map", 55 input: map[string]interface{}{ 56 "msg": "message", 57 "filepath": "a.out", 58 "fskey": "abcd", 59 "resource": "resource", 60 "startline": "123", 61 "endline": "456", 62 "sourceprefix": "git", 63 "explicit": true, 64 "managed": true, 65 }, 66 want: regoResult{ 67 Message: "message", 68 Filepath: "a.out", 69 Resource: "resource", 70 StartLine: 123, 71 EndLine: 456, 72 SourcePrefix: "git", 73 FSKey: "abcd", 74 Explicit: true, 75 Managed: true, 76 }, 77 }, 78 { 79 name: "parent", 80 input: map[string]interface{}{ 81 "msg": "child", 82 "parent": map[string]interface{}{ 83 "msg": "parent", 84 }, 85 }, 86 want: regoResult{ 87 Message: "child", 88 Managed: true, 89 Parent: ®oResult{ 90 Message: "parent", 91 Managed: true, 92 }, 93 }, 94 }, 95 } 96 97 for _, tc := range testCases { 98 t.Run(tc.name, func(t *testing.T) { 99 have := parseResult(tc.input) 100 assert.NotNil(t, have) 101 assert.Equal(t, tc.want, *have) 102 }) 103 } 104 }