github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/pkg/scanners/toml/scanner_test.go (about)

     1  package toml
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/khulnasoft-lab/defsec/pkg/framework"
     8  	"github.com/khulnasoft-lab/defsec/pkg/scanners/options"
     9  
    10  	"github.com/khulnasoft-lab/defsec/pkg/scan"
    11  
    12  	"github.com/khulnasoft-lab/defsec/test/testutil"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func Test_BasicScan(t *testing.T) {
    20  
    21  	fs := testutil.CreateFS(t, map[string]string{
    22  		"/code/code.toml": `
    23  [x]
    24  y = 123
    25  z = ["a", "b", "c"]
    26  `,
    27  		"/rules/rule.rego": `package builtin.toml.lol
    28  
    29  __rego_metadata__ := {
    30  	"id": "ABC123",
    31  	"avd_id": "AVD-AB-0123",
    32  	"title": "title",
    33  	"short_code": "short",
    34  	"severity": "CRITICAL",
    35  	"type": "TOML Check",
    36  	"description": "description",
    37  	"recommended_actions": "actions",
    38  	"url": "https://example.com",
    39  }
    40  
    41  __rego_input__ := {
    42  	"combine": false,
    43  	"selector": [{"type": "toml"}],
    44  }
    45  
    46  deny[res] {
    47  	input.x.y == 123
    48  	res := {
    49  		"msg": "oh no",
    50  		"startline": 1,
    51  		"endline": 2,
    52  	}
    53  }
    54  
    55  `,
    56  	})
    57  
    58  	scanner := NewScanner(options.ScannerWithPolicyDirs("rules"))
    59  
    60  	results, err := scanner.ScanFS(context.TODO(), fs, "code")
    61  	require.NoError(t, err)
    62  
    63  	require.Len(t, results.GetFailed(), 1)
    64  
    65  	assert.Equal(t, scan.Rule{
    66  		AVDID:          "AVD-AB-0123",
    67  		Aliases:        []string{"ABC123"},
    68  		ShortCode:      "short",
    69  		Summary:        "title",
    70  		Explanation:    "description",
    71  		Impact:         "",
    72  		Resolution:     "actions",
    73  		Provider:       "toml",
    74  		Service:        "general",
    75  		Links:          []string{"https://example.com"},
    76  		Severity:       "CRITICAL",
    77  		Terraform:      &scan.EngineMetadata{},
    78  		CloudFormation: &scan.EngineMetadata{},
    79  		CustomChecks: scan.CustomChecks{
    80  			Terraform: (*scan.TerraformCustomCheck)(nil)},
    81  		RegoPackage: "data.builtin.toml.lol",
    82  		Frameworks:  map[framework.Framework][]string{},
    83  	},
    84  		results.GetFailed()[0].Rule(),
    85  	)
    86  }