Merge "Perform a permission check on the title when changing the page language"
[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 ).then( function () {
339 assert.equal( test.server.requests[ 0 ].requestHeaders[ 'X-Foo' ], 'Bar', 'Header sent' );
340
341 return api.postWithToken( 'csrf',
342 { action: 'example' },
343 function () {
344 assert.ok( false, 'This parameter cannot be a callback' );
345 }
346 );
347 } ).then( function ( data ) {
348 assert.equal( data.example, 'quux' );
349
350 assert.equal( test.server.requests.length, 2, 'Request made' );
351 } );
352 } );
353
354 QUnit.test( 'postWithToken() - badtoken', function ( assert ) {
355 var api = new mw.Api();
356
357 this.server.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
358 [
359 '{ "query": { "tokens": { "testbadtokentoken": "bad" } } }',
360 '{ "query": { "tokens": { "testbadtokentoken": "good" } } }'
361 ]
362 ) );
363 this.server.respondWith( 'POST', /api/, function ( request ) {
364 if ( request.requestBody.match( /token=bad/ ) ) {
365 request.respond( 200, { 'Content-Type': 'application/json' },
366 '{ "error": { "code": "badtoken" } }'
367 );
368 }
369 if ( request.requestBody.match( /token=good/ ) ) {
370 request.respond( 200, { 'Content-Type': 'application/json' },
371 '{ "example": { "foo": "quux" } }'
372 );
373 }
374 } );
375
376 // - Request: new token -> bad
377 // - Request: action=example -> badtoken error
378 // - Request: new token -> good
379 // - Request: action=example -> success
380 return api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } )
381 .then( function ( data ) {
382 assert.deepEqual( data, { example: { foo: 'quux' } } );
383 } );
384 } );
385
386 QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) {
387 var sequenceA,
388 api = new mw.Api();
389
390 this.server.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
391 [
392 '{ "query": { "tokens": { "testoncetoken": "good-A" } } }',
393 '{ "query": { "tokens": { "testoncetoken": "good-B" } } }'
394 ]
395 ) );
396 sequenceA = sequenceBodies( 200, { 'Content-Type': 'application/json' },
397 [
398 '{ "example": { "value": "A" } }',
399 '{ "error": { "code": "badtoken" } }'
400 ]
401 );
402 this.server.respondWith( 'POST', /api/, function ( request ) {
403 if ( request.requestBody.match( /token=good-A/ ) ) {
404 sequenceA( request );
405 } else if ( request.requestBody.match( /token=good-B/ ) ) {
406 request.respond( 200, { 'Content-Type': 'application/json' },
407 '{ "example": { "value": "B" } }'
408 );
409 }
410 } );
411
412 // - Request: new token -> A
413 // - Request: action=example
414 return api.postWithToken( 'testonce', { action: 'example', key: 'foo' } )
415 .then( function ( data ) {
416 assert.deepEqual( data, { example: { value: 'A' } } );
417
418 // - Request: action=example w/ token A -> badtoken error
419 // - Request: new token -> B
420 // - Request: action=example w/ token B -> success
421 return api.postWithToken( 'testonce', { action: 'example', key: 'bar' } );
422 } )
423 .then( function ( data ) {
424 assert.deepEqual( data, { example: { value: 'B' } } );
425 } );
426 } );
427
428 QUnit.module( 'mediawiki.api (2)', {
429 setup: function () {
430 var self = this,
431 requests = this.requests = [];
432 this.api = new mw.Api();
433 this.sandbox.stub( jQuery, 'ajax', function () {
434 var request = $.extend( {
435 abort: self.sandbox.spy()
436 }, $.Deferred() );
437 requests.push( request );
438 return request;
439 } );
440 }
441 } );
442
443 QUnit.test( '#abort', function ( assert ) {
444 this.api.get( {
445 a: 1
446 } );
447 this.api.post( {
448 b: 2
449 } );
450 this.api.abort();
451 assert.ok( this.requests.length === 2, 'Check both requests triggered' );
452 $.each( this.requests, function ( i, request ) {
453 assert.ok( request.abort.calledOnce, 'abort request number ' + i );
454 } );
455 } );
456 }( mediaWiki, jQuery ) );