TestCreateGroupHandler tests the createGroupHandler function
(t *testing.T)
| 438 | |
| 439 | // TestCreateGroupHandler tests the createGroupHandler function |
| 440 | func TestCreateGroupHandler(t *testing.T) { |
| 441 | ovl, cleanup := setupTestOverlord(t) |
| 442 | defer cleanup() |
| 443 | |
| 444 | // Get the router with proper configuration |
| 445 | router := ovl.registerRoutes() |
| 446 | |
| 447 | // Login to get a JWT token |
| 448 | adminJWTToken, err := loginAs(router, adminUsername) |
| 449 | if err != nil { |
| 450 | t.Fatalf("Failed to login: %v", err) |
| 451 | } |
| 452 | |
| 453 | // Prepare request body |
| 454 | newGroup := struct { |
| 455 | Name string `json:"name"` |
| 456 | }{ |
| 457 | Name: "newgroup", |
| 458 | } |
| 459 | |
| 460 | body, _ := json.Marshal(newGroup) |
| 461 | |
| 462 | // Test with admin user |
| 463 | req := createAuthRequest(adminJWTToken, "POST", "/api/groups", body) |
| 464 | rr := httptest.NewRecorder() |
| 465 | |
| 466 | router.ServeHTTP(rr, req) |
| 467 | |
| 468 | // Check response |
| 469 | if rr.Code != http.StatusOK { |
| 470 | t.Errorf("Expected status code 200, got %d", rr.Code) |
| 471 | } |
| 472 | |
| 473 | // Verify the group was created |
| 474 | groups, _ := ovl.dbManager.GetAllGroups() |
| 475 | found := false |
| 476 | for _, group := range groups { |
| 477 | if group.Name == "newgroup" { |
| 478 | found = true |
| 479 | break |
| 480 | } |
| 481 | } |
| 482 | if !found { |
| 483 | t.Errorf("Group 'newgroup' should exist in the database") |
| 484 | } |
| 485 | |
| 486 | // Test with non-admin user (should be forbidden by middleware) |
| 487 | userJWTToken, err := loginAs(router, testUsername) |
| 488 | if err != nil { |
| 489 | t.Fatalf("Failed to login: %v", err) |
| 490 | } |
| 491 | |
| 492 | newGroup.Name = "anothergroup" |
| 493 | body, _ = json.Marshal(newGroup) |
| 494 | req = createAuthRequest(userJWTToken, "POST", "/api/groups", body) |
| 495 | rr = httptest.NewRecorder() |
| 496 | |
| 497 | router.ServeHTTP(rr, req) |
nothing calls this directly
no test coverage detected