| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package common
- import (
- "context"
- "net/http"
- "net/http/httptest"
- "testing"
- "time"
- )
- func TestJiraClient_GetProjectAndIssueInfo(t *testing.T) {
- t.Helper()
- mux := http.NewServeMux()
- mux.HandleFunc("/rest/api/2/project/OPMS", func(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodGet {
- t.Fatalf("unexpected method for project endpoint: %s", r.Method)
- }
- w.Header().Set("Content-Type", "application/json")
- _, _ = w.Write([]byte(`{"id":"10001","key":"OPMS","name":"OPMS Project"}`))
- })
- mux.HandleFunc("/rest/api/2/issue/OPMS-123", func(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodGet {
- t.Fatalf("unexpected method for issue endpoint: %s", r.Method)
- }
- if got := r.URL.Query().Get("fields"); got != "summary,description" {
- t.Fatalf("unexpected fields query, got=%q", got)
- }
- w.Header().Set("Content-Type", "application/json")
- _, _ = w.Write([]byte(`{
- "id":"20001",
- "key":"OPMS-123",
- "fields":{
- "summary":"测试任务",
- "description":"测试任务描述"
- }
- }`))
- })
- server := httptest.NewServer(mux)
- defer server.Close()
- client, err := NewJiraClient(JiraClientConfig{
- BaseURL: server.URL,
- AuthType: JiraAuthTypeBasic,
- Username: "tester",
- Password: "tester_password",
- Timeout: 5 * time.Second,
- })
- if err != nil {
- t.Fatalf("NewJiraClient() error = %v", err)
- }
- ctx := context.Background()
- project, _, err := client.RawClient().Project.GetWithContext(ctx, "OPMS")
- if err != nil {
- t.Fatalf("Project.GetWithContext() error = %v", err)
- }
- if project == nil {
- t.Fatal("project is nil")
- }
- if project.Key != "OPMS" || project.Name != "OPMS Project" {
- t.Fatalf("unexpected project, key=%q name=%q", project.Key, project.Name)
- }
- issue, err := client.GetIssue(ctx, "OPMS-123", "summary", "description")
- if err != nil {
- t.Fatalf("GetIssue() error = %v", err)
- }
- if issue == nil || issue.Fields == nil {
- t.Fatal("issue or issue.Fields is nil")
- }
- if issue.Key != "OPMS-123" {
- t.Fatalf("unexpected issue key: %q", issue.Key)
- }
- if issue.Fields.Summary != "测试任务" || issue.Fields.Description != "测试任务描述" {
- t.Fatalf("unexpected issue fields, summary=%q description=%q", issue.Fields.Summary, issue.Fields.Description)
- }
- }
- func TestJiraClient_GetIssue_EmptyIssueKey(t *testing.T) {
- client := &JiraClient{}
- _, err := client.GetIssue(context.Background(), "")
- if err == nil {
- t.Fatal("expected error when issueKey is empty, got nil")
- }
- }
|