sonda/internal/providers/lidarr_settings_test.go
tgiachi 4dbe1074ba fix(lidarr): saved defaults, conflicts, no proxy, readable addresses
- Setting the address alone now stores the default options (monitor all, search
  for missing albums), so what is shown is what will be sent.
- A 409 from Lidarr, when two adds of the same artist cross, means the artist is
  already there; only "an artist with this ID was not found" is an unknown artist.
- The client never goes through a proxy named by the environment, since Lidarr is
  on the admin's own network and the key travels in a header.
- An address that cannot be read back (an IPv6 zone) is refused instead of being
  stored and reported as "not configured".
- A deadline asking Lidarr is a bad gateway, not an internal error; a redirect is
  reported with a hint to use the final address; the request schema of the Lidarr
  options is closed.
2026-11-12 11:11:35 +01:00

243 lines
9.5 KiB
Go

package providers
import (
"context"
"errors"
"strings"
"testing"
)
const lidarrKey = "0123456789abcdef0123456789abcdef"
func lidarrInfo(t *testing.T, s *Service) Info {
t.Helper()
infos, err := s.List(context.Background())
if err != nil {
t.Fatal(err)
}
for _, in := range infos {
if in.Name == Lidarr {
return in
}
}
t.Fatalf("no lidarr provider in %+v", infos)
return Info{}
}
func TestLidarrIsAProviderThatNeedsAKeyAndAnAddressAndIsOffByDefault(t *testing.T) {
s, _ := newSvc()
in := lidarrInfo(t, s)
if in.Enabled || !in.NeedsAPIKey || !in.NeedsURL || in.HasAPIKey || in.URL != "" {
t.Errorf("lidarr = %+v", in)
}
for _, want := range []string{"MusicBrainz id", "your own", "Never"} {
if !strings.Contains(in.Sends, want) {
t.Errorf("sends does not say %q: %q", want, in.Sends)
}
}
if strings.TrimSpace(in.Notes) == "" {
t.Error("no notes")
}
// Until it is set, the options are the ones a new setup starts from.
if in.Lidarr.Monitor != "all" || !in.Lidarr.SearchMissing || in.Lidarr.QualityProfileID != 0 {
t.Errorf("default options = %+v", in.Lidarr)
}
}
func TestLidarrNeedsTheKeyAndTheAddressBeforeItIsEnabled(t *testing.T) {
s, _ := newSvc()
ctx := context.Background()
on := true
if _, err := s.Update(ctx, Lidarr, Patch{Enabled: &on}); !errors.Is(err, ErrInvalidSettings) {
t.Errorf("enabled with nothing: %v", err)
}
if _, err := s.Update(ctx, Lidarr, Patch{APIKey: strp(lidarrKey)}); err != nil {
t.Fatal(err)
}
if _, err := s.Update(ctx, Lidarr, Patch{Enabled: &on}); !errors.Is(err, ErrInvalidSettings) {
t.Errorf("enabled with a key and no address: %v", err)
}
if _, err := s.Update(ctx, Lidarr, Patch{URL: strp("http://192.168.1.20:8686/")}); err != nil {
t.Fatal(err)
}
if in, err := s.Update(ctx, Lidarr, Patch{Enabled: &on}); err != nil || !in.Enabled || in.URL != "http://192.168.1.20:8686" {
t.Fatalf("enable: %+v err=%v, want it on with the address without the closing slash", in, err)
}
set, active, err := s.Active(ctx, Lidarr)
if err != nil || !active || set.URL != "http://192.168.1.20:8686" || set.APIKey != lidarrKey {
t.Errorf("active = %+v %v %v", set, active, err)
}
// Taking the address away switches it off, as taking away the key does.
if in, err := s.Update(ctx, Lidarr, Patch{URL: strp("")}); err != nil || in.Enabled || in.URL != "" {
t.Errorf("after clearing the address: %+v err=%v", in, err)
}
if _, active, _ := s.Active(ctx, Lidarr); active {
t.Error("active without an address")
}
}
func TestLidarrTakesAnAddressWithAPathAndRefusesAnythingElse(t *testing.T) {
s, _ := newSvc()
ctx := context.Background()
for in, want := range map[string]string{
"http://orion:8686": "http://orion:8686",
" https://lidarr.example.org/ ": "https://lidarr.example.org",
"http://nas.local:8686/lidarr/": "http://nas.local:8686/lidarr",
"HTTP://Orion:8686": "http://Orion:8686",
} {
if got, err := s.Update(ctx, Lidarr, Patch{URL: strp(in)}); err != nil || got.URL != want {
t.Errorf("%q: %q err=%v, want %q", in, got.URL, err, want)
}
}
for _, bad := range []string{
"ftp://orion", "orion:8686", "//orion", "http://", "javascript:alert(1)", "http://user:pass@orion",
"http://orion/?apikey=x", "http://orion/#a", "http://orion/a b", "http://" + strings.Repeat("a", 400),
"file:///etc/passwd", "http://orion\n.evil",
} {
if _, err := s.Update(ctx, Lidarr, Patch{URL: strp(bad)}); !errors.Is(err, ErrInvalidSettings) {
t.Errorf("%q: err = %v, want ErrInvalidSettings", bad, err)
}
}
}
func TestOnlyLidarrHasAnAddressAndOptions(t *testing.T) {
s, _ := newSvc()
ctx := context.Background()
if _, err := s.Update(ctx, MusicBrainz, Patch{URL: strp("http://orion:8686")}); !errors.Is(err, ErrInvalidSettings) {
t.Errorf("an address for MusicBrainz: %v", err)
}
if _, err := s.Update(ctx, LastFM, Patch{Lidarr: &LidarrOptions{Monitor: "all"}}); !errors.Is(err, ErrInvalidSettings) {
t.Errorf("options for Last.fm: %v", err)
}
}
func TestALidarrKeyIs32HexCharacters(t *testing.T) {
s, _ := newSvc()
for _, bad := range []string{"short", strings.Repeat("g", 32), lidarrKey + "0"} {
if _, err := s.Update(context.Background(), Lidarr, Patch{APIKey: strp(bad)}); !errors.Is(err, ErrInvalidSettings) {
t.Errorf("%q: %v", bad, err)
}
}
if in, err := s.Update(context.Background(), Lidarr, Patch{APIKey: strp(strings.ToUpper(lidarrKey))}); err != nil || !in.HasAPIKey {
t.Errorf("an upper case key: %+v %v", in, err)
}
}
func TestLidarrOptionsAreCheckedAndReplacedAsAWhole(t *testing.T) {
s, st := newSvc()
ctx := context.Background()
good := LidarrOptions{QualityProfileID: 2, MetadataProfileID: 1, RootFolderPath: "/music", Monitor: "latest", SearchMissing: false}
in, err := s.Update(ctx, Lidarr, Patch{Lidarr: &good})
if err != nil || in.Lidarr != good {
t.Fatalf("options = %+v err=%v", in.Lidarr, err)
}
if st.rows[Lidarr].Lidarr != good {
t.Errorf("stored = %+v", st.rows[Lidarr].Lidarr)
}
// A patch that does not mention them leaves them alone.
if in, err := s.Update(ctx, Lidarr, Patch{APIKey: strp(lidarrKey)}); err != nil || in.Lidarr != good {
t.Errorf("after another change: %+v err=%v", in.Lidarr, err)
}
for name, bad := range map[string]LidarrOptions{
"a monitor option Lidarr does not have": {Monitor: "everything"},
"no monitor option": {QualityProfileID: 1},
"a negative profile": {Monitor: "all", QualityProfileID: -1},
"a folder with a control character": {Monitor: "all", RootFolderPath: "/mu\nsic"},
"a folder that is too long": {Monitor: "all", RootFolderPath: "/" + strings.Repeat("a", 600)},
} {
if _, err := s.Update(ctx, Lidarr, Patch{Lidarr: &bad}); !errors.Is(err, ErrInvalidSettings) {
t.Errorf("%s: err = %v", name, err)
}
}
for _, m := range []string{"all", "future", "missing", "existing", "latest", "first", "none"} {
o := good
o.Monitor = m
if _, err := s.Update(ctx, Lidarr, Patch{Lidarr: &o}); err != nil {
t.Errorf("monitor %q: %v", m, err)
}
}
}
func TestLidarrIsCompleteOnlyWithProfilesAndAFolder(t *testing.T) {
cases := map[string]struct {
o LidarrOptions
want bool
}{
"all set": {LidarrOptions{QualityProfileID: 1, MetadataProfileID: 1, RootFolderPath: "/m", Monitor: "all"}, true},
"no quality": {LidarrOptions{MetadataProfileID: 1, RootFolderPath: "/m", Monitor: "all"}, false},
"no metadata": {LidarrOptions{QualityProfileID: 1, RootFolderPath: "/m", Monitor: "all"}, false},
"no root folder": {LidarrOptions{QualityProfileID: 1, MetadataProfileID: 1, Monitor: "all"}, false},
}
for name, c := range cases {
if got := c.o.Complete(); got != c.want {
t.Errorf("%s: Complete = %v, want %v", name, got, c.want)
}
}
}
func TestATestOfLidarrSaysWhatIsMissing(t *testing.T) {
s, _ := newSvc()
ctx := context.Background()
called := false
s.SetChecker(Lidarr, func(context.Context, string) error { called = true; return nil })
if res, _ := s.Test(ctx, Lidarr); res.Status != TestNoKey || called {
t.Errorf("no key: %+v called=%v", res, called)
}
_, _ = s.Update(ctx, Lidarr, Patch{APIKey: strp(lidarrKey)})
if res, _ := s.Test(ctx, Lidarr); res.Status != TestNoURL || res.OK || called {
t.Errorf("no address: %+v called=%v", res, called)
}
_, _ = s.Update(ctx, Lidarr, Patch{URL: strp("http://orion:8686")})
if res, _ := s.Test(ctx, Lidarr); !res.OK || !called {
t.Errorf("set up: %+v called=%v", res, called)
}
}
func TestStoredGivesTheSettingsWhetherTheProviderIsOnOrNot(t *testing.T) {
s, _ := newSvc()
ctx := context.Background()
if got, err := s.Stored(ctx, Lidarr); err != nil || got.Provider != "" || got.URL != "" {
t.Errorf("never configured: %+v %v", got, err)
}
_, _ = s.Update(ctx, Lidarr, Patch{APIKey: strp(lidarrKey), URL: strp("http://orion:8686")})
got, err := s.Stored(ctx, Lidarr) // still off
if err != nil || got.APIKey != lidarrKey || got.URL != "http://orion:8686" || got.Enabled {
t.Errorf("stored = %+v err=%v", got, err)
}
if _, err := s.Stored(ctx, "spotify"); !errors.Is(err, ErrUnknownProvider) {
t.Errorf("unknown: %v", err)
}
}
func TestSettingTheAddressAloneStoresTheDefaultOptions(t *testing.T) {
s, st := newSvc()
ctx := context.Background()
if _, err := s.Update(ctx, Lidarr, Patch{APIKey: strp(lidarrKey), URL: strp("http://orion:8686")}); err != nil {
t.Fatal(err)
}
// What the list shows is what is stored, so what Sonda sends to Lidarr is what the admin saw.
if got := st.rows[Lidarr].Lidarr; got.Monitor != "all" || !got.SearchMissing {
t.Errorf("stored = %+v, want monitor all and search_missing true", got)
}
got, err := s.Stored(ctx, Lidarr)
if err != nil || got.Lidarr.Monitor != "all" || !got.Lidarr.SearchMissing {
t.Errorf("Stored = %+v err=%v", got.Lidarr, err)
}
// A choice that is made stays, including "do not search".
off := LidarrOptions{QualityProfileID: 1, MetadataProfileID: 1, RootFolderPath: "/m", Monitor: "none", SearchMissing: false}
_, _ = s.Update(ctx, Lidarr, Patch{Lidarr: &off})
_, _ = s.Update(ctx, Lidarr, Patch{Contact: strp("x")})
if got, _ := s.Stored(ctx, Lidarr); got.Lidarr != off {
t.Errorf("after another change = %+v, want %+v", got.Lidarr, off)
}
}
func TestAnAddressWithAZoneThatCannotBeReadBackIsRefused(t *testing.T) {
s, _ := newSvc()
if _, err := s.Update(context.Background(), Lidarr, Patch{URL: strp("http://[fe80::1%25eth0]:8686")}); !errors.Is(err, ErrInvalidSettings) {
t.Errorf("err = %v", err)
}
if in, err := s.Update(context.Background(), Lidarr, Patch{URL: strp("http://[fd00::5]:8686/")}); err != nil || in.URL != "http://[fd00::5]:8686" {
t.Errorf("an IPv6 address: %+v %v", in, err)
}
}