feat: user preview, bulk and record actions and the permanent delete
- the list opens the preview, which shows one status hint (banned, deactivated, not activated, suspended) and the IP address fields - bulk actions activate, deactivate, restore, ban and unban; record actions activate, unban and unsuspend, offered while they apply - delete (form button and bulk) removes the user for good with the throttle rows, group memberships and attachments in one transaction; blobs go after the commit - a privileged-group member is still refused, a bulk delete as a whole
This commit is contained in:
@@ -389,15 +389,14 @@ func (e *adminEnv) join(t *testing.T, user *models.User, groups ...*models.UserG
|
||||
// reload reads a user row, deactivated or not; nil when it is gone.
|
||||
func (e *adminEnv) reload(t *testing.T, id uint) *models.User {
|
||||
t.Helper()
|
||||
var user models.User
|
||||
err := e.gdb.Unscoped().Where("id = ?", id).Take(&user).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
var users []models.User
|
||||
if err := e.gdb.Unscoped().Where("id = ?", id).Limit(1).Find(&users).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return &user
|
||||
if len(users) == 0 {
|
||||
return nil
|
||||
}
|
||||
return &users[0]
|
||||
}
|
||||
|
||||
// groupIDs returns the user's users_groups rows, ordered.
|
||||
@@ -410,6 +409,110 @@ func (e *adminEnv) groupIDs(t *testing.T, userID uint) []uint {
|
||||
return ids
|
||||
}
|
||||
|
||||
// seedAvatar stores an avatar for the user the way the user API does: one
|
||||
// system_files row (field avatar) and its blob. It returns the blob key.
|
||||
func (e *adminEnv) seedAvatar(t *testing.T, user *models.User) string {
|
||||
t.Helper()
|
||||
disk := "avatar" + adminStamp() + ".jpg"
|
||||
key := attach.BlobKey(disk)
|
||||
if err := e.bucket.WriteAll(context.Background(), key, []byte("avatar-bytes"), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
row := attach.File{
|
||||
DiskName: disk,
|
||||
FileName: "avatar.jpg",
|
||||
FileSize: 12,
|
||||
ContentType: "image/jpeg",
|
||||
Field: "avatar",
|
||||
AttachmentID: strconv.FormatUint(uint64(user.ID), 10),
|
||||
AttachmentType: user.MorphName(),
|
||||
}
|
||||
if err := e.gdb.Create(&row).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
// blobExists reports whether the bucket still holds key.
|
||||
func (e *adminEnv) blobExists(t *testing.T, key string) bool {
|
||||
t.Helper()
|
||||
ok, err := e.bucket.Exists(context.Background(), key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
// seedThrottle stores one user_throttle row. ip may be empty for the row
|
||||
// without an address.
|
||||
func (e *adminEnv) seedThrottle(t *testing.T, user *models.User, ip string, banned bool, suspendedAt *time.Time) {
|
||||
t.Helper()
|
||||
row := models.Throttle{UserID: user.ID, IsBanned: banned}
|
||||
if ip != "" {
|
||||
row.IPAddress = &ip
|
||||
}
|
||||
if suspendedAt != nil {
|
||||
row.IsSuspended, row.SuspendedAt, row.Attempts = true, suspendedAt, 5
|
||||
}
|
||||
if err := e.gdb.Create(&row).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// hangers counts what hangs off a user: throttle rows, group memberships
|
||||
// and attachment rows.
|
||||
type hangers struct{ Throttle, Groups, Files int64 }
|
||||
|
||||
func (e *adminEnv) hangers(t *testing.T, user *models.User) hangers {
|
||||
t.Helper()
|
||||
var out hangers
|
||||
count := func(into *int64, query string, args ...any) {
|
||||
t.Helper()
|
||||
if err := e.gdb.Raw(query, args...).Scan(into).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
count(&out.Throttle, `SELECT COUNT(*) FROM user_throttle WHERE user_id = ?`, user.ID)
|
||||
count(&out.Groups, `SELECT COUNT(*) FROM users_groups WHERE user_id = ?`, user.ID)
|
||||
count(&out.Files, `SELECT COUNT(*) FROM system_files WHERE attachment_type = ? AND attachment_id = ?`,
|
||||
user.MorphName(), strconv.FormatUint(uint64(user.ID), 10))
|
||||
return out
|
||||
}
|
||||
|
||||
// bulk posts a declared bulk action, or the built-in bulk delete when action
|
||||
// is "delete", for the given users.
|
||||
func (e *adminEnv) bulk(t *testing.T, token, action string, users ...*models.User) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
ids := make([]uint, len(users))
|
||||
for i, user := range users {
|
||||
ids[i] = user.ID
|
||||
}
|
||||
path := adminAPI(adminUsersPath + "/bulk/" + action)
|
||||
if action == "delete" {
|
||||
path = adminAPI(adminUsersPath + "/bulk-delete")
|
||||
}
|
||||
return e.call(t, http.MethodPost, path, token, map[string]any{"ids": ids})
|
||||
}
|
||||
|
||||
// offered returns the record actions the show response of the user offers.
|
||||
func (e *adminEnv) offered(t *testing.T, token string, id uint) []string {
|
||||
t.Helper()
|
||||
rec := e.expect(t, http.StatusOK, http.MethodGet, userPath(id, ""), token, nil)
|
||||
var meta struct {
|
||||
Actions []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"actions"`
|
||||
}
|
||||
if err := json.Unmarshal(adminDecode(t, rec).Meta, &meta); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
names := []string{}
|
||||
for _, action := range meta.Actions {
|
||||
names = append(names, action.Name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// userLogin signs a frontend user in through the user API route and returns
|
||||
// the status and the token.
|
||||
func (e *adminEnv) userLogin(t *testing.T, email, password string) (int, string) {
|
||||
|
||||
Reference in New Issue
Block a user