sonda/internal/api/http/usersettings_test.go
tgiachi acede4bd91 fix(settings): strict body, deleted-account race, doc wording
- PATCH /me/settings accepts exactly one JSON object: text after it, null, a
  differently cased name or an unknown name are refused (400) and change nothing.
- A user deleted between the read and the write of the settings gives 401, not a
  500, on PostgreSQL too (foreign key violation mapped to user not found).
- The spec says a 401 can also mean the account no longer exists, and that the
  body is strict.
- README no longer says more than the setting does.
- Tests: strict body cases, tokens of a deleted account on GET and PATCH,
  recognising a foreign key violation on both databases.
2026-11-11 19:20:42 +01:00

141 lines
5.4 KiB
Go

package httpapi
import (
"bytes"
"net/http"
"net/http/httptest"
"strconv"
"testing"
)
func TestSettingsNeedASignedInUser(t *testing.T) {
h := newTestAPI(t)
for _, m := range []string{"GET", "PATCH"} {
if r := do(t, h, m, "/api/v1/me/settings", "", map[string]any{}); r.Code != http.StatusUnauthorized {
t.Errorf("%s without a token: %d", m, r.Code)
}
}
}
func TestSettingsStartOffAndStayMine(t *testing.T) {
h := newTestAPI(t)
alice := registerAndLogin(t, h)
r := do(t, h, "GET", "/api/v1/me/settings", alice, nil)
if r.Code != http.StatusOK {
t.Fatalf("%d %v", r.Code, r.Body)
}
sameKeys(t, "settings", r.Body, "discover_from_listening")
if r.Body["discover_from_listening"] != false {
t.Errorf("settings = %v, want everything off", r.Body)
}
r = do(t, h, "PATCH", "/api/v1/me/settings", alice, map[string]any{"discover_from_listening": true})
if r.Code != http.StatusOK || r.Body["discover_from_listening"] != true {
t.Fatalf("turn on: %d %v", r.Code, r.Body)
}
if r = do(t, h, "GET", "/api/v1/me/settings", alice, nil); r.Body["discover_from_listening"] != true {
t.Errorf("not kept: %v", r.Body)
}
// An empty object changes nothing.
if r = do(t, h, "PATCH", "/api/v1/me/settings", alice, map[string]any{}); r.Code != http.StatusOK || r.Body["discover_from_listening"] != true {
t.Errorf("empty patch: %d %v", r.Code, r.Body)
}
// Another user has their own.
cred := map[string]any{"username": "bob", "password": "longenough"}
if c := do(t, h, "POST", "/api/v1/users", alice, cred); c.Code != http.StatusCreated {
t.Fatalf("create bob: %d %v", c.Code, c.Body)
}
bob, _ := do(t, h, "POST", "/api/v1/auth/login", "", cred).Body["access_token"].(string)
if r = do(t, h, "GET", "/api/v1/me/settings", bob, nil); r.Body["discover_from_listening"] != false {
t.Errorf("bob sees alice's setting: %v", r.Body)
}
}
func TestSettingsRejectWhatIsNotABoolean(t *testing.T) {
h := newTestAPI(t)
alice := registerAndLogin(t, h)
for name, body := range map[string]any{
"a string": map[string]any{"discover_from_listening": "yes"},
"a number": map[string]any{"discover_from_listening": 1},
"null": map[string]any{"discover_from_listening": nil},
"not an object": []any{true},
"a setting that does not exist": map[string]any{"discover_from_listenin": true},
} {
r := do(t, h, "PATCH", "/api/v1/me/settings", alice, body)
if name == "null" {
// null is the same as absent for a pointer field: nothing changes.
if r.Code != http.StatusOK {
t.Errorf("null: %d %v", r.Code, r.Body)
}
continue
}
if r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
t.Errorf("%s: %d %v, want 400 invalid_input", name, r.Code, r.Body)
}
}
}
// raw sends a body exactly as given, which do() cannot: it always encodes JSON.
func raw(t *testing.T, h http.Handler, token, body string) (int, string) {
t.Helper()
req := httptest.NewRequest("PATCH", "/api/v1/me/settings", bytes.NewReader([]byte(body)))
req.Header.Set("Authorization", "Bearer "+token)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec.Code, rec.Body.String()
}
func TestSettingsAcceptOnlyOneExactObject(t *testing.T) {
h := newTestAPI(t)
alice := registerAndLogin(t, h)
for name, body := range map[string]string{
"two objects": `{"discover_from_listening":false}{"discover_from_listening":true}`,
"trailing text": `{"discover_from_listening":true} nope`,
"null": `null`,
"empty body": ``,
"another case": `{"DISCOVER_FROM_LISTENING":true}`,
"an array": `[{"discover_from_listening":true}]`,
"a misspelt and a right": `{"discover_from_listening":true,"nope":1}`,
} {
code, out := raw(t, h, alice, body)
if code != http.StatusBadRequest {
t.Errorf("%s: %d %s, want 400", name, code, out)
}
}
// Nothing above changed anything.
if r := do(t, h, "GET", "/api/v1/me/settings", alice, nil); r.Body["discover_from_listening"] != false {
t.Errorf("a refused request changed the setting: %v", r.Body)
}
// What is accepted: an object with the setting, with a boolean or null, or with nothing.
for _, body := range []string{`{"discover_from_listening":true}`, `{"discover_from_listening":null}`, `{}`, ` { } `} {
if code, out := raw(t, h, alice, body); code != http.StatusOK {
t.Errorf("%q: %d %s, want 200", body, code, out)
}
}
}
func TestSettingsOfADeletedAccountAreRefused(t *testing.T) {
h := newTestAPI(t)
admin := registerAndLogin(t, h)
cred := map[string]any{"username": "bob", "password": "longenough"}
created := do(t, h, "POST", "/api/v1/users", admin, cred)
if created.Code != http.StatusCreated {
t.Fatalf("create bob: %d %v", created.Code, created.Body)
}
bob, _ := do(t, h, "POST", "/api/v1/auth/login", "", cred).Body["access_token"].(string)
id := int64(created.Body["id"].(float64))
if r := do(t, h, "DELETE", "/api/v1/users/"+itoa(id), admin, nil); r.Code != http.StatusNoContent {
t.Fatalf("delete bob: %d %v", r.Code, r.Body)
}
if r := do(t, h, "GET", "/api/v1/me/settings", bob, nil); r.Code != http.StatusUnauthorized {
t.Errorf("GET: %d %v, want 401", r.Code, r.Body)
}
on := map[string]any{"discover_from_listening": true}
if r := do(t, h, "PATCH", "/api/v1/me/settings", bob, on); r.Code != http.StatusUnauthorized {
t.Errorf("PATCH: %d %v, want 401", r.Code, r.Body)
}
}
func itoa(n int64) string { return strconv.FormatInt(n, 10) }