sonda/internal/storage/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

80 lines
2.9 KiB
Go

package storage
import (
"context"
"errors"
"testing"
"github.com/uptrace/bun"
"codeberg.org/squidd/sonda/internal/usersettings"
)
func TestUserSettingsRepo(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
users := NewUserRepo(db)
alice, _ := users.CreateUser(ctx, "alice", "h", false)
bob, _ := users.CreateUser(ctx, "bob", "h", false)
repo := NewUserSettingsRepo(db)
got, err := repo.Get(ctx, alice.ID)
if err != nil || got.DiscoverFromListening {
t.Fatalf("new user: %+v err=%v, want everything off", got, err)
}
on, off := true, false
if got, err = repo.Update(ctx, alice.ID, usersettings.Patch{DiscoverFromListening: &on}); err != nil || !got.DiscoverFromListening {
t.Fatalf("turn on: %+v err=%v", got, err)
}
// Asked again later, from the database.
if got, _ = repo.Get(ctx, alice.ID); !got.DiscoverFromListening {
t.Error("the setting was not kept")
}
if got, _ = repo.Get(ctx, bob.ID); got.DiscoverFromListening {
t.Error("bob got alice's setting")
}
// An empty patch changes nothing, also for a user with no row yet.
if got, err = repo.Update(ctx, bob.ID, usersettings.Patch{}); err != nil || got.DiscoverFromListening {
t.Errorf("empty patch: %+v err=%v", got, err)
}
if got, err = repo.Update(ctx, alice.ID, usersettings.Patch{}); err != nil || !got.DiscoverFromListening {
t.Errorf("empty patch on a set value: %+v err=%v", got, err)
}
if got, _ = repo.Update(ctx, alice.ID, usersettings.Patch{DiscoverFromListening: &off}); got.DiscoverFromListening {
t.Error("turn off did not work")
}
// Gone with the account.
if _, err := repo.Update(ctx, bob.ID, usersettings.Patch{DiscoverFromListening: &on}); err != nil {
t.Fatal(err)
}
if err := users.DeleteUser(ctx, bob.ID); err != nil {
t.Fatal(err)
}
var n int
if err := db.QueryRow("SELECT COUNT(*) FROM user_settings WHERE user_id = ?", bob.ID).Scan(&n); err != nil || n != 0 {
t.Errorf("rows left for a deleted user: %d err=%v", n, err)
}
if _, err := repo.Get(ctx, bob.ID); !errors.Is(err, usersettings.ErrUserNotFound) {
t.Errorf("Get for a deleted user: err = %v", err)
}
if _, err := repo.Update(ctx, bob.ID, usersettings.Patch{DiscoverFromListening: &on}); !errors.Is(err, usersettings.ErrUserNotFound) {
t.Errorf("Update for a deleted user: err = %v", err)
}
})
}
func TestAForeignKeyFailureIsRecognised(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
_, err := db.Exec("INSERT INTO user_settings (user_id, discover_from_listening) VALUES (424242, ?)", true)
if err == nil {
t.Fatal("expected a foreign key violation")
}
if !isForeignKeyViolation(err) {
t.Errorf("isForeignKeyViolation(%v) = false", err)
}
if isForeignKeyViolation(errors.New("something else")) {
t.Error("an unrelated error was taken for a foreign key violation")
}
})
}