github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/aws/apigateway/enable_tracing_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 TestCheckEnableTracing(t *testing.T) {
    18  	tests := []struct {
    19  		name     string
    20  		input    v1.APIGateway
    21  		expected bool
    22  	}{
    23  		{
    24  			name: "API Gateway stage with X-Ray tracing disabled",
    25  			input: v1.APIGateway{
    26  				APIs: []v1.API{
    27  					{
    28  						Metadata: defsecTypes.NewTestMetadata(),
    29  						Stages: []v1.Stage{
    30  							{
    31  								Metadata:           defsecTypes.NewTestMetadata(),
    32  								XRayTracingEnabled: defsecTypes.Bool(false, defsecTypes.NewTestMetadata()),
    33  							},
    34  						},
    35  					},
    36  				},
    37  			},
    38  			expected: true,
    39  		},
    40  		{
    41  			name: "API Gateway stage with X-Ray tracing enabled",
    42  			input: v1.APIGateway{
    43  				APIs: []v1.API{
    44  					{
    45  						Metadata: defsecTypes.NewTestMetadata(),
    46  						Stages: []v1.Stage{
    47  							{
    48  								Metadata:           defsecTypes.NewTestMetadata(),
    49  								XRayTracingEnabled: defsecTypes.Bool(true, defsecTypes.NewTestMetadata()),
    50  							},
    51  						},
    52  					},
    53  				},
    54  			},
    55  			expected: false,
    56  		},
    57  	}
    58  	for _, test := range tests {
    59  		t.Run(test.name, func(t *testing.T) {
    60  			var testState state.State
    61  			testState.AWS.APIGateway.V1 = test.input
    62  			results := CheckEnableTracing.Evaluate(&testState)
    63  			var found bool
    64  			for _, result := range results {
    65  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckEnableTracing.Rule().LongID() {
    66  					found = true
    67  				}
    68  			}
    69  			if test.expected {
    70  				assert.True(t, found, "Rule should have been found")
    71  			} else {
    72  				assert.False(t, found, "Rule should not have been found")
    73  			}
    74  		})
    75  	}
    76  }