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

125 lines
3.4 KiB
Go

package api
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-chi/chi/v5"
"github.com/Fabled-Sword/minstrel/internal/auth"
)
func newAdminSMTPRouter(h *handlers) chi.Router {
r := chi.NewRouter()
r.Route("/api/admin", func(admin chi.Router) {
admin.Use(auth.RequireAdmin())
admin.Get("/smtp-config", h.handleGetSMTPConfig)
admin.Put("/smtp-config", h.handleUpdateSMTPConfig)
admin.Post("/smtp-config/test", h.handleTestSMTPConfig)
})
return r
}
func TestSMTPConfig_GetMasksPassword(t *testing.T) {
h, pool := testHandlers(t)
admin := seedUser(t, pool, "smtpadmin", "pw", true)
// Seed a non-empty password.
if _, err := pool.Exec(context.Background(),
"UPDATE smtp_config SET password = 'secret' WHERE id = true"); err != nil {
t.Fatalf("seed: %v", err)
}
req := httptest.NewRequest(http.MethodGet, "/api/admin/smtp-config", nil)
req = withUser(req, admin)
rec := httptest.NewRecorder()
newAdminSMTPRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rec.Code)
}
var resp smtpConfigResp
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if resp.Password != "***" {
t.Errorf("password = %q, want \"***\"", resp.Password)
}
}
func TestSMTPConfig_UpdatePreservesPasswordWhenEmpty(t *testing.T) {
h, pool := testHandlers(t)
admin := seedUser(t, pool, "smtpupd", "pw", true)
// Seed a known password.
if _, err := pool.Exec(context.Background(),
"UPDATE smtp_config SET password = 'original' WHERE id = true"); err != nil {
t.Fatalf("seed: %v", err)
}
body := `{
"enabled": true,
"host": "smtp.example.com",
"port": 587,
"username": "u",
"password": "",
"from_address": "from@example.com",
"from_name": "T",
"use_tls": true
}`
req := httptest.NewRequest(http.MethodPut, "/api/admin/smtp-config",
bytes.NewReader([]byte(body)))
req = withUser(req, admin)
rec := httptest.NewRecorder()
newAdminSMTPRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusNoContent {
t.Fatalf("status = %d, want 204; body=%s", rec.Code, rec.Body.String())
}
// Password preserved?
var pass string
if err := pool.QueryRow(context.Background(),
"SELECT password FROM smtp_config WHERE id = true").Scan(&pass); err != nil {
t.Fatalf("verify: %v", err)
}
if pass != "original" {
t.Errorf("password = %q, want \"original\" (preserved when empty in request)", pass)
}
}
func TestSMTPConfig_UpdateValidatesEnabled(t *testing.T) {
h, pool := testHandlers(t)
admin := seedUser(t, pool, "smtpvalid", "pw", true)
body := `{"enabled": true, "host": "", "from_address": "", "port": 587, "use_tls": true}`
req := httptest.NewRequest(http.MethodPut, "/api/admin/smtp-config",
bytes.NewReader([]byte(body)))
req = withUser(req, admin)
rec := httptest.NewRecorder()
newAdminSMTPRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("status = %d, want 400 (missing host + from_address with enabled=true)", rec.Code)
}
}
func TestSMTPConfig_TestRequiresEmail(t *testing.T) {
h, pool := testHandlers(t)
// Admin without email on file.
admin := seedUser(t, pool, "noemail", "pw", true)
req := httptest.NewRequest(http.MethodPost, "/api/admin/smtp-config/test", nil)
req = withUser(req, admin)
rec := httptest.NewRecorder()
newAdminSMTPRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("status = %d, want 400 (no email on file)", rec.Code)
}
}