sonda/internal/api/http/usersettings_test.go
tgiachi 96ad8b1f62 feat(auth): accounts are identified by an email address
Registration, login and the admin's create-user call take `email` instead of
`username`, and the user object returns `email`. The address is checked for its
shape (a plain address, no display name, at most 254 bytes), trimmed, lowercased
and unique. Sonda sends no mail and does not verify it.

- Migration 0018 renames the users column (and the PostgreSQL index); values are
  kept, so an account made before keeps its plain name and still logs in with it
  (login does not check the shape).
- Error code username_taken becomes email_taken; the JWT claim is now `email`,
  so access tokens issued before the upgrade are refused once and clients refresh.
- OpenAPI spec, README, the web login form (type email), strings (en, it) and
  the generated API types follow.

Closes #65
2026-11-12 14:40:33 +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{"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) }