github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/nifcloud/computing/add_description_to_security_group_test.go (about) 1 package computing 2 3 import ( 4 "testing" 5 6 "github.com/khulnasoft-lab/defsec/pkg/providers/nifcloud/computing" 7 defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types" 8 9 "github.com/khulnasoft-lab/defsec/pkg/state" 10 11 "github.com/khulnasoft-lab/defsec/pkg/scan" 12 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestCheckAddDescriptionToSecurityGroup(t *testing.T) { 17 tests := []struct { 18 name string 19 input computing.Computing 20 expected bool 21 }{ 22 { 23 name: "NIFCLOUD security group with no description provided", 24 input: computing.Computing{ 25 SecurityGroups: []computing.SecurityGroup{ 26 { 27 Metadata: defsecTypes.NewTestMetadata(), 28 Description: defsecTypes.String("", defsecTypes.NewTestMetadata()), 29 }, 30 }, 31 }, 32 expected: true, 33 }, 34 { 35 name: "NIFCLOUD security group with default description", 36 input: computing.Computing{ 37 SecurityGroups: []computing.SecurityGroup{ 38 { 39 Metadata: defsecTypes.NewTestMetadata(), 40 Description: defsecTypes.String("Managed by Terraform", defsecTypes.NewTestMetadata()), 41 }, 42 }, 43 }, 44 expected: true, 45 }, 46 { 47 name: "NIFCLOUD security group with proper description", 48 input: computing.Computing{ 49 SecurityGroups: []computing.SecurityGroup{ 50 { 51 Metadata: defsecTypes.NewTestMetadata(), 52 Description: defsecTypes.String("some proper description", defsecTypes.NewTestMetadata()), 53 }, 54 }, 55 }, 56 expected: false, 57 }, 58 } 59 for _, test := range tests { 60 t.Run(test.name, func(t *testing.T) { 61 var testState state.State 62 testState.Nifcloud.Computing = test.input 63 results := CheckAddDescriptionToSecurityGroup.Evaluate(&testState) 64 var found bool 65 for _, result := range results { 66 if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckAddDescriptionToSecurityGroup.Rule().LongID() { 67 found = true 68 } 69 } 70 if test.expected { 71 assert.True(t, found, "Rule should have been found") 72 } else { 73 assert.False(t, found, "Rule should not have been found") 74 } 75 }) 76 } 77 }