Files
minstrel/internal/api/me_password_test.go
T
bvandeusenandClaude Opus 5.5 3fd3c1f7c6
release / web (push) Successful in 1m34s
release / go (push) Successful in 2m0s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m24s
release / android (push) Successful in 4m46s
release / Build signed APK (push) Successful in 5m12s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
test(api): merge repeated cases into tables, drop skips dbtest already makes (#8295)
- The like/unlike story runs as one sequence that checks the rows after each step.
- The invite-only sign-up cases are one test, which now also checks that a
  refused sign-up creates no account.
- Demoting an admin, and stopping at the last one, is one test.
- A wrong password and an unknown user are one test, which now also checks
  that the two refusals are identical.
- The cover-path resolver tests repeated the cover handler's tests branch
  for branch; the handler tests stay.
- The Range-from-start stream test only re-checked http.ServeContent.
- The APK's nil-user 401 is covered by the mounted-route walk.
- 68 copies of the MINSTREL_TEST_DATABASE_URL skip go: every one of those
  tests reaches dbtest.Pool, which already skips.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 10:35:48 -05:00

101 lines
3.3 KiB
Go

package api
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"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) {
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) {
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) {
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) {
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)
}