|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/rand" |
| 5 | + "crypto/rsa" |
| 6 | + "crypto/x509" |
| 7 | + "encoding/pem" |
4 | 8 | "errors" |
5 | 9 | "net/url" |
| 10 | + "os" |
| 11 | + "path/filepath" |
6 | 12 | "testing" |
7 | 13 |
|
| 14 | + "github.com/bradleyfalzon/ghinstallation/v2" |
| 15 | + "github.com/gofri/go-github-pagination/githubpagination" |
8 | 16 | "github.com/stretchr/testify/assert" |
9 | 17 | ) |
10 | 18 |
|
@@ -228,3 +236,52 @@ func TestGetClient(t *testing.T) { |
228 | 236 | }) |
229 | 237 | } |
230 | 238 | } |
| 239 | + |
| 240 | +func TestGetClientGitHubAppEnterpriseTransportBaseURL(t *testing.T) { |
| 241 | + privateKeyPath := writeTempPrivateKeyFile(t) |
| 242 | + |
| 243 | + t.Setenv("GITHUB_APP", "true") |
| 244 | + t.Setenv("GITHUB_APP_KEY_PATH", privateKeyPath) |
| 245 | + t.Setenv("GITHUB_APP_ID", "1") |
| 246 | + t.Setenv("GITHUB_APP_INSTALLATION_ID", "999999") |
| 247 | + t.Setenv("API_URL", "https://github.enterprise.test/api/v3") |
| 248 | + |
| 249 | + cfg, err := Init() |
| 250 | + assert.NoError(t, err) |
| 251 | + |
| 252 | + client, err := cfg.GetClient() |
| 253 | + assert.NoError(t, err) |
| 254 | + |
| 255 | + paginator, ok := client.Client().Transport.(*githubpagination.GitHubPagination) |
| 256 | + if !assert.True(t, ok) { |
| 257 | + return |
| 258 | + } |
| 259 | + |
| 260 | + installationTransport, ok := paginator.Base.(*ghinstallation.Transport) |
| 261 | + if !assert.True(t, ok) { |
| 262 | + return |
| 263 | + } |
| 264 | + |
| 265 | + assert.Equal(t, "https://github.enterprise.test/api/v3", installationTransport.BaseURL) |
| 266 | +} |
| 267 | + |
| 268 | +func writeTempPrivateKeyFile(t *testing.T) string { |
| 269 | + t.Helper() |
| 270 | + |
| 271 | + key, err := rsa.GenerateKey(rand.Reader, 2048) |
| 272 | + if err != nil { |
| 273 | + t.Fatalf("generating rsa key: %v", err) |
| 274 | + } |
| 275 | + |
| 276 | + keyPEM := pem.EncodeToMemory(&pem.Block{ |
| 277 | + Type: "RSA PRIVATE KEY", |
| 278 | + Bytes: x509.MarshalPKCS1PrivateKey(key), |
| 279 | + }) |
| 280 | + |
| 281 | + path := filepath.Join(t.TempDir(), "github-app-private-key.pem") |
| 282 | + if err := os.WriteFile(path, keyPEM, 0o600); err != nil { |
| 283 | + t.Fatalf("writing private key file: %v", err) |
| 284 | + } |
| 285 | + |
| 286 | + return path |
| 287 | +} |
0 commit comments