mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
fix(admin): offer --purge for users who own repositories and show roles
admin user delete of a user who owns repositories failed with Gitea's raw HTTP 422. The error now says what blocks the deletion and how to proceed, and --purge deletes the account with its repositories, packages and organization memberships after typing the username on a terminal. admin user list shows ROLE and STATUS columns (admin, restricted, user; active, inactive, login prohibited) with the email and creation date instead of admin=true/active=true cells, and --query filters by username, name or email. admin user view shows the account instead of its list row. create asks for a missing username and email address with readable labels and re-asks for invalid answers, mutations print ✓ lines, usernames complete, and every command has examples. Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
6 files changed
+465
-195
No files matched your search
@@ -28,7 +28,7 @@ func TestAdminPromptAndFlagsReachEquivalentPayloads(t *testing.T) {
|
||||
deps.Streams.InTTY = true
|
||||
deps.Streams.ErrTTY = true
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"admin", "user", "create", "account", "--full-name", "Test Account"}))
|
||||
require.Equal(t, []string{"email"}, prompt.asked)
|
||||
require.Equal(t, []string{"Email address"}, prompt.asked)
|
||||
require.Len(t, prompt.secrets, 2)
|
||||
require.Empty(t, prompt.confirmations)
|
||||
out.Reset()
|
||||
@@ -60,3 +60,53 @@ func TestAdminUserEditSetsMustChangePasswordOnlyWhenPassed(t *testing.T) {
|
||||
require.Equal(t, true, edits[1]["must_change_password"])
|
||||
require.Equal(t, false, edits[2]["must_change_password"])
|
||||
}
|
||||
|
||||
func TestAdminUserListShowsRolesAndStatuses(t *testing.T) {
|
||||
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
require.Equal(t, "/api/v1/admin/users", r.URL.Path)
|
||||
require.Equal(t, "li", r.URL.Query().Get("q"))
|
||||
_, _ = w.Write([]byte(`[
|
||||
{"id":1,"login":"alice","full_name":"Alice","email":"alice@example.test","is_admin":true,"active":true,"created":"2026-10-01T12:00:00Z"},
|
||||
{"id":2,"login":"elisa","email":"elisa@example.test","restricted":true,"active":true,"created":"2026-10-02T12:00:00Z"},
|
||||
{"id":3,"login":"lisbeth","email":"lisbeth@example.test","prohibit_login":true,"active":true,"created":"2026-10-03T12:00:00Z"},
|
||||
{"id":4,"login":"lin","email":"lin@example.test","active":false,"created":"2026-10-04T12:00:00Z"}]`))
|
||||
}))
|
||||
defer server.Close()
|
||||
deps, out, _ := serverDeps(t, server.URL)
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"admin", "user", "list", "--query", "li"}))
|
||||
require.Equal(t, "1\talice\tAlice\talice@example.test\tadmin\tactive\t2026-10-01T12:00:00Z\n"+
|
||||
"2\telisa\t\telisa@example.test\trestricted\tactive\t2026-10-02T12:00:00Z\n"+
|
||||
"3\tlisbeth\t\tlisbeth@example.test\tuser\tlogin prohibited\t2026-10-03T12:00:00Z\n"+
|
||||
"4\tlin\t\tlin@example.test\tuser\tinactive\t2026-10-04T12:00:00Z\n", out.String())
|
||||
}
|
||||
|
||||
func TestAdminUserDeleteOffersPurgeForOwners(t *testing.T) {
|
||||
var deletes []string
|
||||
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
require.Equal(t, "/api/v1/users/bob", r.URL.Path)
|
||||
_, _ = w.Write([]byte(`{"id":2,"login":"bob"}`))
|
||||
case http.MethodDelete:
|
||||
deletes = append(deletes, r.URL.RequestURI())
|
||||
if r.URL.Query().Get("purge") != "true" {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
_, _ = w.Write([]byte(`{"message":"user still has ownership of repositories [uid: 2]"}`))
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
deps, _, _ := serverDeps(t, server.URL)
|
||||
err := Execute(context.Background(), deps, []string{"admin", "user", "delete", "bob", "--yes"})
|
||||
require.EqualError(t, err, `user "bob" still owns repositories; transfer or delete them, or pass --purge to delete them with the user`)
|
||||
|
||||
deps.Streams.InTTY, deps.Streams.ErrTTY = true, true
|
||||
const typed = "Delete user bob with their repositories, packages and organization memberships? This cannot be undone. Type bob to confirm:"
|
||||
prompt := &scriptedPrompter{inputs: map[string]string{typed: "bob"}}
|
||||
deps.Prompter = prompt
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"admin", "user", "delete", "bob", "--purge"}))
|
||||
require.Equal(t, []string{typed}, prompt.asked)
|
||||
require.Equal(t, []string{"/api/v1/admin/users/bob", "/api/v1/admin/users/bob?purge=true"}, deletes)
|
||||
}
|
||||
Reference in new issue
Block a user