Files
gitea-tea/internal/app/label_options_test.go
T
bircni 0d013746b5 fix(label): refuse duplicate label names unless --force
tea label create --name bug twice created a second "bug" label, after
which issue edit --add-labels bug failed as ambiguous and issue list
--label bug matched nothing. label create (and --file imports) now fail
with "label "bug" already exists in alice/project; pass --force to
update it", ignoring case; --force updates the existing label. The
ambiguity error for labels that already share a name lists their IDs.

Signed-off-by: bircni <bircni@icloud.com>
2026-10-12 00:14:30 +02:00

86 lines
3.7 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 TestLabelCreateAndEditExclusiveArchived(t *testing.T) {
calls := 0
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
_, _ = w.Write([]byte(`[]`))
return
}
calls++
var payload map[string]any
require.NoError(t, json.NewDecoder(r.Body).Decode(&payload))
switch calls {
case 1:
require.Equal(t, http.MethodPost, r.Method)
require.Equal(t, "/api/v1/repos/owner/repo/labels", r.URL.Path)
require.Equal(t, "scoped", payload["name"])
require.Equal(t, "00aa00", payload["color"])
require.Equal(t, true, payload["exclusive"])
_, _ = w.Write([]byte(`{"id":3,"name":"scoped","color":"00aa00","description":"","exclusive":true,"is_archived":false}`))
case 2:
require.Equal(t, http.MethodPatch, r.Method)
require.Equal(t, "/api/v1/repos/owner/repo/labels/3", r.URL.Path)
require.Equal(t, true, payload["is_archived"])
require.Equal(t, false, payload["exclusive"])
_, _ = w.Write([]byte(`{"id":3,"name":"scoped","color":"00aa00","description":"","exclusive":false,"is_archived":true}`))
default:
t.Fatalf("unexpected call %d %s %s", calls, r.Method, r.URL.Path)
}
}))
defer server.Close()
deps, out, diagnostics := serverDeps(t, server.URL)
fields := "--json=id,name,color,exclusive,is_archived"
require.NoError(t, Execute(context.Background(), deps, []string{"label", "create", "-R", "owner/repo", "--name", "scoped", "--color", "00aa00", "--exclusive", fields, "--no-input"}))
require.JSONEq(t, `{"id":3,"name":"scoped","color":"00aa00","exclusive":true,"is_archived":false}`, out.String())
out.Reset()
require.NoError(t, Execute(context.Background(), deps, []string{"label", "edit", "3", "-R", "owner/repo", "--exclusive=false", "--archived", fields}))
require.JSONEq(t, `{"id":3,"name":"scoped","color":"00aa00","exclusive":false,"is_archived":true}`, out.String())
require.Equal(t, 2, calls)
require.Empty(t, diagnostics.String())
}
func TestLabelCreateRefusesDuplicateNames(t *testing.T) {
var changed []string
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method + " " + r.URL.Path {
case "GET /api/v1/repos/alice/project/labels":
if r.URL.Query().Get("page") == "1" {
_, _ = w.Write([]byte(`[{"id":11,"name":"bug","color":"ee0701","description":"Broken"}]`))
return
}
_, _ = w.Write([]byte(`[]`))
case "PATCH /api/v1/repos/alice/project/labels/11":
requirePayload(t, `{"color":"000000"}`, r.Body)
changed = append(changed, r.Method+" "+r.URL.Path)
_, _ = w.Write([]byte(`{"id":11,"name":"bug","color":"000000","description":"Broken"}`))
default:
changed = append(changed, r.Method+" "+r.URL.Path)
_, _ = w.Write([]byte(`{"id":12,"name":"docs"}`))
}
}))
defer server.Close()
deps, out, _ := serverDeps(t, server.URL)
for _, name := range []string{"bug", "BUG"} {
err := Execute(context.Background(), deps, []string{"label", "create", "-R", "alice/project", "--name", name, "--color", "000000"})
require.EqualError(t, err, `label "bug" already exists in alice/project; pass --force to update it`)
}
require.Empty(t, changed)
require.Empty(t, out.String())
require.NoError(t, Execute(context.Background(), deps, []string{"label", "create", "-R", "alice/project", "--name", "bug", "--color", "000000", "--force", "--json=id,color"}))
require.JSONEq(t, `{"id":11,"color":"000000"}`, out.String())
require.Equal(t, []string{"PATCH /api/v1/repos/alice/project/labels/11"}, changed)
}