package httpapi import ( "context" "encoding/json" "errors" "net/http" "strconv" "strings" "testing" "github.com/uptrace/bun" "go.uber.org/zap" "go.uber.org/zap/zaptest/observer" "git.kmalloc.org/squid/sonda/internal/providers" ) const lastfmKey = "0123456789abcdef0123456789abcdef" func TestProviderRoutesAreAdminOnly(t *testing.T) { h := newTestAPI(t) admin := registerAndLogin(t, h) _, bob := addUser(t, h, admin, "bob", false) for _, c := range []struct{ method, path string }{ {"GET", "/api/v1/providers"}, {"PUT", "/api/v1/providers/lastfm"}, } { if r := do(t, h, c.method, c.path, "", nil); r.Code != http.StatusUnauthorized { t.Errorf("%s %s without a token: %d", c.method, c.path, r.Code) } r := do(t, h, c.method, c.path, bob, map[string]any{"enabled": true}) if r.Code != http.StatusForbidden || errCode(r) != "admin_required" { t.Errorf("%s %s as a normal user: %d %v", c.method, c.path, r.Code, r.Body) } } } func TestProvidersStartOffAndSayWhatTheySend(t *testing.T) { h := newTestAPI(t) admin := registerAndLogin(t, h) r := do(t, h, "GET", "/api/v1/providers", admin, nil) if r.Code != http.StatusOK { t.Fatalf("list: %d %v", r.Code, r.Body) } list := r.Body["providers"].([]any) if len(list) != 7 { t.Fatalf("providers = %v", list) } for _, it := range list { p := it.(map[string]any) keys := []string{"name", "enabled", "needs_api_key", "needs_url", "has_api_key", "contact", "url", "sends", "notes", "updated_at"} if p["name"] == "lidarr" { keys = append(keys, "lidarr") } sameKeys(t, "provider", p, keys...) if p["enabled"] != false || p["has_api_key"] != false { t.Errorf("%v: a provider must start off and without a key", p["name"]) } if v, present := p["updated_at"]; !present || v != nil { t.Errorf("%v: updated_at = %#v, want an explicit null before any change", p["name"], v) } if !strings.Contains(p["sends"].(string), "Never") { t.Errorf("%v: sends = %q", p["name"], p["sends"]) } } if list[0].(map[string]any)["name"] != "lastfm" || list[0].(map[string]any)["needs_api_key"] != true || list[1].(map[string]any)["name"] != "musicbrainz" || list[1].(map[string]any)["needs_api_key"] != false { t.Errorf("order or needs_api_key wrong: %v", list) } } func TestSettingAProviderUp(t *testing.T) { h := newTestAPI(t) admin := registerAndLogin(t, h) // Enabling before there is a key is refused. r := do(t, h, "PUT", "/api/v1/providers/lastfm", admin, map[string]any{"enabled": true}) if r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { t.Fatalf("enable without a key: %d %v", r.Code, r.Body) } // The key is written, not read back. r = do(t, h, "PUT", "/api/v1/providers/lastfm", admin, map[string]any{"api_key": lastfmKey}) if r.Code != http.StatusOK || r.Body["has_api_key"] != true || r.Body["enabled"] != false { t.Fatalf("save the key: %d %v", r.Code, r.Body) } if r.Body["updated_at"] == nil { t.Error("updated_at should be set after a change") } raw, _ := json.Marshal(r.Body) if strings.Contains(string(raw), lastfmKey) { t.Fatal("the response contains the API key") } r = do(t, h, "PUT", "/api/v1/providers/lastfm", admin, map[string]any{"enabled": true}) if r.Code != http.StatusOK || r.Body["enabled"] != true { t.Fatalf("enable: %d %v", r.Code, r.Body) } listed := do(t, h, "GET", "/api/v1/providers", admin, nil) rawList, _ := json.Marshal(listed.Body) if strings.Contains(string(rawList), lastfmKey) { t.Fatal("the list contains the API key") } first := listed.Body["providers"].([]any)[0].(map[string]any) if first["enabled"] != true || first["has_api_key"] != true { t.Errorf("listed = %v", first) } // Clearing the key turns the provider off. r = do(t, h, "PUT", "/api/v1/providers/lastfm", admin, map[string]any{"api_key": ""}) if r.Code != http.StatusOK || r.Body["has_api_key"] != false || r.Body["enabled"] != false { t.Fatalf("clear the key: %d %v", r.Code, r.Body) } } func TestMusicBrainzTakesAContactAndNoKey(t *testing.T) { h := newTestAPI(t) admin := registerAndLogin(t, h) r := do(t, h, "PUT", "/api/v1/providers/musicbrainz", admin, map[string]any{"enabled": true, "contact": "https://example.org/me"}) if r.Code != http.StatusOK || r.Body["enabled"] != true || r.Body["contact"] != "https://example.org/me" { t.Fatalf("%d %v", r.Code, r.Body) } r = do(t, h, "PUT", "/api/v1/providers/musicbrainz", admin, map[string]any{"api_key": lastfmKey}) if r.Code != http.StatusBadRequest { t.Errorf("a key for a provider without keys: %d", r.Code) } } func TestProviderUpdateRejectsBadInputWithoutEchoingTheKey(t *testing.T) { h := newTestAPI(t) admin := registerAndLogin(t, h) badKey := "not-a-valid-key-but-secret-looking-12345" for name, tt := range map[string]struct { path string body any code int err string }{ "empty body": {"/api/v1/providers/lastfm", map[string]any{}, 400, "invalid_input"}, "malformed json": {"/api/v1/providers/lastfm", "{oops", 400, "invalid_input"}, "bad key format": {"/api/v1/providers/lastfm", map[string]any{"api_key": badKey}, 400, "invalid_input"}, "unknown": {"/api/v1/providers/spotify", map[string]any{"enabled": true}, 404, "provider_not_found"}, "contact with ()": {"/api/v1/providers/musicbrainz", map[string]any{"contact": "a (b)"}, 400, "invalid_input"}, } { r := do(t, h, "PUT", tt.path, admin, tt.body) if r.Code != tt.code || errCode(r) != tt.err { t.Errorf("%s: %d %v, want %d %s", name, r.Code, r.Body, tt.code, tt.err) } raw, _ := json.Marshal(r.Body) if strings.Contains(string(raw), badKey) { t.Errorf("%s: the error repeats the key", name) } } } // The key is a secret: it must not appear in the request log either. func TestTheKeyNeverReachesTheLog(t *testing.T) { core, logs := observer.New(zap.DebugLevel) h, _ := newTestEnvDB(t, func(d *Deps, _ *bun.DB) { d.Logger = zap.New(core) }) admin := registerAndLogin(t, h) do(t, h, "PUT", "/api/v1/providers/lastfm", admin, map[string]any{"api_key": lastfmKey}) do(t, h, "PUT", "/api/v1/providers/lastfm", admin, map[string]any{"api_key": "bad" + lastfmKey}) if logs.Len() == 0 { t.Fatal("the test saw no log entries, so it proves nothing") } for _, e := range logs.All() { dump, _ := json.Marshal(map[string]any{"message": e.Message, "fields": e.ContextMap()}) if strings.Contains(string(dump), lastfmKey) { t.Fatalf("a log entry contains the key: %s", dump) } } } // ---- testing a provider ---- func TestProviderTestIsAdminOnly(t *testing.T) { h := newTestAPI(t) admin := registerAndLogin(t, h) _, bob := addUser(t, h, admin, "bob", false) if r := do(t, h, "POST", "/api/v1/providers/lastfm/test", "", nil); r.Code != http.StatusUnauthorized { t.Errorf("no token: %d", r.Code) } if r := do(t, h, "POST", "/api/v1/providers/lastfm/test", bob, nil); r.Code != http.StatusForbidden || errCode(r) != "admin_required" { t.Errorf("normal user: %d %v", r.Code, r.Body) } } func TestProviderTest(t *testing.T) { var asked string var answer error h, _ := newTestEnvDB(t, func(d *Deps, db *bun.DB) { prov := newProviders(db) prov.SetChecker(providers.LastFM, func(_ context.Context, key string) error { asked = key; return answer }) d.Providers = prov }) admin := registerAndLogin(t, h) // Without a key nobody is asked. r := do(t, h, "POST", "/api/v1/providers/lastfm/test", admin, nil) if r.Code != http.StatusOK || r.Body["ok"] != false || r.Body["status"] != "no_api_key" || asked != "" { t.Fatalf("no key: %d %v asked=%q", r.Code, r.Body, asked) } sameKeys(t, "test result", r.Body, "ok", "status") do(t, h, "PUT", "/api/v1/providers/lastfm", admin, map[string]any{"api_key": lastfmKey}) r = do(t, h, "POST", "/api/v1/providers/lastfm/test", admin, nil) if r.Code != http.StatusOK || r.Body["ok"] != true || r.Body["status"] != "ok" || asked != lastfmKey { t.Fatalf("working key: %d %v", r.Code, r.Body) } for want, err := range map[string]error{ "key_refused": providers.ErrKeyRefused, "rate_limited": providers.ErrRateLimited, "unreachable": providers.ErrUnreachable, "error": errors.New("boom " + lastfmKey), } { answer = err r = do(t, h, "POST", "/api/v1/providers/lastfm/test", admin, nil) raw, _ := json.Marshal(r.Body) if r.Code != http.StatusOK || r.Body["ok"] != false || r.Body["status"] != want || strings.Contains(string(raw), lastfmKey) { t.Errorf("%s: %d %v", want, r.Code, r.Body) } } if r = do(t, h, "POST", "/api/v1/providers/spotify/test", admin, nil); r.Code != http.StatusNotFound || errCode(r) != "provider_not_found" { t.Errorf("unknown provider: %d %v", r.Code, r.Body) } if r = do(t, h, "POST", "/api/v1/providers/musicbrainz/test", admin, nil); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { t.Errorf("a provider without a test: %d %v", r.Code, r.Body) } } // ---- taking back a provider's data ---- func TestPurgeIsAdminOnly(t *testing.T) { h := newTestAPI(t) admin := registerAndLogin(t, h) _, bob := addUser(t, h, admin, "bob", false) if r := do(t, h, "DELETE", "/api/v1/providers/lastfm/data", "", nil); r.Code != http.StatusUnauthorized { t.Errorf("no token: %d", r.Code) } if r := do(t, h, "DELETE", "/api/v1/providers/lastfm/data", bob, nil); r.Code != http.StatusForbidden || errCode(r) != "admin_required" { t.Errorf("normal user: %d %v", r.Code, r.Body) } if r := do(t, h, "DELETE", "/api/v1/providers/spotify/data", admin, nil); r.Code != http.StatusNotFound || errCode(r) != "provider_not_found" { t.Errorf("unknown provider: %d %v", r.Code, r.Body) } } func TestPurgeRemovesWhatTheProviderGaveAndNothingOfMine(t *testing.T) { e := newSimilarEnv(t) e.enableLastfm(t) r := do(t, e.h, "GET", e.path(e.alpha), e.admin, nil) // names Beta (mine) and Gamma (discovered) gamma := int64(r.Body["items"].([]any)[1].(map[string]any)["artist"].(map[string]any)["id"].(float64)) if a := do(t, e.h, "GET", "/api/v1/artists/"+strconv.FormatInt(gamma, 10), e.admin, nil); a.Code != http.StatusOK { t.Fatalf("the discovered artist is not there to begin with: %d", a.Code) } p := do(t, e.h, "DELETE", "/api/v1/providers/lastfm/data", e.admin, nil) if p.Code != http.StatusOK { t.Fatalf("%d %v", p.Code, p.Body) } sameKeys(t, "purge", p.Body, "similar_artists", "tags", "descriptions", "images", "artists") if p.Body["similar_artists"] != float64(2) || p.Body["tags"] != float64(0) || p.Body["artists"] != float64(1) { t.Errorf("purge = %v, want 2 similar artists, no tags and the one artist Gamma", p.Body) } raw, _ := json.Marshal(p.Body) if strings.Contains(string(raw), lastfmKey) { t.Fatal("the answer contains the API key") } if a := do(t, e.h, "GET", "/api/v1/artists/"+strconv.FormatInt(gamma, 10), e.admin, nil); a.Code != http.StatusNotFound { t.Errorf("the discovered artist survived: %d", a.Code) } for _, id := range []int64{e.alpha, e.beta} { if a := do(t, e.h, "GET", "/api/v1/artists/"+strconv.FormatInt(id, 10), e.admin, nil); a.Code != http.StatusOK { t.Errorf("one of my artists was touched: %d", a.Code) } } // The provider is as it was: still on, still with its key. list := do(t, e.h, "GET", "/api/v1/providers", e.admin, nil).Body["providers"].([]any)[0].(map[string]any) if list["enabled"] != true || list["has_api_key"] != true { t.Errorf("the purge changed the provider: %v", list) } // And the next look asks again, since nothing is stored. calls := e.source.count() again := do(t, e.h, "GET", e.path(e.alpha), e.admin, nil) if again.Code != http.StatusOK || e.source.count() != calls+1 { t.Errorf("after the purge: %d, calls %d -> %d", again.Code, calls, e.source.count()) } }