Merge "includes/specials: Replace implicit Bugzilla bug numbers with Phab ones"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki.api / mediawiki.api.test.js
1 ( function ( mw, $ ) {
2 QUnit.module( 'mediawiki.api', QUnit.newMwEnvironment( {
3 setup: function () {
4 this.server = this.sandbox.useFakeServer();
5 this.server.respondImmediately = true;
6 }
7 } ) );
8
9 function sequence( responses ) {
10 var i = 0;
11 return function ( request ) {
12 var response = responses[ i ];
13 if ( response ) {
14 i++;
15 request.respond.apply( request, response );
16 }
17 };
18 }
19
20 function sequenceBodies( status, headers, bodies ) {
21 jQuery.each( bodies, function ( i, body ) {
22 bodies[ i ] = [ status, headers, body ];
23 } );
24 return sequence( bodies );
25 }
26
27 // Utility to make inline use with an assert easier
28 function match( text, pattern ) {
29 var m = text.match( pattern );
30 return m && m[ 1 ] || null;
31 }
32
33 QUnit.test( 'get()', function ( assert ) {
34 var api = new mw.Api();
35
36 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] );
37
38 return api.get( {} ).then( function ( data ) {
39 assert.deepEqual( data, [], 'If request succeeds without errors, resolve deferred' );
40 } );
41 } );
42
43 QUnit.test( 'post()', function ( assert ) {
44 var api = new mw.Api();
45
46 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] );
47
48 return api.post( {} ).then( function ( data ) {
49 assert.deepEqual( data, [], 'Simple POST request' );
50 } );
51 } );
52
53 QUnit.test( 'API error errorformat=bc', function ( assert ) {
54 var api = new mw.Api();
55
56 this.server.respond( [ 200, { 'Content-Type': 'application/json' },
57 '{ "error": { "code": "unknown_action" } }'
58 ] );
59
60 api.get( { action: 'doesntexist' } )
61 .fail( function ( errorCode ) {
62 assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
63 } )
64 .always( assert.async() );
65 } );
66
67 QUnit.test( 'API error errorformat!=bc', function ( assert ) {
68 var api = new mw.Api();
69
70 this.server.respond( [ 200, { 'Content-Type': 'application/json' },
71 '{ "errors": [ { "code": "unknown_action", "key": "unknown-error", "params": [] } ] }'
72 ] );
73
74 api.get( { action: 'doesntexist' } )
75 .fail( function ( errorCode ) {
76 assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
77 } )
78 .always( assert.async() );
79 } );
80
81 QUnit.test( 'FormData support', function ( assert ) {
82 var api = new mw.Api();
83
84 this.server.respond( function ( request ) {
85 if ( window.FormData ) {
86 assert.ok( !request.url.match( /action=/ ), 'Request has no query string' );
87 assert.ok( request.requestBody instanceof FormData, 'Request uses FormData body' );
88 } else {
89 assert.ok( !request.url.match( /action=test/ ), 'Request has no query string' );
90 assert.equal( request.requestBody, 'action=test&format=json', 'Request uses query string body' );
91 }
92 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
93 } );
94
95 return api.post( { action: 'test' }, { contentType: 'multipart/form-data' } );
96 } );
97
98 QUnit.test( 'Converting arrays to pipe-separated (string)', function ( assert ) {
99 var api = new mw.Api();
100
101 this.server.respond( function ( request ) {
102 assert.equal( match( request.url, /test=([^&]+)/ ), 'foo%7Cbar%7Cbaz', 'Pipe-separated value was submitted' );
103 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
104 } );
105
106 return api.get( { test: [ 'foo', 'bar', 'baz' ] } );
107 } );
108
109 QUnit.test( 'Converting arrays to pipe-separated (mw.Title)', function ( assert ) {
110 var api = new mw.Api();
111
112 this.server.respond( function ( request ) {
113 assert.equal( match( request.url, /test=([^&]+)/ ), 'Foo%7CBar', 'Pipe-separated value was submitted' );
114 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
115 } );
116
117 return api.get( { test: [ new mw.Title( 'Foo' ), new mw.Title( 'Bar' ) ] } );
118 } );
119
120 QUnit.test( 'Converting arrays to pipe-separated (misc primitives)', function ( assert ) {
121 var api = new mw.Api();
122
123 this.server.respond( function ( request ) {
124 assert.equal( match( request.url, /test=([^&]+)/ ), 'true%7Cfalse%7C%7C%7C0%7C1%2E2', 'Pipe-separated value was submitted' );
125 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
126 } );
127
128 // undefined/null will become empty string
129 return api.get( { test: [ true, false, undefined, null, 0, 1.2 ] } );
130 } );
131
132 QUnit.test( 'Omitting false booleans', function ( assert ) {
133 var api = new mw.Api();
134
135 this.server.respond( function ( request ) {
136 assert.ok( !request.url.match( /foo/ ), 'foo query parameter is not present' );
137 assert.ok( request.url.match( /bar=true/ ), 'bar query parameter is present with value true' );
138 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
139 } );
140
141 return api.get( { foo: false, bar: true } );
142 } );
143
144 QUnit.test( 'getToken() - cached', function ( assert ) {
145 var api = new mw.Api(),
146 test = this;
147
148 // Get csrfToken for local wiki, this should not make
149 // a request as it should be retrieved from mw.user.tokens.
150 return api.getToken( 'csrf' )
151 .then( function ( token ) {
152 assert.ok( token.length, 'Got a token' );
153 }, function ( err ) {
154 assert.equal( '', err, 'API error' );
155 } )
156 .then( function () {
157 assert.equal( test.server.requests.length, 0, 'Requests made' );
158 } );
159 } );
160
161 QUnit.test( 'getToken() - uncached', function ( assert ) {
162 var api = new mw.Api(),
163 firstDone = assert.async(),
164 secondDone = assert.async();
165
166 this.server.respondWith( /type=testuncached/, [ 200, { 'Content-Type': 'application/json' },
167 '{ "query": { "tokens": { "testuncachedtoken": "good" } } }'
168 ] );
169
170 // Get a token of a type that isn't prepopulated by user.tokens.
171 // Could use "block" or "delete" here, but those could in theory
172 // be added to user.tokens, use a fake one instead.
173 api.getToken( 'testuncached' )
174 .done( function ( token ) {
175 assert.equal( token, 'good', 'The token' );
176 } )
177 .fail( function ( err ) {
178 assert.equal( err, '', 'API error' );
179 } )
180 .always( firstDone );
181
182 api.getToken( 'testuncached' )
183 .done( function ( token ) {
184 assert.equal( token, 'good', 'The cached token' );
185 } )
186 .fail( function ( err ) {
187 assert.equal( err, '', 'API error' );
188 } )
189 .always( secondDone );
190
191 assert.equal( this.server.requests.length, 1, 'Requests made' );
192 } );
193
194 QUnit.test( 'getToken() - error', function ( assert ) {
195 var api = new mw.Api();
196
197 this.server.respondWith( /type=testerror/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
198 [
199 '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }',
200 '{ "query": { "tokens": { "testerrortoken": "good" } } }'
201 ]
202 ) );
203
204 // Don't cache error (T67268)
205 return api.getToken( 'testerror' )
206 .then( null, function ( err ) {
207 assert.equal( err, 'bite-me', 'Expected error' );
208
209 return api.getToken( 'testerror' );
210 } )
211 .then( function ( token ) {
212 assert.equal( token, 'good', 'The token' );
213 } );
214 } );
215
216 QUnit.test( 'getToken() - deprecated', function ( assert ) {
217 // Cache API endpoint from default to avoid cachehit in mw.user.tokens
218 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ),
219 test = this;
220
221 this.server.respondWith( /type=csrf/, [ 200, { 'Content-Type': 'application/json' },
222 '{ "query": { "tokens": { "csrftoken": "csrfgood" } } }'
223 ] );
224
225 // Get a token of a type that is in the legacy map.
226 return api.getToken( 'email' )
227 .done( function ( token ) {
228 assert.equal( token, 'csrfgood', 'Token' );
229 } )
230 .fail( function ( err ) {
231 assert.equal( err, '', 'API error' );
232 } )
233 .always( function () {
234 assert.equal( test.server.requests.length, 1, 'Requests made' );
235 } );
236 } );
237
238 QUnit.test( 'badToken()', function ( assert ) {
239 var api = new mw.Api(),
240 test = this;
241
242 this.server.respondWith( /type=testbad/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
243 [
244 '{ "query": { "tokens": { "testbadtoken": "bad" } } }',
245 '{ "query": { "tokens": { "testbadtoken": "good" } } }'
246 ]
247 ) );
248
249 return api.getToken( 'testbad' )
250 .then( function () {
251 api.badToken( 'testbad' );
252 return api.getToken( 'testbad' );
253 } )
254 .then( function ( token ) {
255 assert.equal( token, 'good', 'The token' );
256 assert.equal( test.server.requests.length, 2, 'Requests made' );
257 } );
258
259 } );
260
261 QUnit.test( 'badToken( legacy )', function ( assert ) {
262 var api = new mw.Api( { ajax: { url: '/badTokenLegacy/api.php' } } ),
263 test = this;
264
265 this.server.respondWith( /type=csrf/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
266 [
267 '{ "query": { "tokens": { "csrftoken": "badlegacy" } } }',
268 '{ "query": { "tokens": { "csrftoken": "goodlegacy" } } }'
269 ]
270 ) );
271
272 return api.getToken( 'options' )
273 .then( function () {
274 api.badToken( 'options' );
275 return api.getToken( 'options' );
276 } )
277 .then( function ( token ) {
278 assert.equal( token, 'goodlegacy', 'The token' );
279 assert.equal( test.server.requests.length, 2, 'Request made' );
280 } );
281
282 } );
283
284 QUnit.test( 'postWithToken( tokenType, params )', function ( assert ) {
285 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
286
287 this.server.respondWith( 'GET', /type=testpost/, [ 200, { 'Content-Type': 'application/json' },
288 '{ "query": { "tokens": { "testposttoken": "good" } } }'
289 ] );
290 this.server.respondWith( 'POST', /api/, function ( request ) {
291 if ( request.requestBody.match( /token=good/ ) ) {
292 request.respond( 200, { 'Content-Type': 'application/json' },
293 '{ "example": { "foo": "quux" } }'
294 );
295 }
296 } );
297
298 return api.postWithToken( 'testpost', { action: 'example', key: 'foo' } )
299 .then( function ( data ) {
300 assert.deepEqual( data, { example: { foo: 'quux' } } );
301 } );
302 } );
303
304 QUnit.test( 'postWithToken( tokenType, params with assert )', function ( assert ) {
305 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ),
306 test = this;
307
308 this.server.respondWith( /assert=user/, [ 200, { 'Content-Type': 'application/json' },
309 '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
310 ] );
311
312 return api.postWithToken( 'testassertpost', { action: 'example', key: 'foo', assert: 'user' } )
313 // Cast error to success and vice versa
314 .then( function () {
315 return $.Deferred().reject( 'Unexpected success' );
316 }, function ( errorCode ) {
317 assert.equal( errorCode, 'assertuserfailed', 'getToken fails assert' );
318 return $.Deferred().resolve();
319 } )
320 .then( function () {
321 assert.equal( test.server.requests.length, 1, 'Requests made' );
322 } );
323 } );
324
325 QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) {
326 var api = new mw.Api(),
327 test = this;
328
329 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ] );
330
331 return api.postWithToken( 'csrf',
332 { action: 'example' },
333 {
334 headers: {
335 'X-Foo': 'Bar'
336 }
337 }
338 )
339 .then( function () {
340 assert.equal( test.server.requests[ 0 ].requestHeaders[ 'X-Foo' ], 'Bar', 'Header sent' );
341
342 return api.postWithToken( 'csrf',
343 { action: 'example' },
344 function () {
345 assert.ok( false, 'This parameter cannot be a callback' );
346 }
347 );
348 } )
349 .then( function ( data ) {
350 assert.equal( data.example, 'quux' );
351
352 assert.equal( test.server.requests.length, 2, 'Request made' );
353 } );
354 } );
355
356 QUnit.test( 'postWithToken() - badtoken', function ( assert ) {
357 var api = new mw.Api();
358
359 this.server.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
360 [
361 '{ "query": { "tokens": { "testbadtokentoken": "bad" } } }',
362 '{ "query": { "tokens": { "testbadtokentoken": "good" } } }'
363 ]
364 ) );
365 this.server.respondWith( 'POST', /api/, function ( request ) {
366 if ( request.requestBody.match( /token=bad/ ) ) {
367 request.respond( 200, { 'Content-Type': 'application/json' },
368 '{ "error": { "code": "badtoken" } }'
369 );
370 }
371 if ( request.requestBody.match( /token=good/ ) ) {
372 request.respond( 200, { 'Content-Type': 'application/json' },
373 '{ "example": { "foo": "quux" } }'
374 );
375 }
376 } );
377
378 // - Request: new token -> bad
379 // - Request: action=example -> badtoken error
380 // - Request: new token -> good
381 // - Request: action=example -> success
382 return api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } )
383 .then( function ( data ) {
384 assert.deepEqual( data, { example: { foo: 'quux' } } );
385 } );
386 } );
387
388 QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) {
389 var sequenceA,
390 api = new mw.Api();
391
392 this.server.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
393 [
394 '{ "query": { "tokens": { "testoncetoken": "good-A" } } }',
395 '{ "query": { "tokens": { "testoncetoken": "good-B" } } }'
396 ]
397 ) );
398 sequenceA = sequenceBodies( 200, { 'Content-Type': 'application/json' },
399 [
400 '{ "example": { "value": "A" } }',
401 '{ "error": { "code": "badtoken" } }'
402 ]
403 );
404 this.server.respondWith( 'POST', /api/, function ( request ) {
405 if ( request.requestBody.match( /token=good-A/ ) ) {
406 sequenceA( request );
407 } else if ( request.requestBody.match( /token=good-B/ ) ) {
408 request.respond( 200, { 'Content-Type': 'application/json' },
409 '{ "example": { "value": "B" } }'
410 );
411 }
412 } );
413
414 // - Request: new token -> A
415 // - Request: action=example
416 return api.postWithToken( 'testonce', { action: 'example', key: 'foo' } )
417 .then( function ( data ) {
418 assert.deepEqual( data, { example: { value: 'A' } } );
419
420 // - Request: action=example w/ token A -> badtoken error
421 // - Request: new token -> B
422 // - Request: action=example w/ token B -> success
423 return api.postWithToken( 'testonce', { action: 'example', key: 'bar' } );
424 } )
425 .then( function ( data ) {
426 assert.deepEqual( data, { example: { value: 'B' } } );
427 } );
428 } );
429
430 QUnit.module( 'mediawiki.api (2)', {
431 setup: function () {
432 var self = this,
433 requests = this.requests = [];
434 this.api = new mw.Api();
435 this.sandbox.stub( jQuery, 'ajax', function () {
436 var request = $.extend( {
437 abort: self.sandbox.spy()
438 }, $.Deferred() );
439 requests.push( request );
440 return request;
441 } );
442 }
443 } );
444
445 QUnit.test( '#abort', function ( assert ) {
446 this.api.get( {
447 a: 1
448 } );
449 this.api.post( {
450 b: 2
451 } );
452 this.api.abort();
453 assert.ok( this.requests.length === 2, 'Check both requests triggered' );
454 $.each( this.requests, function ( i, request ) {
455 assert.ok( request.abort.calledOnce, 'abort request number ' + i );
456 } );
457 } );
458 }( mediaWiki, jQuery ) );