From 93fc25c4a84916316c75c2b9b4065ee236a501df Mon Sep 17 00:00:00 2001 From: jrobson Date: Tue, 29 Apr 2014 15:42:43 -0700 Subject: [PATCH] mediawiki.api: Fix API postWithToken method Referred to "this" instead of "api" ("this" here refers to the Deferred object). Fixes "Uncaught TypeError: Cannot read property 'ajax' of undefined". Change-Id: I78ba47a614512f6218e23d03e7c688e2c9efbe45 --- resources/src/mediawiki.api/mediawiki.api.js | 2 +- .../mediawiki.api/mediawiki.api.test.js | 96 +++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/resources/src/mediawiki.api/mediawiki.api.js b/resources/src/mediawiki.api/mediawiki.api.js index b37e2a6e8d..9962534a9f 100644 --- a/resources/src/mediawiki.api/mediawiki.api.js +++ b/resources/src/mediawiki.api/mediawiki.api.js @@ -257,7 +257,7 @@ function ( code ) { if ( code === 'badtoken' ) { // Clear from cache - deferreds[ this.defaults.ajax.url ][ tokenType + 'Token' ] = + deferreds[ api.defaults.ajax.url ][ tokenType + 'Token' ] = params.token = undefined; // Try again, once 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 a93f572d59..05eb6b988e 100644 --- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js +++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js @@ -142,4 +142,100 @@ } ); } ); + QUnit.test( 'postWithToken()', function ( assert ) { + QUnit.expect( 1 ); + + var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ); + + // - Requests token + // - Performs action=example + api.postWithToken( 'testsimpletoken', { action: 'example', key: 'foo' } ) + .done( function ( data ) { + assert.deepEqual( data, { example: { foo: 'quux' } } ); + } ); + + this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' }, + '{ "tokens": { "testsimpletokentoken": "a-bad-token" } }' + ); + + this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' }, + '{ "example": { "foo": "quux" } }' + ); + } ); + + QUnit.test( 'postWithToken() - badtoken', function ( assert ) { + QUnit.expect( 1 ); + + var api = new mw.Api(); + + // - Request: token + // - Request: action=example -> badtoken error + // - Request: new token + // - Request: action=example + api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } ) + .done( function ( data ) { + assert.deepEqual( data, { example: { foo: 'quux' } } ); + } ); + + this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' }, + '{ "tokens": { "testbadtokentoken": "a-bad-token" } }' + ); + + this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' }, + '{ "error": { "code": "badtoken" } }' + ); + + this.server.requests[2].respond( 200, { 'Content-Type': 'application/json' }, + '{ "tokens": { "testbadtokentoken": "a-good-token" } }' + ); + + this.server.requests[3].respond( 200, { 'Content-Type': 'application/json' }, + '{ "example": { "foo": "quux" } }' + ); + + } ); + + QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) { + QUnit.expect( 2 ); + + var api = new mw.Api(); + + // - Request: token + // - Request: action=example + api.postWithToken( 'testbadtokencache', { action: 'example', key: 'foo' } ) + .done( function ( data ) { + assert.deepEqual( data, { example: { foo: 'quux' } } ); + } ); + + // - Cache: Try previously cached token + // - Request: action=example -> badtoken error + // - Request: new token + // - Request: action=example + api.postWithToken( 'testbadtokencache', { action: 'example', key: 'bar' } ) + .done( function ( data ) { + assert.deepEqual( data, { example: { bar: 'quux' } } ); + } ); + + this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' }, + '{ "tokens": { "testbadtokencachetoken": "a-good-token-once" } }' + ); + + this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' }, + '{ "example": { "foo": "quux" } }' + ); + + this.server.requests[2].respond( 200, { 'Content-Type': 'application/json' }, + '{ "error": { "code": "badtoken" } }' + ); + + this.server.requests[3].respond( 200, { 'Content-Type': 'application/json' }, + '{ "tokens": { "testbadtokencachetoken": "a-good-new-token" } }' + ); + + this.server.requests[4].respond( 200, { 'Content-Type': 'application/json' }, + '{ "example": { "bar": "quux" } }' + ); + + } ); + }( mediaWiki ) ); -- 2.20.1