mediawiki.api: fix badtoken handling with legacy token types
authorNiklas Laxström <niklas.laxstrom@gmail.com>
Tue, 1 Dec 2015 16:25:16 +0000 (17:25 +0100)
committerKrinkle <krinklemail@gmail.com>
Mon, 7 Dec 2015 21:57:49 +0000 (21:57 +0000)
Manual test plan:

mw.loader.using( 'mediawiki.api.login' ).done( function () {
var
user = 'XXX',
pass = 'YYY',
api = new mw.Api();

api.login( user, pass ).done( function () {
api.postWithToken( 'options', {
action: 'options',
optionname: 'userjs-tokentest',
optionvalue: 1
} ).fail( function () {
console.log( arguments );
} );
} );
} );

Change-Id: I175cf4e3a845cdfe621b6009920fddba6bc1ff12

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

index c26dd6a..10e0c56 100644 (file)
                 */
                badToken: function ( type ) {
                        var promiseGroup = promises[ this.defaults.ajax.url ];
+
+                       type = mapLegacyToken( type );
                        if ( promiseGroup ) {
                                delete promiseGroup[ type + 'Token' ];
                        }
index a34a5af..394f3bd 100644 (file)
 
        } );
 
+       QUnit.test( 'badToken( legacy )', function ( assert ) {
+               QUnit.expect( 2 );
+               var api = new mw.Api( { ajax: { url: '/badTokenLegacy/api.php' } } ),
+                       test = this;
+
+               this.server.respondWith( /type=csrf/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
+                       [
+                               '{ "query": { "tokens": { "csrftoken": "badlegacy" } } }',
+                               '{ "query": { "tokens": { "csrftoken": "goodlegacy" } } }'
+                       ]
+               ) );
+
+               api.getToken( 'options' )
+                       .then( function () {
+                               api.badToken( 'options' );
+                               return api.getToken( 'options' );
+                       } )
+                       .then( function ( token ) {
+                               assert.equal( token, 'goodlegacy', 'The token' );
+                               assert.equal( test.server.requests.length, 2, 'Request made' );
+                       } );
+
+       } );
+
        QUnit.test( 'postWithToken( tokenType, params )', function ( assert ) {
                QUnit.expect( 1 );
                var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );