1
|
|
|
package client |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"testing" |
5
|
|
|
"log" |
6
|
|
|
"github.com/viasite/planfix-toggl-server/app/config" |
7
|
|
|
"github.com/popstas/planfix-go/planfix" |
8
|
|
|
"net/http/httptest" |
9
|
|
|
"net/http" |
10
|
|
|
"io/ioutil" |
11
|
|
|
"encoding/xml" |
12
|
|
|
"github.com/popstas/go-toggl" |
13
|
|
|
"reflect" |
14
|
|
|
"time" |
15
|
|
|
"bytes" |
16
|
|
|
"strings" |
17
|
|
|
) |
18
|
|
|
|
19
|
|
|
var output bytes.Buffer |
20
|
|
|
|
21
|
|
|
func assert(t *testing.T, data interface{}, expected interface{}) { |
22
|
|
|
if data != expected { |
23
|
|
|
t.Errorf("Expected %v, got, %v", expected, data) |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
func expectError(t *testing.T, err error, msg string) { |
27
|
|
|
if err == nil { |
28
|
|
|
t.Errorf("Expected error, got success %v", msg) |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
func expectSuccess(t *testing.T, err error, msg string) { |
32
|
|
|
if err != nil { |
33
|
|
|
t.Errorf("Expected success, got %v %v", err, msg) |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
type planfixRequestStruct struct { |
38
|
|
|
XMLName xml.Name `xml:"request"` |
39
|
|
|
Method string `xml:"method,attr"` |
40
|
|
|
Account string `xml:"account"` |
41
|
|
|
Sid string `xml:"sid"` |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
func fixtureFromFile(fixtureName string) string { |
45
|
|
|
buf, _ := ioutil.ReadFile("../../tests/fixtures/" + fixtureName) |
46
|
|
|
return string(buf) |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
type MockedServer struct { |
50
|
|
|
*httptest.Server |
51
|
|
|
Requests [][]byte |
52
|
|
|
Responses []string // fifo queue of answers |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
func NewMockedServer(responses []string) *MockedServer { |
56
|
|
|
s := &MockedServer{ |
57
|
|
|
Requests: [][]byte{}, |
58
|
|
|
Responses: responses, |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
s.Server = httptest.NewServer(s) |
62
|
|
|
return s |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
func (s *MockedServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) { |
66
|
|
|
lastRequest, err := ioutil.ReadAll(req.Body) |
67
|
|
|
if err != nil { |
68
|
|
|
panic(err) |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
rs := planfixRequestStruct{} |
72
|
|
|
err = xml.Unmarshal(lastRequest, &rs) |
73
|
|
|
if err != nil { |
74
|
|
|
panic(err) |
75
|
|
|
} |
76
|
|
|
s.Requests = append(s.Requests, lastRequest) |
77
|
|
|
answer := s.Responses[0] |
78
|
|
|
|
79
|
|
|
s.Responses = s.Responses[1:] |
80
|
|
|
resp.Write([]byte(answer)) |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
type MockedTogglSession struct { |
84
|
|
|
TogglSession |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
func (s *MockedTogglSession) GetAccount() (toggl.Account, error) { |
88
|
|
|
return toggl.Account{Data: struct { |
89
|
|
|
APIToken string `json:"api_token"` |
90
|
|
|
Timezone string `json:"timezone"` |
91
|
|
|
ID int `json:"id"` |
92
|
|
|
Workspaces []toggl.Workspace `json:"workspaces"` |
93
|
|
|
Clients []toggl.Client `json:"clients"` |
94
|
|
|
Projects []toggl.Project `json:"projects"` |
95
|
|
|
Tasks []toggl.Task `json:"tasks"` |
96
|
|
|
Tags []toggl.Tag `json:"tags"` |
97
|
|
|
TimeEntries []toggl.TimeEntry `json:"time_entries"` |
98
|
|
|
BeginningOfWeek int `json:"beginning_of_week"` |
99
|
|
|
}{ID: 123}}, nil |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
func (s *MockedTogglSession) GetDetailedReport(workspace int, since, until string, page int) (toggl.DetailedReport, error) { |
103
|
|
|
return toggl.DetailedReport{Data:[]toggl.DetailedTimeEntry{ |
104
|
|
|
{ |
105
|
|
|
ID: 1, |
106
|
|
|
Project: "project1", |
107
|
|
|
Description: "description1", |
108
|
|
|
Tags: []string{"12345", "sent"}, |
109
|
|
|
}, |
110
|
|
|
}}, nil |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
func newClient() TogglClient { |
114
|
|
|
cfg := config.Config{ |
115
|
|
|
TogglSentTag:"sent", |
116
|
|
|
} |
117
|
|
|
api := planfix.New("", "apiKey", "account", "user", "password") |
118
|
|
|
api.Sid = "123" |
119
|
|
|
|
120
|
|
|
sess := MockedTogglSession{} |
121
|
|
|
return TogglClient{ |
122
|
|
|
Session: &sess, |
123
|
|
|
Config: &cfg, |
124
|
|
|
PlanfixAPI: api, |
125
|
|
|
Logger: log.New(&output, "", log.LstdFlags), |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
func getTestEntries() []TogglPlanfixEntry { |
130
|
|
|
return []TogglPlanfixEntry{ |
131
|
|
|
{ |
132
|
|
|
toggl.DetailedTimeEntry{Duration: 1}, |
133
|
|
|
PlanfixEntryData{TaskID: 1, GroupCount: 1}, |
134
|
|
|
}, |
135
|
|
|
{ |
136
|
|
|
toggl.DetailedTimeEntry{Duration: 2}, |
137
|
|
|
PlanfixEntryData{TaskID: 1, GroupCount: 1}, |
138
|
|
|
}, |
139
|
|
|
{ |
140
|
|
|
toggl.DetailedTimeEntry{Duration: 3}, |
141
|
|
|
PlanfixEntryData{TaskID: 2, GroupCount: 1}, |
142
|
|
|
}, |
143
|
|
|
{ |
144
|
|
|
toggl.DetailedTimeEntry{Duration: 4}, |
145
|
|
|
PlanfixEntryData{TaskID: 2, GroupCount: 1}, |
146
|
|
|
}, |
147
|
|
|
{ |
148
|
|
|
toggl.DetailedTimeEntry{Duration: 5}, |
149
|
|
|
PlanfixEntryData{TaskID: 2, GroupCount: 1}, |
150
|
|
|
}, |
151
|
|
|
{ |
152
|
|
|
toggl.DetailedTimeEntry{Duration: 6}, |
153
|
|
|
PlanfixEntryData{TaskID: 3, GroupCount: 1}, |
154
|
|
|
}, |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
func getTestGroupedEntries() map[int][]TogglPlanfixEntry { |
159
|
|
|
return map[int][]TogglPlanfixEntry{ |
160
|
|
|
1: { |
161
|
|
|
{ |
162
|
|
|
toggl.DetailedTimeEntry{Duration: 1}, |
163
|
|
|
PlanfixEntryData{TaskID: 1, GroupCount: 1}, |
164
|
|
|
}, |
165
|
|
|
{ |
166
|
|
|
toggl.DetailedTimeEntry{Duration: 2}, |
167
|
|
|
PlanfixEntryData{TaskID: 1, GroupCount: 1}, |
168
|
|
|
}, |
169
|
|
|
}, |
170
|
|
|
2: { |
171
|
|
|
{ |
172
|
|
|
toggl.DetailedTimeEntry{Duration: 3}, |
173
|
|
|
PlanfixEntryData{TaskID: 2, GroupCount: 1}, |
174
|
|
|
}, |
175
|
|
|
{ |
176
|
|
|
toggl.DetailedTimeEntry{Duration: 4}, |
177
|
|
|
PlanfixEntryData{TaskID: 2, GroupCount: 1}, |
178
|
|
|
}, |
179
|
|
|
{ |
180
|
|
|
toggl.DetailedTimeEntry{Duration: 5}, |
181
|
|
|
PlanfixEntryData{TaskID: 2, GroupCount: 1}, |
182
|
|
|
}, |
183
|
|
|
}, |
184
|
|
|
3: { |
185
|
|
|
{ |
186
|
|
|
toggl.DetailedTimeEntry{Duration: 6}, |
187
|
|
|
PlanfixEntryData{TaskID: 3, GroupCount: 1}, |
188
|
|
|
}, |
189
|
|
|
}, |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
func TestTogglClient_SumEntriesGroup(t *testing.T) { |
194
|
|
|
c := newClient() |
195
|
|
|
groupedEntries := getTestGroupedEntries() |
196
|
|
|
expected := []TogglPlanfixEntry{ |
197
|
|
|
{ |
198
|
|
|
toggl.DetailedTimeEntry{Duration: 3}, |
199
|
|
|
PlanfixEntryData{TaskID: 1, GroupCount: 2}, |
200
|
|
|
}, |
201
|
|
|
{ |
202
|
|
|
toggl.DetailedTimeEntry{Duration: 12}, |
203
|
|
|
PlanfixEntryData{TaskID: 2, GroupCount: 3}, |
204
|
|
|
}, |
205
|
|
|
{ |
206
|
|
|
toggl.DetailedTimeEntry{Duration: 6}, |
207
|
|
|
PlanfixEntryData{TaskID: 3, GroupCount: 1}, |
208
|
|
|
}, |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
summed := c.SumEntriesGroup(groupedEntries) |
212
|
|
|
equals := reflect.DeepEqual(summed, expected) |
213
|
|
|
assert(t, equals, true) |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
func TestTogglClient_GroupEntriesByTask(t *testing.T) { |
217
|
|
|
c := newClient() |
218
|
|
|
entries := getTestEntries() |
219
|
|
|
expected := getTestGroupedEntries() |
220
|
|
|
|
221
|
|
|
grouped := c.GroupEntriesByTask(entries) |
222
|
|
|
equals := reflect.DeepEqual(grouped, expected) |
223
|
|
|
assert(t, equals, true) |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
func TestTogglClient_GroupEntriesByTask_empty(t *testing.T) { |
227
|
|
|
c := newClient() |
228
|
|
|
entries := []TogglPlanfixEntry{} |
229
|
|
|
expected := map[int][]TogglPlanfixEntry{} |
230
|
|
|
|
231
|
|
|
grouped := c.GroupEntriesByTask(entries) |
232
|
|
|
equals := reflect.DeepEqual(grouped, expected) |
233
|
|
|
assert(t, equals, true) |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
func TestTogglClient_sendEntries_dryRun(t *testing.T) { |
237
|
|
|
c := newClient() |
238
|
|
|
c.Config.DryRun = true |
239
|
|
|
now := time.Now() |
240
|
|
|
entries := []TogglPlanfixEntry{ |
241
|
|
|
{ |
242
|
|
|
toggl.DetailedTimeEntry{ |
243
|
|
|
Duration: 60000, |
244
|
|
|
Start: &now, |
245
|
|
|
Project: "project", |
246
|
|
|
Description: "description", |
247
|
|
|
}, |
248
|
|
|
PlanfixEntryData{TaskID: 1, GroupCount: 1}, |
249
|
|
|
}, |
250
|
|
|
{ |
251
|
|
|
toggl.DetailedTimeEntry{Duration: 120000}, |
252
|
|
|
PlanfixEntryData{TaskID: 1, GroupCount: 1}, |
253
|
|
|
}, |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
c.sendEntries(1, entries) |
257
|
|
|
assert(t, strings.Contains(output.String(), "[DEBUG] sending [project] description (3)"), true) |
258
|
|
|
assert(t, strings.Contains(output.String(), "[DEBUG] dry-run"), true) |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
func TestTogglClient_GetTogglUserID(t *testing.T) { |
262
|
|
|
c := newClient() |
263
|
|
|
togglUserID := c.GetTogglUserID() |
264
|
|
|
assert(t, togglUserID, 123) |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
func TestTogglClient_GetPlanfixUserID(t *testing.T) { |
268
|
|
|
c := newClient() |
269
|
|
|
ms := NewMockedServer([]string{ |
270
|
|
|
fixtureFromFile("user.get.xml"), |
271
|
|
|
//fixtureFromFile("error.xml"), |
272
|
|
|
}) |
273
|
|
|
c.PlanfixAPI.URL = ms.URL |
274
|
|
|
|
275
|
|
|
planfixUserID := c.GetPlanfixUserID() |
276
|
|
|
assert(t, planfixUserID, 9230) |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
func TestTogglClient_GetEntries(t *testing.T) { |
280
|
|
|
c := newClient() |
281
|
|
|
entries, err := c.GetEntries( |
282
|
|
|
c.Config.TogglWorkspaceID, |
283
|
|
|
time.Now().AddDate(0, 0, -30).Format("2006-01-02"), |
284
|
|
|
time.Now().AddDate(0, 0, 1).Format("2006-01-02"), |
285
|
|
|
) |
286
|
|
|
|
287
|
|
|
report, err := c.Session.GetDetailedReport( |
288
|
|
|
c.Config.TogglWorkspaceID, |
289
|
|
|
time.Now().AddDate(0, 0, -30).Format("2006-01-02"), |
290
|
|
|
time.Now().AddDate(0, 0, 1).Format("2006-01-02"), |
291
|
|
|
1, |
292
|
|
|
) |
293
|
|
|
|
294
|
|
|
expected := []TogglPlanfixEntry{ |
295
|
|
|
{ |
296
|
|
|
DetailedTimeEntry: report.Data[0], |
297
|
|
|
Planfix: PlanfixEntryData{GroupCount:1, Sent:true, TaskID:12345}, |
298
|
|
|
}, |
299
|
|
|
} |
300
|
|
|
equals := reflect.DeepEqual(entries, expected) |
301
|
|
|
expectSuccess(t, err, "TestTogglClient_GetEntries") |
302
|
|
|
assert(t, equals, true) |
303
|
|
|
} |