-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
256 lines (224 loc) · 5.16 KB
/
client_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
package ticktick
import (
"fmt"
"testing"
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
)
// ********* test utils ********* //
type TestProjectItem struct {
Name string `json:"name"`
Id string `json:"id"`
}
type TestTagItem struct {
Name string `json:"name"`
}
type testsyncTask struct {
Update []TaskItem `json:"update"`
}
type TestSyncResponse struct {
InboxId string `json:"inboxId"`
ProjectGroups []TestProjectItem `json:"projectGroups"`
ProjectProfiles []TestProjectItem `json:"projectProfiles"`
SyncTaskBean testsyncTask `json:"syncTaskBean"`
Tags []TestTagItem `json:"tags"`
}
func BuildSyncResponse() *TestSyncResponse {
sampleResponse := TestSyncResponse{
InboxId: "testinboxid",
ProjectGroups: []TestProjectItem{
{
Name: "pgname1",
Id: "pgid1",
},
{
Name: "pgname2",
Id: "pgid2",
},
},
ProjectProfiles: []TestProjectItem{
{
Name: "pname1",
Id: "pid1",
},
{
Name: "pname2",
Id: "pid2",
},
},
SyncTaskBean: testsyncTask{
Update: []TaskItem{
{
Id: "1",
Title: "1",
ProjectId: "pid1",
Tags: []string{"a", "b"},
StartDate: "2022-12-12T15:04:05.000+0000",
Priority: 5,
},
{
Id: "2",
Title: "2",
ProjectId: "pid1",
Tags: []string{"b", "c"},
StartDate: "2022-12-13T15:04:05.000+0000",
Priority: 0,
},
{
Id: "3",
Title: "3",
ProjectId: "pid2",
Tags: []string{"b", "c"},
StartDate: "2022-12-14T15:04:05.000+0000",
Priority: 1,
},
},
},
Tags: []TestTagItem{
{
Name: "a",
},
{
Name: "b",
},
{
Name: "c",
},
},
}
return &sampleResponse
}
func NewSignInTestServer() {
gock.New(baseUrlV2Test).
Post(signinUrlEndpoint).
MatchType("json").
JSON(map[string]string{
"username": "testuser",
"password": "testpass",
}).
Reply(200).
JSON(map[string]string{"token": "testtoken"})
}
func NewSyncTestServer(syncResponse *TestSyncResponse) {
gock.New(baseUrlV2Test).
Get(queryUnfinishedJobUrlEndpoint).
MatchHeader("Cookie", "t=testtoken").
Reply(200).
JSON(syncResponse)
}
func NewSignInTestServerWithProblem() {
gock.New(baseUrlV2Test).
Post(signinUrlEndpoint).
MatchType("json").
JSON(map[string]string{
"username": "testuser",
"password": "testpass",
}).
Reply(200).
JSON(map[string]string{"faketoken": "testtoken"})
}
// ********* test part ********* //
func TestGetToken(t *testing.T) {
defer gock.Off()
assert := assert.New(t)
// normal case
NewSignInTestServer()
client := &Client{
UserName: "testuser",
PassWord: "testpass",
baseUrlV2: baseUrlV2Test,
projectName2Id: make(map[string]string),
id2ProjectName: make(map[string]string),
}
err := client.GetToken()
assert.Nil(err)
assert.Equal("testtoken", client.loginToken)
// if no token is returned
NewSignInTestServerWithProblem()
err = client.GetToken()
if assert.NotNil(t, err) {
assert.Contains(fmt.Sprint(err), "no token found in the response, full response json is")
}
// if the server responses 404
gock.New(baseUrlV2Test).
Post(signinUrlEndpoint).
Reply(404)
err = client.GetToken()
assert.NotNil(err)
}
func TestSync(t *testing.T) {
defer gock.Off()
assert := assert.New(t)
// build response
syncResponse := BuildSyncResponse()
NewSyncTestServer(syncResponse)
// normal case
client := &Client{
UserName: "testuser",
PassWord: "testpass",
baseUrlV2: baseUrlV2Test,
projectName2Id: make(map[string]string),
id2ProjectName: make(map[string]string),
loginToken: "testtoken",
}
err := client.Sync()
assert.Nil(err)
// if the server responses 404
gock.New(baseUrlV2Test).
Get(queryUnfinishedJobUrlEndpoint).
Reply(404)
err = client.Sync()
assert.NotNil(t, err)
}
func TestInit(t *testing.T) {
defer gock.Off()
assert := assert.New(t)
NewSignInTestServer()
syncResponse := BuildSyncResponse()
NewSyncTestServer(syncResponse)
// normal case
client := &Client{
UserName: "testuser",
PassWord: "testpass",
baseUrlV2: baseUrlV2Test,
projectName2Id: make(map[string]string),
id2ProjectName: make(map[string]string),
}
err := client.Init()
assert.Nil(err)
// if GetToken has problem
gock.New(baseUrlV2Test).
Post(signinUrlEndpoint).
Reply(404)
err = client.Init()
assert.NotNil(err)
// if sync has problem
NewSignInTestServer()
gock.New(baseUrlV2Test).
Get(queryUnfinishedJobUrlEndpoint).
Reply(404)
err = client.Init()
assert.NotNil(err)
}
func TestNewClient(t *testing.T) {
defer gock.Off()
assert := assert.New(t)
// normal case
NewSignInTestServer()
syncResponse := BuildSyncResponse()
NewSyncTestServer(syncResponse)
client, err := NewClient("testuser", "testpass", "test")
assert.Nil(err)
assert.NotNil(client)
// if the server name is not correct
client, err = NewClient("testuser", "testpass", "random")
assert.NotNil(err)
assert.Nil(client)
// if Init has problem
gock.New(baseUrlV2Test).
Post(signinUrlEndpoint).
Reply(404)
client, err = NewClient("testuser", "testpass", "test")
assert.NotNil(err)
assert.Nil(client)
}