summaryrefslogtreecommitdiff
path: root/internal/api/trello_test.go
blob: a209d01b00a3f251a87fd4353cc493d3571b4c23 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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
}

func TestTrelloClient_GetBoards(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			t.Errorf("Expected GET, got %s", r.Method)
		}
		if r.URL.Path != "/members/me/boards" {
			t.Errorf("Expected path /members/me/boards, got %s", r.URL.Path)
		}

		response := []trelloBoardResponse{
			{ID: "board-1", Name: "Board 1"},
			{ID: "board-2", Name: "Board 2"},
		}

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

	client := newTestTrelloClient(server.URL, "test-key", "test-token")
	boards, err := client.GetBoards(context.Background())
	if err != nil {
		t.Fatalf("GetBoards failed: %v", err)
	}

	if len(boards) != 2 {
		t.Errorf("Expected 2 boards, got %d", len(boards))
	}
	if boards[0].ID != "board-1" {
		t.Errorf("Expected board ID 'board-1', got '%s'", boards[0].ID)
	}
}

func TestTrelloClient_GetCards(t *testing.T) {
	dueDate := "2024-01-15T12:00:00Z"
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			t.Errorf("Expected GET, got %s", r.Method)
		}

		w.Header().Set("Content-Type", "application/json")

		// GetCards calls getLists internally
		if strings.Contains(r.URL.Path, "/lists") {
			response := []trelloListResponse{
				{ID: "list-1", Name: "To Do"},
			}
			json.NewEncoder(w).Encode(response)
			return
		}

		if strings.Contains(r.URL.Path, "/cards") {
			response := []trelloCardResponse{
				{ID: "card-1", Name: "Card 1", IDList: "list-1", IDBoard: "board-1", Due: &dueDate},
				{ID: "card-2", Name: "Card 2", IDList: "list-1", IDBoard: "board-1"},
			}
			json.NewEncoder(w).Encode(response)
			return
		}

		t.Errorf("Unexpected path: %s", r.URL.Path)
	}))
	defer server.Close()

	client := newTestTrelloClient(server.URL, "test-key", "test-token")
	cards, err := client.GetCards(context.Background(), "board-1")
	if err != nil {
		t.Fatalf("GetCards failed: %v", err)
	}

	if len(cards) != 2 {
		t.Errorf("Expected 2 cards, got %d", len(cards))
	}
	if cards[0].DueDate == nil {
		t.Error("Expected due date for card 1")
	}
}

func TestTrelloClient_GetLists(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			t.Errorf("Expected GET, got %s", r.Method)
		}
		if !strings.Contains(r.URL.Path, "/boards/board-1/lists") {
			t.Errorf("Expected path to contain /boards/board-1/lists, got %s", r.URL.Path)
		}

		response := []trelloListResponse{
			{ID: "list-1", Name: "To Do"},
			{ID: "list-2", Name: "Done"},
		}

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

	client := newTestTrelloClient(server.URL, "test-key", "test-token")
	lists, err := client.GetLists(context.Background(), "board-1")
	if err != nil {
		t.Fatalf("GetLists failed: %v", err)
	}

	if len(lists) != 2 {
		t.Errorf("Expected 2 lists, got %d", len(lists))
	}
	if lists[0].Name != "To Do" {
		t.Errorf("Expected 'To Do', got '%s'", lists[0].Name)
	}
}

func TestTrelloClient_GetBoardsWithCards(t *testing.T) {
	requestCount := 0
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		requestCount++

		if strings.Contains(r.URL.Path, "/members/me/boards") {
			response := []trelloBoardResponse{
				{ID: "board-1", Name: "Board 1"},
			}
			w.Header().Set("Content-Type", "application/json")
			json.NewEncoder(w).Encode(response)
			return
		}

		if strings.Contains(r.URL.Path, "/lists") {
			response := []trelloListResponse{
				{ID: "list-1", Name: "To Do"},
			}
			w.Header().Set("Content-Type", "application/json")
			json.NewEncoder(w).Encode(response)
			return
		}

		if strings.Contains(r.URL.Path, "/cards") {
			response := []trelloCardResponse{
				{ID: "card-1", Name: "Card 1", IDList: "list-1", IDBoard: "board-1"},
			}
			w.Header().Set("Content-Type", "application/json")
			json.NewEncoder(w).Encode(response)
			return
		}

		t.Errorf("Unexpected path: %s", r.URL.Path)
	}))
	defer server.Close()

	client := newTestTrelloClient(server.URL, "test-key", "test-token")
	boards, err := client.GetBoardsWithCards(context.Background())
	if err != nil {
		t.Fatalf("GetBoardsWithCards failed: %v", err)
	}

	if len(boards) != 1 {
		t.Errorf("Expected 1 board, got %d", len(boards))
	}
	if len(boards[0].Cards) != 1 {
		t.Errorf("Expected 1 card, got %d", len(boards[0].Cards))
	}
	if boards[0].Cards[0].ListName != "To Do" {
		t.Errorf("Expected list name 'To Do', got '%s'", boards[0].Cards[0].ListName)
	}
}