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>
248 lines
8.1 KiB
Go
248 lines
8.1 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"github.com/Fabled-Sword/minstrel/internal/auth"
|
|
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
|
|
)
|
|
|
|
func newAdminUsersRouter(h *handlers) chi.Router {
|
|
r := chi.NewRouter()
|
|
r.Route("/api/admin", func(admin chi.Router) {
|
|
admin.Use(auth.RequireAdmin())
|
|
admin.Get("/users", h.handleAdminListUsers)
|
|
admin.Put("/users/{id}/admin", h.handleUpdateUserAdmin)
|
|
// U2 additions:
|
|
admin.Post("/users", h.handleAdminCreateUser)
|
|
admin.Delete("/users/{id}", h.handleAdminDeleteUser)
|
|
admin.Post("/users/{id}/reset-password", h.handleAdminResetPassword)
|
|
})
|
|
return r
|
|
}
|
|
|
|
func TestAdminUsers_List(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
admin := seedUser(t, pool, "listadmin", "pw", true)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/admin/users", nil)
|
|
req = withUser(req, admin)
|
|
rec := httptest.NewRecorder()
|
|
newAdminUsersRouter(h).ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
var resp struct {
|
|
Users []struct{ Username string } `json:"users"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("decode: %v", err)
|
|
}
|
|
if len(resp.Users) == 0 {
|
|
t.Errorf("users list is empty; expected at least the seeded admin")
|
|
}
|
|
}
|
|
|
|
func TestAdminUsers_Promote(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
admin := seedUser(t, pool, "promadmin", "pw", true)
|
|
target := seedUser(t, pool, "promtarget", "pw", false)
|
|
|
|
body := `{"is_admin": true}`
|
|
req := httptest.NewRequest(http.MethodPut, "/api/admin/users/"+uuidToString(target.ID)+"/admin",
|
|
bytes.NewReader([]byte(body)))
|
|
req = withUser(req, admin)
|
|
rec := httptest.NewRecorder()
|
|
newAdminUsersRouter(h).ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
var isAdmin bool
|
|
if err := pool.QueryRow(context.Background(),
|
|
"SELECT is_admin FROM users WHERE id = $1", target.ID).Scan(&isAdmin); err != nil {
|
|
t.Fatalf("verify: %v", err)
|
|
}
|
|
if !isAdmin {
|
|
t.Errorf("target not promoted")
|
|
}
|
|
}
|
|
|
|
func TestAdminCreateUser_Happy(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
admin := seedUser(t, pool, "creator", "pw", true)
|
|
|
|
body := `{"username":"newuser1","password":"abcd1234","is_admin":false}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/admin/users", bytes.NewReader([]byte(body)))
|
|
req = withUser(req, admin)
|
|
rec := httptest.NewRecorder()
|
|
newAdminUsersRouter(h).ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
var resp adminUserView
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("decode: %v", err)
|
|
}
|
|
if resp.Username != "newuser1" || resp.IsAdmin {
|
|
t.Errorf("response unexpected: %+v", resp)
|
|
}
|
|
}
|
|
|
|
func TestAdminCreateUser_DuplicateUsername_409(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
admin := seedUser(t, pool, "duper", "pw", true)
|
|
seedUser(t, pool, "existing", "pw", false)
|
|
|
|
// seedUser prefixes usernames with dbtest.TestUserPrefix, so the
|
|
// row above is "test-existing"; POST that exact name to actually
|
|
// collide (stays test-prefixed for ResetDB).
|
|
body := `{"username":"test-existing","password":"abcd1234"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/admin/users", bytes.NewReader([]byte(body)))
|
|
req = withUser(req, admin)
|
|
rec := httptest.NewRecorder()
|
|
newAdminUsersRouter(h).ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusConflict {
|
|
t.Errorf("status = %d, want 409", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestAdminDeleteUser_Happy(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
admin := seedUser(t, pool, "delcaller", "pw", true)
|
|
target := seedUser(t, pool, "deltarget", "pw", false)
|
|
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/admin/users/"+uuidToString(target.ID), nil)
|
|
req = withUser(req, admin)
|
|
rec := httptest.NewRecorder()
|
|
newAdminUsersRouter(h).ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Errorf("status = %d, want 204", rec.Code)
|
|
}
|
|
var count int
|
|
if err := pool.QueryRow(context.Background(),
|
|
"SELECT count(*) FROM users WHERE id = $1", target.ID).Scan(&count); err != nil {
|
|
t.Fatalf("verify: %v", err)
|
|
}
|
|
if count != 0 {
|
|
t.Errorf("user still exists after delete (count=%d)", count)
|
|
}
|
|
}
|
|
|
|
func TestAdminDeleteUser_LastAdmin_Refused(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
if _, err := pool.Exec(context.Background(), "DELETE FROM users"); err != nil {
|
|
t.Fatalf("reset: %v", err)
|
|
}
|
|
soleAdmin := seedUser(t, pool, "soleadmin", "pw", true)
|
|
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/admin/users/"+uuidToString(soleAdmin.ID), nil)
|
|
req = withUser(req, soleAdmin)
|
|
rec := httptest.NewRecorder()
|
|
newAdminUsersRouter(h).ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusConflict {
|
|
t.Errorf("status = %d, want 409 (last_admin)", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestAdminResetPassword_Happy(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
admin := seedUser(t, pool, "rcaller", "pw", true)
|
|
target := seedUser(t, pool, "rtarget", "oldpw1234", false)
|
|
|
|
body := `{"password":"newpassword1"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/admin/users/"+uuidToString(target.ID)+"/reset-password",
|
|
bytes.NewReader([]byte(body)))
|
|
req = withUser(req, admin)
|
|
rec := httptest.NewRecorder()
|
|
newAdminUsersRouter(h).ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Errorf("status = %d, want 204; body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
// Old password should no longer match.
|
|
var hash string
|
|
if err := pool.QueryRow(context.Background(),
|
|
"SELECT password_hash FROM users WHERE id = $1", target.ID).Scan(&hash); err != nil {
|
|
t.Fatalf("read hash: %v", err)
|
|
}
|
|
if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte("oldpw1234")); err == nil {
|
|
t.Errorf("old password still matches; reset did nothing")
|
|
}
|
|
}
|
|
|
|
func TestAdminResetPassword_TooShort_400(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
admin := seedUser(t, pool, "rsadm", "pw", true)
|
|
target := seedUser(t, pool, "rstgt", "pw", false)
|
|
|
|
body := `{"password":"abc"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/admin/users/"+uuidToString(target.ID)+"/reset-password",
|
|
bytes.NewReader([]byte(body)))
|
|
req = withUser(req, admin)
|
|
rec := httptest.NewRecorder()
|
|
newAdminUsersRouter(h).ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Errorf("status = %d, want 400", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestAdminResetPassword_SignsOutSubsonicApps(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
admin := seedUser(t, pool, "rsubadm", "pw", true)
|
|
target := seedUser(t, pool, "rsubtgt", "oldpw1234", false)
|
|
giveSubsonicPassword(t, h, target)
|
|
before, _ := subsonicCredentials(t, h, target)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/admin/users/"+uuidToString(target.ID)+"/reset-password",
|
|
bytes.NewReader([]byte(`{"password":"newpassword1"}`)))
|
|
rec := httptest.NewRecorder()
|
|
newAdminUsersRouter(h).ServeHTTP(rec, withUser(req, admin))
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Fatalf("status = %d, want 204; body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
assertSubsonicSignedOut(t, h, target, before)
|
|
}
|
|
|
|
// An admin can take admin away from another admin, but never from the last
|
|
// one: the server always keeps someone who can manage it.
|
|
func TestAdminUsers_DemotingStopsAtTheLastAdmin(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
if _, err := pool.Exec(context.Background(), "DELETE FROM users"); err != nil {
|
|
t.Fatalf("reset: %v", err)
|
|
}
|
|
caller := seedUser(t, pool, "caller", "pw", true)
|
|
other := seedUser(t, pool, "otheradmin", "pw", true)
|
|
|
|
for _, step := range []struct {
|
|
target dbq.User
|
|
status int
|
|
}{
|
|
{other, http.StatusOK}, // two admins: one can go
|
|
{caller, http.StatusConflict}, // the last admin stays
|
|
} {
|
|
req := withUser(httptest.NewRequest(http.MethodPut,
|
|
"/api/admin/users/"+uuidToString(step.target.ID)+"/admin",
|
|
bytes.NewReader([]byte(`{"is_admin": false}`))), caller)
|
|
rec := httptest.NewRecorder()
|
|
newAdminUsersRouter(h).ServeHTTP(rec, req)
|
|
if rec.Code != step.status {
|
|
t.Errorf("demote %s: status %d, want %d; body=%s", step.target.Username, rec.Code, step.status, rec.Body.String())
|
|
}
|
|
}
|
|
}
|