From 6a9a196e311c03585b39aaa0f1900172acc56436 Mon Sep 17 00:00:00 2001 From: Lado Golijashvili Date: Fri, 24 Nov 2023 17:14:12 +0400 Subject: [PATCH] implement tests for organization actions enabled repos --- internal/clients/fake/client.go | 18 ++++++++ .../organization/organization_test.go | 45 ++++++++++++++++--- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/internal/clients/fake/client.go b/internal/clients/fake/client.go index 576210e..929ce4b 100644 --- a/internal/clients/fake/client.go +++ b/internal/clients/fake/client.go @@ -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) diff --git a/internal/controller/organization/organization_test.go b/internal/controller/organization/organization_test.go index 39d202f..4d2e349 100644 --- a/internal/controller/organization/organization_test.go +++ b/internal/controller/organization/organization_test.go @@ -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" @@ -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) @@ -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 { @@ -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 @@ -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{ @@ -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{ @@ -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{