github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/internal/adapters/terraform/aws/sns/adapt_test.go (about) 1 package sns 2 3 import ( 4 "testing" 5 6 defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types" 7 8 "github.com/khulnasoft-lab/defsec/pkg/providers/aws/sns" 9 10 "github.com/khulnasoft-lab/defsec/internal/adapters/terraform/tftestutil" 11 12 "github.com/khulnasoft-lab/defsec/test/testutil" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func Test_adaptTopic(t *testing.T) { 18 tests := []struct { 19 name string 20 terraform string 21 expected sns.Topic 22 }{ 23 { 24 name: "defined", 25 terraform: ` 26 resource "aws_sns_topic" "good_example" { 27 kms_master_key_id = "/blah" 28 } 29 `, 30 expected: sns.Topic{ 31 Metadata: defsecTypes.NewTestMetadata(), 32 ARN: defsecTypes.String("", defsecTypes.NewTestMetadata()), 33 Encryption: sns.Encryption{ 34 Metadata: defsecTypes.NewTestMetadata(), 35 KMSKeyID: defsecTypes.String("/blah", defsecTypes.NewTestMetadata()), 36 }, 37 }, 38 }, 39 { 40 name: "default", 41 terraform: ` 42 resource "aws_sns_topic" "good_example" { 43 } 44 `, 45 expected: sns.Topic{ 46 Metadata: defsecTypes.NewTestMetadata(), 47 ARN: defsecTypes.String("", defsecTypes.NewTestMetadata()), 48 Encryption: sns.Encryption{ 49 Metadata: defsecTypes.NewTestMetadata(), 50 KMSKeyID: defsecTypes.String("", defsecTypes.NewTestMetadata()), 51 }, 52 }, 53 }, 54 } 55 56 for _, test := range tests { 57 t.Run(test.name, func(t *testing.T) { 58 modules := tftestutil.CreateModulesFromSource(t, test.terraform, ".tf") 59 adapted := adaptTopic(modules.GetBlocks()[0]) 60 testutil.AssertDefsecEqual(t, test.expected, adapted) 61 }) 62 } 63 } 64 65 func TestLines(t *testing.T) { 66 src := ` 67 resource "aws_sns_topic" "good_example" { 68 kms_master_key_id = "/blah" 69 }` 70 71 modules := tftestutil.CreateModulesFromSource(t, src, ".tf") 72 adapted := Adapt(modules) 73 74 require.Len(t, adapted.Topics, 1) 75 topic := adapted.Topics[0] 76 77 assert.Equal(t, 2, topic.Metadata.Range().GetStartLine()) 78 assert.Equal(t, 4, topic.Metadata.Range().GetEndLine()) 79 80 assert.Equal(t, 3, topic.Encryption.KMSKeyID.GetMetadata().Range().GetStartLine()) 81 assert.Equal(t, 3, topic.Encryption.KMSKeyID.GetMetadata().Range().GetEndLine()) 82 }