Files
d5caefb310 Feature/discussion 391 add GitHub app support (#396)
* feat: Add GitHub App support for authentication in configuration and code

* feat: Enhance GitHub App support by updating repository fetching logic and authentication method

* test: Update dependencies, enhance configuration handling, and add GitHub client tests

- Updated dependencies in go.sum to latest versions for improved stability and security.
- Added a defer statement to close the configuration file in readConfigFile function to prevent resource leaks.
- Implemented unit tests for GenRepo's HasAppAuth method to validate authentication conditions.
- Created new tests for the GitHub client to verify behavior with and without authentication tokens, including handling of invalid and missing private key files.

* feat: Exclude G101 gosec warning for github/github_test.go

* fixed go.mod and go.sum

---------

Co-authored-by: Andreas Wachter <andreas.wachter@buddyspencer.monster>
2026-06-16 14:12:59 +02:00

85 lines
1.8 KiB
Go

package github
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/cooperspencer/gickup/types"
)
func TestNewGithubClientUnauthenticated(t *testing.T) {
t.Parallel()
repo := types.GenRepo{} // no token, no app auth
client, token, err := newGithubClient(context.Background(), repo)
if err != nil {
t.Fatalf("newGithubClient() error = %v", err)
}
if client == nil {
t.Fatal("expected non-nil client")
}
if token != "" {
t.Fatalf("expected empty token, got %q", token)
}
}
func TestNewGithubClientWithToken(t *testing.T) {
t.Setenv("GITHUB_TEST_TOKEN", "my-personal-token")
repo := types.GenRepo{Token: "GITHUB_TEST_TOKEN"}
client, token, err := newGithubClient(context.Background(), repo)
if err != nil {
t.Fatalf("newGithubClient() error = %v", err)
}
if client == nil {
t.Fatal("expected non-nil client")
}
if token != "my-personal-token" {
t.Fatalf("token = %q, want %q", token, "my-personal-token")
}
}
func TestNewGithubClientAppAuthInvalidKeyFile(t *testing.T) {
t.Parallel()
// A file that exists but contains an invalid PEM key.
keyFile := filepath.Join(t.TempDir(), "invalid.pem")
if err := os.WriteFile(keyFile, []byte("not-a-valid-pem-key"), 0o600); err != nil {
t.Fatalf("write key file: %v", err)
}
repo := types.GenRepo{
AppID: 1,
AppInstallationID: 2,
AppPrivateKeyFile: keyFile,
}
_, _, err := newGithubClient(context.Background(), repo)
if err == nil {
t.Fatal("expected error for invalid App private key file")
}
}
func TestNewGithubClientAppAuthMissingKeyFile(t *testing.T) {
t.Parallel()
repo := types.GenRepo{
AppID: 1,
AppInstallationID: 2,
AppPrivateKeyFile: "/nonexistent/path/key.pem",
}
_, _, err := newGithubClient(context.Background(), repo)
if err == nil {
t.Fatal("expected error when App private key file does not exist")
}
}