2021-07-01 15:45:19 +00:00
package remote
import (
"testing"
2022-06-28 07:23:29 +00:00
"github.com/snyk/driftctl/enumeration"
"github.com/snyk/driftctl/enumeration/remote/alerts"
"github.com/snyk/driftctl/enumeration/remote/cache"
2022-07-06 08:44:42 +00:00
"github.com/snyk/driftctl/enumeration/remote/common"
2022-06-28 07:23:29 +00:00
remoteerr "github.com/snyk/driftctl/enumeration/remote/error"
2022-07-06 14:45:43 +00:00
"github.com/snyk/driftctl/enumeration/remote/github"
2022-07-06 08:58:36 +00:00
"github.com/snyk/driftctl/enumeration/terraform"
2022-06-28 07:23:29 +00:00
2021-08-20 13:15:51 +00:00
"github.com/pkg/errors"
2022-06-28 07:23:29 +00:00
githubres "github.com/snyk/driftctl/enumeration/resource/github"
2021-12-06 13:29:39 +00:00
"github.com/snyk/driftctl/mocks"
2022-06-28 07:23:29 +00:00
2021-12-06 13:29:39 +00:00
tftest "github.com/snyk/driftctl/test/terraform"
2021-07-26 09:55:59 +00:00
"github.com/stretchr/testify/mock"
2021-07-01 15:45:19 +00:00
2022-06-28 07:23:29 +00:00
"github.com/snyk/driftctl/enumeration/resource"
2021-12-06 13:29:39 +00:00
"github.com/snyk/driftctl/test"
"github.com/snyk/driftctl/test/goldenfile"
2021-07-01 15:45:19 +00:00
"github.com/stretchr/testify/assert"
)
func TestScanGithubBranchProtection ( t * testing . T ) {
cases := [ ] struct {
test string
dirName string
2022-07-06 14:45:43 +00:00
mocks func ( * github . MockGithubRepository , * mocks . AlerterInterface )
2021-07-01 15:45:19 +00:00
err error
} {
{
test : "no branch protection" ,
dirName : "github_branch_protection_empty" ,
2022-07-06 14:45:43 +00:00
mocks : func ( client * github . MockGithubRepository , alerter * mocks . AlerterInterface ) {
2021-07-01 15:45:19 +00:00
client . On ( "ListBranchProtection" ) . Return ( [ ] string { } , nil )
} ,
err : nil ,
} ,
{
test : "Multiple branch protections" ,
dirName : "github_branch_protection_multiples" ,
2022-07-06 14:45:43 +00:00
mocks : func ( client * github . MockGithubRepository , alerter * mocks . AlerterInterface ) {
2021-07-01 15:45:19 +00:00
client . On ( "ListBranchProtection" ) . Return ( [ ] string {
2022-06-28 07:23:29 +00:00
"MDIwOkJyYW5jaFByb3RlY3Rpb25SdWxlMTk1NDg0NzI=" , // "repo0:main"
"MDIwOkJyYW5jaFByb3RlY3Rpb25SdWxlMTk1NDg0Nzg=" , // "repo0:toto"
"MDIwOkJyYW5jaFByb3RlY3Rpb25SdWxlMTk1NDg0NzQ=" , // "repo1:main"
"MDIwOkJyYW5jaFByb3RlY3Rpb25SdWxlMTk1NDg0ODA=" , // "repo1:toto"
"MDIwOkJyYW5jaFByb3RlY3Rpb25SdWxlMTk1NDg0NzE=" , // "repo2:main"
"MDIwOkJyYW5jaFByb3RlY3Rpb25SdWxlMTk1NDg0Nzc=" , // "repo2:toto"
2021-07-01 15:45:19 +00:00
} , nil )
} ,
err : nil ,
} ,
2021-08-20 13:15:51 +00:00
{
test : "cannot list branch protections" ,
dirName : "github_branch_protection_empty" ,
2022-07-06 14:45:43 +00:00
mocks : func ( client * github . MockGithubRepository , alerter * mocks . AlerterInterface ) {
2021-08-20 13:15:51 +00:00
client . On ( "ListBranchProtection" ) . Return ( nil , errors . New ( "Your token has not been granted the required scopes to execute this query." ) )
2022-07-06 08:44:42 +00:00
alerter . On ( "SendAlert" , githubres . GithubBranchProtectionResourceType , alerts . NewRemoteAccessDeniedAlert ( common . RemoteGithubTerraform , remoteerr . NewResourceListingErrorWithType ( errors . New ( "Your token has not been granted the required scopes to execute this query." ) , githubres . GithubBranchProtectionResourceType , githubres . GithubBranchProtectionResourceType ) , alerts . EnumerationPhase ) ) . Return ( )
2021-08-20 13:15:51 +00:00
} ,
err : nil ,
} ,
2021-07-01 15:45:19 +00:00
}
2022-07-21 08:37:03 +00:00
factory := terraform . NewTerraformResourceFactory ( )
2021-07-01 15:45:19 +00:00
deserializer := resource . NewDeserializer ( factory )
for _ , c := range cases {
t . Run ( c . test , func ( tt * testing . T ) {
shouldUpdate := c . dirName == * goldenfile . Update
scanOptions := ScannerOptions { Deep : true }
2022-07-06 08:58:36 +00:00
providerLibrary := terraform . NewProviderLibrary ( )
2022-07-06 08:44:42 +00:00
remoteLibrary := common . NewRemoteLibrary ( )
2021-07-01 15:45:19 +00:00
2021-08-20 13:15:51 +00:00
// Initialize mocks
alerter := & mocks . AlerterInterface { }
2022-07-06 14:45:43 +00:00
mockedRepo := github . MockGithubRepository { }
2021-08-20 13:15:51 +00:00
c . mocks ( & mockedRepo , alerter )
2022-07-06 14:45:43 +00:00
var repo github . GithubRepository = & mockedRepo
2021-07-01 15:45:19 +00:00
realProvider , err := tftest . InitTestGithubProvider ( providerLibrary , "4.4.0" )
if err != nil {
t . Fatal ( err )
}
provider := tftest . NewFakeTerraformProvider ( realProvider )
provider . WithResponse ( c . dirName )
if shouldUpdate {
err := realProvider . Init ( )
if err != nil {
t . Fatal ( err )
}
provider . ShouldUpdate ( )
2022-07-06 14:45:43 +00:00
repo = github . NewGithubRepository ( realProvider . GetConfig ( ) , cache . New ( 0 ) )
2021-07-01 15:45:19 +00:00
}
2022-07-06 14:45:43 +00:00
remoteLibrary . AddEnumerator ( github . NewGithubBranchProtectionEnumerator ( repo , factory ) )
2022-07-06 08:44:42 +00:00
remoteLibrary . AddDetailsFetcher ( githubres . GithubBranchProtectionResourceType , common . NewGenericDetailsFetcher ( githubres . GithubBranchProtectionResourceType , provider , deserializer ) )
2021-07-01 15:45:19 +00:00
2022-06-28 07:23:29 +00:00
testFilter := & enumeration . MockFilter { }
2021-07-26 09:55:59 +00:00
testFilter . On ( "IsTypeIgnored" , mock . Anything ) . Return ( false )
s := NewScanner ( remoteLibrary , alerter , scanOptions , testFilter )
2021-07-01 15:45:19 +00:00
got , err := s . Resources ( )
assert . Equal ( tt , err , c . err )
if err != nil {
return
}
test . TestAgainstGoldenFile ( got , githubres . GithubBranchProtectionResourceType , c . dirName , provider , deserializer , shouldUpdate , tt )
2021-08-20 13:15:51 +00:00
mockedRepo . AssertExpectations ( tt )
alerter . AssertExpectations ( tt )
2021-07-01 15:45:19 +00:00
} )
}
}