Files
minstrel/internal/subsonic/user_test.go
T
bvandeusenandClaude Opus 5.5 10b10b8179
release / web (push) Successful in 1m35s
release / go (push) Successful in 1m58s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m0s
release / android (push) Successful in 4m26s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Publish the release image to GHCR (dispatch only) (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 5m8s
test(coverart, subsonic, server): merge repeated cases into tables (#8298)
- coverart: the four providers' identity and default tests, Last.fm's five
  not-found-without-asking tests, the four "which artists are asked again"
  database tests (now one reset over four artists), and the thumbnail-only
  and fanart-only tests.
- subsonic: getUser's six tests and decodePassword's three, one table each.
  Dropped TestTokenMD5Derivation: it checked crypto/md5 against a constant
  and called no Minstrel code.
- server: the four web-app-versus-API path tests start one server, not four.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 13:58:07 -05:00

98 lines
3.3 KiB
Go

package subsonic
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/jackc/pgx/v5"
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
)
// getUser describes the caller, or for an admin any user asked about. The
// stream role is always on, since clients disable playback without it; admin
// and settings roles follow the user described, not the caller. A non-admin
// asking about someone else is refused, an unknown name is not found, and
// asking about yourself by name needs no lookup (none of the cases below
// that name the caller supply a known user).
func TestHandleGetUser(t *testing.T) {
alice := dbq.User{Username: "alice"}
admin := dbq.User{Username: "admin", IsAdmin: true}
bob := dbq.User{Username: "bob"}
for _, tc := range []struct {
name string
caller dbq.User
asked string
known []dbq.User
wantErr int // 0 for success
wantUser string
wantAdmin bool
}{
{"yourself", alice, "", nil, 0, "alice", false},
{"yourself by name", alice, "alice", nil, 0, "alice", false},
{"an admin, yourself", admin, "", nil, 0, "admin", true},
{"a non-admin asking about another", alice, "bob", []dbq.User{bob}, ErrNotAuthorized, "", false},
{"an admin asking about another", admin, "bob", []dbq.User{bob}, 0, "bob", false},
{"an admin asking about nobody", admin, "nobody", nil, ErrDataNotFound, "", false},
} {
t.Run(tc.name, func(t *testing.T) {
w, body := callGetUser(t, tc.caller, tc.asked, tc.known...)
if w.Code != http.StatusOK {
t.Fatalf("HTTP status = %d, want 200 (Subsonic errors ride in the body)", w.Code)
}
if tc.wantErr != 0 {
if body.Resp.Status != "failed" || body.Resp.Error == nil || body.Resp.Error.Code != tc.wantErr {
t.Errorf("status %q, error %+v; want error %d", body.Resp.Status, body.Resp.Error, tc.wantErr)
}
return
}
u := body.Resp.User
if body.Resp.Status != "ok" || u.Username != tc.wantUser || !u.StreamRole ||
u.AdminRole != tc.wantAdmin || u.SettingsRole != tc.wantAdmin {
t.Errorf("got status %q, user %+v; want %q, stream role, admin and settings roles %v",
body.Resp.Status, u, tc.wantUser, tc.wantAdmin)
}
})
}
}
type getUserPayload struct {
Resp struct {
Status string `json:"status"`
Error *Error `json:"error"`
User User `json:"user"`
} `json:"subsonic-response"`
}
// callGetUser runs getUser as caller, with the users in known as the only
// accounts a lookup can find.
func callGetUser(t *testing.T, user dbq.User, requestedUsername string, known ...dbq.User) (*httptest.ResponseRecorder, getUserPayload) {
t.Helper()
h := &userHandlers{lookup: func(_ context.Context, username string) (dbq.User, error) {
for _, u := range known {
if u.Username == username {
return u, nil
}
}
return dbq.User{}, pgx.ErrNoRows
}}
url := "/rest/getUser.view?f=json"
if requestedUsername != "" {
url += "&username=" + requestedUsername
}
req := httptest.NewRequest("GET", url, nil)
req = req.WithContext(context.WithValue(req.Context(), userCtxKey, user))
w := httptest.NewRecorder()
h.getUser(w, req)
var body getUserPayload
if err := json.NewDecoder(strings.NewReader(w.Body.String())).Decode(&body); err != nil {
t.Fatalf("decode response: %v\nbody=%s", err, w.Body.String())
}
return w, body
}