From: Niklas Laxström Date: Mon, 25 Aug 2014 12:04:29 +0000 (+0200) Subject: mediawiki.api: Support assert parameter in getToken and postWithToken X-Git-Tag: 1.31.0-rc.0~13869^2 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/pie.php?a=commitdiff_plain;h=e4a3cb3b5725cdec0705c1752b15be6d7d87e6c7;p=lhc%2Fweb%2Fwiklou.git mediawiki.api: Support assert parameter in getToken and postWithToken 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 --- diff --git a/resources/src/mediawiki.api/mediawiki.api.js b/resources/src/mediawiki.api/mediawiki.api.js index f8dc836320..fa008856ce 100644 --- a/resources/src/mediawiki.api/mediawiki.api.js +++ b/resources/src/mediawiki.api/mediawiki.api.js @@ -257,7 +257,7 @@ 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 @@ -270,7 +270,7 @@ 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 ); } ); @@ -286,19 +286,21 @@ /** * 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 ) { diff --git a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js index f156c7285d..76fa6bedb2 100644 --- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js +++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js @@ -196,6 +196,28 @@ ); } ); + 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 );