mediawiki.api: Support assert parameter in getToken and postWithToken
authorNiklas Laxström <niklas.laxstrom@gmail.com>
Mon, 25 Aug 2014 12:04:29 +0000 (14:04 +0200)
committerNiklas Laxström <niklas.laxstrom@gmail.com>
Sun, 21 Sep 2014 09:32:25 +0000 (11:32 +0200)
Currently if you try to use assert=user with postWithToken the
request will fail with "token-missing" instead of "assertuserfailed"
because the assertion is not copied to getToken request.

Change-Id: Ibe48956adc6d2f171f88e6ef2bba8a39ae0127f2

resources/src/mediawiki.api/mediawiki.api.js
tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js

index f8dc836..fa00885 100644 (file)
                                ajaxOptions = undefined;
                        }
 
-                       return api.getToken( tokenType ).then( function ( token ) {
+                       return api.getToken( tokenType, params.assert ).then( function ( token ) {
                                params.token = token;
                                return api.post( params, ajaxOptions ).then(
                                        // If no error, return to caller as-is
                                                                params.token = undefined;
 
                                                        // Try again, once
-                                                       return api.getToken( tokenType ).then( function ( token ) {
+                                                       return api.getToken( tokenType, params.assert ).then( function ( token ) {
                                                                params.token = token;
                                                                return api.post( params, ajaxOptions );
                                                        } );
                /**
                 * Get a token for a certain action from the API.
                 *
+                * The assert parameter is only for internal use by postWithToken.
+                *
                 * @param {string} type Token type
                 * @return {jQuery.Promise}
                 * @return {Function} return.done
                 * @return {string} return.done.token Received token.
                 * @since 1.22
                 */
-               getToken: function ( type ) {
+               getToken: function ( type, assert ) {
                        var apiPromise,
                                promiseGroup = promises[ this.defaults.ajax.url ],
                                d = promiseGroup && promiseGroup[ type + 'Token' ];
 
                        if ( !d ) {
-                               apiPromise = this.get( { action: 'tokens', type: type } );
+                               apiPromise = this.get( { action: 'tokens', type: type, assert: assert } );
 
                                d = apiPromise
                                        .then( function ( data ) {
index f156c72..76fa6be 100644 (file)
                );
        } );
 
+       QUnit.test( 'postWithToken( tokenType, params with assert )', function ( assert ) {
+               QUnit.expect( 2 );
+
+               var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
+
+               api.postWithToken( 'testasserttoken', { action: 'example', key: 'foo', assert: 'user' } )
+                       .fail( function ( errorCode ) {
+                               assert.equal( errorCode, 'assertuserfailed', 'getToken fails assert' );
+                       } );
+
+               assert.equal( this.server.requests.length, 1, 'Request for token made' );
+               this.server.respondWith( /assert=user/, function ( request ) {
+                       request.respond(
+                               200,
+                               { 'Content-Type': 'application/json' },
+                               '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
+                       );
+               } );
+
+               this.server.respond();
+       } );
+
        QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) {
                QUnit.expect( 3 );