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

     1  package apigateway
     2  
     3  import (
     4  	"testing"
     5  
     6  	defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types"
     7  
     8  	v1 "github.com/khulnasoft-lab/defsec/pkg/providers/aws/apigateway/v1"
     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 TestCheckEnableCache(t *testing.T) {
    18  	tests := []struct {
    19  		name     string
    20  		input    v1.APIGateway
    21  		expected bool
    22  	}{
    23  		{
    24  			name: "API Gateway stage with caching disabled",
    25  			input: v1.APIGateway{
    26  				APIs: []v1.API{
    27  					{
    28  						Metadata: defsecTypes.NewTestMetadata(),
    29  						Stages: []v1.Stage{
    30  							{
    31  								Metadata: defsecTypes.NewTestMetadata(),
    32  								RESTMethodSettings: []v1.RESTMethodSettings{
    33  									{
    34  										Metadata:     defsecTypes.NewTestMetadata(),
    35  										CacheEnabled: defsecTypes.Bool(false, defsecTypes.NewTestMetadata()),
    36  									},
    37  								},
    38  							},
    39  						},
    40  					},
    41  				},
    42  			},
    43  			expected: true,
    44  		},
    45  
    46  		{
    47  			name: "API Gateway stage with caching enabled",
    48  			input: v1.APIGateway{
    49  				APIs: []v1.API{
    50  					{
    51  						Metadata: defsecTypes.NewTestMetadata(),
    52  						Stages: []v1.Stage{
    53  							{
    54  								Metadata: defsecTypes.NewTestMetadata(),
    55  								RESTMethodSettings: []v1.RESTMethodSettings{
    56  									{
    57  										Metadata:     defsecTypes.NewTestMetadata(),
    58  										CacheEnabled: defsecTypes.Bool(true, defsecTypes.NewTestMetadata()),
    59  									},
    60  								},
    61  							},
    62  						},
    63  					},
    64  				},
    65  			},
    66  			expected: false,
    67  		},
    68  	}
    69  	for _, test := range tests {
    70  		t.Run(test.name, func(t *testing.T) {
    71  			var testState state.State
    72  			testState.AWS.APIGateway.V1 = test.input
    73  			results := CheckEnableCache.Evaluate(&testState)
    74  			var found bool
    75  			for _, result := range results {
    76  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckEnableCache.Rule().LongID() {
    77  					found = true
    78  				}
    79  			}
    80  			if test.expected {
    81  				assert.True(t, found, "Rule should have been found")
    82  			} else {
    83  				assert.False(t, found, "Rule should not have been found")
    84  			}
    85  		})
    86  	}
    87  }