mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-06-17 15:05:39 +02:00
harden deezer auth errors and mixed playlist preflight
This commit is contained in:
@@ -42,29 +42,32 @@ func TestSearchTrack(t *testing.T) {
|
||||
func TestGetMetadataArtistPaginatesAlbums(t *testing.T) {
|
||||
callCount := 0
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/artist/9/albums" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
callCount++
|
||||
index := r.URL.Query().Get("index")
|
||||
limit := r.URL.Query().Get("limit")
|
||||
if limit != "100" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{"message": "bad limit"}})
|
||||
return
|
||||
}
|
||||
switch index {
|
||||
case "0":
|
||||
items := make([]any, 0, 100)
|
||||
for i := 0; i < 100; i++ {
|
||||
items = append(items, map[string]any{"id": i + 1, "title": "Album"})
|
||||
switch r.URL.Path {
|
||||
case "/artist/9":
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"id": 9, "name": "Lost Frequencies"})
|
||||
case "/artist/9/albums":
|
||||
callCount++
|
||||
index := r.URL.Query().Get("index")
|
||||
limit := r.URL.Query().Get("limit")
|
||||
if limit != "100" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{"message": "bad limit"}})
|
||||
return
|
||||
}
|
||||
switch index {
|
||||
case "0":
|
||||
items := make([]any, 0, 100)
|
||||
for i := 0; i < 100; i++ {
|
||||
items = append(items, map[string]any{"id": i + 1, "title": "Album"})
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"data": items, "total": 101})
|
||||
case "100":
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"data": []any{map[string]any{"id": 101, "title": "Album 101"}}, "total": 101})
|
||||
default:
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"data": items, "total": 101})
|
||||
case "100":
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"data": []any{map[string]any{"id": 101, "title": "Album 101"}}, "total": 101})
|
||||
default:
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
defer ts.Close()
|
||||
@@ -86,6 +89,9 @@ func TestGetMetadataArtistPaginatesAlbums(t *testing.T) {
|
||||
if len(items) != 101 {
|
||||
t.Fatalf("albums len = %d, want 101", len(items))
|
||||
}
|
||||
if got := strings.TrimSpace(stringFromAny(meta["name"])); got != "Lost Frequencies" {
|
||||
t.Fatalf("artist name = %q, want Lost Frequencies", got)
|
||||
}
|
||||
if callCount != 2 {
|
||||
t.Fatalf("call count = %d, want 2", callCount)
|
||||
}
|
||||
@@ -333,3 +339,44 @@ func TestLoginWithRefreshToken(t *testing.T) {
|
||||
t.Fatalf("session refresh token = %q", c.cfg.Session.Deezer.RefreshToken)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefreshJWTHTTPError(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{"message": "bad refresh"}})
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
cfgData := config.DefaultConfigData()
|
||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||
c.refresh = "refresh-token"
|
||||
|
||||
origAuth := authURL
|
||||
authURL = ts.URL
|
||||
defer func() { authURL = origAuth }()
|
||||
|
||||
err := c.refreshJWT(context.Background())
|
||||
if err == nil || !strings.Contains(strings.ToLower(err.Error()), "status=401") {
|
||||
t.Fatalf("expected http status error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefreshLicenseFromPipeGraphQLError(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"errors": []any{map[string]any{"message": "token expired"}}})
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
cfgData := config.DefaultConfigData()
|
||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||
c.jwt = "jwt-token"
|
||||
|
||||
origPipe := pipeURL
|
||||
pipeURL = ts.URL
|
||||
defer func() { pipeURL = origPipe }()
|
||||
|
||||
err := c.refreshLicenseFromPipe(context.Background())
|
||||
if err == nil || !strings.Contains(strings.ToLower(err.Error()), "token expired") {
|
||||
t.Fatalf("expected graphql error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user