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

     1  package storage
     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/storage"
    11  	"github.com/khulnasoft-lab/defsec/pkg/scan"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestCheckQueueServicesLoggingEnabled(t *testing.T) {
    17  	tests := []struct {
    18  		name     string
    19  		input    storage.Storage
    20  		expected bool
    21  	}{
    22  		{
    23  			name: "Storage account queue properties logging disabled",
    24  			input: storage.Storage{
    25  				Accounts: []storage.Account{
    26  					{
    27  						Metadata: defsecTypes.NewTestMetadata(),
    28  						QueueProperties: storage.QueueProperties{
    29  							Metadata:      defsecTypes.NewTestMetadata(),
    30  							EnableLogging: defsecTypes.Bool(false, defsecTypes.NewTestMetadata()),
    31  						},
    32  						Queues: []storage.Queue{
    33  							{
    34  								Metadata: defsecTypes.NewTestMetadata(),
    35  								Name:     defsecTypes.String("my-queue", defsecTypes.NewTestMetadata()),
    36  							},
    37  						},
    38  					},
    39  				},
    40  			},
    41  			expected: true,
    42  		},
    43  		{
    44  			name: "Storage account queue properties logging disabled with no queues",
    45  			input: storage.Storage{
    46  				Accounts: []storage.Account{
    47  					{
    48  						Metadata: defsecTypes.NewTestMetadata(),
    49  						QueueProperties: storage.QueueProperties{
    50  							Metadata:      defsecTypes.NewTestMetadata(),
    51  							EnableLogging: defsecTypes.Bool(false, defsecTypes.NewTestMetadata()),
    52  						},
    53  					},
    54  				},
    55  			},
    56  			expected: false,
    57  		},
    58  		{
    59  			name: "Storage account queue properties logging enabled",
    60  			input: storage.Storage{
    61  				Accounts: []storage.Account{
    62  					{
    63  						Metadata: defsecTypes.NewTestMetadata(),
    64  						QueueProperties: storage.QueueProperties{
    65  							Metadata:      defsecTypes.NewTestMetadata(),
    66  							EnableLogging: defsecTypes.Bool(true, defsecTypes.NewTestMetadata()),
    67  						},
    68  					},
    69  				},
    70  			},
    71  			expected: false,
    72  		},
    73  	}
    74  	for _, test := range tests {
    75  		t.Run(test.name, func(t *testing.T) {
    76  			var testState state.State
    77  			testState.Azure.Storage = test.input
    78  			results := CheckQueueServicesLoggingEnabled.Evaluate(&testState)
    79  			var found bool
    80  			for _, result := range results {
    81  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckQueueServicesLoggingEnabled.Rule().LongID() {
    82  					found = true
    83  				}
    84  			}
    85  			if test.expected {
    86  				assert.True(t, found, "Rule should have been found")
    87  			} else {
    88  				assert.False(t, found, "Rule should not have been found")
    89  			}
    90  		})
    91  	}
    92  }