Merge "Support mustache partials in server-side templates"
[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( 'getToken() - cached', function ( assert ) {
94 QUnit.expect( 2 );
95 var api = new mw.Api();
96
97 // Get editToken for local wiki, this should not make
98 // a request as it should be retrieved from mw.user.tokens.
99 api.getToken( 'edit' )
100 .done( function ( token ) {
101 assert.ok( token.length, 'Got a token' );
102 } )
103 .fail( function ( err ) {
104 assert.equal( '', err, 'API error' );
105 } );
106
107 assert.equal( this.server.requests.length, 0, 'Requests made' );
108 } );
109
110 QUnit.test( 'getToken() - uncached', function ( assert ) {
111 QUnit.expect( 3 );
112 var api = new mw.Api();
113
114 this.server.respondWith( /type=testuncached/, [ 200, { 'Content-Type': 'application/json' },
115 '{ "tokens": { "testuncachedtoken": "good" } }'
116 ] );
117
118 // Get a token of a type that isn't prepopulated by user.tokens.
119 // Could use "block" or "delete" here, but those could in theory
120 // be added to user.tokens, use a fake one instead.
121 api.getToken( 'testuncached' )
122 .done( function ( token ) {
123 assert.equal( token, 'good', 'The token' );
124 } )
125 .fail( function ( err ) {
126 assert.equal( err, '', 'API error' );
127 } );
128
129 api.getToken( 'testuncached' )
130 .done( function ( token ) {
131 assert.equal( token, 'good', 'The cached token' );
132 } )
133 .fail( function ( err ) {
134 assert.equal( err, '', 'API error' );
135 } );
136
137 assert.equal( this.server.requests.length, 1, 'Requests made' );
138 } );
139
140 QUnit.test( 'getToken() - error', function ( assert ) {
141 QUnit.expect( 2 );
142 var api = new mw.Api();
143
144 this.server.respondWith( /type=testerror/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
145 [
146 '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }',
147 '{ "tokens": { "testerrortoken": "good" } }'
148 ]
149 ) );
150
151 // Don't cache error (bug 65268)
152 api.getToken( 'testerror' ).fail( function ( err ) {
153 assert.equal( err, 'bite-me', 'Expected error' );
154
155 // Make this request after the first one has finished.
156 // If we make it simultaneously we still want it to share
157 // the cache, but as soon as it is fulfilled as error we
158 // reject it so that the next one tries fresh.
159 api.getToken( 'testerror' ).done( function ( token ) {
160 assert.equal( token, 'good', 'The token' );
161 } );
162 } );
163 } );
164
165 QUnit.test( 'badToken()', function ( assert ) {
166 QUnit.expect( 2 );
167 var api = new mw.Api(),
168 test = this;
169
170 this.server.respondWith( /type=testbad/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
171 [
172 '{ "tokens": { "testbadtoken": "bad" } }',
173 '{ "tokens": { "testbadtoken": "good" } }'
174 ]
175 ) );
176
177 api.getToken( 'testbad' )
178 .then( function () {
179 api.badToken( 'testbad' );
180 return api.getToken( 'testbad' );
181 } )
182 .then( function ( token ) {
183 assert.equal( token, 'good', 'The token' );
184 assert.equal( test.server.requests.length, 2, 'Requests made' );
185 } );
186
187 } );
188
189 QUnit.test( 'postWithToken( tokenType, params )', function ( assert ) {
190 QUnit.expect( 1 );
191 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
192
193 this.server.respondWith( 'GET', /type=testpost/, [ 200, { 'Content-Type': 'application/json' },
194 '{ "tokens": { "testposttoken": "good" } }'
195 ] );
196 this.server.respondWith( 'POST', /api/, function ( request ) {
197 if ( request.requestBody.match( /token=good/ ) ) {
198 request.respond( 200, { 'Content-Type': 'application/json' },
199 '{ "example": { "foo": "quux" } }'
200 );
201 }
202 } );
203
204 api.postWithToken( 'testpost', { action: 'example', key: 'foo' } )
205 .done( function ( data ) {
206 assert.deepEqual( data, { example: { foo: 'quux' } } );
207 } );
208 } );
209
210 QUnit.test( 'postWithToken( tokenType, params with assert )', function ( assert ) {
211 QUnit.expect( 2 );
212 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
213
214 this.server.respondWith( /assert=user/, [ 200, { 'Content-Type': 'application/json' },
215 '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
216 ] );
217
218 api.postWithToken( 'testassertpost', { action: 'example', key: 'foo', assert: 'user' } )
219 .fail( function ( errorCode ) {
220 assert.equal( errorCode, 'assertuserfailed', 'getToken fails assert' );
221 } );
222
223 assert.equal( this.server.requests.length, 1, 'Requests made' );
224 } );
225
226 QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) {
227 QUnit.expect( 3 );
228 var api = new mw.Api();
229
230 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ] );
231
232 api.postWithToken(
233 'edit',
234 {
235 action: 'example'
236 },
237 {
238 headers: {
239 'X-Foo': 'Bar'
240 }
241 }
242 );
243
244 api.postWithToken(
245 'edit',
246 {
247 action: 'example'
248 },
249 function () {
250 assert.ok( false, 'This parameter cannot be a callback' );
251 }
252 )
253 .always( function ( data ) {
254 assert.equal( data.example, 'quux' );
255 } );
256
257 assert.equal( this.server.requests.length, 2, 'Request made' );
258 assert.equal( this.server.requests[0].requestHeaders['X-Foo'], 'Bar', 'Header sent' );
259 } );
260
261 QUnit.test( 'postWithToken() - badtoken', function ( assert ) {
262 QUnit.expect( 1 );
263 var api = new mw.Api();
264
265 this.server.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
266 [
267 '{ "tokens": { "testbadtokentoken": "bad" } }',
268 '{ "tokens": { "testbadtokentoken": "good" } }'
269 ]
270 ) );
271 this.server.respondWith( 'POST', /api/, function ( request ) {
272 if ( request.requestBody.match( /token=bad/ ) ) {
273 request.respond( 200, { 'Content-Type': 'application/json' },
274 '{ "error": { "code": "badtoken" } }'
275 );
276 }
277 if ( request.requestBody.match( /token=good/ ) ) {
278 request.respond( 200, { 'Content-Type': 'application/json' },
279 '{ "example": { "foo": "quux" } }'
280 );
281 }
282 } );
283
284 // - Request: new token -> bad
285 // - Request: action=example -> badtoken error
286 // - Request: new token -> good
287 // - Request: action=example -> success
288 api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } )
289 .done( function ( data ) {
290 assert.deepEqual( data, { example: { foo: 'quux' } } );
291 } );
292 } );
293
294 QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) {
295 QUnit.expect( 2 );
296 var sequenceA,
297 api = new mw.Api();
298
299 this.server.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
300 [
301 '{ "tokens": { "testoncetoken": "good-A" } }',
302 '{ "tokens": { "testoncetoken": "good-B" } }'
303 ]
304 ) );
305 sequenceA = sequenceBodies( 200, { 'Content-Type': 'application/json' },
306 [
307 '{ "example": { "value": "A" } }',
308 '{ "error": { "code": "badtoken" } }'
309 ]
310 );
311 this.server.respondWith( 'POST', /api/, function ( request ) {
312 if ( request.requestBody.match( /token=good-A/ ) ) {
313 sequenceA( request );
314 } else if ( request.requestBody.match( /token=good-B/ ) ) {
315 request.respond( 200, { 'Content-Type': 'application/json' },
316 '{ "example": { "value": "B" } }'
317 );
318 }
319 } );
320
321 // - Request: new token -> A
322 // - Request: action=example
323 api.postWithToken( 'testonce', { action: 'example', key: 'foo' } )
324 .done( function ( data ) {
325 assert.deepEqual( data, { example: { value: 'A' } } );
326 } );
327
328 // - Request: action=example w/ token A -> badtoken error
329 // - Request: new token -> B
330 // - Request: action=example w/ token B -> success
331 api.postWithToken( 'testonce', { action: 'example', key: 'bar' } )
332 .done( function ( data ) {
333 assert.deepEqual( data, { example: { value: 'B' } } );
334 } );
335 } );
336 }( mediaWiki ) );