|
1 | 1 | package github |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 | "regexp" |
6 | 7 | "strings" |
@@ -199,3 +200,105 @@ func TestAccGithubTeamSettings(t *testing.T) { |
199 | 200 | }) |
200 | 201 | }) |
201 | 202 | } |
| 203 | + |
| 204 | +func TestBatchUserNodeIds(t *testing.T) { |
| 205 | + t.Run("fetches user node IDs in batch", func(t *testing.T) { |
| 206 | + if len(testAccConf.testExternalUser) == 0 { |
| 207 | + t.Skip("No external user provided") |
| 208 | + } |
| 209 | + |
| 210 | + // Skip if not authenticated |
| 211 | + skipUnauthenticated(t) |
| 212 | + |
| 213 | + // Create a test owner/meta object |
| 214 | + owner, err := getTestMeta() |
| 215 | + if err != nil { |
| 216 | + t.Fatalf("Failed to get test meta: %v", err) |
| 217 | + } |
| 218 | + |
| 219 | + ctx := context.Background() |
| 220 | + |
| 221 | + // Test with single user |
| 222 | + t.Run("single user", func(t *testing.T) { |
| 223 | + usernames := []string{testAccConf.testExternalUser} |
| 224 | + result, err := getBatchUserNodeIds(ctx, owner, usernames) |
| 225 | + |
| 226 | + if err != nil { |
| 227 | + t.Fatalf("getBatchUserNodeIds failed: %v", err) |
| 228 | + } |
| 229 | + |
| 230 | + if len(result) != 1 { |
| 231 | + t.Errorf("Expected 1 result, got %d", len(result)) |
| 232 | + } |
| 233 | + |
| 234 | + nodeId, exists := result[testAccConf.testExternalUser] |
| 235 | + if !exists { |
| 236 | + t.Errorf("Expected node ID for user %s", testAccConf.testExternalUser) |
| 237 | + } |
| 238 | + |
| 239 | + if nodeId == "" { |
| 240 | + t.Errorf("Node ID should not be empty") |
| 241 | + } |
| 242 | + |
| 243 | + t.Logf("Successfully fetched node ID %s for user %s", nodeId, testAccConf.testExternalUser) |
| 244 | + }) |
| 245 | + |
| 246 | + // Test with multiple users (using same user multiple times for simplicity) |
| 247 | + t.Run("multiple users", func(t *testing.T) { |
| 248 | + usernames := []string{testAccConf.testExternalUser, "octocat"} // octocat is a well-known GitHub user |
| 249 | + result, err := getBatchUserNodeIds(ctx, owner, usernames) |
| 250 | + |
| 251 | + if err != nil { |
| 252 | + t.Fatalf("getBatchUserNodeIds failed: %v", err) |
| 253 | + } |
| 254 | + |
| 255 | + // We expect at least the test user to exist |
| 256 | + if len(result) == 0 { |
| 257 | + t.Errorf("Expected at least 1 result, got %d", len(result)) |
| 258 | + } |
| 259 | + |
| 260 | + // Check that our test user exists |
| 261 | + nodeId, exists := result[testAccConf.testExternalUser] |
| 262 | + if !exists { |
| 263 | + t.Errorf("Expected node ID for user %s", testAccConf.testExternalUser) |
| 264 | + } else if nodeId == "" { |
| 265 | + t.Errorf("Node ID should not be empty") |
| 266 | + } |
| 267 | + |
| 268 | + t.Logf("Successfully fetched %d node IDs in batch request", len(result)) |
| 269 | + for username, nodeId := range result { |
| 270 | + t.Logf(" User: %s, Node ID: %s", username, nodeId) |
| 271 | + } |
| 272 | + }) |
| 273 | + |
| 274 | + // Test with empty list |
| 275 | + t.Run("empty usernames list", func(t *testing.T) { |
| 276 | + usernames := []string{} |
| 277 | + result, err := getBatchUserNodeIds(ctx, owner, usernames) |
| 278 | + |
| 279 | + if err != nil { |
| 280 | + t.Fatalf("getBatchUserNodeIds failed: %v", err) |
| 281 | + } |
| 282 | + |
| 283 | + if len(result) != 0 { |
| 284 | + t.Errorf("Expected 0 results for empty list, got %d", len(result)) |
| 285 | + } |
| 286 | + }) |
| 287 | + |
| 288 | + // Test with non-existent user |
| 289 | + t.Run("non-existent user", func(t *testing.T) { |
| 290 | + nonExistentUser := "this-user-definitely-does-not-exist-12345" |
| 291 | + usernames := []string{nonExistentUser} |
| 292 | + result, err := getBatchUserNodeIds(ctx, owner, usernames) |
| 293 | + |
| 294 | + if err != nil { |
| 295 | + t.Fatalf("getBatchUserNodeIds failed: %v", err) |
| 296 | + } |
| 297 | + |
| 298 | + // Non-existent users should not appear in results |
| 299 | + if len(result) != 0 { |
| 300 | + t.Errorf("Expected 0 results for non-existent user, got %d", len(result)) |
| 301 | + } |
| 302 | + }) |
| 303 | + }) |
| 304 | +} |
0 commit comments