-
Notifications
You must be signed in to change notification settings - Fork 4
/
todoist_test.go
482 lines (408 loc) · 11.4 KB
/
todoist_test.go
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
476
477
478
479
480
481
482
package todoist
import (
"bytes"
"context"
"log"
"net/http"
"net/url"
"os"
"reflect"
"strconv"
"strings"
"testing"
"time"
)
var (
// Get the Todoist API token from an environment variable
apiToken = os.Getenv("TODOIST_API_TOKEN")
)
func Test_Projects(t *testing.T) {
// Create the client to interact with Todoist
client, err := NewClient(apiToken)
if err != nil {
t.Fatal(err)
}
client.SetDebug(false)
// List all projects
projects, _, err := client.Projects.List(context.Background(), "")
if err != nil {
t.Fatal(err)
}
// Add a new project
// Specify a TempID if you want to use it in the future, otherwise it will create one for you
parentProjectTempID := "project1"
_, resp, err := client.Projects.Add(context.Background(), "", AddProject{
Name: "Parent Project",
TempID: parentProjectTempID,
})
if err != nil {
t.Fatal(err)
}
parentProjectID := strconv.Itoa(int(resp.TempIDMapping[parentProjectTempID]))
childProject1TempID := "project2"
_, resp, err = client.Projects.Add(context.Background(), "", AddProject{
Name: "Child Project 1",
TempID: childProject1TempID,
})
if err != nil {
t.Fatal(err)
}
childProject1ID := strconv.Itoa(int(resp.TempIDMapping[childProject1TempID]))
childProject2TempID := "project3"
_, resp, err = client.Projects.Add(context.Background(), "", AddProject{
Name: "Child Project 2",
TempID: childProject2TempID,
})
if err != nil {
t.Fatal(err)
}
childProject2ID := strconv.Itoa(int(resp.TempIDMapping[childProject2TempID]))
// Update the project we just added
_, _, err = client.Projects.Update(context.Background(), "", UpdateProject{
// get the temp ID of the project we just added so we can update the title
ID: parentProjectID,
Name: "Updated Project 1",
})
if err != nil {
t.Fatal(err)
}
// Make project 2 a child of project 1
_, _, err = client.Projects.Move(context.Background(), "", MoveProject{
ID: childProject1ID,
ParentID: parentProjectID,
})
if err != nil {
t.Fatal(err)
}
// Make project 3 a child of project 1
_, _, err = client.Projects.Move(context.Background(), "", MoveProject{
ID: childProject2ID,
ParentID: parentProjectID,
})
if err != nil {
t.Fatal(err)
}
_, _, err = client.Projects.Reorder(context.Background(), "", ReorderProjects{
Projects: []ReorderedProject{
{
ID: childProject2ID,
ChildOrder: 0,
},
},
})
if err != nil {
t.Fatal(err)
}
_, _, err = client.Projects.Archive(context.Background(), "", ArchiveProject{
ID: parentProjectID,
})
if err != nil {
t.Fatal(err)
}
projects, _, err = client.Projects.Unarchive(context.Background(), "", UnarchiveProject{
ID: childProject2ID,
})
if err != nil {
t.Fatal(err)
}
_, err = client.Projects.GetProjectInfo(context.Background(), "", parentProjectID, true)
if err != nil {
t.Fatal(err)
}
_, err = client.Projects.GetProjectData(context.Background(), "", parentProjectID)
if err != nil {
t.Fatal(err)
}
archivedProejcts, err := client.Projects.GetArchivedProjects(context.Background(), "", nil)
if err != nil {
t.Fatal(err)
}
archivedProjectsP, err := client.Projects.GetArchivedProjects(context.Background(), "", &Pagination{Limit: 1, Offset: 0})
if err != nil {
t.Fatal(err)
}
if len(archivedProjectsP) != 1 {
t.Fatalf("expected the number of archived projects returned to be 1, received %d", len(archivedProjectsP))
}
// Cleanup
for _, project := range projects {
if _, _, err = client.Projects.Delete(context.Background(), "", DeleteProject{
ID: strconv.Itoa(project.ID),
}); err != nil {
t.Fatal(err)
}
}
for _, archivedProject := range archivedProejcts {
if _, _, err = client.Projects.Delete(context.Background(), "", DeleteProject{
ID: strconv.Itoa(archivedProject.ID),
}); err != nil {
t.Fatal(err)
}
}
}
func Test_Sections(t *testing.T) {
client, err := NewClient(apiToken)
if err != nil {
t.Fatal(err)
}
client.SetDebug(false)
tempInboxSectionID := "inboxSectionID"
_, resp, err := client.Sections.Add(context.Background(), "", AddSection{
Name: "New Inbox section",
ProjectID: 2252888543, // Inbox project
SectionOrder: 0,
TempID: tempInboxSectionID,
})
if err != nil {
t.Fatal(err)
}
inboxSectionID := strconv.Itoa(int(resp.TempIDMapping[tempInboxSectionID]))
_, _, err = client.Sections.Update(context.Background(), "", UpdateSection{
ID: inboxSectionID,
Name: "Updated Inbox section",
Collapsed: true,
})
if err != nil {
t.Fatal(err)
}
tempSectionMoveProjectID := "sectionMoveProjectID"
_, resp, err = client.Projects.Add(context.Background(), "", AddProject{
Name: "Section Move test",
TempID: tempSectionMoveProjectID,
})
if err != nil {
t.Fatal(err)
}
sectionMoveProjectID := resp.TempIDMapping[tempSectionMoveProjectID]
strSectionMoveProjectID := strconv.Itoa(int(resp.TempIDMapping[tempSectionMoveProjectID]))
_, _, err = client.Sections.Move(context.Background(), "", MoveSection{
ID: inboxSectionID,
ProjectID: strSectionMoveProjectID,
})
if err != nil {
t.Fatal(err)
}
tempSectionReorderSectionID := "sectionReorderSectionID"
_, resp, err = client.Sections.Add(context.Background(), "", AddSection{
Name: "Reorder section test",
ProjectID: sectionMoveProjectID,
TempID: tempSectionReorderSectionID,
})
if err != nil {
t.Fatal(err)
}
sectionReorderSectionID := strconv.Itoa(int(resp.TempIDMapping[tempSectionReorderSectionID]))
_, _, err = client.Sections.Reorder(context.Background(), "", ReorderSections{
Sections: []ReorderedSection{
{
ID: inboxSectionID,
SectionOrder: 2,
},
{
ID: sectionReorderSectionID,
SectionOrder: 1,
},
},
})
if err != nil {
t.Fatal(err)
}
_, _, err = client.Sections.Archive(context.Background(), "", ArchiveSection{
ID: sectionReorderSectionID,
})
if err != nil {
t.Fatal(err)
}
_, _, err = client.Sections.Unarchive(context.Background(), "", UnarchiveSection{
ID: sectionReorderSectionID,
})
if err != nil {
t.Fatal(err)
}
// Cleanup
_, _, err = client.Projects.Delete(context.Background(), "", DeleteProject{
ID: strSectionMoveProjectID,
})
if err != nil {
t.Fatal(err)
}
sections, _, err := client.Sections.List(context.Background(), "")
if err != nil {
t.Fatal(err)
}
for _, section := range sections {
_, _, err = client.Sections.Delete(context.Background(), "", DeleteSection{
ID: strconv.Itoa(section.ID),
})
if err != nil {
t.Fatal(err)
}
}
}
func Test_Tasks(t *testing.T) {
client, err := NewClient(apiToken)
if err != nil {
t.Fatal(err)
}
client.SetDebug(true)
// TODO - add more testing for Add
_, _, err = client.Tasks.Add(context.Background(), "", AddTask{
Content: "New task content",
Description: "New task description",
Priority: 4,
})
if err != nil {
t.Fatal(err)
}
tasks, _, err := client.Tasks.List(context.Background(), "")
if err != nil {
t.Fatal(err)
}
for _, task := range tasks {
t.Logf("%+v\n", task)
}
}
func Test_Client_Logging(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()
c, _ := NewClient("12345")
// Non-debug Logln
c.Logln("test")
cString := buf.String()
buf.Reset()
if cString != "" {
t.Errorf("expected client to not log, got '%s'", cString)
}
// Non-debug Logf
c.Logf("test %s", "case")
cString = buf.String()
buf.Reset()
if cString != "" {
t.Errorf("expected client to not log, got '%s'", cString)
}
c1, _ := NewClient("12345")
c1.SetDebug(true)
// Debug Logln
c1.Logln("test")
cString = buf.String()
buf.Reset()
if !strings.HasSuffix(cString, "test\n") {
t.Errorf("expected client to log, got '%s'", cString)
}
// Debug Logf
c1.Logf("test %s", "case")
cString = buf.String()
buf.Reset()
if !strings.HasSuffix(cString, "test case\n") {
t.Errorf("expected client to log, got '%s'", cString)
}
}
func Test_NewClient(t *testing.T) {
_, err := NewClient("")
if err == nil {
t.Error("expected an error for an empty API token, got nil")
}
c, _ := NewClient("12345")
emptyClient := &http.Client{}
if !reflect.DeepEqual(emptyClient, c.client) {
t.Errorf("expected http client to be 'emptyClient', got %+v", c.client)
}
c1, _ := NewClient("12345")
timeoutHTTPClient := &http.Client{Timeout: 5 * time.Second}
c1.SetHTTPClient(timeoutHTTPClient)
if c1.client != timeoutHTTPClient {
t.Errorf("expected http client to be 'timeoutHTTPClient', got %+v", c1.client)
}
if c1.debug != false {
t.Error("expected client debug flag to be false, got true")
}
c1.SetDebug(true)
if c1.debug != true {
t.Error("expected client debug flag to be true, got false")
}
c2, err := NewClient("12345")
c2.SetDebug(true)
if err != nil {
t.Errorf("expected no error, received %v", err)
}
if c2.debug != true {
t.Errorf("expected client debug flag to be true, got %t", c2.debug)
}
}
func Test_NewRequest(t *testing.T) {
c, err := NewClient("12345")
if err != nil {
t.Errorf("expcted no error, received %v", err)
}
req, err := c.NewRequest("", []string{}, nil)
if err != nil {
t.Errorf("expcted no error, received %v", err)
}
syncTokenFormValue := req.FormValue("sync_token")
if syncTokenFormValue != "*" {
t.Errorf("sync_token should default to \"*\", received %s", syncTokenFormValue)
}
resourceTypesFormValue := req.FormValue("resource_types")
if resourceTypesFormValue != "[\"all\"]" {
t.Errorf("resource_types should default to [\"all\"], received %s", resourceTypesFormValue)
}
if commandsFormValue, exists := req.Form["commands"]; exists {
t.Errorf("commands should not be included in form, received %s", commandsFormValue)
}
tokenFormValue := req.FormValue("token")
if tokenFormValue != c.APIToken {
t.Errorf("token should be %s, received %s", c.APIToken, tokenFormValue)
}
contentType := req.Header.Get("Content-Type")
if contentType != "application/x-www-form-urlencoded" {
t.Errorf("Content-Type header must be application/x-www-form-urlencoded, received %s", contentType)
}
userAgent := req.Header.Get("User-Agent")
if userAgent != c.userAgent {
t.Errorf("User-Agent should be %s, received %s", c.userAgent, userAgent)
}
c.userAgent = ""
req, _ = c.NewRequest("", []string{"projects"}, []Command{
{
Type: "command_type",
Args: "args",
UUID: "uuid",
TempID: "temp_id",
},
})
resourceTypesFormValue = req.FormValue("resource_types")
if resourceTypesFormValue != "[\"projects\"]" {
t.Errorf("resource_types JSONified incorrectly, received %s", resourceTypesFormValue)
}
_, exists := req.Form["commands"]
if !exists {
t.Error("commands expected in form, but were not included")
} else {
commandsFormValue := req.Form.Get("commands")
if commandsFormValue != `[{"type":"command_type","args":"args","uuid":"uuid","temp_id":"temp_id"}]` {
t.Errorf("commands JSONified incorrectly, received %s", commandsFormValue)
}
}
if userAgent, exists := req.Header["User-Agent"]; exists {
t.Errorf("User-Agent should not be set in request, received %s", userAgent)
}
_, err = c.NewRequest("", []string{"all"}, []Command{
{
Type: "type",
Args: c.client, // Just need something that is unserializable
UUID: "uuid",
TempID: "temp_id",
},
})
if err == nil {
t.Error("expected err serializing commands, received nil")
}
c.BaseURL = &url.URL{Host: "localhost#bad-url"}
_, err = c.NewRequest("", []string{"all"}, nil)
if err == nil {
t.Errorf("expected err in new request, received nil")
}
}