feat(backend): book list/detail/delete with embedded progress + cache cleanup

This commit is contained in:
2026-09-05 00:42:08 +08:00
parent 193d7612b5
commit 249619b857
7 changed files with 339 additions and 26 deletions
+9 -7
View File
@@ -9,14 +9,11 @@ import (
func adminToken(t *testing.T, h http.Handler) string {
t.Helper()
w := do(h, "POST", "/api/auth/login", "", map[string]string{"username": "alice", "password": "pw12345"})
var v struct{ Token string }
json.Unmarshal(w.Body.Bytes(), &v)
return v.Token
return loginAs(t, h, "alice", testPW)
}
func TestUserCRUD(t *testing.T) {
_, h := setupAPI(t)
_, _, h, _ := setupAPI(t)
tok := adminToken(t, h)
w := do(h, "POST", "/api/users", tok, map[string]string{"username": "carol", "password": "pw123456", "role": "member"})
if w.Code != 201 {
@@ -59,7 +56,7 @@ func TestUserCRUD(t *testing.T) {
}
func TestBadRoleRejected(t *testing.T) {
_, h := setupAPI(t)
_, _, h, _ := setupAPI(t)
tok := adminToken(t, h)
w := do(h, "POST", "/api/users", tok, map[string]string{"username": "e", "password": "pw123456", "role": "god"})
if w.Code != 400 {
@@ -76,4 +73,9 @@ func findID(rows []map[string]any, name string) float64 {
return 0
}
func itoa(v any) string { return strconv.FormatFloat(v.(float64), 'f', 0, 64) }
func itoa(v any) string {
if i, ok := v.(int64); ok {
return strconv.FormatInt(i, 10) // DB 行里的 id (如 store.Library.ID)
}
return strconv.FormatFloat(v.(float64), 'f', 0, 64) // json.Unmarshal 出来的数字
}