forked from sadbox/mediawiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediawiki_test.go
398 lines (370 loc) · 11.7 KB
/
mediawiki_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
package mediawiki
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
const (
editTokenReponse = `{"query":{"pages":{"15580374":{"pageid":15580374,"ns":0,"title":"Main Page","contentmodel":"wikitext","pagelanguage":"en","touched":"2013-11-02T14:08:05Z","lastrevid":574690625,"counter":"","length":6391,"starttimestamp":"2013-11-02T15:24:43Z","edittoken":"+\\","revisions":[{"revid":574690625,"parentid":574690493,"minor":"","user":"Tariqabjotu","timestamp":"2013-09-27T03:10:17Z","comment":"removing unnecessary pipe"}]}}}}`
firstLogin = `{"login":{"result":"NeedToken","token":"8f48670ddc7fa9d5fa7e7fa2ae147e80","cookieprefix":"wikidb","sessionid":"927e0d298f6f3b5bb21228803fd9c0eb"}}`
secondLogin = `{"login":{"result":"Success","token":"8f48670ddc7fa9d5fa7e7fa2ae147e80","cookieprefix":"wikidb","sessionid":"927e0d298f6f3b5bb21228803fd9c0eb"}}`
failedLogin = `{"login":{"result":"ERROR THING","token":"8f48670ddc7fa9d5fa7e7fa2ae147e80","cookieprefix":"wikidb","sessionid":"927e0d298f6f3b5bb21228803fd9c0eb"}}`
readPage = `{"query-continue":{"revisions":{"rvcontinue":574690493}},"query":{"pages":{"15580374":{"pageid":15580374,"ns":0,"title":"Main Page","revisions":[{"user":"Tariqabjotu","timestamp":"2013-09-27T03:10:17Z","comment":"removing unnecessary pipe","contentformat":"text/x-wiki","contentmodel":"wikitext","*":"FULL PAGE TEXT"}]}}}}`
fileURL = `{"query":{"pages":{"107":{"pageid":107,"ns":6,"title":"File:stuff.pdf","imagerepository":"local","imageinfo":[{"url":"%s","descriptionurl":"TEST"}]}}}}`
fileURLFailed = `{"query":{"pages":{"544100":{"pageid":544100,"ns":0,"title":"Asdf"}}}}`
mwerror = `{"servedby":"mw1123","error":{"code":"unknown_action","info":"Unrecognized value for parameter 'action': blah"}}`
editsuccess = `{"Edit":{"result":"Success","pageid":12,"title":"Talk:Main Page","oldrevid":465,"newrevid":471}}`
editfailure = `{"Edit":{"result":"Failure!","pageid":12,"title":"Talk:Main Page","oldrevid":465,"newrevid":471}}`
)
type Test struct {
ts *httptest.Server
client *MWApi
}
func (t *Test) TearDown() {
t.ts.Close()
}
func BuildUp(response string, t *testing.T) *Test {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, response)
}))
client, err := New(ts.URL, "TESTING")
if err != nil {
t.Fatalf("Error in BuildUp: %s", err)
}
return &Test{ts: ts, client: client}
}
func TestGetEditToken(t *testing.T) {
test := BuildUp(editTokenReponse, t)
defer test.TearDown()
err := test.client.GetEditToken()
if err != nil {
t.Errorf("Could not get edit token: %s", err.Error())
} else {
t.Log("Got edit token")
}
if test.client.edittoken == `+\` {
t.Log("Edit token correct")
} else {
t.Errorf("Incorrect edit token: %s", test.client.edittoken)
}
}
func TestLogin(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
panic(err)
}
if r.Form.Get("lgtoken") == "" {
fmt.Fprintln(w, firstLogin)
} else {
fmt.Fprintln(w, secondLogin)
}
}))
defer ts.Close()
client, err := New(ts.URL, "TESTING")
if err != nil {
t.Fatalf("Error creating client: %s", err.Error())
}
client.Domain = "asdf"
err = client.Login("asdf", "asdf")
if err != nil {
t.Errorf("Client failed to login: %s", err)
} else {
t.Log("Client logged in successfully.")
}
}
func TestLoginFailedSecondary(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
panic(err)
}
if r.Form.Get("lgtoken") == "" {
fmt.Fprintln(w, firstLogin)
} else {
fmt.Fprintln(w, failedLogin)
}
}))
defer ts.Close()
client, err := New(ts.URL, "TESTING")
if err != nil {
t.Fatalf("Error creating client: %s", err)
}
client.Domain = "asdf"
err = client.Login("asdf", "asdf")
if err == nil {
t.Error("Client logged in successfully. (BUT THIS IS BAD!)")
} else {
t.Logf("Client failed to login: %s (BUT THIS IS GOOD!)", err)
}
}
func TestLoginNoPW(t *testing.T) {
test := BuildUp(failedLogin, t)
defer test.TearDown()
err := test.client.Login("", "")
if err != nil {
t.Log("Client refused to login with password.")
} else {
t.Error("Client did not a return an error with no password set")
}
}
func TestLoginFailed(t *testing.T) {
test := BuildUp(failedLogin, t)
defer test.TearDown()
err := test.client.Login("asdf", "JKLa")
if err != nil {
t.Logf("Failed to log in: %s", err.Error())
} else {
t.Error("Client logged in successfully to incorrect response")
}
}
func TestPostForm(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
panic(err)
}
if r.Header.Get("user-agent") != "mediawiki (Golang) https://github.com/sadbox/mediawiki TESTING" {
fmt.Fprintln(w, "USERAGENT")
} else {
fmt.Fprintln(w, r.Form.Get("KEY"))
}
}))
defer ts.Close()
client, err := New(ts.URL, "TESTING")
if err != nil {
t.Fatalf("Error creating client: %s", err.Error())
}
value, err := client.postForm(url.Values{"KEY": []string{"VALUE"}})
if err != nil {
t.Errorf("Error posting data: %s", err.Error())
}
returnValue := strings.TrimSpace(string(value))
if returnValue == "VALUE" {
t.Log("Successfully posted to web service.")
} else if returnValue == "USERAGENT" {
t.Error("Wrong useragent used!")
} else {
t.Error("Did not retrieve right value from web service")
}
}
func TestAPI(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
panic(err)
}
if r.Form.Get("KEY") != "VALUE" || r.Form.Get("OTHER KEY") != "OTHER VALUE" || r.Form.Get("format") != "json" {
fmt.Fprintln(w, `{"status":"FAIL"}`)
} else {
fmt.Fprintln(w, `{"status":"PASS"}`)
}
}))
defer ts.Close()
client, err := New(ts.URL, "TESTING")
if err != nil {
t.Fatalf("Error creating client: %s", err.Error())
}
body, err := client.API(map[string]string{"KEY": "VALUE", "OTHER KEY": "OTHER VALUE"})
if err != nil {
t.Fatalf("API() returned a non-nil error: %s", err.Error())
}
if strings.TrimSpace(string(body)) == `{"status":"PASS"}` {
t.Log("Successfully posted all information via API() call")
} else {
t.Error("Did not get PASS back from test server when posting via API()")
}
}
func TestRead(t *testing.T) {
test := BuildUp(readPage, t)
defer test.TearDown()
page, err := test.client.Read("TESTING PAGE")
if err != nil {
t.Fatalf("Unable to read page: %s", err)
}
if page.Revisions[0].Body != "FULL PAGE TEXT" {
t.Error("Page content not correct")
}
}
func TestDownload(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
panic(err)
}
if r.Method == "POST" {
fmt.Fprintln(w, fmt.Sprintf(fileURL, r.Form.Get("titles")))
} else if r.Method == "GET" {
fmt.Fprintf(w, `THINGS`)
}
}))
defer ts.Close()
client, err := New(ts.URL, "TESTING")
if err != nil {
t.Fatalf("Error creating client: %s", err.Error())
}
file, err := client.Download(ts.URL)
if err != nil {
t.Fatalf("Error downloading file: %s", err.Error())
}
defer file.Close()
returned, err := ioutil.ReadAll(file)
if err != nil {
t.Fatalf("Error reading downloaded file: %s", err.Error())
}
if string(returned) != "THINGS" {
t.Fatalf("Returned file was not correct")
}
}
func TestDownloadNoFiles(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
panic(err)
}
if r.Method == "POST" {
fmt.Fprintln(w, fileURLFailed)
} else if r.Method == "GET" {
fmt.Fprintf(w, `THINGS`)
}
}))
defer ts.Close()
client, err := New(ts.URL, "TESTING")
if err != nil {
t.Fatalf("Error creating client: %s", err)
}
_, err = client.Download(ts.URL)
if err != nil {
t.Log("Successfully returned error when no files were found", err)
} else {
t.Fatal("No error return when no files were found", err)
}
}
func TestUpload(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(int64(10000))
if err != nil {
panic(err)
}
formValues := r.MultipartForm.Value
referenceValues := map[string]string{
"action": "upload",
"format": "json",
"filename": "test.txt",
"token": "ASDFASDF",
}
NextKey:
for key, value := range formValues {
for innerKey, innerValue := range referenceValues {
if key == innerKey && value[0] == innerValue {
continue NextKey
}
}
fmt.Fprintln(w, fmt.Sprintf(`{"upload":{"result":"Value did not match: %s"}}`, key))
return
}
uploadedFile, err := r.MultipartForm.File["file"][0].Open()
if err != nil {
fmt.Fprintln(w, `{"upload":{"result":"Error opening uploaded file"}}`)
return
}
defer uploadedFile.Close()
contents, err := ioutil.ReadAll(uploadedFile)
if err != nil {
panic(err)
}
if string(contents) != "THIS IS A TEST" {
fmt.Fprintln(w, `{"upload":{"result":"File contents are not valid"}}`)
return
}
fmt.Fprintln(w, `{"upload":{"result":"Success"}}`)
}))
defer ts.Close()
client, err := New(ts.URL, "TESTING")
if err != nil {
t.Fatalf("Error creating client: %s", err.Error())
}
client.edittoken = "ASDFASDF"
err = client.Upload("test.txt", strings.NewReader("THIS IS A TEST"))
if err != nil {
t.Fatalf("Error trying to upload file: %s", err)
}
}
func TestUploadAutoToken(t *testing.T) {
test := BuildUp(editTokenReponse, t)
defer test.TearDown()
test.client.Upload("stuff", strings.NewReader("stuff"))
if test.client.edittoken == "" {
t.Fatalf("Edit token did not get set properly")
}
}
func TestUploadFailResponse(t *testing.T) {
test := BuildUp(`{"upload":{"result":"THIS SHOULD BE AN ERROR"}}`, t)
defer test.TearDown()
test.client.edittoken = "ASDF"
err := test.client.Upload("stuff", strings.NewReader("stuff"))
if err == nil {
t.Fatal("Did not generate error when upload returned failed result", err)
} else {
t.Log("Successfully generated error from failed upload", err)
}
}
func TestMWError(t *testing.T) {
test := BuildUp(mwerror, t)
defer test.TearDown()
_, err := test.client.Read("DOESN'T MATTER")
if err != nil {
t.Log("Properly recieved error:", err)
} else {
t.Fatalf("Mediawiki error did not get translated to a go error")
}
}
func TestEditAutoToken(t *testing.T) {
test := BuildUp(editTokenReponse, t)
defer test.TearDown()
test.client.Edit(map[string]string{"thing": "OTHER THING"})
if test.client.edittoken == "" {
t.Fatalf("Edit token did not get set properly")
}
}
func TestEditSuccess(t *testing.T) {
test := BuildUp(editsuccess, t)
defer test.TearDown()
test.client.edittoken = "asdf"
err := test.client.Edit(map[string]string{"title": "somepage"})
if err != nil {
t.Fatal("Did not get non-error response back", err)
}
}
func TestEditFail(t *testing.T) {
test := BuildUp(editfailure, t)
defer test.TearDown()
err := test.client.Edit(map[string]string{"title": "somepage"})
if err == nil {
t.Fatal("Did not get non-error response back", err)
}
}
func TestBasicAuth(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Basic Zm9vOmJhcg==" {
t.Fatalf("Did not recieve correct basic auth header: %s", r.Header.Get(r.Header.Get("Authorization")))
}
}))
defer ts.Close()
client, err := New(ts.URL, "TESTING")
if err != nil {
t.Fatalf("Error creating client: %s", err.Error())
}
client.UseBasicAuth = true
client.BasicAuthUser = "foo"
client.BasicAuthPass = "bar"
t.Log(client)
_, err = client.API()
if err != nil {
t.Fatalf("API() returned a non-nil error: %s", err.Error())
}
}