From 1915c98e1686517414ea94d3f0cb5eeac1336763 Mon Sep 17 00:00:00 2001 From: Mateusz Szostok Date: Fri, 15 Apr 2022 13:13:30 +0200 Subject: [PATCH] Adjust integration tests, add test e2e test for GitHub App auth --- .github/workflows/pull-requests.yml | 1 + internal/github/client.go | 4 +-- tests/integration/integration_test.go | 33 +++++++++++++++++++ .../testdata/TestGitHubAppAuth.golden.txt | 4 +++ .../token_not_specified.golden.txt | 2 +- 5 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 tests/integration/testdata/TestGitHubAppAuth.golden.txt diff --git a/.github/workflows/pull-requests.yml b/.github/workflows/pull-requests.yml index 4e4d8b6..8291c5c 100644 --- a/.github/workflows/pull-requests.yml +++ b/.github/workflows/pull-requests.yml @@ -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 diff --git a/internal/github/client.go b/internal/github/client.go index 5ddefa8..9661bf3 100644 --- a/internal/github/client.go +++ b/internal/github/client.go @@ -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" @@ -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") } diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index a451816..c30e67b 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -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 { @@ -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") } diff --git a/tests/integration/testdata/TestGitHubAppAuth.golden.txt b/tests/integration/testdata/TestGitHubAppAuth.golden.txt new file mode 100644 index 0000000..e93cda6 --- /dev/null +++ b/tests/integration/testdata/TestGitHubAppAuth.golden.txt @@ -0,0 +1,4 @@ +==> Executing Valid Owner Checker () + Check OK + +1 check(s) executed, no failure(s) diff --git a/tests/integration/testdata/TestOwnerCheckAuthZAndAuthN/token_not_specified.golden.txt b/tests/integration/testdata/TestOwnerCheckAuthZAndAuthN/token_not_specified.golden.txt index fddcd69..6b3983b 100644 --- a/tests/integration/testdata/TestOwnerCheckAuthZAndAuthN/token_not_specified.golden.txt +++ b/tests/integration/testdata/TestOwnerCheckAuthZAndAuthN/token_not_specified.golden.txt @@ -1 +1 @@ -time="