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 }