Repository navigation
Expand file tree
/
Copy pathclient_test.go
More file actions
475 lines (424 loc) · 12.4 KB
/
Copy pathclient_test.go
File metadata and controls
475 lines (424 loc) · 12.4 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package main
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func testClient(handler http.HandlerFunc) (*Client, *httptest.Server) {
srv := httptest.NewServer(handler)
cfg := &Config{
APIKey: "test-key",
Company: "test-co",
EmployeeID: "42",
}
client := NewClient(cfg)
client.BaseURL = srv.URL
return client, srv
}
func TestClockIn_Success(t *testing.T) {
client, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("method = %s, want POST", r.Method)
}
if !strings.HasSuffix(r.URL.Path, "/time_tracking/employees/42/clock_in") {
t.Errorf("path = %s, want .../clock_in", r.URL.Path)
}
user, pass, ok := r.BasicAuth()
if !ok || user != "test-key" || pass != "x" {
t.Errorf("auth = (%q, %q, %v), want (test-key, x, true)", user, pass, ok)
}
w.WriteHeader(http.StatusOK)
})
defer srv.Close()
if err := client.ClockIn(nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestClockIn_WithTime(t *testing.T) {
client, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
var payload map[string]string
json.Unmarshal(body, &payload)
if payload["start"] != "09:00" {
t.Errorf("start = %q, want %q", payload["start"], "09:00")
}
if payload["date"] == "" {
t.Error("date should be set")
}
if r.Header.Get("Content-Type") != "application/json" {
t.Errorf("Content-Type = %q, want application/json", r.Header.Get("Content-Type"))
}
w.WriteHeader(http.StatusOK)
})
defer srv.Close()
now := time.Now()
at := time.Date(now.Year(), now.Month(), now.Day(), 9, 0, 0, 0, now.Location())
if err := client.ClockIn(&at); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestClockIn_Error(t *testing.T) {
client, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte(`{"error":{"code":"FORBIDDEN","message":"not allowed"}}`))
})
defer srv.Close()
err := client.ClockIn(nil)
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "not allowed") {
t.Errorf("error should contain message: %v", err)
}
}
func TestClockOut_Success(t *testing.T) {
client, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
if !strings.HasSuffix(r.URL.Path, "/time_tracking/employees/42/clock_out") {
t.Errorf("path = %s, want .../clock_out", r.URL.Path)
}
w.WriteHeader(http.StatusOK)
})
defer srv.Close()
if err := client.ClockOut(nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestClockOut_WithTime(t *testing.T) {
client, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
var payload map[string]string
json.Unmarshal(body, &payload)
if payload["end"] != "17:30" {
t.Errorf("end = %q, want %q", payload["end"], "17:30")
}
w.WriteHeader(http.StatusOK)
})
defer srv.Close()
now := time.Now()
at := time.Date(now.Year(), now.Month(), now.Day(), 17, 30, 0, 0, now.Location())
if err := client.ClockOut(&at); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestGetEmployee_Success(t *testing.T) {
client, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("method = %s, want GET", r.Method)
}
if !strings.Contains(r.URL.Path, "/employees/42") {
t.Errorf("path = %s, want .../employees/42", r.URL.Path)
}
fields := r.URL.Query().Get("fields")
if !strings.Contains(fields, "displayName") {
t.Errorf("fields = %s, want displayName included", fields)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(Employee{
DisplayName: "Edoardo Dusi",
JobTitle: "DevRel Manager",
Department: "Engineering",
Location: "Remote",
})
})
defer srv.Close()
emp, err := client.GetEmployee()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if emp.DisplayName != "Edoardo Dusi" {
t.Errorf("DisplayName = %q, want %q", emp.DisplayName, "Edoardo Dusi")
}
if emp.JobTitle != "DevRel Manager" {
t.Errorf("JobTitle = %q, want %q", emp.JobTitle, "DevRel Manager")
}
}
func TestGetEmployee_Error(t *testing.T) {
client, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"error":{"code":"UNAUTHORIZED","message":"unauthorized"}}`))
})
defer srv.Close()
_, err := client.GetEmployee()
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "unauthorized") {
t.Errorf("error should contain message: %v", err)
}
}
func TestStatus_Success(t *testing.T) {
entries := []TimesheetEntry{
{ID: 1, EmployeeID: 42, Date: "2026-04-03", Start: "09:00", End: "12:30"},
{ID: 2, EmployeeID: 42, Date: "2026-04-03", Start: "13:30", End: ""},
}
client, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("method = %s, want GET", r.Method)
}
if !strings.HasPrefix(r.URL.Path, "/time_tracking/timesheet_entries") {
t.Errorf("path = %s, want .../timesheet_entries", r.URL.Path)
}
if r.URL.Query().Get("employeeIds") != "42" {
t.Errorf("employeeIds = %s, want 42", r.URL.Query().Get("employeeIds"))
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(entries)
})
defer srv.Close()
result, err := client.Status()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) != 2 {
t.Fatalf("got %d entries, want 2", len(result))
}
if result[0].Start != "09:00" {
t.Errorf("first entry start = %q, want %q", result[0].Start, "09:00")
}
if result[1].End != "" {
t.Errorf("second entry end = %q, want empty (still clocked in)", result[1].End)
}
}
func TestStatus_Empty(t *testing.T) {
client, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("[]"))
})
defer srv.Close()
result, err := client.Status()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) != 0 {
t.Fatalf("got %d entries, want 0", len(result))
}
}
func TestLogEntry_Success(t *testing.T) {
client, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("method = %s, want POST", r.Method)
}
if !strings.HasSuffix(r.URL.Path, "/time_tracking/clock_entries/store") {
t.Errorf("path = %s, want .../clock_entries/store", r.URL.Path)
}
var payload struct {
Entries []struct {
EmployeeID int `json:"employeeId"`
Date string `json:"date"`
Start string `json:"start"`
End string `json:"end"`
Timezone string `json:"timezone"`
Note string `json:"note"`
} `json:"entries"`
}
body, _ := io.ReadAll(r.Body)
if err := json.Unmarshal(body, &payload); err != nil {
t.Fatalf("unmarshal payload: %v", err)
}
if len(payload.Entries) != 1 {
t.Fatalf("got %d entries, want 1", len(payload.Entries))
}
e := payload.Entries[0]
if e.EmployeeID != 42 {
t.Errorf("employeeId = %d, want 42", e.EmployeeID)
}
if e.Date != "2026-06-18" {
t.Errorf("date = %q, want 2026-06-18", e.Date)
}
if e.Start != "09:00" || e.End != "14:00" {
t.Errorf("interval = %q-%q, want 09:00-14:00", e.Start, e.End)
}
if e.Timezone == "" {
t.Error("timezone should be set")
}
if e.Note != "health appt" {
t.Errorf("note = %q, want %q", e.Note, "health appt")
}
w.WriteHeader(http.StatusOK)
})
defer srv.Close()
if err := client.LogEntry("2026-06-18", "09:00", "14:00", "health appt"); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestLogEntry_OmitsEmptyNote(t *testing.T) {
client, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
if strings.Contains(string(body), "note") {
t.Errorf("payload should omit note when empty: %s", body)
}
w.WriteHeader(http.StatusOK)
})
defer srv.Close()
if err := client.LogEntry("2026-06-18", "09:00", "14:00", ""); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestLogEntry_Error(t *testing.T) {
client, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error":{"code":"BAD","message":"overlapping entry"}}`))
})
defer srv.Close()
err := client.LogEntry("2026-06-18", "09:00", "14:00", "")
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "overlapping entry") {
t.Errorf("error should contain message: %v", err)
}
}
func TestLogEntry_InvalidEmployeeID(t *testing.T) {
client, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
t.Error("should not reach server with invalid employee ID")
w.WriteHeader(http.StatusOK)
})
defer srv.Close()
client.Config.EmployeeID = "not-a-number"
if err := client.LogEntry("2026-06-18", "09:00", "14:00", ""); err == nil {
t.Fatal("expected error for non-numeric employee ID")
}
}
func TestParseClockTime(t *testing.T) {
tests := []struct {
in string
want string
}{
{"9am", "09:00"},
{"9:00am", "09:00"},
{"9:30am", "09:30"},
{"5pm", "17:00"},
{"5:30pm", "17:30"},
{"17:30", "17:30"},
{"14", "14:00"},
{"9:00", "09:00"},
{"12pm", "12:00"},
{"12am", "00:00"},
{" 9:00 am ", "09:00"},
}
for _, tt := range tests {
got, err := parseClockTime(tt.in)
if err != nil {
t.Errorf("parseClockTime(%q) error: %v", tt.in, err)
continue
}
if got != tt.want {
t.Errorf("parseClockTime(%q) = %q, want %q", tt.in, got, tt.want)
}
}
}
func TestParseClockTime_Invalid(t *testing.T) {
if _, err := parseClockTime("banana"); err == nil {
t.Fatal("expected error for invalid time")
}
}
func TestParseDateArg(t *testing.T) {
got, err := parseDateArg("2026-06-18")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != "2026-06-18" {
t.Errorf("parseDateArg = %q, want 2026-06-18", got)
}
if _, err := parseDateArg("06/18/2026"); err == nil {
t.Fatal("expected error for wrong date format")
}
}
func TestParseTime_RFC3339(t *testing.T) {
got := parseTime("2026-04-03T09:15:00+02:00")
if got.Hour() != 9 || got.Minute() != 15 {
t.Errorf("parseTime RFC3339 = %v, want 09:15", got)
}
}
func TestParseTime_Short(t *testing.T) {
got := parseTime("14:30")
if got.Hour() != 14 || got.Minute() != 30 {
t.Errorf("parseTime short = %v, want 14:30", got)
}
}
func TestFormatDuration(t *testing.T) {
tests := []struct {
d time.Duration
want string
}{
{3*time.Hour + 30*time.Minute, "3h30m"},
{45 * time.Minute, "45m"},
{0, "0m"},
{8 * time.Hour, "8h00m"},
}
for _, tt := range tests {
got := formatDuration(tt.d)
if got != tt.want {
t.Errorf("formatDuration(%v) = %q, want %q", tt.d, got, tt.want)
}
}
}
func TestParseTimeArg(t *testing.T) {
tests := []struct {
args []string
wantH int
wantM int
wantNil bool
}{
{nil, 0, 0, true},
{[]string{}, 0, 0, true},
{[]string{"9am"}, 9, 0, false},
{[]string{"9:00am"}, 9, 0, false},
{[]string{"9:30am"}, 9, 30, false},
{[]string{"9", "am"}, 9, 0, false},
{[]string{"9:00", "am"}, 9, 0, false},
{[]string{"5pm"}, 17, 0, false},
{[]string{"5:30pm"}, 17, 30, false},
{[]string{"5:30", "pm"}, 17, 30, false},
{[]string{"17:30"}, 17, 30, false},
{[]string{"9:00"}, 9, 0, false},
{[]string{"12pm"}, 12, 0, false},
{[]string{"12am"}, 0, 0, false},
}
for _, tt := range tests {
got, err := parseTimeArg(tt.args)
if err != nil {
t.Errorf("parseTimeArg(%v) error: %v", tt.args, err)
continue
}
if tt.wantNil {
if got != nil {
t.Errorf("parseTimeArg(%v) = %v, want nil", tt.args, got)
}
continue
}
if got == nil {
t.Errorf("parseTimeArg(%v) = nil, want %d:%02d", tt.args, tt.wantH, tt.wantM)
continue
}
if got.Hour() != tt.wantH || got.Minute() != tt.wantM {
t.Errorf("parseTimeArg(%v) = %d:%02d, want %d:%02d", tt.args, got.Hour(), got.Minute(), tt.wantH, tt.wantM)
}
}
}
func TestParseTimeArg_Invalid(t *testing.T) {
_, err := parseTimeArg([]string{"banana"})
if err == nil {
t.Fatal("expected error for invalid time")
}
}
func TestRun_NoArgs(t *testing.T) {
code := run([]string{"bamboo"})
if code != 1 {
t.Errorf("exit code = %d, want 1", code)
}
}
func TestRun_UnknownCommand(t *testing.T) {
noEnvFiles()
t.Setenv("BAMBOO_API_KEY", "k")
t.Setenv("BAMBOO_COMPANY", "c")
t.Setenv("BAMBOO_EMPLOYEE_ID", "1")
code := run([]string{"bamboo", "nope"})
if code != 1 {
t.Errorf("exit code = %d, want 1", code)
}
}