Lidarr is a server of the user's own that looks for and downloads music. Sonda never downloads; this is the first piece of letting an admin send it an artist. - The `lidarr` provider, off by default: an address, an API key (32 hexadecimal characters, never returned) and, chosen once, a quality profile, a metadata profile, a root folder, the monitor option and whether to search for missing albums. It cannot be enabled without an address and a key. - The address is checked: http or https, no user and password, no query, no fragment, no closing slash. - A small client of its own, apart from the outbound one (which only makes plain GETs): the key travels in a header, no redirect is ever followed, answers and time are bounded, and the address and key are read for every request. Status, options, artist by MusicBrainz id and add (lookup, then the add that Lidarr's own screen sends). - `GET /providers/lidarr/options` lists the profiles and root folders; the test of the provider checks the address and the key (`no_address` when it has none). - Migration 0017 adds the address and the options to the provider settings. - OpenAPI, contract test and README say what is sent and where.
107 lines
3.8 KiB
Go
107 lines
3.8 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"codeberg.org/squidd/sonda/internal/providers"
|
|
)
|
|
|
|
func TestProviderRepoSaveGetList(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewProviderRepo(db)
|
|
|
|
if _, found, err := repo.Get(ctx, providers.LastFM); err != nil || found {
|
|
t.Fatalf("never configured: found=%v err=%v", found, err)
|
|
}
|
|
if rows, err := repo.List(ctx); err != nil || len(rows) != 0 {
|
|
t.Fatalf("empty list: %v err=%v", rows, err)
|
|
}
|
|
|
|
at := time.Unix(1_700_000_000, 0)
|
|
want := providers.Settings{Provider: providers.LastFM, Enabled: true, APIKey: "0123456789abcdef0123456789abcdef", Contact: "x", UpdatedAt: at}
|
|
if err := repo.Save(ctx, want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, found, err := repo.Get(ctx, providers.LastFM)
|
|
if err != nil || !found {
|
|
t.Fatalf("Get: found=%v err=%v", found, err)
|
|
}
|
|
if got.Provider != want.Provider || !got.Enabled || got.APIKey != want.APIKey || got.Contact != "x" || !got.UpdatedAt.Equal(at) {
|
|
t.Errorf("got %+v, want %+v", got, want)
|
|
}
|
|
|
|
// Saving again updates the same row.
|
|
want.Enabled, want.APIKey, want.Contact = false, "", ""
|
|
want.UpdatedAt = at.Add(time.Hour)
|
|
if err := repo.Save(ctx, want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _, _ = repo.Get(ctx, providers.LastFM)
|
|
if got.Enabled || got.APIKey != "" || got.Contact != "" || !got.UpdatedAt.Equal(at.Add(time.Hour)) {
|
|
t.Errorf("after the second save: %+v", got)
|
|
}
|
|
if n := count(t, db, "SELECT COUNT(*) FROM provider_settings"); n != 1 {
|
|
t.Errorf("rows = %d, want 1: a save must update, not duplicate", n)
|
|
}
|
|
|
|
_ = repo.Save(ctx, providers.Settings{Provider: providers.MusicBrainz, Enabled: true, UpdatedAt: at})
|
|
rows, err := repo.List(ctx)
|
|
if err != nil || len(rows) != 2 {
|
|
t.Fatalf("list: %v err=%v", rows, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
// A provider that was never configured must not appear as enabled: the table
|
|
// default is "off".
|
|
func TestProviderTableDefaultsToOff(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
if _, err := db.Exec("INSERT INTO provider_settings (provider, updated_at) VALUES ('lastfm', 1)"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, found, err := NewProviderRepo(db).Get(context.Background(), providers.LastFM)
|
|
if err != nil || !found || got.Enabled || got.APIKey != "" || got.Contact != "" {
|
|
t.Fatalf("defaults: %+v found=%v err=%v", got, found, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestProviderRepoKeepsTheAddressAndTheLidarrOptions(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewProviderRepo(db)
|
|
at := time.Unix(1_700_000_000, 0)
|
|
want := providers.Settings{
|
|
Provider: providers.Lidarr, Enabled: true, APIKey: "0123456789abcdef0123456789abcdef", URL: "http://orion:8686/lidarr",
|
|
Lidarr: providers.LidarrOptions{QualityProfileID: 2, MetadataProfileID: 1, RootFolderPath: "/música/New", Monitor: "latest", SearchMissing: false},
|
|
UpdatedAt: at,
|
|
}
|
|
if err := repo.Save(ctx, want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, found, err := repo.Get(ctx, providers.Lidarr)
|
|
if err != nil || !found || got.URL != want.URL || got.Lidarr != want.Lidarr {
|
|
t.Fatalf("got %+v found=%v err=%v, want %+v", got, found, err, want)
|
|
}
|
|
want.Lidarr.SearchMissing = true
|
|
want.URL = ""
|
|
if err := repo.Save(ctx, want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _, _ = repo.Get(ctx, providers.Lidarr); got.URL != "" || !got.Lidarr.SearchMissing {
|
|
t.Errorf("after the second save: %+v", got)
|
|
}
|
|
// A provider that never had them reads them as empty.
|
|
if err := repo.Save(ctx, providers.Settings{Provider: providers.LastFM, UpdatedAt: at}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _, _ := repo.Get(ctx, providers.LastFM); got.URL != "" || got.Lidarr != (providers.LidarrOptions{}) {
|
|
t.Errorf("last.fm = %+v", got)
|
|
}
|
|
})
|
|
}
|