Skip to content

Commit

Permalink
Adjust integration tests, add test e2e test for GitHub App auth
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok committed Apr 15, 2022
1 parent dc708af commit 1915c98
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pull-requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.TOKEN_INTEGRATION_TESTS }}
TOKEN_WITH_NO_SCOPES: ${{ secrets.TOKEN_WITH_NO_SCOPES }}
APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }}
run: |
echo "${{ env.BINARY_PATH }}"
make test-integration
4 changes: 1 addition & 3 deletions internal/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package github
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"time"

"github.com/bradleyfalzon/ghinstallation/v2"

"github.com/mszostok/codeowners-validator/pkg/url"

"github.com/google/go-github/v41/github"
Expand All @@ -29,7 +28,6 @@ type ClientConfig struct {

// Validate validates if provided client options are valid.
func (c *ClientConfig) Validate() error {
fmt.Fprintf(os.Stderr, "%#v", c)
if c.AccessToken == "" && c.AppID == 0 {
return errors.New("GitHub authorization is required, provide ACCESS_TOKEN or APP_ID")
}
Expand Down
33 changes: 33 additions & 0 deletions tests/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const (
binaryPathEnvName = "BINARY_PATH"
codeownersSamplesRepo = "https://github.com/gh-codeowners/codeowners-samples.git"
caseInsensitiveOrgCodeownersSamplesRepo = "https://github.com/GitHubCODEOWNERS/codeowners-samples.git"

gitHubAppId = "190766"
gitHubAppInstallationId = "24938181"
gitHubAppPrivateKeyEnvName = "APP_PRIVATE_KEY"
)

var repositories = []struct {
Expand Down Expand Up @@ -348,6 +352,35 @@ func TestOwnerCheckAuthZAndAuthN(t *testing.T) {
})
}

// To update golden file, run:
// TEST=TestGitHubAppAuth APP_PRIVATE_KEY=`cat private-key.pem` UPDATE_GOLDEN=true make test-integration
func TestGitHubAppAuth(t *testing.T) {
t.Parallel()

// given
repoDir, cleanup := CloneRepo(t, caseInsensitiveOrgCodeownersSamplesRepo, "happy-path")
defer cleanup()

codeownersCmd := Exec().
Binary(os.Getenv(binaryPathEnvName)).
WithEnv("REPOSITORY_PATH", repoDir).
WithEnv("CHECKS", "owners").
WithEnv("OWNER_CHECKER_REPOSITORY", "GitHubCODEOWNERS/codeowners-samples").
WithEnv("GITHUB_APP_ID", gitHubAppId).
WithEnv("GITHUB_APP_INSTALLATION_ID", gitHubAppInstallationId).
WithEnv("GITHUB_APP_PRIVATE_KEY", os.Getenv(gitHubAppPrivateKeyEnvName))

// when
result := codeownersCmd.AwaitResultAtMost(time.Minute)

// then
assert.Equal(t, 0, result.ExitCode)

normalizedOutput := normalizeTimeDurations(result.Stdout)
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt"))
g.Assert(t, t.Name(), []byte(normalizedOutput))
}

func TestMultipleChecksSuccess(t *testing.T) {
t.Skip("not implemented yet")
}
4 changes: 4 additions & 0 deletions tests/integration/testdata/TestGitHubAppAuth.golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
==> Executing Valid Owner Checker (<duration>)
Check OK

1 check(s) executed, no failure(s)
Original file line number Diff line number Diff line change
@@ -1 +1 @@
time="<time>" level=fatal msg="while loading config for owners: envconfig: keys GITHUB_ACCESSTOKEN, GITHUB_ACCESS_TOKEN, github_access_token, github_accesstoken not found"
time="<time>" level=fatal msg="while creating GitHub client: GitHub authorization is required, provide ACCESS_TOKEN or APP_ID"

0 comments on commit 1915c98

Please sign in to comment.