Files
minstrel/internal/api/me_password_test.go
T
bvandeusenandClaude Opus 5.5 c6a9535cf7 fix(auth): a password reset signs out Subsonic apps too, and a change can (#8107)
A reset ended every session but left the API key and Subsonic password
working, and anyone signed in to the account can make either. So whoever had
the account kept it through a Subsonic app after the owner reset the
password. The forgotten-password reset and an admin's reset now replace the
API key with one nobody holds and clear the Subsonic password.

A self-service change keeps Subsonic apps signed in by default, since it
would sign out the owner's own apps too. The web and Android password forms
gain "Sign out Subsonic apps too", sent as sign_out_subsonic.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 23:48:21 -05:00

114 lines
3.7 KiB
Go

package api
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/go-chi/chi/v5"
"golang.org/x/crypto/bcrypt"
)
func newMePasswordRouter(h *handlers) chi.Router {
r := chi.NewRouter()
r.Put("/api/me/password", h.handleChangePassword)
return r
}
func TestChangePassword_Happy(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
user := seedUser(t, pool, "chpw1", "oldpassword1", false)
body := `{"current_password":"oldpassword1","new_password":"newpassword1"}`
req := httptest.NewRequest(http.MethodPut, "/api/me/password", bytes.NewReader([]byte(body)))
req = withUser(req, user)
rec := httptest.NewRecorder()
newMePasswordRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusNoContent {
t.Fatalf("status = %d, want 204; body=%s", rec.Code, rec.Body.String())
}
// Verify DB row was updated — new password should match.
var hash string
if err := pool.QueryRow(context.Background(),
"SELECT password_hash FROM users WHERE id = $1", user.ID).Scan(&hash); err != nil {
t.Fatalf("read hash: %v", err)
}
if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte("newpassword1")); err != nil {
t.Errorf("new password does not match DB hash: %v", err)
}
}
func TestChangePassword_WrongCurrent_401(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
user := seedUser(t, pool, "chpw2", "correctpw1", false)
body := `{"current_password":"wrongpassword","new_password":"newpassword1"}`
req := httptest.NewRequest(http.MethodPut, "/api/me/password", bytes.NewReader([]byte(body)))
req = withUser(req, user)
rec := httptest.NewRecorder()
newMePasswordRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusUnauthorized {
t.Errorf("status = %d, want 401 (wrong_password)", rec.Code)
}
}
func TestChangePassword_TooShort_400(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
user := seedUser(t, pool, "chpw3", "somepassword1", false)
body := `{"current_password":"somepassword1","new_password":"short"}`
req := httptest.NewRequest(http.MethodPut, "/api/me/password", bytes.NewReader([]byte(body)))
req = withUser(req, user)
rec := httptest.NewRecorder()
newMePasswordRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("status = %d, want 400 (password_too_short)", rec.Code)
}
}
// A change keeps Subsonic apps signed in unless the person asks for them to
// be signed out as well.
func TestChangePassword_SignsOutSubsonicAppsOnlyWhenAsked(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
user := seedUser(t, pool, "chpwsub", "oldpassword1", false)
giveSubsonicPassword(t, h, user)
before, _ := subsonicCredentials(t, h, user)
change := func(body string) {
t.Helper()
req := httptest.NewRequest(http.MethodPut, "/api/me/password", bytes.NewReader([]byte(body)))
rec := httptest.NewRecorder()
newMePasswordRouter(h).ServeHTTP(rec, withUser(req, user))
if rec.Code != http.StatusNoContent {
t.Fatalf("status = %d, want 204; body=%s", rec.Code, rec.Body.String())
}
}
change(`{"current_password":"oldpassword1","new_password":"newpassword1"}`)
if after, hasPassword := subsonicCredentials(t, h, user); after != before || !hasPassword {
t.Fatal("a plain password change signed out Subsonic apps")
}
change(`{"current_password":"newpassword1","new_password":"newpassword2","sign_out_subsonic":true}`)
assertSubsonicSignedOut(t, h, user, before)
}