A new setting, radio_in_apps, off until the person turns it on (Settings, Apps and devices, or PATCH /me/settings). With it off, the list of playlists of an OpenSubsonic app is only the playlists of the person, and a station asked for by id is not found. With it on, the stations come as playlists called Radio · Rock. - migration 0036: user_settings.radio_in_apps - usersettings, the settings API and the OpenAPI spec have the new field; the PATCH now keeps each setting apart (naming one no longer sets the other) - Settings has a switch
110 lines
4.2 KiB
Go
110 lines
4.2 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"git.kmalloc.org/squid/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")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestTheRadioInAppsSettingIsKeptApartFromTheOthers(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
alice, _ := NewUserRepo(db).CreateUser(ctx, "alice", "h", false)
|
|
repo := NewUserSettingsRepo(db)
|
|
on, off := true, false
|
|
|
|
if got, _ := repo.Get(ctx, alice.ID); got.RadioInApps || got.DiscoverFromListening {
|
|
t.Fatalf("new user: %+v, want everything off", got)
|
|
}
|
|
got, err := repo.Update(ctx, alice.ID, usersettings.Patch{RadioInApps: &on})
|
|
if err != nil || !got.RadioInApps || got.DiscoverFromListening {
|
|
t.Fatalf("radio on: %+v %v", got, err)
|
|
}
|
|
// The other setting turned on and then off does not touch it, and both can be named at once.
|
|
if got, _ = repo.Update(ctx, alice.ID, usersettings.Patch{DiscoverFromListening: &on}); !got.RadioInApps || !got.DiscoverFromListening {
|
|
t.Fatalf("both on: %+v", got)
|
|
}
|
|
if got, _ = repo.Update(ctx, alice.ID, usersettings.Patch{DiscoverFromListening: &off, RadioInApps: &off}); got.RadioInApps || got.DiscoverFromListening {
|
|
t.Fatalf("both off: %+v", got)
|
|
}
|
|
if got, _ = repo.Update(ctx, alice.ID, usersettings.Patch{RadioInApps: &on}); !got.RadioInApps {
|
|
t.Fatalf("on again: %+v", got)
|
|
}
|
|
if got, _ = repo.Get(ctx, alice.ID); !got.RadioInApps || got.DiscoverFromListening {
|
|
t.Fatalf("kept: %+v", got)
|
|
}
|
|
})
|
|
}
|