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", "radio_in_apps") 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{"email": "bob@example.org", "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{"email": "bob@example.org", "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) } func TestTheRadioInAppsSettingStartsOffAndNamesOnlyItself(t *testing.T) { h := newTestAPI(t) alice := registerAndLogin(t, h) if r := do(t, h, "GET", "/api/v1/me/settings", alice, nil); r.Body["radio_in_apps"] != false { t.Fatalf("at the start: %v", r.Body) } r := do(t, h, "PATCH", "/api/v1/me/settings", alice, map[string]any{"radio_in_apps": true}) if r.Code != http.StatusOK || r.Body["radio_in_apps"] != true || r.Body["discover_from_listening"] != false { t.Fatalf("turn on: %d %v", r.Code, r.Body) } // The other setting is not touched by naming this one, nor this one by naming the other. r = do(t, h, "PATCH", "/api/v1/me/settings", alice, map[string]any{"discover_from_listening": true}) if r.Body["radio_in_apps"] != true || r.Body["discover_from_listening"] != true { t.Fatalf("the other on: %v", r.Body) } r = do(t, h, "PATCH", "/api/v1/me/settings", alice, map[string]any{"radio_in_apps": nil}) if r.Code != http.StatusOK || r.Body["radio_in_apps"] != true { t.Fatalf("null leaves it alone: %d %v", r.Code, r.Body) } if r = do(t, h, "PATCH", "/api/v1/me/settings", alice, map[string]any{"radio_in_apps": "yes"}); r.Code != http.StatusBadRequest { t.Fatalf("a string: %d", r.Code) } if r = do(t, h, "PATCH", "/api/v1/me/settings", alice, map[string]any{"radio_in_app": true}); r.Code != http.StatusBadRequest { t.Fatalf("a misspelt name: %d", r.Code) } }