Files
gitea-tea/internal/app/admin_users_test.go
T
bircni 1c9cc32d28 fix(admin): keep must_change_password unless it is passed to user edit
--must-change-password defaulted to true on admin user edit as well, so
changing a password with --password silently forced the user to change
it again at the next sign-in. On edit the flag now defaults to unset and
is sent only when passed; create keeps requiring a change by default.

Signed-off-by: bircni <bircni@icloud.com>
2026-10-11 22:00:29 +02:00

63 lines
2.5 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package app
import (
"context"
"encoding/json"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
func TestAdminPromptAndFlagsReachEquivalentPayloads(t *testing.T) {
var payloads []map[string]any
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodPost, r.Method)
var payload map[string]any
require.NoError(t, json.NewDecoder(r.Body).Decode(&payload))
payloads = append(payloads, payload)
_, _ = w.Write([]byte(`{"id":1,"login":"account"}`))
}))
defer server.Close()
deps, out, _ := serverDeps(t, server.URL)
prompt := &scriptedPrompter{input: "account@example.test", secret: " preserved password ", accept: true}
deps.Prompter = prompt
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.Len(t, prompt.secrets, 2)
require.Empty(t, prompt.confirmations)
out.Reset()
require.NoError(t, Execute(context.Background(), deps, []string{"admin", "user", "create", "account", "--full-name", "Test Account", "--email", "account@example.test", "--password", " preserved password ", "--no-input"}))
require.Equal(t, payloads[0], payloads[1])
require.Len(t, prompt.asked, 1)
require.Len(t, prompt.secrets, 2)
require.NotContains(t, out.String(), "password")
}
func TestAdminUserEditSetsMustChangePasswordOnlyWhenPassed(t *testing.T) {
var edits []map[string]any
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPatch {
var payload map[string]any
require.NoError(t, json.NewDecoder(r.Body).Decode(&payload))
edits = append(edits, payload)
}
_, _ = w.Write([]byte(`{"id":2,"login":"bob"}`))
}))
defer server.Close()
deps, _, _ := serverDeps(t, server.URL)
for _, flags := range [][]string{{"--password", "new password"}, {"--password", "new password", "--must-change-password"}, {"--must-change-password=false"}} {
require.NoError(t, Execute(context.Background(), deps, append([]string{"admin", "user", "edit", "bob", "--no-input"}, flags...)))
}
require.Len(t, edits, 3)
require.Equal(t, "new password", edits[0]["password"])
require.Nil(t, edits[0]["must_change_password"])
require.Equal(t, true, edits[1]["must_change_password"])
require.Equal(t, false, edits[2]["must_change_password"])
}