github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/internal/adapters/terraform/aws/apigateway/apiv1_test.go (about) 1 package apigateway 2 3 import ( 4 "testing" 5 6 v1 "github.com/khulnasoft-lab/defsec/pkg/providers/aws/apigateway/v1" 7 8 "github.com/khulnasoft-lab/defsec/internal/adapters/terraform/tftestutil" 9 10 "github.com/khulnasoft-lab/defsec/test/testutil" 11 ) 12 13 func Test_adaptAPIMethodsV1(t *testing.T) { 14 tests := []struct { 15 name string 16 terraform string 17 expected []v1.Method 18 }{ 19 { 20 name: "defaults", 21 terraform: ` 22 resource "aws_api_gateway_rest_api" "MyDemoAPI" { 23 name = "MyDemoAPI" 24 description = "This is my API for demonstration purposes" 25 } 26 27 resource "aws_api_gateway_resource" "example" { 28 rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id 29 } 30 31 resource "aws_api_gateway_method" "example" { 32 rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id 33 resource_id = aws_api_gateway_resource.example.id 34 http_method = "GET" 35 authorization = "NONE" 36 } 37 `, 38 expected: []v1.Method{ 39 { 40 HTTPMethod: String("GET"), 41 AuthorizationType: String("NONE"), 42 APIKeyRequired: Bool(false), 43 }, 44 }, 45 }, 46 { 47 name: "basic", 48 terraform: ` 49 resource "aws_api_gateway_rest_api" "MyDemoAPI" { 50 name = "MyDemoAPI" 51 description = "This is my API for demonstration purposes" 52 } 53 54 resource "aws_api_gateway_resource" "example" { 55 rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id 56 } 57 58 resource "aws_api_gateway_method" "example" { 59 rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id 60 resource_id = aws_api_gateway_resource.example.id 61 http_method = "GET" 62 authorization = "NONE" 63 api_key_required = true 64 } 65 `, 66 expected: []v1.Method{ 67 { 68 HTTPMethod: String("GET"), 69 AuthorizationType: String("NONE"), 70 APIKeyRequired: Bool(true), 71 }, 72 }, 73 }, 74 } 75 76 for _, test := range tests { 77 t.Run(test.name, func(t *testing.T) { 78 modules := tftestutil.CreateModulesFromSource(t, test.terraform, ".tf") 79 restApiBlock := modules.GetBlocks()[1] 80 adapted := adaptAPIMethodsV1(modules, restApiBlock) 81 testutil.AssertDefsecEqual(t, test.expected, adapted) 82 }) 83 } 84 } 85 86 func Test_adaptAPIsV1(t *testing.T) { 87 tests := []struct { 88 name string 89 terraform string 90 expected []v1.API 91 }{ 92 { 93 name: "defaults", 94 terraform: ` 95 resource "aws_api_gateway_rest_api" "example" { 96 97 } 98 `, 99 expected: []v1.API{ 100 { 101 Name: String(""), 102 }, 103 }, 104 }, 105 { 106 name: "full", 107 terraform: ` 108 resource "aws_api_gateway_rest_api" "example" { 109 name = "terrasec" 110 } 111 `, 112 expected: []v1.API{ 113 { 114 Name: String("terrasec"), 115 }, 116 }, 117 }, 118 } 119 120 for _, test := range tests { 121 t.Run(test.name, func(t *testing.T) { 122 modules := tftestutil.CreateModulesFromSource(t, test.terraform, ".tf") 123 adapted := adaptAPIsV1(modules) 124 testutil.AssertDefsecEqual(t, test.expected, adapted) 125 }) 126 } 127 }