(t *testing.T)
| 308 | } |
| 309 | |
| 310 | func TestHandleUpdateMe_ValidationErrors(t *testing.T) { |
| 311 | ua, _, token := setupUserAuth(t) |
| 312 | |
| 313 | // Each case asserts that the validation gate fires and a 400 is |
| 314 | // returned BEFORE any DB write. The handler chooses to reject rather |
| 315 | // than normalize so a caller's exact bytes round-trip cleanly through |
| 316 | // /me on the next GET. |
| 317 | cases := []struct { |
| 318 | name string |
| 319 | body string |
| 320 | }{ |
| 321 | {"empty string", `{"name":""}`}, |
| 322 | {"leading whitespace", `{"name":" Jamie"}`}, |
| 323 | {"trailing whitespace", `{"name":"Jamie "}`}, |
| 324 | {"too long", `{"name":"` + strings.Repeat("a", 81) + `"}`}, |
| 325 | {"missing name field", `{}`}, |
| 326 | {"malformed JSON", `{not-json}`}, |
| 327 | } |
| 328 | for _, tc := range cases { |
| 329 | t.Run(tc.name, func(t *testing.T) { |
| 330 | req := authedJSON("PATCH", "/api/auth/me", token, tc.body) |
| 331 | w := httptest.NewRecorder() |
| 332 | ua.HandleUpdateMe(w, req) |
| 333 | if w.Code != http.StatusBadRequest { |
| 334 | t.Errorf("status = %d, want 400; body=%s", w.Code, w.Body.String()) |
| 335 | } |
| 336 | }) |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | func TestHandleUpdateMe_MaxBoundary(t *testing.T) { |
| 341 | // 80 chars must succeed; 81 already covered by ValidationErrors. |
nothing calls this directly
no test coverage detected