summaryrefslogtreecommitdiff
path: root/internal/api/trello_test.go
blob: d67736361131d147d452f24253240c4c0a6bd074 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package api

import (
	"context"
	"encoding/json"
	"io"
	"net/http"
	"net/http/httptest"
	"net/url"
	"strings"
	"testing"
	"time"
)

// newTestTrelloClient creates a TrelloClient for testing with custom base URL
func newTestTrelloClient(baseURL, apiKey, token string) *TrelloClient {
	client := NewTrelloClient(apiKey, token)
	client.BaseURL = baseURL
	return client
}

func TestTrelloClient_CreateCard(t *testing.T) {
	// Mock server
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Verify method and path
		if r.Method != "POST" {
			t.Errorf("Expected POST request, got %s", r.Method)
		}
		if r.URL.Path != "/cards" {
			t.Errorf("Expected path /cards, got %s", r.URL.Path)
		}

		// Verify Content-Type
		if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
			t.Errorf("Expected Content-Type application/x-www-form-urlencoded, got %s", r.Header.Get("Content-Type"))
		}

		// Parse form data
		body, _ := io.ReadAll(r.Body)
		values, _ := parseFormData(string(body))

		// Verify required fields
		if values["key"] != "test-key" {
			t.Errorf("Expected key=test-key, got %s", values["key"])
		}
		if values["token"] != "test-token" {
			t.Errorf("Expected token=test-token, got %s", values["token"])
		}
		if values["idList"] != "list-123" {
			t.Errorf("Expected idList=list-123, got %s", values["idList"])
		}
		if values["name"] != "Test Card" {
			t.Errorf("Expected name=Test Card, got %s", values["name"])
		}
		if values["desc"] != "Test description" {
			t.Errorf("Expected desc=Test description, got %s", values["desc"])
		}

		// Return mock response
		response := trelloCardResponse{
			ID:      "card-456",
			Name:    "Test Card",
			IDList:  "list-123",
			URL:     "https://trello.com/c/card-456",
			Desc:    "Test description",
			IDBoard: "board-789",
		}

		w.Header().Set("Content-Type", "application/json")
		_ = json.NewEncoder(w).Encode(response)
	}))
	defer server.Close()

	// Create client with mock server URL
	client := newTestTrelloClient(server.URL, "test-key", "test-token")

	// Test CreateCard
	ctx := context.Background()
	card, err := client.CreateCard(ctx, "list-123", "Test Card", "Test description", nil)

	if err != nil {
		t.Fatalf("CreateCard failed: %v", err)
	}

	// Verify response
	if card.ID != "card-456" {
		t.Errorf("Expected card ID card-456, got %s", card.ID)
	}
	if card.Name != "Test Card" {
		t.Errorf("Expected card name Test Card, got %s", card.Name)
	}
	if card.ListID != "list-123" {
		t.Errorf("Expected list ID list-123, got %s", card.ListID)
	}
	if card.URL != "https://trello.com/c/card-456" {
		t.Errorf("Expected URL https://trello.com/c/card-456, got %s", card.URL)
	}
}

func TestTrelloClient_CreateCard_WithDueDate(t *testing.T) {
	dueDate := time.Date(2026, 1, 15, 12, 0, 0, 0, time.UTC)

	// Mock server
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Parse form data
		body, _ := io.ReadAll(r.Body)
		values, _ := parseFormData(string(body))

		// Verify due date is present
		if values["due"] != dueDate.Format(time.RFC3339) {
			t.Errorf("Expected due=%s, got %s", dueDate.Format(time.RFC3339), values["due"])
		}

		// Return mock response with due date
		dueString := dueDate.Format(time.RFC3339)
		response := trelloCardResponse{
			ID:     "card-789",
			Name:   "Card with Due Date",
			IDList: "list-456",
			URL:    "https://trello.com/c/card-789",
			Due:    &dueString,
		}

		w.Header().Set("Content-Type", "application/json")
		_ = json.NewEncoder(w).Encode(response)
	}))
	defer server.Close()

	// Create client
	client := newTestTrelloClient(server.URL, "test-key", "test-token")

	// Test CreateCard with due date
	ctx := context.Background()
	card, err := client.CreateCard(ctx, "list-456", "Card with Due Date", "", &dueDate)

	if err != nil {
		t.Fatalf("CreateCard failed: %v", err)
	}

	// Verify due date is set
	if card.DueDate == nil {
		t.Error("Expected due date to be set")
	} else if !card.DueDate.Equal(dueDate) {
		t.Errorf("Expected due date %v, got %v", dueDate, *card.DueDate)
	}
}

func TestTrelloClient_UpdateCard(t *testing.T) {
	// Mock server
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Verify method and path
		if r.Method != "PUT" {
			t.Errorf("Expected PUT request, got %s", r.Method)
		}
		if !strings.HasPrefix(r.URL.Path, "/cards/") {
			t.Errorf("Expected path to start with /cards/, got %s", r.URL.Path)
		}

		// Extract card ID from path
		cardID := strings.TrimPrefix(r.URL.Path, "/cards/")
		if cardID != "card-123" {
			t.Errorf("Expected card ID card-123, got %s", cardID)
		}

		// Parse form data
		body, _ := io.ReadAll(r.Body)
		values, _ := parseFormData(string(body))

		// Verify updated fields
		if values["name"] != "Updated Name" {
			t.Errorf("Expected name=Updated Name, got %s", values["name"])
		}
		if values["desc"] != "Updated description" {
			t.Errorf("Expected desc=Updated description, got %s", values["desc"])
		}

		// Return 200 OK
		w.WriteHeader(http.StatusOK)
		_, _ = w.Write([]byte(`{"id":"card-123","name":"Updated Name"}`))
	}))
	defer server.Close()

	// Create client
	client := newTestTrelloClient(server.URL, "test-key", "test-token")

	// Test UpdateCard
	ctx := context.Background()
	updates := map[string]interface{}{
		"name": "Updated Name",
		"desc": "Updated description",
	}

	err := client.UpdateCard(ctx, "card-123", updates)

	if err != nil {
		t.Fatalf("UpdateCard failed: %v", err)
	}
}

func TestTrelloClient_UpdateCard_Error(t *testing.T) {
	// Mock server that returns error
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusBadRequest)
		_, _ = w.Write([]byte(`{"error":"Invalid card ID"}`))
	}))
	defer server.Close()

	// Create client
	client := newTestTrelloClient(server.URL, "test-key", "test-token")

	// Test UpdateCard with error
	ctx := context.Background()
	updates := map[string]interface{}{
		"name": "Should Fail",
	}

	err := client.UpdateCard(ctx, "invalid-card", updates)

	if err == nil {
		t.Error("Expected error, got nil")
	}
	if !strings.Contains(err.Error(), "400") {
		t.Errorf("Expected error to contain status 400, got: %v", err)
	}
}

// Helper function to parse form-encoded data
func parseFormData(data string) (map[string]string, error) {
	values, err := url.ParseQuery(data)
	if err != nil {
		return nil, err
	}

	result := make(map[string]string)
	for key, vals := range values {
		if len(vals) > 0 {
			result[key] = vals[0]
		}
	}
	return result, nil
}