|
1 | 1 | package pkg
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
4 | 7 | "os"
|
| 8 | + "strings" |
5 | 9 | "testing"
|
6 | 10 |
|
| 11 | + abs "github.com/microsoft/kiota-abstractions-go" |
| 12 | + "github.com/octokit/go-sdk-enterprise-server/pkg/github/installation" |
7 | 13 | "github.com/octokit/go-sdk-enterprise-server/pkg/headers"
|
8 | 14 | )
|
9 | 15 |
|
@@ -91,6 +97,49 @@ func TestNewApiClientAppAuthHappyPath(t *testing.T) {
|
91 | 97 | }
|
92 | 98 | }
|
93 | 99 |
|
| 100 | +func TestNewApiClientAppAuthBaseUrl(t *testing.T) { |
| 101 | + tmpfile, err := os.CreateTemp("", pemFileName) |
| 102 | + if err != nil { |
| 103 | + t.Fatal(err) |
| 104 | + } |
| 105 | + defer os.Remove(tmpfile.Name()) |
| 106 | + |
| 107 | + if _, err := tmpfile.Write(key); err != nil { |
| 108 | + t.Fatal(err) |
| 109 | + } |
| 110 | + if err := tmpfile.Close(); err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + |
| 114 | + expectedCall := false |
| 115 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 116 | + if strings.Contains(r.URL.Path, "/app/installations") { |
| 117 | + expectedCall = true |
| 118 | + } |
| 119 | + })) |
| 120 | + |
| 121 | + client, err := NewApiClient( |
| 122 | + WithGitHubAppAuthentication(tmpfile.Name(), clientID, installationID), |
| 123 | + WithBaseUrl(server.URL), |
| 124 | + ) |
| 125 | + if err != nil { |
| 126 | + t.Fatalf("error creating client: %v", err) |
| 127 | + } |
| 128 | + if client == nil { |
| 129 | + t.Fatalf("client is nil") |
| 130 | + } |
| 131 | + queryParams := &installation.RepositoriesRequestBuilderGetQueryParameters{} |
| 132 | + requestConfig := &abs.RequestConfiguration[installation.RepositoriesRequestBuilderGetQueryParameters]{ |
| 133 | + QueryParameters: queryParams, |
| 134 | + } |
| 135 | + |
| 136 | + // trigger a refresh of the installation token to the expected url |
| 137 | + _, _ = client.Installation().Repositories().Get(context.Background(), requestConfig) |
| 138 | + if !expectedCall { |
| 139 | + t.Errorf("installation token endpoint not called") |
| 140 | + } |
| 141 | +} |
| 142 | + |
94 | 143 | func TestNewApiClientAppAuthErrorGettingKeyFromFile(t *testing.T) {
|
95 | 144 | client, err := NewApiClient(WithGitHubAppAuthentication("pem/file/does/not/exist.pem", clientID, installationID))
|
96 | 145 | if err == nil {
|
|
0 commit comments