github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/internal/adapters/terraform/aws/apigateway/apiv2_test.go (about)

     1  package apigateway
     2  
     3  import (
     4  	"testing"
     5  
     6  	v2 "github.com/aquasecurity/defsec/pkg/providers/aws/apigateway/v2"
     7  	"github.com/aquasecurity/trivy-iac/internal/adapters/terraform/tftestutil"
     8  	"github.com/aquasecurity/trivy-iac/test/testutil"
     9  )
    10  
    11  func Test_adaptAPIsV2(t *testing.T) {
    12  	tests := []struct {
    13  		name      string
    14  		terraform string
    15  		expected  []v2.API
    16  	}{
    17  		{
    18  			name: "defaults",
    19  			terraform: `
    20  resource "aws_apigatewayv2_api" "example" {
    21      protocol_type = "HTTP"
    22  }
    23  `,
    24  			expected: []v2.API{
    25  				{
    26  					Name:         String(""),
    27  					ProtocolType: String("HTTP"),
    28  				},
    29  			},
    30  		},
    31  		{
    32  			name: "full",
    33  			terraform: `
    34  resource "aws_apigatewayv2_api" "example" {
    35      name = "tfsec"
    36      protocol_type = "HTTP"
    37  }
    38  `,
    39  			expected: []v2.API{
    40  				{
    41  					Name:         String("tfsec"),
    42  					ProtocolType: String("HTTP"),
    43  				},
    44  			},
    45  		},
    46  	}
    47  
    48  	for _, test := range tests {
    49  		t.Run(test.name, func(t *testing.T) {
    50  			modules := tftestutil.CreateModulesFromSource(t, test.terraform, ".tf")
    51  			adapted := adaptAPIsV2(modules)
    52  			testutil.AssertDefsecEqual(t, test.expected, adapted)
    53  		})
    54  	}
    55  }
    56  
    57  func Test_adaptStageV2(t *testing.T) {
    58  	tests := []struct {
    59  		name      string
    60  		terraform string
    61  		expected  v2.Stage
    62  	}{
    63  		{
    64  			name: "defaults",
    65  			terraform: `
    66  resource "aws_apigatewayv2_stage" "example" {
    67      
    68  }
    69  `,
    70  			expected: v2.Stage{
    71  				Name: String(""),
    72  				AccessLogging: v2.AccessLogging{
    73  					CloudwatchLogGroupARN: String(""),
    74  				},
    75  			},
    76  		},
    77  		{
    78  			name: "basics",
    79  			terraform: `
    80  resource "aws_apigatewayv2_stage" "example" {
    81      name = "tfsec" 
    82      access_log_settings {
    83          destination_arn = "arn:123"
    84      }
    85  }
    86  `,
    87  			expected: v2.Stage{
    88  				Name: String("tfsec"),
    89  				AccessLogging: v2.AccessLogging{
    90  					CloudwatchLogGroupARN: String("arn:123"),
    91  				},
    92  			},
    93  		},
    94  	}
    95  
    96  	for _, test := range tests {
    97  		t.Run(test.name, func(t *testing.T) {
    98  			modules := tftestutil.CreateModulesFromSource(t, test.terraform, ".tf")
    99  			adapted := adaptStageV2(modules.GetBlocks()[0])
   100  			testutil.AssertDefsecEqual(t, test.expected, adapted)
   101  		})
   102  	}
   103  }