|
| 1 | +package githubauth |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "net/url" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_githubClient_withEnterpriseURL(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + baseURL string |
| 17 | + wantErr bool |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "valid URL", |
| 21 | + baseURL: "https://github.example.com", |
| 22 | + wantErr: false, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "invalid URL with control characters", |
| 26 | + baseURL: "ht\ntp://invalid", |
| 27 | + wantErr: true, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "URL with spaces", |
| 31 | + baseURL: "http://invalid url with spaces", |
| 32 | + wantErr: true, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + for _, tt := range tests { |
| 37 | + t.Run(tt.name, func(t *testing.T) { |
| 38 | + client := newGitHubClient(&http.Client{}) |
| 39 | + _, err := client.withEnterpriseURL(tt.baseURL) |
| 40 | + if (err != nil) != tt.wantErr { |
| 41 | + t.Errorf("withEnterpriseURL() error = %v, wantErr %v", err, tt.wantErr) |
| 42 | + } |
| 43 | + }) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func Test_githubClient_createInstallationToken_ErrorCases(t *testing.T) { |
| 48 | + tests := []struct { |
| 49 | + name string |
| 50 | + setupServer func() *httptest.Server |
| 51 | + opts *InstallationTokenOptions |
| 52 | + wantErr bool |
| 53 | + errorSubstring string |
| 54 | + }{ |
| 55 | + { |
| 56 | + name: "invalid JSON in options", |
| 57 | + setupServer: func() *httptest.Server { |
| 58 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 59 | + w.WriteHeader(http.StatusCreated) |
| 60 | + json.NewEncoder(w).Encode(InstallationToken{ |
| 61 | + Token: "test-token", |
| 62 | + ExpiresAt: time.Now().Add(1 * time.Hour), |
| 63 | + }) |
| 64 | + })) |
| 65 | + }, |
| 66 | + opts: &InstallationTokenOptions{ |
| 67 | + Repositories: []string{"repo1"}, |
| 68 | + }, |
| 69 | + wantErr: false, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "bad request - 400", |
| 73 | + setupServer: func() *httptest.Server { |
| 74 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 75 | + w.WriteHeader(http.StatusBadRequest) |
| 76 | + w.Write([]byte(`{"message":"Bad Request"}`)) |
| 77 | + })) |
| 78 | + }, |
| 79 | + opts: nil, |
| 80 | + wantErr: true, |
| 81 | + errorSubstring: "400", |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "unauthorized - 401", |
| 85 | + setupServer: func() *httptest.Server { |
| 86 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 87 | + w.WriteHeader(http.StatusUnauthorized) |
| 88 | + w.Write([]byte(`{"message":"Unauthorized"}`)) |
| 89 | + })) |
| 90 | + }, |
| 91 | + opts: nil, |
| 92 | + wantErr: true, |
| 93 | + errorSubstring: "401", |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "forbidden - 403", |
| 97 | + setupServer: func() *httptest.Server { |
| 98 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 99 | + w.WriteHeader(http.StatusForbidden) |
| 100 | + w.Write([]byte(`{"message":"Forbidden"}`)) |
| 101 | + })) |
| 102 | + }, |
| 103 | + opts: nil, |
| 104 | + wantErr: true, |
| 105 | + errorSubstring: "403", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "not found - 404", |
| 109 | + setupServer: func() *httptest.Server { |
| 110 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 111 | + w.WriteHeader(http.StatusNotFound) |
| 112 | + w.Write([]byte(`{"message":"Not Found"}`)) |
| 113 | + })) |
| 114 | + }, |
| 115 | + opts: nil, |
| 116 | + wantErr: true, |
| 117 | + errorSubstring: "404", |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "invalid JSON response", |
| 121 | + setupServer: func() *httptest.Server { |
| 122 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 123 | + w.WriteHeader(http.StatusCreated) |
| 124 | + w.Write([]byte(`{invalid json`)) |
| 125 | + })) |
| 126 | + }, |
| 127 | + opts: nil, |
| 128 | + wantErr: true, |
| 129 | + errorSubstring: "failed to decode response", |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "success with nil options", |
| 133 | + setupServer: func() *httptest.Server { |
| 134 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 135 | + w.WriteHeader(http.StatusCreated) |
| 136 | + json.NewEncoder(w).Encode(InstallationToken{ |
| 137 | + Token: "test-token", |
| 138 | + ExpiresAt: time.Now().Add(1 * time.Hour), |
| 139 | + }) |
| 140 | + })) |
| 141 | + }, |
| 142 | + opts: nil, |
| 143 | + wantErr: false, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "success with HTTP 200", |
| 147 | + setupServer: func() *httptest.Server { |
| 148 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 149 | + w.WriteHeader(http.StatusOK) |
| 150 | + json.NewEncoder(w).Encode(InstallationToken{ |
| 151 | + Token: "test-token", |
| 152 | + ExpiresAt: time.Now().Add(1 * time.Hour), |
| 153 | + }) |
| 154 | + })) |
| 155 | + }, |
| 156 | + opts: nil, |
| 157 | + wantErr: false, |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + for _, tt := range tests { |
| 162 | + t.Run(tt.name, func(t *testing.T) { |
| 163 | + server := tt.setupServer() |
| 164 | + defer server.Close() |
| 165 | + |
| 166 | + client := newGitHubClient(&http.Client{}) |
| 167 | + client.baseURL, _ = client.baseURL.Parse(server.URL) |
| 168 | + |
| 169 | + _, err := client.createInstallationToken(context.Background(), 12345, tt.opts) |
| 170 | + if (err != nil) != tt.wantErr { |
| 171 | + t.Errorf("createInstallationToken() error = %v, wantErr %v", err, tt.wantErr) |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + if tt.wantErr && tt.errorSubstring != "" { |
| 176 | + if err == nil { |
| 177 | + t.Errorf("expected error containing %q, got nil", tt.errorSubstring) |
| 178 | + } else if !contains(err.Error(), tt.errorSubstring) { |
| 179 | + t.Errorf("expected error containing %q, got %q", tt.errorSubstring, err.Error()) |
| 180 | + } |
| 181 | + } |
| 182 | + }) |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func contains(s, substr string) bool { |
| 187 | + return len(s) >= len(substr) && (s == substr || len(substr) == 0 || (len(s) > 0 && len(substr) > 0 && hasSubstring(s, substr))) |
| 188 | +} |
| 189 | + |
| 190 | +func hasSubstring(s, substr string) bool { |
| 191 | + for i := 0; i <= len(s)-len(substr); i++ { |
| 192 | + if s[i:i+len(substr)] == substr { |
| 193 | + return true |
| 194 | + } |
| 195 | + } |
| 196 | + return false |
| 197 | +} |
| 198 | + |
| 199 | +func Test_Ptr(t *testing.T) { |
| 200 | + t.Run("string pointer", func(t *testing.T) { |
| 201 | + s := "test" |
| 202 | + p := Ptr(s) |
| 203 | + if p == nil { |
| 204 | + t.Error("Ptr() returned nil") |
| 205 | + } |
| 206 | + if *p != s { |
| 207 | + t.Errorf("Ptr() = %v, want %v", *p, s) |
| 208 | + } |
| 209 | + }) |
| 210 | + |
| 211 | + t.Run("int pointer", func(t *testing.T) { |
| 212 | + i := 42 |
| 213 | + p := Ptr(i) |
| 214 | + if p == nil { |
| 215 | + t.Error("Ptr() returned nil") |
| 216 | + } |
| 217 | + if *p != i { |
| 218 | + t.Errorf("Ptr() = %v, want %v", *p, i) |
| 219 | + } |
| 220 | + }) |
| 221 | + |
| 222 | + t.Run("int64 pointer", func(t *testing.T) { |
| 223 | + i := int64(123456) |
| 224 | + p := Ptr(i) |
| 225 | + if p == nil { |
| 226 | + t.Error("Ptr() returned nil") |
| 227 | + } |
| 228 | + if *p != i { |
| 229 | + t.Errorf("Ptr() = %v, want %v", *p, i) |
| 230 | + } |
| 231 | + }) |
| 232 | +} |
| 233 | + |
| 234 | +func Test_createInstallationToken_ErrorPaths(t *testing.T) { |
| 235 | + t.Run("error parsing endpoint URL", func(t *testing.T) { |
| 236 | + // Create a client with an invalid base URL that will cause Parse to fail |
| 237 | + client := &githubClient{ |
| 238 | + baseURL: &url.URL{Scheme: "http", Host: "example.com", Path: ":::invalid"}, |
| 239 | + client: &http.Client{}, |
| 240 | + } |
| 241 | + |
| 242 | + _, err := client.createInstallationToken(context.Background(), 12345, nil) |
| 243 | + if err == nil { |
| 244 | + t.Error("Expected error for invalid base URL, got nil") |
| 245 | + } |
| 246 | + }) |
| 247 | + |
| 248 | + t.Run("error marshaling options", func(t *testing.T) { |
| 249 | + // This is difficult to trigger with InstallationTokenOptions as it has simple fields |
| 250 | + // We would need to use reflection or create a custom type |
| 251 | + // For now, we test with valid options and nil options which are both covered |
| 252 | + client := newGitHubClient(&http.Client{}) |
| 253 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 254 | + w.WriteHeader(http.StatusCreated) |
| 255 | + json.NewEncoder(w).Encode(InstallationToken{ |
| 256 | + Token: "test-token", |
| 257 | + ExpiresAt: time.Now().Add(1 * time.Hour), |
| 258 | + }) |
| 259 | + })) |
| 260 | + defer server.Close() |
| 261 | + |
| 262 | + client.baseURL, _ = client.baseURL.Parse(server.URL) |
| 263 | + |
| 264 | + opts := &InstallationTokenOptions{ |
| 265 | + Repositories: []string{"repo1", "repo2"}, |
| 266 | + Permissions: &InstallationPermissions{ |
| 267 | + Contents: Ptr("read"), |
| 268 | + Issues: Ptr("write"), |
| 269 | + }, |
| 270 | + } |
| 271 | + |
| 272 | + _, err := client.createInstallationToken(context.Background(), 12345, opts) |
| 273 | + if err != nil { |
| 274 | + t.Errorf("Unexpected error: %v", err) |
| 275 | + } |
| 276 | + }) |
| 277 | +} |
0 commit comments