Skip to content

Commit

Permalink
chore: more tests (installationCanReadRepo)
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Oct 30, 2024
1 parent ebf1786 commit 6be732d
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 23 deletions.
1 change: 1 addition & 0 deletions scm/github/app_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ func WithSigner(signer Signer) AppsTransportOption {
// NewTestAppsTransport creates a new AppsTransport for testing purposes.
func NewTestAppsTransport(baseURL string) *AppsTransport {
pk, _ := rsa.GenerateKey(rand.Reader, 2048)

return &AppsTransport{
BaseURL: baseURL,
Client: &http.Client{Transport: http.DefaultTransport},
Expand Down
File renamed without changes.
125 changes: 125 additions & 0 deletions scm/github/github_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// SPDX-License-Identifier: Apache-2.0

package github

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"

Check failure on line 11 in scm/github/github_client_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] scm/github/github_client_test.go#L11

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(github.com/go-vela) --custom-order (gci)
Raw output
scm/github/github_client_test.go:11: File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(github.com/go-vela) --custom-order (gci)
	"github.com/gin-gonic/gin"
api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/constants"
"github.com/google/go-github/v65/github"

Check failure on line 14 in scm/github/github_client_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] scm/github/github_client_test.go#L14

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(github.com/go-vela) --custom-order (gci)
Raw output
scm/github/github_client_test.go:14: File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(github.com/go-vela) --custom-order (gci)
	"github.com/google/go-github/v65/github"
)

func TestClient_installationCanReadRepo(t *testing.T) {
// setup types
accessibleRepo := new(api.Repo)
accessibleRepo.SetOrg("octocat")
accessibleRepo.SetName("Hello-World")
accessibleRepo.SetFullName("octocat/Hello-World")
accessibleRepo.SetInstallID(0)

inaccessibleRepo := new(api.Repo)
inaccessibleRepo.SetOrg("octocat")
inaccessibleRepo.SetName("Hello-World")
inaccessibleRepo.SetFullName("octocat/Hello-World2")
inaccessibleRepo.SetInstallID(4)

resp := httptest.NewRecorder()
_, engine := gin.CreateTestContext(resp)

// setup mock server
engine.POST("/api/v3/app/installations/:id/access_tokens", func(c *gin.Context) {
c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/installations_access_tokens.json")
})
engine.GET("/api/v3/installation/repositories", func(c *gin.Context) {
c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/installation_repositories.json")
})

s := httptest.NewServer(engine)
defer s.Close()

oauthClient, _ := NewTest(s.URL)

appsClient, err := NewTest(s.URL)
if err != nil {
t.Errorf("unable to create GitHub App client: %v", err)
}

appsClient.AppsTransport = NewTestAppsTransport("")

// setup tests
tests := []struct {
name string
client *client
repo *api.Repo
installation *github.Installation
appsTransport bool
want bool
wantErr bool
}{
{
name: "installation can read repo",
client: appsClient,
repo: accessibleRepo,
installation: &github.Installation{
ID: github.Int64(1),
Account: &github.User{
Login: github.String("github"),
},
RepositorySelection: github.String(constants.AppInstallRepositoriesSelectionSelected),
},
want: true,
wantErr: false,
},
{
name: "installation cannot read repo",
client: appsClient,
repo: inaccessibleRepo,
installation: &github.Installation{
ID: github.Int64(2),
Account: &github.User{
Login: github.String("github"),
},
RepositorySelection: github.String(constants.AppInstallRepositoriesSelectionSelected),
},
want: false,
wantErr: false,
},
{
name: "no GitHub App client",
client: oauthClient,
repo: accessibleRepo,
installation: &github.Installation{
ID: github.Int64(1),
Account: &github.User{
Login: github.String("github"),
},
RepositorySelection: github.String(constants.AppInstallRepositoriesSelectionSelected),
},
want: false,
wantErr: true,
},
}

// run tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.client.installationCanReadRepo(context.Background(), tt.repo, tt.installation)
if (err != nil) != tt.wantErr {
t.Errorf("installationCanReadRepo() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("installationCanReadRepo() = %v, want %v", got, tt.want)
}
})
}
}
46 changes: 23 additions & 23 deletions scm/github/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1810,13 +1810,13 @@ func TestGithub_GetNetrcPassword(t *testing.T) {
u.SetToken("bar")

tests := []struct {
name string
repo *api.Repo
user *api.User
git yaml.Git
appTransport bool
wantToken string
wantErr bool
name string
repo *api.Repo
user *api.User
git yaml.Git
appsTransport bool
wantToken string
wantErr bool
}{
{
name: "installation token",
Expand All @@ -1828,9 +1828,9 @@ func TestGithub_GetNetrcPassword(t *testing.T) {
Permissions: map[string]string{"contents": "read"},
},
},
appTransport: true,
wantToken: "ghs_16C7e42F292c6912E7710c838347Ae178B4a",
wantErr: false,
appsTransport: true,
wantToken: "ghs_16C7e42F292c6912E7710c838347Ae178B4a",
wantErr: false,
},
{
name: "no app configured returns user oauth token",
Expand All @@ -1842,9 +1842,9 @@ func TestGithub_GetNetrcPassword(t *testing.T) {
Permissions: map[string]string{"contents": "read"},
},
},
appTransport: false,
wantToken: "bar",
wantErr: false,
appsTransport: false,
wantToken: "bar",
wantErr: false,
},
{
name: "repo not installed returns user oauth token",
Expand All @@ -1856,9 +1856,9 @@ func TestGithub_GetNetrcPassword(t *testing.T) {
Permissions: map[string]string{"contents": "read"},
},
},
appTransport: true,
wantToken: "bar",
wantErr: false,
appsTransport: true,
wantToken: "bar",
wantErr: false,
},
{
name: "invalid permission resource",
Expand All @@ -1870,9 +1870,9 @@ func TestGithub_GetNetrcPassword(t *testing.T) {
Permissions: map[string]string{"invalid": "read"},
},
},
appTransport: true,
wantToken: "bar",
wantErr: true,
appsTransport: true,
wantToken: "bar",
wantErr: true,
},
{
name: "invalid permission level",
Expand All @@ -1884,16 +1884,16 @@ func TestGithub_GetNetrcPassword(t *testing.T) {
Permissions: map[string]string{"contents": "invalid"},
},
},
appTransport: true,
wantToken: "bar",
wantErr: true,
appsTransport: true,
wantToken: "bar",
wantErr: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
client, _ := NewTest(s.URL)
if test.appTransport {
if test.appsTransport {
client.AppsTransport = NewTestAppsTransport(s.URL)
}

Expand Down

0 comments on commit 6be732d

Please sign in to comment.