1
|
|
|
package planfix_test |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"encoding/xml" |
5
|
|
|
"github.com/popstas/planfix-go/planfix" |
6
|
|
|
"io/ioutil" |
7
|
|
|
"net/http" |
8
|
|
|
"net/http/httptest" |
9
|
|
|
"strings" |
10
|
|
|
"testing" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
func assert(t *testing.T, data interface{}, expected interface{}) { |
14
|
|
|
if data != expected { |
15
|
|
|
t.Errorf("Expected %v, got, %v", expected, data) |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
func expectError(t *testing.T, err error, msg string) { |
19
|
|
|
if err == nil { |
20
|
|
|
t.Errorf("Expected error, got success %v", msg) |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
func expectSuccess(t *testing.T, err error, msg string) { |
24
|
|
|
if err != nil { |
25
|
|
|
t.Errorf("Expected success, got %v %v", err, msg) |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
type requestStruct struct { |
30
|
|
|
XMLName xml.Name `xml:"request"` |
31
|
|
|
Method string `xml:"method,attr"` |
32
|
|
|
Account string `xml:"account"` |
33
|
|
|
Sid string `xml:"sid"` |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
func fixtureFromFile(fixtureName string) string { |
37
|
|
|
buf, _ := ioutil.ReadFile("../tests/fixtures/" + fixtureName) |
38
|
|
|
return string(buf) |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
type MockedServer struct { |
42
|
|
|
*httptest.Server |
43
|
|
|
Requests [][]byte |
44
|
|
|
Responses []string // fifo queue of answers |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
func NewMockedServer(responses []string) *MockedServer { |
48
|
|
|
s := &MockedServer{ |
49
|
|
|
Requests: [][]byte{}, |
50
|
|
|
Responses: responses, |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
s.Server = httptest.NewServer(s) |
54
|
|
|
return s |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
func (s *MockedServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) { |
58
|
|
|
lastRequest, err := ioutil.ReadAll(req.Body) |
59
|
|
|
if err != nil { |
60
|
|
|
panic(err) |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
rs := requestStruct{} |
64
|
|
|
err = xml.Unmarshal(lastRequest, &rs) |
65
|
|
|
if err != nil { |
66
|
|
|
panic(err) |
67
|
|
|
} |
68
|
|
|
s.Requests = append(s.Requests, lastRequest) |
69
|
|
|
answer := s.Responses[0] |
70
|
|
|
|
71
|
|
|
// simulate network error |
72
|
|
|
if answer == "panic" { |
73
|
|
|
panic(err) |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// simulate 502 |
77
|
|
|
if answer == "502" { |
78
|
|
|
resp.WriteHeader(http.StatusBadGateway) |
79
|
|
|
} |
80
|
|
|
s.Responses = s.Responses[1:] |
81
|
|
|
resp.Write([]byte(answer)) |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
func newAPI(responses []string) planfix.API { |
85
|
|
|
ms := NewMockedServer(responses) |
86
|
|
|
api := planfix.New(ms.URL, "apiKey", "account", "user", "password") |
87
|
|
|
api.Sid = "123" |
88
|
|
|
return api |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
func TestAPI_ErrorCode(t *testing.T) { |
92
|
|
|
api := newAPI([]string{fixtureFromFile("error.xml")}) |
93
|
|
|
_, err := api.AuthLogin(api.User, api.Password) |
94
|
|
|
expectError(t, err, "TestAPI_ErrorCode") |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
func TestAPI_ErrorCodeUnknown(t *testing.T) { |
98
|
|
|
api := newAPI([]string{fixtureFromFile("error.unknown.xml")}) |
99
|
|
|
_, err := api.AuthLogin(api.User, api.Password) |
100
|
|
|
if !strings.Contains(string(err.Error()), "Неизвестная ошибка") { |
101
|
|
|
t.Error("Failed to output unknown error") |
102
|
|
|
} |
103
|
|
|
expectError(t, err, "TestAPI_ErrorCodeUnknown") |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// auth.login |
107
|
|
|
func TestAPI_AuthLogin(t *testing.T) { |
108
|
|
|
api := newAPI([]string{fixtureFromFile("auth.login.xml")}) |
109
|
|
|
api.Sid = "" |
110
|
|
|
answer, err := api.AuthLogin(api.User, api.Password) |
111
|
|
|
|
112
|
|
|
expectSuccess(t, err, "TestAPI_AuthLogin") |
113
|
|
|
assert(t, answer, "sid_after_login") |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// fail to authenticate |
117
|
|
|
func TestAPI_EnsureAuthenticatedFailed(t *testing.T) { |
118
|
|
|
api := newAPI([]string{fixtureFromFile("error.xml")}) |
119
|
|
|
api.Sid = "" |
120
|
|
|
_, err := api.ActionGet(456) |
121
|
|
|
|
122
|
|
|
expectError(t, err, "TestAPI_EnsureAuthenticatedFailed") |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// authenticate before api request if not authenticated |
126
|
|
|
func TestAPI_EnsureAuthenticated(t *testing.T) { |
127
|
|
|
api := newAPI([]string{ |
128
|
|
|
fixtureFromFile("auth.login.xml"), |
129
|
|
|
fixtureFromFile("action.get.xml"), |
130
|
|
|
fixtureFromFile("action.get.xml"), |
131
|
|
|
}) |
132
|
|
|
api.Sid = "" |
133
|
|
|
_, _ = api.ActionGet(456) |
134
|
|
|
assert(t, api.Sid, "sid_after_login") |
135
|
|
|
|
136
|
|
|
api.Sid = "789" |
137
|
|
|
_, _ = api.ActionGet(456) |
138
|
|
|
assert(t, api.Sid, "789") |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// reauthenticate if session expired |
142
|
|
|
func TestAPI_AuthenticatedExpire(t *testing.T) { |
143
|
|
|
api := newAPI([]string{ |
144
|
|
|
fixtureFromFile("error.sessionExpired.xml"), |
145
|
|
|
fixtureFromFile("auth.login.xml"), |
146
|
|
|
fixtureFromFile("action.get.xml"), |
147
|
|
|
}) |
148
|
|
|
action, err := api.ActionGet(456) |
149
|
|
|
|
150
|
|
|
expectSuccess(t, err, "TestAPI_AuthenticatedExpire") |
151
|
|
|
assert(t, action.Action.TaskID, 1128468) |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// error response while reauthenticate |
155
|
|
|
func TestAPI_AuthenticatedExpireFailed(t *testing.T) { |
156
|
|
|
api := newAPI([]string{ |
157
|
|
|
fixtureFromFile("error.sessionExpired.xml"), |
158
|
|
|
fixtureFromFile("error.xml"), |
159
|
|
|
}) |
160
|
|
|
_, err := api.ActionGet(456) |
161
|
|
|
|
162
|
|
|
expectError(t, err, "TestAPI_AuthenticatedExpireFailed") |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// error response after reauthenticated session |
166
|
|
|
func TestAPI_AuthenticatedExpireFailedAfter(t *testing.T) { |
167
|
|
|
api := newAPI([]string{ |
168
|
|
|
fixtureFromFile("error.sessionExpired.xml"), |
169
|
|
|
fixtureFromFile("auth.login.xml"), |
170
|
|
|
fixtureFromFile("error.xml"), |
171
|
|
|
}) |
172
|
|
|
_, err := api.ActionGet(456) |
173
|
|
|
|
174
|
|
|
expectError(t, err, "TestAPI_AuthenticatedExpireFailedAfter") |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// invalid xml |
178
|
|
|
func TestAPI_InvalidXML(t *testing.T) { |
179
|
|
|
api := newAPI([]string{fixtureFromFile("error.invalidXML.xml")}) |
180
|
|
|
_, err := api.ActionGet(123) |
181
|
|
|
expectError(t, err, "TestAPI_InvalidXML") |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// network error |
185
|
|
|
func TestAPI_NetworkError(t *testing.T) { |
186
|
|
|
api := newAPI([]string{"panic"}) |
187
|
|
|
_, err := api.ActionGet(123) |
188
|
|
|
expectError(t, err, "TestAPI_NetworkError") |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// 502 error |
192
|
|
|
func TestAPI_502Error(t *testing.T) { |
193
|
|
|
api := newAPI([]string{"502"}) |
194
|
|
|
_, err := api.ActionGet(123) |
195
|
|
|
expectError(t, err, "TestAPI_502Error") |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// action.get |
199
|
|
|
func TestAPI_ActionGet(t *testing.T) { |
200
|
|
|
api := newAPI([]string{fixtureFromFile("action.get.xml")}) |
201
|
|
|
var action planfix.XMLResponseActionGet |
202
|
|
|
action, err := api.ActionGet(123) |
203
|
|
|
|
204
|
|
|
expectSuccess(t, err, "TestAPI_ActionGet") |
205
|
|
|
assert(t, action.Action.TaskID, 1128468) |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// action.getList |
209
|
|
|
func TestAPI_ActionGetList(t *testing.T) { |
210
|
|
|
api := newAPI([]string{fixtureFromFile("action.getList.xml")}) |
211
|
|
|
request := planfix.XMLRequestActionGetList{ |
212
|
|
|
TaskGeneral: 525330, |
213
|
|
|
} |
214
|
|
|
var actionList planfix.XMLResponseActionGetList |
215
|
|
|
actionList, err := api.ActionGetList(request) |
216
|
|
|
|
217
|
|
|
expectSuccess(t, err, "TestAPI_ActionGetList") |
218
|
|
|
assert(t, actionList.Actions.ActionsTotalCount, 31) |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// analitic.getList |
222
|
|
|
func TestAPI_AnaliticGetList(t *testing.T) { |
223
|
|
|
api := newAPI([]string{fixtureFromFile("analitic.getList.xml")}) |
224
|
|
|
var analiticList planfix.XMLResponseAnaliticGetList |
225
|
|
|
analiticList, err := api.AnaliticGetList(0) |
226
|
|
|
|
227
|
|
|
expectSuccess(t, err, "TestAPI_AnaliticGetList") |
228
|
|
|
assert(t, analiticList.Analitics.AnaliticsTotalCount, 2) |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
// analitic.getOptiions |
232
|
|
|
func TestAPI_AnaliticGetOptions(t *testing.T) { |
233
|
|
|
api := newAPI([]string{fixtureFromFile("analitic.getOptions.xml")}) |
234
|
|
|
var analitic planfix.XMLResponseAnaliticGetOptions |
235
|
|
|
analitic, err := api.AnaliticGetOptions(123) |
236
|
|
|
|
237
|
|
|
expectSuccess(t, err, "TestAPI_AnaliticGetOptions") |
238
|
|
|
assert(t, analitic.Analitic.GroupID, 1) |
239
|
|
|
assert(t, analitic.Analitic.Fields[0].HandbookID, 131) |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
// analitic.getHandbook |
243
|
|
|
func TestAPI_AnaliticGetHandbook(t *testing.T) { |
244
|
|
|
api := newAPI([]string{fixtureFromFile("analitic.getHandbook.xml")}) |
245
|
|
|
var handbook planfix.XMLResponseAnaliticGetHandbook |
246
|
|
|
handbook, err := api.AnaliticGetHandbook(123) |
247
|
|
|
|
248
|
|
|
expectSuccess(t, err, "TestAPI_AnaliticGetHandbook") |
249
|
|
|
assert(t, handbook.Records[4].Values[0].Value, "Поминутная работа программиста") |
250
|
|
|
assert(t, handbook.Records[4].ValuesMap["Название"], "Поминутная работа программиста") |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
// action.add |
254
|
|
|
func TestAPI_ActionAdd(t *testing.T) { |
255
|
|
|
api := newAPI([]string{fixtureFromFile("action.add.xml")}) |
256
|
|
|
request := planfix.XMLRequestActionAdd{ |
257
|
|
|
TaskGeneral: 123, |
258
|
|
|
Description: "asdf", |
259
|
|
|
} |
260
|
|
|
var actionAdded planfix.XMLResponseActionAdd |
261
|
|
|
actionAdded, err := api.ActionAdd(request) |
262
|
|
|
|
263
|
|
|
expectSuccess(t, err, "TestAPI_ActionAdd") |
264
|
|
|
assert(t, actionAdded.ActionID, 123) |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// action.add both task and contact defined |
268
|
|
|
func TestAPI_ActionAddBothTaskContact(t *testing.T) { |
269
|
|
|
api := newAPI([]string{fixtureFromFile("action.add.xml")}) |
270
|
|
|
request := planfix.XMLRequestActionAdd{ |
271
|
|
|
TaskGeneral: 123, |
272
|
|
|
ContactGeneral: 123, |
273
|
|
|
Description: "asdf", |
274
|
|
|
} |
275
|
|
|
_, err := api.ActionAdd(request) |
276
|
|
|
|
277
|
|
|
expectError(t, err, "TestAPI_ActionAddBothTaskContact") |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// task.get |
281
|
|
|
func TestAPI_TaskGet(t *testing.T) { |
282
|
|
|
api := newAPI([]string{fixtureFromFile("task.get.xml")}) |
283
|
|
|
var task planfix.XMLResponseTaskGet |
284
|
|
|
task, err := api.TaskGet(123, 0) |
285
|
|
|
|
286
|
|
|
expectSuccess(t, err, "TestAPI_TaskGet") |
287
|
|
|
assert(t, task.Task.ProjectID, 9830) |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
// user.get |
291
|
|
|
func TestAPI_UserGet(t *testing.T) { |
292
|
|
|
api := newAPI([]string{fixtureFromFile("user.get.xml")}) |
293
|
|
|
var user planfix.XMLResponseUserGet |
294
|
|
|
user, err := api.UserGet(0) |
295
|
|
|
|
296
|
|
|
expectSuccess(t, err, "TestAPI_UserGet") |
297
|
|
|
assert(t, user.User.Login, "popstas") |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
// user.getList |
301
|
|
|
func TestAPI_UserGetList(t *testing.T) { |
302
|
|
|
api := newAPI([]string{fixtureFromFile("user.getList.xml")}) |
303
|
|
|
var userList planfix.XMLResponseUserGetList |
304
|
|
|
var err error |
305
|
|
|
|
306
|
|
|
userList, err = api.UserGetList(planfix.XMLRequestUserGetList{}) |
307
|
|
|
expectSuccess(t, err, "TestAPI_UserGetList") |
308
|
|
|
assert(t, userList.Users.UsersTotalCount, 2) |
309
|
|
|
|
310
|
|
|
userList, err = api.UserGetList(planfix.XMLRequestUserGetList{Status: "sdl;kfj"}) |
311
|
|
|
expectError(t, err, "TestAPI_UserGetList_invalid_status") |
312
|
|
|
|
313
|
|
|
userList, err = api.UserGetList(planfix.XMLRequestUserGetList{SortType: "sdl;kfj"}) |
314
|
|
|
expectError(t, err, "TestAPI_UserGetList_invalid_sort_type") |
315
|
|
|
} |
316
|
|
|
|