MCPcopy Create free account
hub / github.com/aitjcize/Overlord / TestCreateUserHandler

Function TestCreateUserHandler

overlord/admin_api_test.go:228–300  ·  view source on GitHub ↗

TestCreateUserHandler tests the createUserHandler function

(t *testing.T)

Source from the content-addressed store, hash-verified

226
227// TestCreateUserHandler tests the createUserHandler function
228func TestCreateUserHandler(t *testing.T) {
229 ovl, cleanup := setupTestOverlord(t)
230 defer cleanup()
231
232 // Get the router with proper configuration
233 router := ovl.registerRoutes()
234
235 // Login to get a JWT token
236 adminJWTToken, err := loginAs(router, adminUsername)
237 if err != nil {
238 t.Fatalf("Failed to login: %v", err)
239 }
240
241 // Prepare request body
242 newUser := struct {
243 Username string `json:"username"`
244 Password string `json:"password"`
245 IsAdmin bool `json:"is_admin"`
246 }{
247 Username: "newuser",
248 Password: "newpass",
249 IsAdmin: false,
250 }
251
252 body, err := json.Marshal(newUser)
253 if err != nil {
254 t.Fatalf("Failed to marshal new user: %v", err)
255 }
256
257 // Test with admin user
258 req := createAuthRequest(adminJWTToken, "POST", "/api/users", body)
259 rr := httptest.NewRecorder()
260
261 router.ServeHTTP(rr, req)
262
263 // Check response
264 if rr.Code != http.StatusOK {
265 t.Errorf("Expected status code 200, got %d", rr.Code)
266 }
267
268 // Verify the user was created in the database
269 exists, err := userExists(ovl.dbManager, "newuser")
270 if err != nil {
271 t.Fatalf("Error checking if user exists: %v", err)
272 }
273 if !exists {
274 t.Errorf("User 'newuser' should exist in the database")
275 }
276
277 // Test with non-admin user (should be forbidden by middleware)
278 userJWTToken, err := loginAs(router, testUsername)
279 if err != nil {
280 t.Fatalf("Failed to login: %v", err)
281 }
282
283 newUser.Username = "anotheruser"
284 body, _ = json.Marshal(newUser)
285 req = createAuthRequest(userJWTToken, "POST", "/api/users", body)

Callers

nothing calls this directly

Calls 7

setupTestOverlordFunction · 0.85
loginAsFunction · 0.85
createAuthRequestFunction · 0.85
userExistsFunction · 0.85
registerRoutesMethod · 0.80
ServeHTTPMethod · 0.80
MarshalMethod · 0.65

Tested by

no test coverage detected