github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/internal/adapters/terraform/github/branch_protections/adapt_test.go (about)

     1  package branch_protections
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/khulnasoft-lab/defsec/internal/adapters/terraform/tftestutil"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func Test_AdaptDefaults(t *testing.T) {
    13  
    14  	src := `
    15  resource "github_branch_protection" "my-repo" {
    16  	
    17  }
    18  `
    19  	modules := tftestutil.CreateModulesFromSource(t, src, ".tf")
    20  	branchProtections := Adapt(modules)
    21  	require.Len(t, branchProtections, 1)
    22  	branchProtection := branchProtections[0]
    23  
    24  	assert.True(t, branchProtection.RequireSignedCommits.IsFalse())
    25  }
    26  
    27  func Test_Adapt_RequireSignedCommitsEnabled(t *testing.T) {
    28  
    29  	src := `
    30  resource "github_branch_protection" "my-repo" {
    31  	require_signed_commits = true
    32  }
    33  `
    34  	modules := tftestutil.CreateModulesFromSource(t, src, ".tf")
    35  	branchProtections := Adapt(modules)
    36  	require.Len(t, branchProtections, 1)
    37  	branchProtection := branchProtections[0]
    38  
    39  	assert.True(t, branchProtection.RequireSignedCommits.IsTrue())
    40  	assert.Equal(t, 3, branchProtection.RequireSignedCommits.GetMetadata().Range().GetStartLine())
    41  	assert.Equal(t, 3, branchProtection.RequireSignedCommits.GetMetadata().Range().GetEndLine())
    42  }
    43  
    44  func Test_Adapt_RequireSignedCommitsDisabled(t *testing.T) {
    45  
    46  	src := `
    47  resource "github_branch_protection" "my-repo" {
    48  	require_signed_commits = false
    49  }
    50  `
    51  	modules := tftestutil.CreateModulesFromSource(t, src, ".tf")
    52  	branchProtections := Adapt(modules)
    53  	require.Len(t, branchProtections, 1)
    54  	branchProtection := branchProtections[0]
    55  
    56  	assert.False(t, branchProtection.RequireSignedCommits.IsTrue())
    57  	assert.Equal(t, 3, branchProtection.RequireSignedCommits.GetMetadata().Range().GetStartLine())
    58  	assert.Equal(t, 3, branchProtection.RequireSignedCommits.GetMetadata().Range().GetEndLine())
    59  }