Files
minstrel/internal/api/auth_reset_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

154 lines
4.9 KiB
Go

package api
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/jackc/pgx/v5/pgtype"
"golang.org/x/crypto/bcrypt"
)
func TestResetPassword_Happy(t *testing.T) {
h, pool := testHandlers(t)
user := seedUser(t, pool, "resetuser", "oldpw1234", false)
// Insert a fresh token.
token := "test-reset-token-abc"
if _, err := pool.Exec(context.Background(),
`INSERT INTO password_resets (token, user_id, expires_at)
VALUES ($1, $2, $3)`, token, user.ID, time.Now().Add(time.Hour),
); err != nil {
t.Fatalf("seed: %v", err)
}
body := `{"token":"test-reset-token-abc","new_password":"newpassword1"}`
req := httptest.NewRequest(http.MethodPost, "/api/auth/reset-password",
bytes.NewReader([]byte(body)))
rec := httptest.NewRecorder()
h.handleResetPassword(rec, req)
if rec.Code != http.StatusNoContent {
t.Fatalf("status = %d, want 204; body=%s", rec.Code, rec.Body.String())
}
// New password works?
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 doesn't match: %v", err)
}
// Token marked used?
var usedAt pgtype.Timestamptz
if err := pool.QueryRow(context.Background(),
"SELECT used_at FROM password_resets WHERE token = $1", token).Scan(&usedAt); err != nil {
t.Fatalf("verify used_at: %v", err)
}
if !usedAt.Valid {
t.Errorf("used_at is NULL after reset; should be set")
}
}
func TestResetPassword_AlreadyUsed_400(t *testing.T) {
h, pool := testHandlers(t)
user := seedUser(t, pool, "alreadyused", "pw", false)
token := "test-already-used"
if _, err := pool.Exec(context.Background(),
`INSERT INTO password_resets (token, user_id, expires_at, used_at)
VALUES ($1, $2, $3, now())`, token, user.ID, time.Now().Add(time.Hour),
); err != nil {
t.Fatalf("seed: %v", err)
}
body := `{"token":"test-already-used","new_password":"abcd1234"}`
req := httptest.NewRequest(http.MethodPost, "/api/auth/reset-password",
bytes.NewReader([]byte(body)))
rec := httptest.NewRecorder()
h.handleResetPassword(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("status = %d, want 400 (invalid_token for already-used)", rec.Code)
}
}
func TestResetPassword_Expired_400(t *testing.T) {
h, pool := testHandlers(t)
user := seedUser(t, pool, "expiredreset", "pw", false)
token := "test-expired-token"
if _, err := pool.Exec(context.Background(),
`INSERT INTO password_resets (token, user_id, expires_at)
VALUES ($1, $2, $3)`, token, user.ID, time.Now().Add(-time.Hour), // already expired
); err != nil {
t.Fatalf("seed: %v", err)
}
body := `{"token":"test-expired-token","new_password":"abcd1234"}`
req := httptest.NewRequest(http.MethodPost, "/api/auth/reset-password",
bytes.NewReader([]byte(body)))
rec := httptest.NewRecorder()
h.handleResetPassword(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("status = %d, want 400 (invalid_token for expired)", rec.Code)
}
}
func TestResetPassword_PasswordTooShort_400(t *testing.T) {
h, _ := testHandlers(t)
body := `{"token":"any-token","new_password":"abc"}`
req := httptest.NewRequest(http.MethodPost, "/api/auth/reset-password",
bytes.NewReader([]byte(body)))
rec := httptest.NewRecorder()
h.handleResetPassword(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("status = %d, want 400", rec.Code)
}
}
func TestResetPassword_BogusToken_400(t *testing.T) {
h, _ := testHandlers(t)
body := `{"token":"bogus-token","new_password":"abcd1234"}`
req := httptest.NewRequest(http.MethodPost, "/api/auth/reset-password",
bytes.NewReader([]byte(body)))
rec := httptest.NewRecorder()
h.handleResetPassword(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("status = %d, want 400 (invalid_token for bogus)", rec.Code)
}
}
// A reset ends Subsonic apps' access too: they sign in with the API key or
// the Subsonic password, which whoever had the account could have made.
func TestResetPassword_SignsOutSubsonicApps(t *testing.T) {
h, pool := testHandlers(t)
user := seedUser(t, pool, "resetsubsonic", "oldpw1234", false)
giveSubsonicPassword(t, h, user)
before, _ := subsonicCredentials(t, h, user)
token := "test-reset-token-subsonic"
if _, err := pool.Exec(context.Background(),
`INSERT INTO password_resets (token, user_id, expires_at)
VALUES ($1, $2, $3)`, token, user.ID, time.Now().Add(time.Hour),
); err != nil {
t.Fatalf("seed: %v", err)
}
body := `{"token":"` + token + `","new_password":"newpassword1"}`
rec := httptest.NewRecorder()
h.handleResetPassword(rec, httptest.NewRequest(http.MethodPost, "/api/auth/reset-password", bytes.NewReader([]byte(body))))
if rec.Code != http.StatusNoContent {
t.Fatalf("status = %d, want 204; body=%s", rec.Code, rec.Body.String())
}
assertSubsonicSignedOut(t, h, user, before)
}