2021-02-15 13:24:20 +00:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/mocks"
|
2021-06-04 14:10:00 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
2021-02-15 13:24:20 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/shurcooL/githubv4"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestListRepositoriesForUser_WithError(t *testing.T) {
|
|
|
|
mockedClient := mocks.GithubGraphQLClient{}
|
|
|
|
expectedError := errors.New("test error from graphql")
|
|
|
|
mockedClient.On("Query", mock.Anything, mock.Anything, mock.Anything).Return(expectedError)
|
|
|
|
|
|
|
|
r := githubRepository{
|
|
|
|
client: &mockedClient,
|
|
|
|
config: githubConfig{},
|
2021-06-04 14:10:00 +00:00
|
|
|
cache: cache.New(1),
|
2021-02-15 13:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := r.ListRepositories()
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Equal(t, expectedError, err)
|
2021-02-15 13:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListRepositoriesForUser(t *testing.T) {
|
|
|
|
mockedClient := mocks.GithubGraphQLClient{}
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listRepoForOwnerQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2021-02-16 10:46:03 +00:00
|
|
|
q.Viewer.Repositories.Nodes = []struct{ Name string }{
|
2021-02-15 13:24:20 +00:00
|
|
|
{
|
|
|
|
Name: "repo1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "repo2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Viewer.Repositories.PageInfo = pageInfo{
|
|
|
|
EndCursor: "next",
|
|
|
|
HasNextPage: true,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"cursor": (*githubv4.String)(nil),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-02-15 13:24:20 +00:00
|
|
|
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listRepoForOwnerQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2021-02-16 10:46:03 +00:00
|
|
|
q.Viewer.Repositories.Nodes = []struct{ Name string }{
|
2021-02-15 13:24:20 +00:00
|
|
|
{
|
|
|
|
Name: "repo3",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "repo4",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Viewer.Repositories.PageInfo = pageInfo{
|
|
|
|
HasNextPage: false,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"cursor": githubv4.NewString("next"),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-02-15 13:24:20 +00:00
|
|
|
|
2021-06-04 14:10:00 +00:00
|
|
|
store := cache.New(1)
|
2021-02-15 13:24:20 +00:00
|
|
|
r := githubRepository{
|
|
|
|
client: &mockedClient,
|
|
|
|
ctx: context.TODO(),
|
|
|
|
config: githubConfig{},
|
2021-06-04 14:10:00 +00:00
|
|
|
cache: store,
|
2021-02-15 13:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
repos, err := r.ListRepositories()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Equal(t, []string{
|
2021-02-16 10:46:03 +00:00
|
|
|
"repo1",
|
|
|
|
"repo2",
|
|
|
|
"repo3",
|
|
|
|
"repo4",
|
2021-02-15 13:24:20 +00:00
|
|
|
}, repos)
|
2021-06-04 14:10:00 +00:00
|
|
|
|
|
|
|
// Check that results were cached
|
|
|
|
cachedData, err := r.ListRepositories()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, repos, cachedData)
|
|
|
|
assert.IsType(t, []string{}, store.Get("githubListRepositories"))
|
2021-02-15 13:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListRepositoriesForOrganization_WithError(t *testing.T) {
|
|
|
|
mockedClient := mocks.GithubGraphQLClient{}
|
|
|
|
expectedError := errors.New("test error from graphql")
|
|
|
|
mockedClient.On("Query", mock.Anything, mock.Anything, mock.Anything).Return(expectedError)
|
|
|
|
|
|
|
|
r := githubRepository{
|
|
|
|
client: &mockedClient,
|
|
|
|
config: githubConfig{
|
|
|
|
Organization: "testorg",
|
|
|
|
},
|
2021-06-04 14:10:00 +00:00
|
|
|
cache: cache.New(1),
|
2021-02-15 13:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := r.ListRepositories()
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Equal(t, expectedError, err)
|
2021-02-15 13:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListRepositoriesForOrganization(t *testing.T) {
|
|
|
|
mockedClient := mocks.GithubGraphQLClient{}
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listRepoForOrgQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2021-02-16 10:46:03 +00:00
|
|
|
q.Organization.Repositories.Nodes = []struct {
|
|
|
|
Name string
|
|
|
|
}{
|
2021-02-15 13:24:20 +00:00
|
|
|
{
|
|
|
|
Name: "repo1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "repo2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Organization.Repositories.PageInfo = pageInfo{
|
|
|
|
EndCursor: "next",
|
|
|
|
HasNextPage: true,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"org": (githubv4.String)("testorg"),
|
|
|
|
"cursor": (*githubv4.String)(nil),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-02-15 13:24:20 +00:00
|
|
|
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listRepoForOrgQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2021-02-16 10:46:03 +00:00
|
|
|
q.Organization.Repositories.Nodes = []struct {
|
|
|
|
Name string
|
|
|
|
}{
|
2021-02-15 13:24:20 +00:00
|
|
|
{
|
|
|
|
Name: "repo3",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "repo4",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Organization.Repositories.PageInfo = pageInfo{
|
|
|
|
HasNextPage: false,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"org": (githubv4.String)("testorg"),
|
|
|
|
"cursor": githubv4.NewString("next"),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-02-15 13:24:20 +00:00
|
|
|
|
2021-06-04 14:10:00 +00:00
|
|
|
store := cache.New(1)
|
2021-02-15 13:24:20 +00:00
|
|
|
r := githubRepository{
|
|
|
|
client: &mockedClient,
|
|
|
|
ctx: context.TODO(),
|
|
|
|
config: githubConfig{
|
|
|
|
Organization: "testorg",
|
|
|
|
},
|
2021-06-04 14:10:00 +00:00
|
|
|
cache: store,
|
2021-02-15 13:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
repos, err := r.ListRepositories()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Equal(t, []string{
|
2021-02-16 10:46:03 +00:00
|
|
|
"repo1",
|
|
|
|
"repo2",
|
|
|
|
"repo3",
|
|
|
|
"repo4",
|
2021-02-15 13:24:20 +00:00
|
|
|
}, repos)
|
2021-06-04 14:10:00 +00:00
|
|
|
|
|
|
|
// Check that results were cached
|
|
|
|
cachedData, err := r.ListRepositories()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, repos, cachedData)
|
|
|
|
assert.IsType(t, []string{}, store.Get("githubListRepositories"))
|
2021-02-15 13:24:20 +00:00
|
|
|
}
|
2021-02-24 12:55:26 +00:00
|
|
|
|
|
|
|
func TestListTeams_WithError(t *testing.T) {
|
|
|
|
mockedClient := mocks.GithubGraphQLClient{}
|
|
|
|
expectedError := errors.New("test error from graphql")
|
|
|
|
mockedClient.On("Query", mock.Anything, mock.Anything, mock.Anything).Return(expectedError)
|
|
|
|
|
|
|
|
r := githubRepository{
|
|
|
|
client: &mockedClient,
|
|
|
|
config: githubConfig{
|
|
|
|
Organization: "testorg",
|
|
|
|
},
|
2021-06-04 14:10:00 +00:00
|
|
|
cache: cache.New(1),
|
2021-02-24 12:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := r.ListTeams()
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Equal(t, expectedError, err)
|
2021-02-24 12:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListTeams_WithoutOrganization(t *testing.T) {
|
2021-06-04 14:10:00 +00:00
|
|
|
r := githubRepository{cache: cache.New(1)}
|
2021-02-24 12:55:26 +00:00
|
|
|
|
|
|
|
teams, err := r.ListTeams()
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, []Team{}, teams)
|
2021-02-24 12:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListTeams(t *testing.T) {
|
|
|
|
mockedClient := mocks.GithubGraphQLClient{}
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listTeamsQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Organization.Teams.Nodes = []struct {
|
|
|
|
DatabaseId int
|
2021-03-01 12:39:00 +00:00
|
|
|
Slug string
|
2021-02-24 12:55:26 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
DatabaseId: 1,
|
2021-03-01 12:39:00 +00:00
|
|
|
Slug: "1",
|
2021-02-24 12:55:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
DatabaseId: 2,
|
2021-03-01 12:39:00 +00:00
|
|
|
Slug: "2",
|
2021-02-24 12:55:26 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Organization.Teams.PageInfo = pageInfo{
|
|
|
|
EndCursor: "next",
|
|
|
|
HasNextPage: true,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"login": (githubv4.String)("testorg"),
|
|
|
|
"cursor": (*githubv4.String)(nil),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-02-24 12:55:26 +00:00
|
|
|
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listTeamsQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Organization.Teams.Nodes = []struct {
|
|
|
|
DatabaseId int
|
2021-03-01 12:39:00 +00:00
|
|
|
Slug string
|
2021-02-24 12:55:26 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
DatabaseId: 3,
|
2021-03-01 12:39:00 +00:00
|
|
|
Slug: "3",
|
2021-02-24 12:55:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
DatabaseId: 4,
|
2021-03-01 12:39:00 +00:00
|
|
|
Slug: "4",
|
2021-02-24 12:55:26 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Organization.Teams.PageInfo = pageInfo{
|
|
|
|
HasNextPage: false,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"login": (githubv4.String)("testorg"),
|
|
|
|
"cursor": githubv4.NewString("next"),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-02-24 12:55:26 +00:00
|
|
|
|
2021-06-04 14:10:00 +00:00
|
|
|
store := cache.New(1)
|
2021-02-24 12:55:26 +00:00
|
|
|
r := githubRepository{
|
|
|
|
client: &mockedClient,
|
|
|
|
ctx: context.TODO(),
|
|
|
|
config: githubConfig{
|
|
|
|
Organization: "testorg",
|
|
|
|
},
|
2021-06-04 14:10:00 +00:00
|
|
|
cache: store,
|
2021-02-24 12:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
teams, err := r.ListTeams()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Equal(t, []Team{
|
2021-03-01 12:39:00 +00:00
|
|
|
{1, "1"},
|
|
|
|
{2, "2"},
|
|
|
|
{3, "3"},
|
|
|
|
{4, "4"},
|
|
|
|
}, teams)
|
2021-06-04 14:10:00 +00:00
|
|
|
|
|
|
|
// Check that results were cached
|
|
|
|
cachedData, err := r.ListTeams()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, teams, cachedData)
|
|
|
|
assert.IsType(t, []Team{}, store.Get("githubListTeams"))
|
2021-03-01 12:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListTeamMemberships_WithTeamListingError(t *testing.T) {
|
|
|
|
mockedClient := mocks.GithubGraphQLClient{}
|
|
|
|
expectedError := errors.New("test error from graphql")
|
|
|
|
mockedClient.On("Query", mock.Anything, mock.Anything, mock.Anything).Return(expectedError)
|
|
|
|
|
|
|
|
r := githubRepository{
|
|
|
|
client: &mockedClient,
|
|
|
|
config: githubConfig{
|
|
|
|
Organization: "testorg",
|
|
|
|
},
|
2021-06-04 14:10:00 +00:00
|
|
|
cache: cache.New(1),
|
2021-03-01 12:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := r.ListTeamMemberships()
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Equal(t, expectedError, err)
|
2021-03-01 12:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListTeamMemberships_WithError(t *testing.T) {
|
|
|
|
mockedClient := mocks.GithubGraphQLClient{}
|
|
|
|
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listTeamsQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Organization.Teams.Nodes = []struct {
|
|
|
|
DatabaseId int
|
|
|
|
Slug string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
DatabaseId: 1,
|
|
|
|
Slug: "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Organization.Teams.PageInfo = pageInfo{
|
|
|
|
HasNextPage: false,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"login": (githubv4.String)("testorg"),
|
|
|
|
"cursor": (*githubv4.String)(nil),
|
|
|
|
}).Return(nil)
|
|
|
|
|
|
|
|
expectedError := errors.New("test error from graphql")
|
|
|
|
mockedClient.On("Query", mock.Anything, mock.Anything, mock.Anything).Return(expectedError)
|
|
|
|
|
|
|
|
r := githubRepository{
|
|
|
|
client: &mockedClient,
|
|
|
|
config: githubConfig{
|
|
|
|
Organization: "testorg",
|
|
|
|
},
|
2021-06-04 14:10:00 +00:00
|
|
|
cache: cache.New(1),
|
2021-03-01 12:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := r.ListTeamMemberships()
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Equal(t, expectedError, err)
|
2021-03-01 12:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListTeamMemberships_WithoutOrganization(t *testing.T) {
|
2021-06-04 14:10:00 +00:00
|
|
|
r := githubRepository{cache: cache.New(1)}
|
2021-03-01 12:39:00 +00:00
|
|
|
|
|
|
|
teams, err := r.ListTeamMemberships()
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, []string{}, teams)
|
2021-03-01 12:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListTeamMemberships(t *testing.T) {
|
|
|
|
mockedClient := mocks.GithubGraphQLClient{}
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listTeamsQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Organization.Teams.Nodes = []struct {
|
|
|
|
DatabaseId int
|
|
|
|
Slug string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
DatabaseId: 1,
|
|
|
|
Slug: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DatabaseId: 2,
|
|
|
|
Slug: "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Organization.Teams.PageInfo = pageInfo{
|
|
|
|
HasNextPage: false,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"login": (githubv4.String)("testorg"),
|
|
|
|
"cursor": (*githubv4.String)(nil),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-03-01 12:39:00 +00:00
|
|
|
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listTeamMembershipsQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Organization.Team.Members.Nodes = []struct {
|
|
|
|
Login string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Login: "user-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Login: "user-2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Organization.Team.Members.PageInfo = pageInfo{
|
|
|
|
EndCursor: "next",
|
|
|
|
HasNextPage: true,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"login": (githubv4.String)("testorg"),
|
|
|
|
"cursor": (*githubv4.String)(nil),
|
|
|
|
"slug": (githubv4.String)("foo"),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-03-01 12:39:00 +00:00
|
|
|
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listTeamMembershipsQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Organization.Team.Members.Nodes = []struct {
|
|
|
|
Login string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Login: "user-3",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Login: "user-4",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Organization.Team.Members.PageInfo = pageInfo{
|
|
|
|
HasNextPage: false,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"login": (githubv4.String)("testorg"),
|
2021-03-02 10:31:22 +00:00
|
|
|
"cursor": (githubv4.String)("next"),
|
2021-03-01 12:39:00 +00:00
|
|
|
"slug": (githubv4.String)("foo"),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-03-01 12:39:00 +00:00
|
|
|
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listTeamMembershipsQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Organization.Team.Members.Nodes = []struct {
|
|
|
|
Login string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Login: "user-5",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Login: "user-6",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Organization.Team.Members.PageInfo = pageInfo{
|
|
|
|
HasNextPage: false,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"login": (githubv4.String)("testorg"),
|
|
|
|
"cursor": (*githubv4.String)(nil),
|
|
|
|
"slug": (githubv4.String)("bar"),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-03-01 12:39:00 +00:00
|
|
|
|
2021-06-04 14:10:00 +00:00
|
|
|
store := cache.New(1)
|
2021-03-01 12:39:00 +00:00
|
|
|
r := githubRepository{
|
|
|
|
client: &mockedClient,
|
|
|
|
ctx: context.TODO(),
|
|
|
|
config: githubConfig{
|
|
|
|
Organization: "testorg",
|
|
|
|
},
|
2021-06-04 14:10:00 +00:00
|
|
|
cache: store,
|
2021-03-01 12:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memberships, err := r.ListTeamMemberships()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Equal(t, []string{
|
2021-03-01 12:39:00 +00:00
|
|
|
"1:user-1",
|
|
|
|
"1:user-2",
|
|
|
|
"1:user-3",
|
|
|
|
"1:user-4",
|
|
|
|
"2:user-5",
|
|
|
|
"2:user-6",
|
|
|
|
}, memberships)
|
2021-06-04 14:10:00 +00:00
|
|
|
|
|
|
|
// Check that results were cached
|
|
|
|
cachedData, err := r.ListTeamMemberships()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, memberships, cachedData)
|
|
|
|
assert.IsType(t, []string{}, store.Get("githubListTeamMemberships"))
|
2021-02-24 12:55:26 +00:00
|
|
|
}
|
2021-02-26 15:12:27 +00:00
|
|
|
|
|
|
|
func TestListMembership_WithError(t *testing.T) {
|
|
|
|
mockedClient := mocks.GithubGraphQLClient{}
|
|
|
|
expectedError := errors.New("test error from graphql")
|
|
|
|
mockedClient.On("Query", mock.Anything, mock.Anything, mock.Anything).Return(expectedError)
|
|
|
|
|
|
|
|
r := githubRepository{
|
|
|
|
client: &mockedClient,
|
|
|
|
config: githubConfig{
|
|
|
|
Organization: "testorg",
|
|
|
|
},
|
2021-06-04 14:10:00 +00:00
|
|
|
cache: cache.New(1),
|
2021-02-26 15:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := r.ListMembership()
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Equal(t, expectedError, err)
|
2021-02-26 15:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListMembership_WithoutOrganization(t *testing.T) {
|
2021-06-04 14:10:00 +00:00
|
|
|
r := githubRepository{cache: cache.New(1)}
|
2021-02-26 15:12:27 +00:00
|
|
|
|
|
|
|
teams, err := r.ListMembership()
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, []string{}, teams)
|
2021-02-26 15:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListMembership(t *testing.T) {
|
|
|
|
mockedClient := mocks.GithubGraphQLClient{}
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listMembership)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Organization.MembersWithRole.Nodes = []struct {
|
|
|
|
Login string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Login: "user-admin",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Login: "user-non-admin-1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Organization.MembersWithRole.PageInfo = pageInfo{
|
|
|
|
EndCursor: "next",
|
|
|
|
HasNextPage: true,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"login": (githubv4.String)("testorg"),
|
|
|
|
"cursor": (*githubv4.String)(nil),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-02-26 15:12:27 +00:00
|
|
|
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listMembership)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Organization.MembersWithRole.Nodes = []struct {
|
|
|
|
Login string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Login: "user-non-admin-2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Login: "user-non-admin-3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Organization.MembersWithRole.PageInfo = pageInfo{
|
|
|
|
HasNextPage: false,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"login": (githubv4.String)("testorg"),
|
|
|
|
"cursor": githubv4.NewString("next"),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-02-26 15:12:27 +00:00
|
|
|
|
2021-06-04 14:10:00 +00:00
|
|
|
store := cache.New(1)
|
2021-02-26 15:12:27 +00:00
|
|
|
r := githubRepository{
|
|
|
|
client: &mockedClient,
|
|
|
|
ctx: context.TODO(),
|
|
|
|
config: githubConfig{
|
|
|
|
Organization: "testorg",
|
|
|
|
},
|
2021-06-04 14:10:00 +00:00
|
|
|
cache: store,
|
2021-02-26 15:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
teams, err := r.ListMembership()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Equal(t, []string{
|
2021-02-26 15:12:27 +00:00
|
|
|
"testorg:user-admin",
|
|
|
|
"testorg:user-non-admin-1",
|
|
|
|
"testorg:user-non-admin-2",
|
|
|
|
"testorg:user-non-admin-3",
|
|
|
|
}, teams)
|
2021-06-04 14:10:00 +00:00
|
|
|
|
|
|
|
// Check that results were cached
|
|
|
|
cachedData, err := r.ListMembership()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, teams, cachedData)
|
|
|
|
assert.IsType(t, []string{}, store.Get("githubListMembership"))
|
|
|
|
|
2021-02-26 15:12:27 +00:00
|
|
|
}
|
2021-02-26 11:12:36 +00:00
|
|
|
|
|
|
|
func TestListBranchProtection_WithRepoListingError(t *testing.T) {
|
|
|
|
mockedClient := mocks.GithubGraphQLClient{}
|
|
|
|
expectedError := errors.New("test error from graphql")
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listRepoForOrgQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Organization.Repositories.Nodes = []struct {
|
|
|
|
Name string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "repo1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "repo2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Organization.Repositories.PageInfo = pageInfo{
|
|
|
|
HasNextPage: false,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"org": (githubv4.String)("my-organization"),
|
|
|
|
"cursor": (*githubv4.String)(nil),
|
|
|
|
}).Return(expectedError)
|
|
|
|
|
|
|
|
r := githubRepository{
|
|
|
|
client: &mockedClient,
|
|
|
|
config: githubConfig{
|
|
|
|
Organization: "my-organization",
|
|
|
|
},
|
2021-06-04 14:10:00 +00:00
|
|
|
cache: cache.New(1),
|
2021-02-26 11:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := r.ListBranchProtection()
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Equal(t, expectedError, err)
|
2021-02-26 11:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListBranchProtection_WithError(t *testing.T) {
|
|
|
|
mockedClient := mocks.GithubGraphQLClient{}
|
|
|
|
expectedError := errors.New("test error from graphql")
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listRepoForOrgQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Organization.Repositories.Nodes = []struct {
|
|
|
|
Name string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "repo1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "repo2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Organization.Repositories.PageInfo = pageInfo{
|
|
|
|
HasNextPage: false,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"org": (githubv4.String)("testorg"),
|
|
|
|
"cursor": (*githubv4.String)(nil),
|
|
|
|
}).Return(nil)
|
|
|
|
|
|
|
|
mockedClient.On("Query", mock.Anything, mock.Anything, mock.Anything).Return(expectedError)
|
|
|
|
|
|
|
|
r := githubRepository{
|
|
|
|
client: &mockedClient,
|
|
|
|
config: githubConfig{
|
|
|
|
Organization: "testorg",
|
|
|
|
},
|
2021-06-04 14:10:00 +00:00
|
|
|
cache: cache.New(1),
|
2021-02-26 11:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := r.ListBranchProtection()
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Equal(t, expectedError, err)
|
2021-02-26 11:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListBranchProtection(t *testing.T) {
|
|
|
|
mockedClient := mocks.GithubGraphQLClient{}
|
|
|
|
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listRepoForOrgQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Organization.Repositories.Nodes = []struct {
|
|
|
|
Name string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "repo1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "repo2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Organization.Repositories.PageInfo = pageInfo{
|
|
|
|
HasNextPage: false,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"org": (githubv4.String)("my-organization"),
|
|
|
|
"cursor": (*githubv4.String)(nil),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-02-26 11:12:36 +00:00
|
|
|
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listBranchProtectionQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Repository.BranchProtectionRules.Nodes = []struct {
|
|
|
|
Id string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Id: "id1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "id2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Repository.BranchProtectionRules.PageInfo = pageInfo{
|
|
|
|
EndCursor: "nextPage",
|
|
|
|
HasNextPage: true,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"owner": (githubv4.String)("my-organization"),
|
|
|
|
"name": (githubv4.String)("repo1"),
|
|
|
|
"cursor": (*githubv4.String)(nil),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-02-26 11:12:36 +00:00
|
|
|
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listBranchProtectionQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Repository.BranchProtectionRules.Nodes = []struct {
|
|
|
|
Id string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Id: "id3",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "id4",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Repository.BranchProtectionRules.PageInfo = pageInfo{
|
|
|
|
EndCursor: "nextPage",
|
|
|
|
HasNextPage: false,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"owner": (githubv4.String)("my-organization"),
|
|
|
|
"name": (githubv4.String)("repo1"),
|
|
|
|
"cursor": (githubv4.String)("nextPage"),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-02-26 11:12:36 +00:00
|
|
|
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listBranchProtectionQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Repository.BranchProtectionRules.Nodes = []struct {
|
|
|
|
Id string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Id: "id5",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "id6",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Repository.BranchProtectionRules.PageInfo = pageInfo{
|
|
|
|
EndCursor: "nextPage",
|
|
|
|
HasNextPage: true,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"owner": (githubv4.String)("my-organization"),
|
|
|
|
"name": (githubv4.String)("repo2"),
|
|
|
|
"cursor": (*githubv4.String)(nil),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-02-26 11:12:36 +00:00
|
|
|
|
|
|
|
mockedClient.On("Query",
|
|
|
|
mock.Anything,
|
|
|
|
mock.MatchedBy(func(query interface{}) bool {
|
|
|
|
q, ok := query.(*listBranchProtectionQuery)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
q.Repository.BranchProtectionRules.Nodes = []struct {
|
|
|
|
Id string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Id: "id7",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "id8",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
q.Repository.BranchProtectionRules.PageInfo = pageInfo{
|
|
|
|
EndCursor: "nextPage",
|
|
|
|
HasNextPage: false,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
map[string]interface{}{
|
|
|
|
"owner": (githubv4.String)("my-organization"),
|
|
|
|
"name": (githubv4.String)("repo2"),
|
|
|
|
"cursor": (githubv4.String)("nextPage"),
|
2021-06-04 14:10:00 +00:00
|
|
|
}).Return(nil).Once()
|
2021-02-26 11:12:36 +00:00
|
|
|
|
2021-06-04 14:10:00 +00:00
|
|
|
store := cache.New(1)
|
2021-02-26 11:12:36 +00:00
|
|
|
r := githubRepository{
|
|
|
|
client: &mockedClient,
|
|
|
|
ctx: context.TODO(),
|
|
|
|
config: githubConfig{
|
|
|
|
Organization: "my-organization",
|
|
|
|
},
|
2021-06-04 14:10:00 +00:00
|
|
|
cache: store,
|
2021-02-26 11:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
teams, err := r.ListBranchProtection()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-06-04 14:10:00 +00:00
|
|
|
assert.Equal(t, []string{
|
2021-02-26 11:12:36 +00:00
|
|
|
"id1",
|
|
|
|
"id2",
|
|
|
|
"id3",
|
|
|
|
"id4",
|
|
|
|
"id5",
|
|
|
|
"id6",
|
|
|
|
"id7",
|
|
|
|
"id8",
|
|
|
|
}, teams)
|
2021-06-04 14:10:00 +00:00
|
|
|
|
|
|
|
// Check that results were cached
|
|
|
|
cachedData, err := r.ListBranchProtection()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, teams, cachedData)
|
|
|
|
assert.IsType(t, []string{}, store.Get("githubListBranchProtection"))
|
2021-02-26 11:12:36 +00:00
|
|
|
}
|