forgejo-tickets/internal/forgejo/client_test.go

168 lines
4.7 KiB
Go

package forgejo
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestCreateIssue_Success(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Verify request method and path
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
expectedPath := "/api/v1/repos/testowner/testrepo/issues"
if r.URL.Path != expectedPath {
t.Errorf("expected path %s, got %s", expectedPath, r.URL.Path)
}
// Verify auth header
authHeader := r.Header.Get("Authorization")
if authHeader != "token test-token" {
t.Errorf("expected Authorization header %q, got %q", "token test-token", authHeader)
}
// Verify content type
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
t.Errorf("expected Content-Type application/json, got %s", ct)
}
// Decode request body
var req CreateIssueRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
if req.Title != "Test Issue" {
t.Errorf("expected title %q, got %q", "Test Issue", req.Title)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(Issue{
Number: 7,
Title: "Test Issue",
State: "open",
})
}))
defer server.Close()
client := NewClient(server.URL, "test-token")
issue, err := client.CreateIssue("testowner", "testrepo", CreateIssueRequest{
Title: "Test Issue",
Body: "This is a test issue body.",
})
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if issue.Number != 7 {
t.Errorf("expected issue number 7, got %d", issue.Number)
}
if issue.Title != "Test Issue" {
t.Errorf("expected issue title %q, got %q", "Test Issue", issue.Title)
}
if issue.State != "open" {
t.Errorf("expected issue state %q, got %q", "open", issue.State)
}
}
func TestCreateIssue_ServerError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("internal server error"))
}))
defer server.Close()
client := NewClient(server.URL, "test-token")
_, err := client.CreateIssue("testowner", "testrepo", CreateIssueRequest{
Title: "Test Issue",
Body: "Body",
})
if err == nil {
t.Fatal("expected error for 500 response, got nil")
}
}
func TestCreateIssue_NotFound(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("not found"))
}))
defer server.Close()
client := NewClient(server.URL, "test-token")
_, err := client.CreateIssue("testowner", "testrepo", CreateIssueRequest{
Title: "Test Issue",
Body: "Body",
})
if err == nil {
t.Fatal("expected error for 404 response, got nil")
}
}
func TestCreateComment_Success(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
expectedPath := "/api/v1/repos/owner/repo/issues/5/comments"
if r.URL.Path != expectedPath {
t.Errorf("expected path %s, got %s", expectedPath, r.URL.Path)
}
authHeader := r.Header.Get("Authorization")
if authHeader != "token my-api-token" {
t.Errorf("expected Authorization header %q, got %q", "token my-api-token", authHeader)
}
var req CreateCommentRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
if req.Body != "A comment" {
t.Errorf("expected body %q, got %q", "A comment", req.Body)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(Comment{
ID: 99,
Body: "A comment",
})
}))
defer server.Close()
client := NewClient(server.URL, "my-api-token")
comment, err := client.CreateComment("owner", "repo", 5, CreateCommentRequest{
Body: "A comment",
})
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if comment.ID != 99 {
t.Errorf("expected comment ID 99, got %d", comment.ID)
}
if comment.Body != "A comment" {
t.Errorf("expected comment body %q, got %q", "A comment", comment.Body)
}
}
func TestCreateComment_ServerError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("error"))
}))
defer server.Close()
client := NewClient(server.URL, "test-token")
_, err := client.CreateComment("owner", "repo", 1, CreateCommentRequest{
Body: "A comment",
})
if err == nil {
t.Fatal("expected error for 500 response, got nil")
}
}