Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / qunit / suites / resources / mediawiki.api / mediawiki.api.test.js
blob6a00ac9b179e6bfc38c6082425b9a20fa41c52e2
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         } ) );
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         }
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         }
27         QUnit.test( 'get()', function ( assert ) {
28                 var api = new mw.Api();
30                 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] );
32                 return api.get( {} ).then( function ( data ) {
33                         assert.deepEqual( data, [], 'If request succeeds without errors, resolve deferred' );
34                 } );
35         } );
37         QUnit.test( 'post()', function ( assert ) {
38                 var api = new mw.Api();
40                 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] );
42                 return api.post( {} ).then( function ( data ) {
43                         assert.deepEqual( data, [], 'Simple POST request' );
44                 } );
45         } );
47         QUnit.test( 'API error errorformat=bc', function ( assert ) {
48                 var api = new mw.Api();
50                 this.server.respond( [ 200, { 'Content-Type': 'application/json' },
51                         '{ "error": { "code": "unknown_action" } }'
52                 ] );
54                 api.get( { action: 'doesntexist' } )
55                         .fail( function ( errorCode ) {
56                                 assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
57                         } )
58                         .always( assert.async() );
59         } );
61         QUnit.test( 'API error errorformat!=bc', function ( assert ) {
62                 var api = new mw.Api();
64                 this.server.respond( [ 200, { 'Content-Type': 'application/json' },
65                         '{ "errors": [ { "code": "unknown_action", "key": "unknown-error", "params": [] } ] }'
66                 ] );
68                 api.get( { action: 'doesntexist' } )
69                         .fail( function ( errorCode ) {
70                                 assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
71                         } )
72                         .always( assert.async() );
73         } );
75         QUnit.test( 'FormData support', function ( assert ) {
76                 var api = new mw.Api();
78                 this.server.respond( function ( request ) {
79                         if ( window.FormData ) {
80                                 assert.ok( !request.url.match( /action=/ ), 'Request has no query string' );
81                                 assert.ok( request.requestBody instanceof FormData, 'Request uses FormData body' );
82                         } else {
83                                 assert.ok( !request.url.match( /action=test/ ), 'Request has no query string' );
84                                 assert.equal( request.requestBody, 'action=test&format=json', 'Request uses query string body' );
85                         }
86                         request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
87                 } );
89                 return api.post( { action: 'test' }, { contentType: 'multipart/form-data' } );
90         } );
92         QUnit.test( 'Converting arrays to pipe-separated', function ( assert ) {
93                 var api = new mw.Api();
95                 this.server.respond( function ( request ) {
96                         assert.ok( request.url.match( /test=foo%7Cbar%7Cbaz/ ), 'Pipe-separated value was submitted' );
97                         request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
98                 } );
100                 return api.get( { test: [ 'foo', 'bar', 'baz' ] } );
101         } );
103         QUnit.test( 'Omitting false booleans', function ( assert ) {
104                 var api = new mw.Api();
106                 this.server.respond( function ( request ) {
107                         assert.ok( !request.url.match( /foo/ ), 'foo query parameter is not present' );
108                         assert.ok( request.url.match( /bar=true/ ), 'bar query parameter is present with value true' );
109                         request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
110                 } );
112                 return api.get( { foo: false, bar: true } );
113         } );
115         QUnit.test( 'getToken() - cached', function ( assert ) {
116                 var api = new mw.Api(),
117                         test = this;
119                 // Get csrfToken for local wiki, this should not make
120                 // a request as it should be retrieved from mw.user.tokens.
121                 return api.getToken( 'csrf' )
122                         .then( function ( token ) {
123                                 assert.ok( token.length, 'Got a token' );
124                         }, function ( err ) {
125                                 assert.equal( '', err, 'API error' );
126                         } )
127                         .then( function () {
128                                 assert.equal( test.server.requests.length, 0, 'Requests made' );
129                         } );
130         } );
132         QUnit.test( 'getToken() - uncached', function ( assert ) {
133                 var api = new mw.Api(),
134                         firstDone = assert.async(),
135                         secondDone = assert.async();
137                 this.server.respondWith( /type=testuncached/, [ 200, { 'Content-Type': 'application/json' },
138                         '{ "query": { "tokens": { "testuncachedtoken": "good" } } }'
139                 ] );
141                 // Get a token of a type that isn't prepopulated by user.tokens.
142                 // Could use "block" or "delete" here, but those could in theory
143                 // be added to user.tokens, use a fake one instead.
144                 api.getToken( 'testuncached' )
145                         .done( function ( token ) {
146                                 assert.equal( token, 'good', 'The token' );
147                         } )
148                         .fail( function ( err ) {
149                                 assert.equal( err, '', 'API error' );
150                         } )
151                         .always( firstDone );
153                 api.getToken( 'testuncached' )
154                         .done( function ( token ) {
155                                 assert.equal( token, 'good', 'The cached token' );
156                         } )
157                         .fail( function ( err ) {
158                                 assert.equal( err, '', 'API error' );
159                         } )
160                         .always( secondDone );
162                 assert.equal( this.server.requests.length, 1, 'Requests made' );
163         } );
165         QUnit.test( 'getToken() - error', function ( assert ) {
166                 var api = new mw.Api();
168                 this.server.respondWith( /type=testerror/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
169                         [
170                                 '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }',
171                                 '{ "query": { "tokens": { "testerrortoken": "good" } } }'
172                         ]
173                 ) );
175                 // Don't cache error (bug 65268)
176                 return api.getToken( 'testerror' )
177                         .then( null, function ( err ) {
178                                 assert.equal( err, 'bite-me', 'Expected error' );
180                                 return api.getToken( 'testerror' );
181                         } )
182                         .then( function ( token ) {
183                                 assert.equal( token, 'good', 'The token' );
184                         } );
185         } );
187         QUnit.test( 'getToken() - deprecated', function ( assert ) {
188                 // Cache API endpoint from default to avoid cachehit in mw.user.tokens
189                 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ),
190                         test = this;
192                 this.server.respondWith( /type=csrf/, [ 200, { 'Content-Type': 'application/json' },
193                         '{ "query": { "tokens": { "csrftoken": "csrfgood" } } }'
194                 ] );
196                 // Get a token of a type that is in the legacy map.
197                 return api.getToken( 'email' )
198                         .done( function ( token ) {
199                                 assert.equal( token, 'csrfgood', 'Token' );
200                         } )
201                         .fail( function ( err ) {
202                                 assert.equal( err, '', 'API error' );
203                         } )
204                         .always( function () {
205                                 assert.equal( test.server.requests.length, 1, 'Requests made' );
206                         } );
207         } );
209         QUnit.test( 'badToken()', function ( assert ) {
210                 var api = new mw.Api(),
211                         test = this;
213                 this.server.respondWith( /type=testbad/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
214                         [
215                                 '{ "query": { "tokens": { "testbadtoken": "bad" } } }',
216                                 '{ "query": { "tokens": { "testbadtoken": "good" } } }'
217                         ]
218                 ) );
220                 return api.getToken( 'testbad' )
221                         .then( function () {
222                                 api.badToken( 'testbad' );
223                                 return api.getToken( 'testbad' );
224                         } )
225                         .then( function ( token ) {
226                                 assert.equal( token, 'good', 'The token' );
227                                 assert.equal( test.server.requests.length, 2, 'Requests made' );
228                         } );
230         } );
232         QUnit.test( 'badToken( legacy )', function ( assert ) {
233                 var api = new mw.Api( { ajax: { url: '/badTokenLegacy/api.php' } } ),
234                         test = this;
236                 this.server.respondWith( /type=csrf/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
237                         [
238                                 '{ "query": { "tokens": { "csrftoken": "badlegacy" } } }',
239                                 '{ "query": { "tokens": { "csrftoken": "goodlegacy" } } }'
240                         ]
241                 ) );
243                 return api.getToken( 'options' )
244                         .then( function () {
245                                 api.badToken( 'options' );
246                                 return api.getToken( 'options' );
247                         } )
248                         .then( function ( token ) {
249                                 assert.equal( token, 'goodlegacy', 'The token' );
250                                 assert.equal( test.server.requests.length, 2, 'Request made' );
251                         } );
253         } );
255         QUnit.test( 'postWithToken( tokenType, params )', function ( assert ) {
256                 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
258                 this.server.respondWith( 'GET', /type=testpost/, [ 200, { 'Content-Type': 'application/json' },
259                         '{ "query": { "tokens": { "testposttoken": "good" } } }'
260                 ] );
261                 this.server.respondWith( 'POST', /api/, function ( request ) {
262                         if ( request.requestBody.match( /token=good/ ) ) {
263                                 request.respond( 200, { 'Content-Type': 'application/json' },
264                                         '{ "example": { "foo": "quux" } }'
265                                 );
266                         }
267                 } );
269                 return api.postWithToken( 'testpost', { action: 'example', key: 'foo' } )
270                         .then( function ( data ) {
271                                 assert.deepEqual( data, { example: { foo: 'quux' } } );
272                         } );
273         } );
275         QUnit.test( 'postWithToken( tokenType, params with assert )', function ( assert ) {
276                 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ),
277                         test = this;
279                 this.server.respondWith( /assert=user/, [ 200, { 'Content-Type': 'application/json' },
280                         '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
281                 ] );
283                 return api.postWithToken( 'testassertpost', { action: 'example', key: 'foo', assert: 'user' } )
284                         // Cast error to success and vice versa
285                         .then( function ( ) {
286                                 return $.Deferred().reject( 'Unexpected success' );
287                         }, function ( errorCode ) {
288                                 assert.equal( errorCode, 'assertuserfailed', 'getToken fails assert' );
289                                 return $.Deferred().resolve();
290                         } )
291                         .then( function () {
292                                 assert.equal( test.server.requests.length, 1, 'Requests made' );
293                         } );
294         } );
296         QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) {
297                 var api = new mw.Api(),
298                         test = this;
300                 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ] );
302                 return api.postWithToken( 'csrf',
303                                 { action: 'example' },
304                                 {
305                                         headers: {
306                                                 'X-Foo': 'Bar'
307                                         }
308                                 }
309                         )
310                         .then( function () {
311                                 assert.equal( test.server.requests[ 0 ].requestHeaders[ 'X-Foo' ], 'Bar', 'Header sent' );
313                                 return api.postWithToken( 'csrf',
314                                         { action: 'example' },
315                                         function () {
316                                                 assert.ok( false, 'This parameter cannot be a callback' );
317                                         }
318                                 );
319                         } )
320                         .then( function ( data ) {
321                                 assert.equal( data.example, 'quux' );
323                                 assert.equal( test.server.requests.length, 2, 'Request made' );
324                         } );
325         } );
327         QUnit.test( 'postWithToken() - badtoken', function ( assert ) {
328                 var api = new mw.Api();
330                 this.server.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
331                         [
332                                 '{ "query": { "tokens": { "testbadtokentoken": "bad" } } }',
333                                 '{ "query": { "tokens": { "testbadtokentoken": "good" } } }'
334                         ]
335                 ) );
336                 this.server.respondWith( 'POST', /api/, function ( request ) {
337                         if ( request.requestBody.match( /token=bad/ ) ) {
338                                 request.respond( 200, { 'Content-Type': 'application/json' },
339                                         '{ "error": { "code": "badtoken" } }'
340                                 );
341                         }
342                         if ( request.requestBody.match( /token=good/ ) ) {
343                                 request.respond( 200, { 'Content-Type': 'application/json' },
344                                         '{ "example": { "foo": "quux" } }'
345                                 );
346                         }
347                 } );
349                 // - Request: new token -> bad
350                 // - Request: action=example -> badtoken error
351                 // - Request: new token -> good
352                 // - Request: action=example -> success
353                 return api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } )
354                         .then( function ( data ) {
355                                 assert.deepEqual( data, { example: { foo: 'quux' } } );
356                         } );
357         } );
359         QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) {
360                 var sequenceA,
361                         api = new mw.Api();
363                 this.server.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
364                         [
365                                 '{ "query": { "tokens": { "testoncetoken": "good-A" } } }',
366                                 '{ "query": { "tokens": { "testoncetoken": "good-B" } } }'
367                         ]
368                 ) );
369                 sequenceA = sequenceBodies( 200, { 'Content-Type': 'application/json' },
370                         [
371                                 '{ "example": { "value": "A" } }',
372                                 '{ "error": { "code": "badtoken" } }'
373                         ]
374                 );
375                 this.server.respondWith( 'POST', /api/, function ( request ) {
376                         if ( request.requestBody.match( /token=good-A/ ) ) {
377                                 sequenceA( request );
378                         } else if ( request.requestBody.match( /token=good-B/ ) ) {
379                                 request.respond( 200, { 'Content-Type': 'application/json' },
380                                         '{ "example": { "value": "B" } }'
381                                 );
382                         }
383                 } );
385                 // - Request: new token -> A
386                 // - Request: action=example
387                 return api.postWithToken( 'testonce', { action: 'example', key: 'foo' } )
388                         .then( function ( data ) {
389                                 assert.deepEqual( data, { example: { value: 'A' } } );
391                                 // - Request: action=example w/ token A -> badtoken error
392                                 // - Request: new token -> B
393                                 // - Request: action=example w/ token B -> success
394                                 return api.postWithToken( 'testonce', { action: 'example', key: 'bar' } );
395                         } )
396                         .then( function ( data ) {
397                                 assert.deepEqual( data, { example: { value: 'B' } } );
398                         } );
399         } );
401         QUnit.module( 'mediawiki.api (2)', {
402                 setup: function () {
403                         var self = this,
404                                 requests = this.requests = [];
405                         this.api = new mw.Api();
406                         this.sandbox.stub( jQuery, 'ajax', function () {
407                                 var request = $.extend( {
408                                         abort: self.sandbox.spy()
409                                 }, $.Deferred() );
410                                 requests.push( request );
411                                 return request;
412                         } );
413                 }
414         } );
416         QUnit.test( '#abort', 3, function ( assert ) {
417                 this.api.get( {
418                         a: 1
419                 } );
420                 this.api.post( {
421                         b: 2
422                 } );
423                 this.api.abort();
424                 assert.ok( this.requests.length === 2, 'Check both requests triggered' );
425                 $.each( this.requests, function ( i, request ) {
426                         assert.ok( request.abort.calledOnce, 'abort request number ' + i );
427                 } );
428         } );
429 }( mediaWiki, jQuery ) );