github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/aws/ec2/add_description_to_security_group_test.go (about)

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