Files
infra/license-bot/internal/gitea/client_test.go
T
2026-10-20 02:43:27 +01:00

148 lines
4.3 KiB
Go

// Copyright (C) 2026 Dcntrlized
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package gitea
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
)
func TestAuthHeaderAndListPullFilesPagination(t *testing.T) {
var pages []int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/repos/o/r/pulls/3/files" {
t.Errorf("unexpected path %s", r.URL.Path)
}
if got := r.Header.Get("Authorization"); got != "token tok" {
t.Errorf("Authorization = %q", got)
}
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
pages = append(pages, page)
n := pageSize
if page == 2 {
n = 3
}
files := make([]File, 0, n)
for i := 0; i < n; i++ {
files = append(files, File{Filename: fmt.Sprintf("f%d-%d.go", page, i), Status: "added"})
}
_ = json.NewEncoder(w).Encode(files)
}))
defer srv.Close()
got, err := New(srv.URL, "tok").ListPullFiles(context.Background(), "o/r", 3)
if err != nil {
t.Fatal(err)
}
if len(got) != pageSize+3 {
t.Fatalf("got %d files, want %d", len(got), pageSize+3)
}
if len(pages) != 2 || pages[0] != 1 || pages[1] != 2 {
t.Fatalf("pages = %v, want [1 2]", pages)
}
}
func TestCreateStatus(t *testing.T) {
var gotMethod, gotPath, gotBody string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotMethod, gotPath = r.Method, r.URL.Path
b, _ := io.ReadAll(r.Body)
gotBody = string(b)
w.WriteHeader(http.StatusCreated)
}))
defer srv.Close()
err := New(srv.URL, "tok").CreateStatus(context.Background(), "o/r", "abc123", "success", "license-bot / addlicense", "ok", "")
if err != nil {
t.Fatal(err)
}
if gotMethod != http.MethodPost || gotPath != "/api/v1/repos/o/r/statuses/abc123" {
t.Fatalf("%s %s", gotMethod, gotPath)
}
for _, want := range []string{`"state":"success"`, `"context":"license-bot / addlicense"`, `"description":"ok"`} {
if !strings.Contains(gotBody, want) {
t.Fatalf("body %s missing %s", gotBody, want)
}
}
}
func TestCommentLifecyclePaths(t *testing.T) {
var paths []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
paths = append(paths, r.Method+" "+r.URL.Path)
w.Header().Set("Content-Type", "application/json")
if r.Method == http.MethodGet {
_ = json.NewEncoder(w).Encode([]Comment{{ID: 42, Body: "hi"}})
return
}
if r.Method == http.MethodPost {
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(Comment{ID: 43})
return
}
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
c := New(srv.URL, "tok")
ctx := context.Background()
if _, err := c.ListIssueComments(ctx, "o/r", 9); err != nil {
t.Fatal(err)
}
if _, err := c.CreateIssueComment(ctx, "o/r", 9, "body"); err != nil {
t.Fatal(err)
}
if err := c.EditIssueComment(ctx, "o/r", 42, "body"); err != nil {
t.Fatal(err)
}
want := []string{
"GET /api/v1/repos/o/r/issues/9/comments",
"POST /api/v1/repos/o/r/issues/9/comments",
"PATCH /api/v1/repos/o/r/issues/comments/42",
}
if len(paths) != len(want) {
t.Fatalf("paths = %v", paths)
}
for i := range want {
if paths[i] != want[i] {
t.Fatalf("paths = %v, want %v", paths, want)
}
}
}
func TestAPIErrorCarriesStatus(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "nope", http.StatusForbidden)
}))
defer srv.Close()
_, err := New(srv.URL, "tok").GetPull(context.Background(), "o/r", 1)
if err == nil {
t.Fatal("expected an error")
}
apiErr, ok := err.(*APIError)
if !ok || apiErr.Status != http.StatusForbidden {
t.Fatalf("err = %#v", err)
}
}