Files
minstrel/internal/subsonic/user_test.go
T
bvandeusenandClaude Opus 5.5 e1f270362d
release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
chore(release): everything that ships points at GitHub and GHCR (#7897)
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every
  import and the Dockerfile's version ldflags.
- buildinfo.ProjectURL, the contact outside services see in Minstrel's
  User-Agent: https://github.com/Fabled-Sword/minstrel.
- README's compose example pulls ghcr.io/fabled-sword/minstrel:latest.
- docs/security.md sends problem reports to the GitHub issues.
- The demo-library stack builds Minstrel from the checkout, since :dev
  is not published to GHCR.
- hygiene.go's same-site example uses example.com names.

The workbench CI (.gitea/, ci/, export-ignored) still builds and
publishes :dev to the private registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:28:16 -05:00

126 lines
4.0 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"
)
func TestHandleGetUser_Self(t *testing.T) {
user := dbq.User{Username: "alice", IsAdmin: false}
w, body := callGetUser(t, user, "")
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", w.Code)
}
if body.Resp.Status != "ok" {
t.Errorf("status = %q, want ok", body.Resp.Status)
}
if body.Resp.User.Username != "alice" {
t.Errorf("username = %q, want alice", body.Resp.User.Username)
}
if body.Resp.User.AdminRole {
t.Error("non-admin should not have admin role")
}
if !body.Resp.User.StreamRole {
t.Error("stream role must be on so clients enable playback")
}
}
func TestHandleGetUser_AdminGetsAdminRole(t *testing.T) {
admin := dbq.User{Username: "admin", IsAdmin: true}
_, body := callGetUser(t, admin, "")
if !body.Resp.User.AdminRole || !body.Resp.User.SettingsRole {
t.Errorf("admin/settings roles = %v/%v, want true/true",
body.Resp.User.AdminRole, body.Resp.User.SettingsRole)
}
}
func TestHandleGetUser_NonAdminCannotLookupOthers(t *testing.T) {
user := dbq.User{Username: "alice", IsAdmin: false}
_, body := callGetUser(t, user, "bob")
if body.Resp.Status != "failed" || body.Resp.Error == nil ||
body.Resp.Error.Code != ErrNotAuthorized {
t.Errorf("expected ErrNotAuthorized, got status=%q err=%+v",
body.Resp.Status, body.Resp.Error)
}
}
func TestHandleGetUser_AdminLooksUpAnotherUser(t *testing.T) {
admin := dbq.User{Username: "admin", IsAdmin: true}
bob := dbq.User{Username: "bob", IsAdmin: false}
_, body := callGetUser(t, admin, "bob", bob)
if body.Resp.Status != "ok" {
t.Fatalf("status = %q, want ok (err=%+v)", body.Resp.Status, body.Resp.Error)
}
if body.Resp.User.Username != "bob" {
t.Errorf("username = %q, want bob: the answer must describe the user asked about, not the caller", body.Resp.User.Username)
}
if body.Resp.User.AdminRole || body.Resp.User.SettingsRole {
t.Errorf("bob is not an admin, but the answer carries admin/settings roles %v/%v",
body.Resp.User.AdminRole, body.Resp.User.SettingsRole)
}
}
func TestHandleGetUser_AdminLooksUpUnknownUser(t *testing.T) {
admin := dbq.User{Username: "admin", IsAdmin: true}
_, body := callGetUser(t, admin, "nobody")
if body.Resp.Status != "failed" || body.Resp.Error == nil ||
body.Resp.Error.Code != ErrDataNotFound {
t.Errorf("expected ErrDataNotFound, got status=%q err=%+v",
body.Resp.Status, body.Resp.Error)
}
}
func TestHandleGetUser_OwnUsernameNeedsNoLookup(t *testing.T) {
// No known users: a lookup would fail, so success proves none was made.
user := dbq.User{Username: "alice", IsAdmin: false}
_, body := callGetUser(t, user, "alice")
if body.Resp.Status != "ok" || body.Resp.User.Username != "alice" {
t.Errorf("status=%q username=%q, want ok/alice", body.Resp.Status, body.Resp.User.Username)
}
}
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
}