package http import ( "net/http" "net/http/cookiejar" "strings" "testing" ) // Settings → Profile: the identity projection carries the particulars, // PATCH /api/me applies partial allowlisted edits (empty string clears), // validation is rune-based, and the endpoint is session-gated. func TestProfileUpdate(t *testing.T) { srv, _ := testServerDB(t) jar, _ := cookiejar.New(nil) alice := apiClient{t, &http.Client{Jar: jar}, srv.URL} alice.request("POST", "/api/signup", map[string]any{"email": "alice@superbrain.test", "password": "a-long-test-password"}, 201) alice.request("POST", "/api/auth/login", map[string]any{"email": "alice@superbrain.test", "password": "a-long-test-password"}, 200) fields := []string{"bio", "location", "occupation", "organization", "website"} user := alice.request("GET", "/api/me", nil, 200)["user"].(map[string]any) for _, f := range fields { if v, present := user[f]; !present || v != nil { t.Fatalf("fresh profile field %s = %v (present=%v), want null", f, v, present) } } saved := alice.request("PATCH", "/api/me", map[string]any{ "name": "Alice Example", "bio": "Knowledge worker", "location": "Berlin, Germany", "occupation": "Engineer", "organization": "Acme", "website": "https://alice.example.com", }, 200) if saved["name"] != "Alice Example" || saved["bio"] != "Knowledge worker" || saved["location"] != "Berlin, Germany" || saved["occupation"] != "Engineer" || saved["organization"] != "Acme" || saved["website"] != "https://alice.example.com" { t.Fatalf("save response wrong: %v", saved) } // The session identity is reloaded per request: GET /api/me reflects it. user = alice.request("GET", "/api/me", nil, 200)["user"].(map[string]any) if user["name"] != "Alice Example" || user["occupation"] != "Engineer" { t.Fatalf("GET /api/me did not reflect the save: %v", user) } // Partial edit: only the submitted field moves. alice.request("PATCH", "/api/me", map[string]any{"bio": "Updated bio"}, 200) user = alice.request("GET", "/api/me", nil, 200)["user"].(map[string]any) if user["bio"] != "Updated bio" || user["location"] != "Berlin, Germany" || user["name"] != "Alice Example" { t.Fatalf("partial edit clobbered other fields: %v", user) } // Empty string clears to null; whitespace-only is empty too. alice.request("PATCH", "/api/me", map[string]any{"bio": " ", "website": ""}, 200) user = alice.request("GET", "/api/me", nil, 200)["user"].(map[string]any) if user["bio"] != nil || user["website"] != nil { t.Fatalf("clear did not null the fields: %v", user) } // Rune-based caps: 280 multi-byte runes fit, 281 do not. alice.request("PATCH", "/api/me", map[string]any{"bio": strings.Repeat("é", 280)}, 200) alice.request("PATCH", "/api/me", map[string]any{"bio": strings.Repeat("é", 281)}, 400) alice.request("PATCH", "/api/me", map[string]any{"name": strings.Repeat("x", 121)}, 400) // Website shape: http(s) with a host only. for _, badURL := range []string{"javascript:alert(1)", "ftp://x.test", "not a url", "https://", "/relative"} { alice.request("PATCH", "/api/me", map[string]any{"website": badURL}, 400) } alice.request("PATCH", "/api/me", map[string]any{"website": "http://alice.example.com/path"}, 200) // Type and emptiness guards. alice.request("PATCH", "/api/me", map[string]any{"bio": 42}, 400) alice.request("PATCH", "/api/me", map[string]any{"unknown": "x"}, 400) alice.request("PATCH", "/api/me", map[string]any{}, 400) // Session-gated like every data endpoint. anon := apiClient{t, &http.Client{}, srv.URL} anon.request("PATCH", "/api/me", map[string]any{"bio": "intruder"}, 401) }