github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/aws/iam/remove_expired_certificates_test.go (about) 1 package iam 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/khulnasoft-lab/defsec/pkg/state" 8 defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types" 9 10 "github.com/khulnasoft-lab/defsec/pkg/providers/aws/iam" 11 "github.com/khulnasoft-lab/defsec/pkg/scan" 12 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestCheckRemoveExpiredCertificates(t *testing.T) { 17 tests := []struct { 18 name string 19 input iam.IAM 20 expected bool 21 }{ 22 { 23 name: "No certs", 24 input: iam.IAM{}, 25 expected: false, 26 }, 27 { 28 name: "Valid cert", 29 input: iam.IAM{ 30 ServerCertificates: []iam.ServerCertificate{ 31 { 32 Metadata: defsecTypes.NewTestMetadata(), 33 Expiration: defsecTypes.Time(time.Now().Add(time.Hour), defsecTypes.NewTestMetadata()), 34 }, 35 }, 36 }, 37 expected: false, 38 }, 39 { 40 name: "Expired cert", 41 input: iam.IAM{ 42 ServerCertificates: []iam.ServerCertificate{ 43 { 44 Metadata: defsecTypes.NewTestMetadata(), 45 Expiration: defsecTypes.Time(time.Now().Add(-time.Hour), defsecTypes.NewTestMetadata()), 46 }, 47 }, 48 }, 49 expected: true, 50 }, 51 } 52 for _, test := range tests { 53 t.Run(test.name, func(t *testing.T) { 54 var testState state.State 55 testState.AWS.IAM = test.input 56 results := CheckRemoveExpiredCertificates.Evaluate(&testState) 57 var found bool 58 for _, result := range results { 59 if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckRemoveExpiredCertificates.Rule().LongID() { 60 found = true 61 } 62 } 63 if test.expected { 64 assert.True(t, found, "Rule should have been found") 65 } else { 66 assert.False(t, found, "Rule should not have been found") 67 } 68 }) 69 } 70 }