github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/azure/monitor/capture_all_activities_test.go (about)

     1  package monitor
     2  
     3  import (
     4  	"testing"
     5  
     6  	defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types"
     7  
     8  	"github.com/khulnasoft-lab/defsec/pkg/state"
     9  
    10  	"github.com/khulnasoft-lab/defsec/pkg/providers/azure/monitor"
    11  	"github.com/khulnasoft-lab/defsec/pkg/scan"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestCheckCaptureAllActivities(t *testing.T) {
    17  	tests := []struct {
    18  		name     string
    19  		input    monitor.Monitor
    20  		expected bool
    21  	}{
    22  		{
    23  			name: "Log profile captures only write activities",
    24  			input: monitor.Monitor{
    25  				LogProfiles: []monitor.LogProfile{
    26  					{
    27  						Metadata: defsecTypes.NewTestMetadata(),
    28  						Categories: []defsecTypes.StringValue{
    29  							defsecTypes.String("Write", defsecTypes.NewTestMetadata()),
    30  						},
    31  					},
    32  				},
    33  			},
    34  			expected: true,
    35  		},
    36  		{
    37  			name: "Log profile captures action, write, delete activities",
    38  			input: monitor.Monitor{
    39  				LogProfiles: []monitor.LogProfile{
    40  					{
    41  						Metadata: defsecTypes.NewTestMetadata(),
    42  						Categories: []defsecTypes.StringValue{
    43  							defsecTypes.String("Action", defsecTypes.NewTestMetadata()),
    44  							defsecTypes.String("Write", defsecTypes.NewTestMetadata()),
    45  							defsecTypes.String("Delete", defsecTypes.NewTestMetadata()),
    46  						},
    47  					},
    48  				},
    49  			},
    50  			expected: false,
    51  		},
    52  	}
    53  	for _, test := range tests {
    54  		t.Run(test.name, func(t *testing.T) {
    55  			var testState state.State
    56  			testState.Azure.Monitor = test.input
    57  			results := CheckCaptureAllActivities.Evaluate(&testState)
    58  			var found bool
    59  			for _, result := range results {
    60  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckCaptureAllActivities.Rule().LongID() {
    61  					found = true
    62  				}
    63  			}
    64  			if test.expected {
    65  				assert.True(t, found, "Rule should have been found")
    66  			} else {
    67  				assert.False(t, found, "Rule should not have been found")
    68  			}
    69  		})
    70  	}
    71  }