Skip to content

Commit

Permalink
implement tests for organization actions enabled repos
Browse files Browse the repository at this point in the history
  • Loading branch information
lado936 committed Nov 24, 2023
1 parent 2f5a8a1 commit 6a9a196
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
18 changes: 18 additions & 0 deletions internal/clients/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ import (
"github.com/google/go-github/v54/github"
)

type MockActionsClient struct {
MockListEnabledReposInOrg func(ctx context.Context, owner string, opts *github.ListOptions) (*github.ActionsEnabledOnOrgRepos, *github.Response, error)
MockAddEnabledReposInOrg func(ctx context.Context, owner string, repositoryID int64) (*github.Response, error)
MockRemoveEnabledRepoInOrg func(ctx context.Context, owner string, repositoryID int64) (*github.Response, error)
}

func (m *MockActionsClient) ListEnabledReposInOrg(ctx context.Context, owner string, opts *github.ListOptions) (*github.ActionsEnabledOnOrgRepos, *github.Response, error) {
return m.MockListEnabledReposInOrg(ctx, owner, opts)
}

func (m *MockActionsClient) AddEnabledReposInOrg(ctx context.Context, owner string, repositoryID int64) (*github.Response, error) {
return m.MockAddEnabledReposInOrg(ctx, owner, repositoryID)
}

func (m *MockActionsClient) RemoveEnabledRepoInOrg(ctx context.Context, owner string, repositoryID int64) (*github.Response, error) {
return m.MockRemoveEnabledRepoInOrg(ctx, owner, repositoryID)
}

type MockOrganizationsClient struct {
MockGet func(ctx context.Context, org string) (*github.Organization, *github.Response, error)
MockEdit func(ctx context.Context, name string, org *github.Organization) (*github.Organization, *github.Response, error)
Expand Down
45 changes: 39 additions & 6 deletions internal/controller/organization/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ import (
"context"
"testing"

"github.com/google/go-cmp/cmp"

"github.com/crossplane/provider-github/apis/organizations/v1alpha1"
ghclient "github.com/crossplane/provider-github/internal/clients"
"github.com/crossplane/provider-github/internal/clients/fake"
"github.com/google/go-cmp/cmp"

"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
Expand All @@ -45,6 +44,8 @@ var (
org = "test-org"
description = "test description"
otherDescription = "other description"
repo = "test-repo"
repo2 = "test-repo2"
)

type organizationModifier func(*v1alpha1.Organization)
Expand All @@ -55,10 +56,19 @@ func withDescription() organizationModifier {
}
}

func organization(m ...organizationModifier) *v1alpha1.Organization {
func organization(repos []string, m ...organizationModifier) *v1alpha1.Organization {
cr := &v1alpha1.Organization{}

cr.Spec.ForProvider.Description = description
cr.Spec.ForProvider.Actions = v1alpha1.ActionsConfiguration{
EnabledRepos: make([]v1alpha1.ActionEnabledRepo, len(repos)),
}
for i, repo := range repos {
cr.Spec.ForProvider.Actions.EnabledRepos[i] = v1alpha1.ActionEnabledRepo{
Repo: repo,
}
}

meta.SetExternalName(cr, org)

for _, f := range m {
Expand All @@ -74,6 +84,14 @@ func githubOrganization() *github.Organization {
}
}

func githubOrgRepoActions() *github.ActionsEnabledOnOrgRepos {
repos := []*github.Repository{
{Name: &repo},
{Name: &repo2},
}
return &github.ActionsEnabledOnOrgRepos{Repositories: repos}
}

func TestObserve(t *testing.T) {
type fields struct {
github *ghclient.Client
Expand Down Expand Up @@ -103,10 +121,15 @@ func TestObserve(t *testing.T) {
return githubOrganization(), nil, nil
},
},
Actions: &fake.MockActionsClient{
MockListEnabledReposInOrg: func(ctx context.Context, owner string, opts *github.ListOptions) (*github.ActionsEnabledOnOrgRepos, *github.Response, error) {
return githubOrgRepoActions(), nil, nil
},
},
},
},
args: args{
mg: organization(withDescription()),
mg: organization([]string{repo, repo2}, withDescription()),
},
want: want{
o: managed.ExternalObservation{
Expand All @@ -124,10 +147,15 @@ func TestObserve(t *testing.T) {
return githubOrganization(), nil, nil
},
},
Actions: &fake.MockActionsClient{
MockListEnabledReposInOrg: func(ctx context.Context, owner string, opts *github.ListOptions) (*github.ActionsEnabledOnOrgRepos, *github.Response, error) {
return githubOrgRepoActions(), nil, nil
},
},
},
},
args: args{
mg: organization(),
mg: organization([]string{repo, repo2}),
},
want: want{
o: managed.ExternalObservation{
Expand All @@ -145,10 +173,15 @@ func TestObserve(t *testing.T) {
return nil, nil, fake.Generate404Response()
},
},
Actions: &fake.MockActionsClient{
MockListEnabledReposInOrg: func(ctx context.Context, owner string, opts *github.ListOptions) (*github.ActionsEnabledOnOrgRepos, *github.Response, error) {
return nil, nil, fake.Generate404Response()
},
},
},
},
args: args{
mg: organization(),
mg: organization([]string{repo, repo2}),
},
want: want{
o: managed.ExternalObservation{
Expand Down

0 comments on commit 6a9a196

Please sign in to comment.