gitea.tea/cmd/pulls/reviewers_test.go
Ross Golder 32eebf7040
refactor(pulls): tighten reviewer-request flow
Follow-up to 5d3db5d (#571). No user-visible CLI changes.

- Replace tautological metadata unit tests with httptest-based e2e
  tests that exercise runRequestReview, runCancelReview, and
  runReviewersList against a mock Gitea API; each test captures
  method, path, and request body.
- Add tests/integration/pulls_reviewers_test.go covering create-time
  --reviewer/--team-reviewer, request-review add, cancel-review by
  user and by team, validation error, and bad-PR error against a
  real Gitea instance.
- Extract parseReviewRequestArgs in cmd/pulls/review_helpers.go so
  runRequestReview and runCancelReview share one validation/parsing
  path; both now route through task.ApplyReviewerChanges instead of
  inlining the SDK call.
- Extend task.ApplyReviewerChanges to accept teamAdd/teamRm and use
  it from request-review, cancel-review, and the existing edit flow.
- Replace task.CreatePull 8-positional-arg signature with a
  CreatePullOptions struct; update cmd/pulls/create.go and
  modules/interact/pull_create.go call sites.
- Defensively filter empty CSV entries in parseReviewRequestArgs via
  nonEmptyValues so the validation is correct regardless of whether
  the underlying CsvFlag trims empty entries (kept independent of
  the separate CsvFlag bugfix PR).

Verification:
  make fmt fmt-check vet lint test docs docs-check build
2026-09-05 09:53:33 +07:00

75 lines
2 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package pulls
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRunReviewersListCallsGetReviewers(t *testing.T) {
repo := "owner/repo"
var (
gotMethod string
gotPath string
)
withMockServer(t, func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v1/version":
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"version":"1.22.0"}`))
case "/api/v1/repos/" + repo + "/reviewers":
gotMethod = r.Method
gotPath = r.URL.Path
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`[
{"id":1,"login":"alice","full_name":"Alice","email":"alice@example.com"},
{"id":2,"login":"bob","full_name":"Bob","email":"bob@example.com"}
]`))
default:
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusNotFound)
}
})
cmd := runAction(t, CmdPullsReviewers, []string{
"--login", "test",
"--repo", repo,
"--output", "json",
})
require.NoError(t, runReviewersList(context.Background(), cmd))
assert.Equal(t, http.MethodGet, gotMethod)
assert.Equal(t, "/api/v1/repos/"+repo+"/reviewers", gotPath)
}
func TestRunReviewersListPropagatesAPIError(t *testing.T) {
repo := "owner/repo"
withMockServer(t, func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v1/version":
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"version":"1.22.0"}`))
case "/api/v1/repos/" + repo + "/reviewers":
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`{"message":"boom"}`))
default:
w.WriteHeader(http.StatusNotFound)
}
})
cmd := runAction(t, CmdPullsReviewers, []string{
"--login", "test",
"--repo", repo,
"--output", "json",
})
err := runReviewersList(context.Background(), cmd)
require.Error(t, err)
assert.Contains(t, err.Error(), "boom")
}