Merge "SpecialMovepage: Convert form to use OOUI controls"
[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 this.clock = this.sandbox.useFakeTimers();
7 },
8 teardown: function () {
9 // https://github.com/jquery/jquery/issues/2453
10 this.clock.tick();
11 }
12 } ) );
13
14 function sequence( responses ) {
15 var i = 0;
16 return function ( request ) {
17 var response = responses[ i ];
18 if ( response ) {
19 i++;
20 request.respond.apply( request, response );
21 }
22 };
23 }
24
25 function sequenceBodies( status, headers, bodies ) {
26 jQuery.each( bodies, function ( i, body ) {
27 bodies[ i ] = [ status, headers, body ];
28 } );
29 return sequence( bodies );
30 }
31
32 QUnit.test( 'Basic functionality', function ( assert ) {
33 QUnit.expect( 2 );
34 var api = new mw.Api();
35
36 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] );
37
38 api.get( {} )
39 .done( function ( data ) {
40 assert.deepEqual( data, [], 'If request succeeds without errors, resolve deferred' );
41 } );
42
43 api.post( {} )
44 .done( function ( data ) {
45 assert.deepEqual( data, [], 'Simple POST request' );
46 } );
47 } );
48
49 QUnit.test( 'API error', function ( assert ) {
50 QUnit.expect( 1 );
51 var api = new mw.Api();
52
53 this.server.respond( [ 200, { 'Content-Type': 'application/json' },
54 '{ "error": { "code": "unknown_action" } }'
55 ] );
56
57 api.get( { action: 'doesntexist' } )
58 .fail( function ( errorCode ) {
59 assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
60 } );
61 } );
62
63 QUnit.test( 'FormData support', function ( assert ) {
64 QUnit.expect( 2 );
65 var api = new mw.Api();
66
67 this.server.respond( function ( request ) {
68 if ( window.FormData ) {
69 assert.ok( !request.url.match( /action=/ ), 'Request has no query string' );
70 assert.ok( request.requestBody instanceof FormData, 'Request uses FormData body' );
71 } else {
72 assert.ok( !request.url.match( /action=test/ ), 'Request has no query string' );
73 assert.equal( request.requestBody, 'action=test&format=json', 'Request uses query string body' );
74 }
75 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
76 } );
77
78 api.post( { action: 'test' }, { contentType: 'multipart/form-data' } );
79 } );
80
81 QUnit.test( 'Converting arrays to pipe-separated', function ( assert ) {
82 QUnit.expect( 1 );
83 var api = new mw.Api();
84
85 this.server.respond( function ( request ) {
86 assert.ok( request.url.match( /test=foo%7Cbar%7Cbaz/ ), 'Pipe-separated value was submitted' );
87 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
88 } );
89
90 api.get( { test: [ 'foo', 'bar', 'baz' ] } );
91 } );
92
93 QUnit.test( 'Omitting false booleans', function ( assert ) {
94 QUnit.expect( 2 );
95 var api = new mw.Api();
96
97 this.server.respond( function ( request ) {
98 assert.ok( !request.url.match( /foo/ ), 'foo query parameter is not present' );
99 assert.ok( request.url.match( /bar=true/ ), 'bar query parameter is present with value true' );
100 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
101 } );
102
103 api.get( { foo: false, bar: true } );
104 } );
105
106 QUnit.test( 'getToken() - cached', function ( assert ) {
107 QUnit.expect( 2 );
108 var api = new mw.Api();
109
110 // Get editToken for local wiki, this should not make
111 // a request as it should be retrieved from mw.user.tokens.
112 api.getToken( 'edit' )
113 .done( function ( token ) {
114 assert.ok( token.length, 'Got a token' );
115 } )
116 .fail( function ( err ) {
117 assert.equal( '', err, 'API error' );
118 } );
119
120 assert.equal( this.server.requests.length, 0, 'Requests made' );
121 } );
122
123 QUnit.test( 'getToken() - uncached', function ( assert ) {
124 QUnit.expect( 3 );
125 var api = new mw.Api();
126
127 this.server.respondWith( /type=testuncached/, [ 200, { 'Content-Type': 'application/json' },
128 '{ "tokens": { "testuncachedtoken": "good" } }'
129 ] );
130
131 // Get a token of a type that isn't prepopulated by user.tokens.
132 // Could use "block" or "delete" here, but those could in theory
133 // be added to user.tokens, use a fake one instead.
134 api.getToken( 'testuncached' )
135 .done( function ( token ) {
136 assert.equal( token, 'good', 'The token' );
137 } )
138 .fail( function ( err ) {
139 assert.equal( err, '', 'API error' );
140 } );
141
142 api.getToken( 'testuncached' )
143 .done( function ( token ) {
144 assert.equal( token, 'good', 'The cached token' );
145 } )
146 .fail( function ( err ) {
147 assert.equal( err, '', 'API error' );
148 } );
149
150 assert.equal( this.server.requests.length, 1, 'Requests made' );
151 } );
152
153 QUnit.test( 'getToken() - error', function ( assert ) {
154 QUnit.expect( 2 );
155 var api = new mw.Api();
156
157 this.server.respondWith( /type=testerror/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
158 [
159 '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }',
160 '{ "tokens": { "testerrortoken": "good" } }'
161 ]
162 ) );
163
164 // Don't cache error (bug 65268)
165 api.getToken( 'testerror' ).fail( function ( err ) {
166 assert.equal( err, 'bite-me', 'Expected error' );
167
168 // Make this request after the first one has finished.
169 // If we make it simultaneously we still want it to share
170 // the cache, but as soon as it is fulfilled as error we
171 // reject it so that the next one tries fresh.
172 api.getToken( 'testerror' ).done( function ( token ) {
173 assert.equal( token, 'good', 'The token' );
174 } );
175 } );
176 } );
177
178 QUnit.test( 'badToken()', function ( assert ) {
179 QUnit.expect( 2 );
180 var api = new mw.Api(),
181 test = this;
182
183 this.server.respondWith( /type=testbad/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
184 [
185 '{ "tokens": { "testbadtoken": "bad" } }',
186 '{ "tokens": { "testbadtoken": "good" } }'
187 ]
188 ) );
189
190 api.getToken( 'testbad' )
191 .then( function () {
192 api.badToken( 'testbad' );
193 return api.getToken( 'testbad' );
194 } )
195 .then( function ( token ) {
196 assert.equal( token, 'good', 'The token' );
197 assert.equal( test.server.requests.length, 2, 'Requests made' );
198 } );
199
200 } );
201
202 QUnit.test( 'postWithToken( tokenType, params )', function ( assert ) {
203 QUnit.expect( 1 );
204 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
205
206 this.server.respondWith( 'GET', /type=testpost/, [ 200, { 'Content-Type': 'application/json' },
207 '{ "tokens": { "testposttoken": "good" } }'
208 ] );
209 this.server.respondWith( 'POST', /api/, function ( request ) {
210 if ( request.requestBody.match( /token=good/ ) ) {
211 request.respond( 200, { 'Content-Type': 'application/json' },
212 '{ "example": { "foo": "quux" } }'
213 );
214 }
215 } );
216
217 api.postWithToken( 'testpost', { action: 'example', key: 'foo' } )
218 .done( function ( data ) {
219 assert.deepEqual( data, { example: { foo: 'quux' } } );
220 } );
221 } );
222
223 QUnit.test( 'postWithToken( tokenType, params with assert )', function ( assert ) {
224 QUnit.expect( 2 );
225 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
226
227 this.server.respondWith( /assert=user/, [ 200, { 'Content-Type': 'application/json' },
228 '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
229 ] );
230
231 api.postWithToken( 'testassertpost', { action: 'example', key: 'foo', assert: 'user' } )
232 .fail( function ( errorCode ) {
233 assert.equal( errorCode, 'assertuserfailed', 'getToken fails assert' );
234 } );
235
236 assert.equal( this.server.requests.length, 1, 'Requests made' );
237 } );
238
239 QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) {
240 QUnit.expect( 3 );
241 var api = new mw.Api();
242
243 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ] );
244
245 api.postWithToken(
246 'edit',
247 {
248 action: 'example'
249 },
250 {
251 headers: {
252 'X-Foo': 'Bar'
253 }
254 }
255 );
256
257 api.postWithToken(
258 'edit',
259 {
260 action: 'example'
261 },
262 function () {
263 assert.ok( false, 'This parameter cannot be a callback' );
264 }
265 )
266 .always( function ( data ) {
267 assert.equal( data.example, 'quux' );
268 } );
269
270 assert.equal( this.server.requests.length, 2, 'Request made' );
271 assert.equal( this.server.requests[ 0 ].requestHeaders[ 'X-Foo' ], 'Bar', 'Header sent' );
272 } );
273
274 QUnit.test( 'postWithToken() - badtoken', function ( assert ) {
275 QUnit.expect( 1 );
276 var api = new mw.Api();
277
278 this.server.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
279 [
280 '{ "tokens": { "testbadtokentoken": "bad" } }',
281 '{ "tokens": { "testbadtokentoken": "good" } }'
282 ]
283 ) );
284 this.server.respondWith( 'POST', /api/, function ( request ) {
285 if ( request.requestBody.match( /token=bad/ ) ) {
286 request.respond( 200, { 'Content-Type': 'application/json' },
287 '{ "error": { "code": "badtoken" } }'
288 );
289 }
290 if ( request.requestBody.match( /token=good/ ) ) {
291 request.respond( 200, { 'Content-Type': 'application/json' },
292 '{ "example": { "foo": "quux" } }'
293 );
294 }
295 } );
296
297 // - Request: new token -> bad
298 // - Request: action=example -> badtoken error
299 // - Request: new token -> good
300 // - Request: action=example -> success
301 api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } )
302 .done( function ( data ) {
303 assert.deepEqual( data, { example: { foo: 'quux' } } );
304 } );
305 } );
306
307 QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) {
308 QUnit.expect( 2 );
309 var sequenceA,
310 api = new mw.Api();
311
312 this.server.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
313 [
314 '{ "tokens": { "testoncetoken": "good-A" } }',
315 '{ "tokens": { "testoncetoken": "good-B" } }'
316 ]
317 ) );
318 sequenceA = sequenceBodies( 200, { 'Content-Type': 'application/json' },
319 [
320 '{ "example": { "value": "A" } }',
321 '{ "error": { "code": "badtoken" } }'
322 ]
323 );
324 this.server.respondWith( 'POST', /api/, function ( request ) {
325 if ( request.requestBody.match( /token=good-A/ ) ) {
326 sequenceA( request );
327 } else if ( request.requestBody.match( /token=good-B/ ) ) {
328 request.respond( 200, { 'Content-Type': 'application/json' },
329 '{ "example": { "value": "B" } }'
330 );
331 }
332 } );
333
334 // - Request: new token -> A
335 // - Request: action=example
336 api.postWithToken( 'testonce', { action: 'example', key: 'foo' } )
337 .done( function ( data ) {
338 assert.deepEqual( data, { example: { value: 'A' } } );
339 } );
340
341 // - Request: action=example w/ token A -> badtoken error
342 // - Request: new token -> B
343 // - Request: action=example w/ token B -> success
344 api.postWithToken( 'testonce', { action: 'example', key: 'bar' } )
345 .done( function ( data ) {
346 assert.deepEqual( data, { example: { value: 'B' } } );
347 } );
348 } );
349 }( mediaWiki ) );