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
- 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>
146 lines
4.5 KiB
Go
146 lines
4.5 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/Fabled-Sword/minstrel/internal/mailer"
|
|
"github.com/Fabled-Sword/minstrel/internal/netsettings"
|
|
)
|
|
|
|
// withPublicURL gives h a network-settings service holding url, restoring an
|
|
// empty value afterwards so other tests see the default.
|
|
func withPublicURL(t *testing.T, h *handlers, pool *pgxpool.Pool, url string) {
|
|
t.Helper()
|
|
ns, err := netsettings.New(context.Background(), pool, nil)
|
|
if err != nil {
|
|
t.Fatalf("netsettings: %v", err)
|
|
}
|
|
if err := ns.SetPublicURL(context.Background(), url); err != nil {
|
|
t.Fatalf("set public url: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = ns.SetPublicURL(context.Background(), "") })
|
|
h.netSettings = ns
|
|
}
|
|
|
|
// With no public URL set, a reset email is not sent at all, and the response
|
|
// is still the same opaque 200.
|
|
func TestForgotPassword_NoPublicURL_SendsNothing(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
fake := &mailer.FakeSender{}
|
|
h.mailer = fake
|
|
withPublicURL(t, h, pool, "")
|
|
|
|
user := seedUser(t, pool, "nourl", "pw", false)
|
|
if _, err := pool.Exec(context.Background(),
|
|
"UPDATE users SET email = 'nourl@example.com' WHERE id = $1", user.ID); err != nil {
|
|
t.Fatalf("seed email: %v", err)
|
|
}
|
|
req := httptest.NewRequest(http.MethodPost, "/api/auth/forgot-password",
|
|
bytes.NewReader([]byte(`{"email":"nourl@example.com"}`)))
|
|
rec := httptest.NewRecorder()
|
|
h.handleForgotPassword(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Errorf("status = %d, want 200", rec.Code)
|
|
}
|
|
if len(fake.Sent) != 0 {
|
|
t.Errorf("sent %d emails with no public URL set, want 0", len(fake.Sent))
|
|
}
|
|
}
|
|
|
|
func TestForgotPassword_KnownEmail_SendsMail(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
fake := &mailer.FakeSender{}
|
|
h.mailer = fake
|
|
withPublicURL(t, h, pool, "https://music.example.com")
|
|
|
|
user := seedUser(t, pool, "forgotuser", "pw", false)
|
|
if _, err := pool.Exec(context.Background(),
|
|
"UPDATE users SET email = 'forgot@example.com' WHERE id = $1", user.ID); err != nil {
|
|
t.Fatalf("seed email: %v", err)
|
|
}
|
|
|
|
body := `{"email":"forgot@example.com"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/auth/forgot-password",
|
|
bytes.NewReader([]byte(body)))
|
|
// A forged Host must not reach the link: it comes from the public URL.
|
|
req.Host = "attacker.example.net"
|
|
rec := httptest.NewRecorder()
|
|
h.handleForgotPassword(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Errorf("status = %d, want 200", rec.Code)
|
|
}
|
|
if len(fake.Sent) != 1 {
|
|
t.Fatalf("Sent len = %d, want 1", len(fake.Sent))
|
|
}
|
|
got := fake.LastSent()
|
|
if got.To != "forgot@example.com" {
|
|
t.Errorf("To = %q, want forgot@example.com", got.To)
|
|
}
|
|
if !strings.Contains(got.TextBody, "https://music.example.com/reset-password/") {
|
|
t.Errorf("reset link not built from the public URL:\n%s", got.TextBody)
|
|
}
|
|
if strings.Contains(got.TextBody+got.HTMLBody, "attacker.example.net") {
|
|
t.Error("the request's Host header reached the emailed link")
|
|
}
|
|
// Token row was inserted.
|
|
var tokenCount int
|
|
if err := pool.QueryRow(context.Background(),
|
|
"SELECT count(*) FROM password_resets WHERE user_id = $1", user.ID).Scan(&tokenCount); err != nil {
|
|
t.Fatalf("verify token: %v", err)
|
|
}
|
|
if tokenCount == 0 {
|
|
t.Errorf("no password_resets row inserted")
|
|
}
|
|
}
|
|
|
|
func TestForgotPassword_UnknownEmail_Returns200_NoSend(t *testing.T) {
|
|
h, _ := testHandlers(t)
|
|
fake := &mailer.FakeSender{}
|
|
h.mailer = fake
|
|
|
|
body := `{"email":"nonexistent@example.com"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/auth/forgot-password",
|
|
bytes.NewReader([]byte(body)))
|
|
rec := httptest.NewRecorder()
|
|
h.handleForgotPassword(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Errorf("status = %d, want 200 (always; no enumeration)", rec.Code)
|
|
}
|
|
if len(fake.Sent) != 0 {
|
|
t.Errorf("Sent len = %d, want 0 for unknown email", len(fake.Sent))
|
|
}
|
|
}
|
|
|
|
func TestForgotPassword_MailerFailure_StillReturns200(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
fake := &mailer.FakeSender{FailNext: errors.New("simulated SMTP failure")}
|
|
h.mailer = fake
|
|
|
|
user := seedUser(t, pool, "mailfail", "pw", false)
|
|
if _, err := pool.Exec(context.Background(),
|
|
"UPDATE users SET email = 'mailfail@example.com' WHERE id = $1", user.ID); err != nil {
|
|
t.Fatalf("seed: %v", err)
|
|
}
|
|
|
|
body := `{"email":"mailfail@example.com"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/auth/forgot-password",
|
|
bytes.NewReader([]byte(body)))
|
|
rec := httptest.NewRecorder()
|
|
h.handleForgotPassword(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Errorf("status = %d, want 200 (mailer failures don't propagate)", rec.Code)
|
|
}
|
|
}
|