2021-06-30 18:42:02 +00:00
package remote
import (
2021-08-20 13:15:51 +00:00
"errors"
2021-06-30 18:42:02 +00:00
"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"
github2 "github.com/snyk/driftctl/enumeration/remote/github"
terraform2 "github.com/snyk/driftctl/enumeration/terraform"
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
testresource "github.com/snyk/driftctl/test/resource"
tftest "github.com/snyk/driftctl/test/terraform"
2021-07-26 09:55:59 +00:00
"github.com/stretchr/testify/mock"
2021-06-30 18:42:02 +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-06-30 18:42:02 +00:00
"github.com/stretchr/testify/assert"
)
func TestScanGithubTeam ( t * testing . T ) {
tests := [ ] struct {
test string
dirName string
2022-06-28 07:23:29 +00:00
mocks func ( * github2 . MockGithubRepository , * mocks . AlerterInterface )
2021-06-30 18:42:02 +00:00
err error
} {
{
test : "no github teams" ,
dirName : "github_teams_empty" ,
2022-06-28 07:23:29 +00:00
mocks : func ( client * github2 . MockGithubRepository , alerter * mocks . AlerterInterface ) {
client . On ( "ListTeams" ) . Return ( [ ] github2 . Team { } , nil )
2021-06-30 18:42:02 +00:00
} ,
err : nil ,
} ,
{
test : "Multiple github teams with parent" ,
dirName : "github_teams_multiple" ,
2022-06-28 07:23:29 +00:00
mocks : func ( client * github2 . MockGithubRepository , alerter * mocks . AlerterInterface ) {
client . On ( "ListTeams" ) . Return ( [ ] github2 . Team {
2021-06-30 18:42:02 +00:00
{ DatabaseId : 4556811 } , // github_team.team1
{ DatabaseId : 4556812 } , // github_team.team2
{ DatabaseId : 4556814 } , // github_team.with_parent
} , nil )
} ,
err : nil ,
} ,
2021-08-20 13:15:51 +00:00
{
test : "cannot list teams" ,
dirName : "github_teams_empty" ,
2022-06-28 07:23:29 +00:00
mocks : func ( client * github2 . MockGithubRepository , alerter * mocks . AlerterInterface ) {
2021-08-20 13:15:51 +00:00
client . On ( "ListTeams" ) . 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 . GithubTeamResourceType , alerts . NewRemoteAccessDeniedAlert ( common . RemoteGithubTerraform , remoteerr . NewResourceListingErrorWithType ( errors . New ( "Your token has not been granted the required scopes to execute this query." ) , githubres . GithubTeamResourceType , githubres . GithubTeamResourceType ) , alerts . EnumerationPhase ) ) . Return ( )
2021-08-20 13:15:51 +00:00
} ,
err : nil ,
} ,
2021-06-30 18:42:02 +00:00
}
schemaRepository := testresource . InitFakeSchemaRepository ( "github" , "4.4.0" )
githubres . InitResourcesMetadata ( schemaRepository )
2022-06-28 07:23:29 +00:00
factory := terraform2 . NewTerraformResourceFactory ( schemaRepository )
2021-06-30 18:42:02 +00:00
deserializer := resource . NewDeserializer ( factory )
for _ , c := range tests {
t . Run ( c . test , func ( tt * testing . T ) {
shouldUpdate := c . dirName == * goldenfile . Update
scanOptions := ScannerOptions { Deep : true }
2022-06-28 07:23:29 +00:00
providerLibrary := terraform2 . NewProviderLibrary ( )
2022-07-06 08:44:42 +00:00
remoteLibrary := common . NewRemoteLibrary ( )
2021-06-30 18:42:02 +00:00
2021-08-20 13:15:51 +00:00
// Initialize mocks
alerter := & mocks . AlerterInterface { }
2022-06-28 07:23:29 +00:00
mockedRepo := github2 . MockGithubRepository { }
2021-08-20 13:15:51 +00:00
c . mocks ( & mockedRepo , alerter )
2022-06-28 07:23:29 +00:00
var repo github2 . GithubRepository = & mockedRepo
2021-06-30 18:42:02 +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-06-28 07:23:29 +00:00
repo = github2 . NewGithubRepository ( realProvider . GetConfig ( ) , cache . New ( 0 ) )
2021-06-30 18:42:02 +00:00
}
2022-06-28 07:23:29 +00:00
remoteLibrary . AddEnumerator ( github2 . NewGithubTeamEnumerator ( repo , factory ) )
2022-07-06 08:44:42 +00:00
remoteLibrary . AddDetailsFetcher ( githubres . GithubTeamResourceType , common . NewGenericDetailsFetcher ( githubres . GithubTeamResourceType , provider , deserializer ) )
2021-06-30 18:42:02 +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-06-30 18:42:02 +00:00
got , err := s . Resources ( )
assert . Equal ( tt , err , c . err )
if err != nil {
return
}
test . TestAgainstGoldenFile ( got , githubres . GithubTeamResourceType , c . dirName , provider , deserializer , shouldUpdate , tt )
2021-08-20 13:15:51 +00:00
mockedRepo . AssertExpectations ( tt )
alerter . AssertExpectations ( tt )
2021-06-30 18:42:02 +00:00
} )
}
}